Skip to content

Commit 0f23ecb

Browse files
committed
Encapsulate render control.
Moving this to a centrally controlled location so it's easier to migrate to choreographer. Also turn the stupid int into a bool.
1 parent e06800b commit 0f23ecb

File tree

1 file changed

+20
-9
lines changed
  • native-activity/app/src/main/cpp

1 file changed

+20
-9
lines changed

native-activity/app/src/main/cpp/main.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ struct Engine {
8383
const ASensor* accelerometerSensor;
8484
ASensorEventQueue* sensorEventQueue;
8585

86-
int animating;
8786
EGLDisplay display;
8887
EGLSurface surface;
8988
EGLContext context;
@@ -104,6 +103,19 @@ struct Engine {
104103
sensorEventQueue = ASensorManager_createEventQueue(
105104
sensorManager, app->looper, ALOOPER_POLL_CALLBACK, callback, this);
106105
}
106+
107+
bool animating() const { return animating_; }
108+
109+
void StartRendering() {
110+
animating_ = true;
111+
}
112+
113+
void StopRendering() {
114+
animating_ = false;
115+
}
116+
117+
private:
118+
bool animating_;
107119
};
108120

109121
/**
@@ -238,7 +250,7 @@ static void engine_term_display(Engine* engine) {
238250
}
239251
eglTerminate(engine->display);
240252
}
241-
engine->animating = 0;
253+
engine->StopRendering();
242254
engine->display = EGL_NO_DISPLAY;
243255
engine->context = EGL_NO_CONTEXT;
244256
engine->surface = EGL_NO_SURFACE;
@@ -251,7 +263,7 @@ static int32_t engine_handle_input(android_app* app,
251263
AInputEvent* event) {
252264
auto* engine = (Engine*)app->userData;
253265
if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
254-
engine->animating = 1;
266+
engine->StartRendering();
255267
engine->state.x = AMotionEvent_getX(event, 0);
256268
engine->state.y = AMotionEvent_getY(event, 0);
257269
return 1;
@@ -300,8 +312,7 @@ static void engine_handle_cmd(android_app* app, int32_t cmd) {
300312
ASensorEventQueue_disableSensor(engine->sensorEventQueue,
301313
engine->accelerometerSensor);
302314
}
303-
// Also stop animating.
304-
engine->animating = 0;
315+
engine->StopRendering();
305316
engine_draw_frame(engine);
306317
break;
307318
default:
@@ -356,10 +367,10 @@ void android_main(android_app* state) {
356367
int events;
357368
android_poll_source* source;
358369

359-
// If not animating, we will block forever waiting for events.
360-
// If animating, we loop until all events are read, then continue
370+
// If not animating_, we will block forever waiting for events.
371+
// If animating_, we loop until all events are read, then continue
361372
// to draw the next frame of animation.
362-
while ((ALooper_pollAll(engine.animating ? 0 : -1, nullptr, &events,
373+
while ((ALooper_pollAll(engine.animating() ? 0 : -1, nullptr, &events,
363374
(void**)&source)) >= 0) {
364375
// Process this event.
365376
if (source != nullptr) {
@@ -373,7 +384,7 @@ void android_main(android_app* state) {
373384
}
374385
}
375386

376-
if (engine.animating) {
387+
if (engine.animating()) {
377388
// Done with events; draw next animation frame.
378389
engine.state.angle += .01f;
379390
if (engine.state.angle > 1) {

0 commit comments

Comments
 (0)