Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove odd engine_draw_frame call patterns. #1006

Merged
merged 2 commits into from
May 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 20 additions & 11 deletions native-activity/app/src/main/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ struct Engine {
const ASensor* accelerometerSensor;
ASensorEventQueue* sensorEventQueue;

int animating;
EGLDisplay display;
EGLSurface surface;
EGLContext context;
Expand All @@ -104,6 +103,19 @@ struct Engine {
sensorEventQueue = ASensorManager_createEventQueue(
sensorManager, app->looper, ALOOPER_POLL_CALLBACK, callback, this);
}

bool animating() const { return animating_; }

void StartRendering() {
animating_ = true;
}

void StopRendering() {
animating_ = false;
}

private:
bool animating_;
};

/**
Expand Down Expand Up @@ -238,7 +250,7 @@ static void engine_term_display(Engine* engine) {
}
eglTerminate(engine->display);
}
engine->animating = 0;
engine->StopRendering();
engine->display = EGL_NO_DISPLAY;
engine->context = EGL_NO_CONTEXT;
engine->surface = EGL_NO_SURFACE;
Expand All @@ -251,7 +263,6 @@ static int32_t engine_handle_input(android_app* app,
AInputEvent* event) {
auto* engine = (Engine*)app->userData;
if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
engine->animating = 1;
engine->state.x = AMotionEvent_getX(event, 0);
engine->state.y = AMotionEvent_getY(event, 0);
return 1;
Expand All @@ -275,7 +286,6 @@ static void engine_handle_cmd(android_app* app, int32_t cmd) {
// The window is being shown, get it ready.
if (engine->app->window != nullptr) {
engine_init_display(engine);
engine_draw_frame(engine);
}
break;
case APP_CMD_TERM_WINDOW:
Expand All @@ -292,6 +302,7 @@ static void engine_handle_cmd(android_app* app, int32_t cmd) {
engine->accelerometerSensor,
(1000L / 60) * 1000);
}
engine->StartRendering();
break;
case APP_CMD_LOST_FOCUS:
// When our app loses focus, we stop monitoring the accelerometer.
Expand All @@ -300,9 +311,7 @@ static void engine_handle_cmd(android_app* app, int32_t cmd) {
ASensorEventQueue_disableSensor(engine->sensorEventQueue,
engine->accelerometerSensor);
}
// Also stop animating.
engine->animating = 0;
engine_draw_frame(engine);
engine->StopRendering();
break;
default:
break;
Expand Down Expand Up @@ -356,10 +365,10 @@ void android_main(android_app* state) {
int events;
android_poll_source* source;

// If not animating, we will block forever waiting for events.
// If animating, we loop until all events are read, then continue
// If not animating_, we will block forever waiting for events.
// If animating_, we loop until all events are read, then continue
// to draw the next frame of animation.
while ((ALooper_pollAll(engine.animating ? 0 : -1, nullptr, &events,
while ((ALooper_pollAll(engine.animating() ? 0 : -1, nullptr, &events,
(void**)&source)) >= 0) {
// Process this event.
if (source != nullptr) {
Expand All @@ -373,7 +382,7 @@ void android_main(android_app* state) {
}
}

if (engine.animating) {
if (engine.animating()) {
// Done with events; draw next animation frame.
engine.state.angle += .01f;
if (engine.state.angle > 1) {
Expand Down
Loading