MCPcopy Create free account
hub / github.com/android/ndk-samples / Engine

Class Engine

native-activity/app/src/main/cpp/main.cpp:72–231  ·  view source on GitHub ↗

* The implementation for our app. * * This class implements the activity lifecycle behaviors akin to how Activity * would in a Java app. With native_app_glue, those lifecycle events are instead * communicated to this class from engine_handle_cmd, which is in turned called * by looper (see the description below in android_main). * * The comments here will briefly explain some aspects of the

Source from the content-addressed store, hash-verified

70 * and the other docs in that section for more information.
71 */
72class Engine {
73 public:
74 explicit Engine(android_app* app) : app_(app) {}
75
76 void AttachWindow() {
77 // This is called whenever a new native window is created for our app, so we
78 // need to reinitialize the buffer format to the format our render loop
79 // expects.
80 //
81 // Attaching the window will not cause the app to start running its update
82 // and render loop. The app's update cycle is separately enabled by
83 // Engine::Resume.
84 if (ANativeWindow_setBuffersGeometry(
85 app_->window, 0, 0, AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM) < 0) {
86 LOGE("Unable to set window buffer geometry");
87 window_initialized = false;
88 return;
89 }
90 window_initialized = true;
91 color_ = Color::kRed;
92 last_update_ = std::chrono::steady_clock::now();
93 }
94
95 void DetachWindow() {
96 // This is called whenever the native window for our app is destroyed. That
97 // does not necessarily mean that the app is being killed, as it is also
98 // done when the screen rotates.
99 //
100 // For a more typical app where the rendering is done with OpenGL or Vulkan,
101 // this is where you'd perform any window cleanup needed by those
102 // frameworks. For our app, it's sufficient to just set a flag to disable
103 // our render loop.
104 window_initialized = false;
105 }
106
107 /// Resumes ticking the application.
108 void Resume() {
109 // This is called whenever the activity is resumed (brought into the
110 // foreground). When that happens, we schedule our next update tick with
111 // Choreographer. Choreographer is the Android system that paces app render
112 // loops. If you instead render new frames in a loop without frame pacing,
113 // you risk rendering more quickly than the display pipeline is able to
114 // present new frames. This will increase the latency between frame
115 // submission and presentation.
116 // https://developer.android.com/ndk/reference/group/choreographer
117
118 // Checked to make sure we don't double schedule Choreographer.
119 if (!running_) {
120 running_ = true;
121 ScheduleNextTick();
122 }
123 }
124
125 /// Pauses ticking the application.
126 ///
127 /// When paused, sensor and input events will still be processed, but the
128 /// update and render parts of the loop will not run.
129 void Pause() {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected