12
12
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13
13
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS.
14
14
*
15
- * In no event and under no legal theory, whether in tort (including negligence),
16
- * contract, or otherwise, unless required by applicable law (such as deliberate
15
+ * In no event and under no legal theory, whether in tort (including negligence),
16
+ * contract, or otherwise, unless required by applicable law (such as deliberate
17
17
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
18
- * liable for any damages, including any direct, indirect, special, incidental,
19
- * or consequential damages of any character arising as a result of this License or
20
- * out of the use or inability to use the software (including but not limited to damages
21
- * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
22
- * all other commercial damages or losses), even if such Contributor has been advised
18
+ * liable for any damages, including any direct, indirect, special, incidental,
19
+ * or consequential damages of any character arising as a result of this License or
20
+ * out of the use or inability to use the software (including but not limited to damages
21
+ * for loss of goodwill, work stoppage, computer failure or malfunction, or any and
22
+ * all other commercial damages or losses), even if such Contributor has been advised
23
23
* of the possibility of such damages.
24
24
*/
25
25
26
26
#include " AndroidAppBase.hpp"
27
+ #include " Errors.hpp"
27
28
#include " Timer.hpp"
28
29
29
30
#include < android/sensor.h>
@@ -118,22 +119,47 @@ void AndroidAppBase::HandleCmd(struct android_app* app, int32_t cmd)
118
119
AndroidAppBase* eng = (AndroidAppBase*)app->userData ;
119
120
switch (cmd)
120
121
{
121
- case APP_CMD_SAVE_STATE:
122
- break ;
123
-
124
122
case APP_CMD_INIT_WINDOW:
125
- // The window is being shown, get it ready.
126
- if (app->window != NULL )
123
+ LOG_INFO_MESSAGE (" APP_CMD_INIT_WINDOW" );
124
+ app->window = app->pendingWindow ;
125
+ if (app->window &&
126
+ ANativeWindow_getWidth (app->window ) &&
127
+ ANativeWindow_getHeight (app->window ))
127
128
{
129
+ LOG_INFO_MESSAGE (" INIT DISPLAY - HAS SURFACE" );
128
130
eng->InitDisplay ();
129
131
eng->DrawFrame ();
132
+ std::lock_guard<std::mutex> lock (eng->mutex );
133
+ eng->app_status_ |= APP_STATUS_FLAG_HAS_REAL_SURFACE;
134
+ }
135
+ else
136
+ {
137
+ LOG_INFO_MESSAGE (" NO SURFACE" );
138
+ std::lock_guard<std::mutex> lock (eng->mutex );
139
+ eng->app_status_ &= ~APP_STATUS_FLAG_HAS_REAL_SURFACE;
130
140
}
131
141
break ;
132
142
133
- case APP_CMD_CONFIG_CHANGED:
143
+ case APP_CMD_TERM_WINDOW:
144
+ LOG_INFO_MESSAGE (" APP_CMD_TERM_WINDOW - LOST SURFACE - TERM DISPLAY" );
145
+ {
146
+ std::lock_guard<std::mutex> lock (eng->mutex );
147
+ eng->app_status_ &= ~APP_STATUS_FLAG_HAS_REAL_SURFACE;
148
+ }
149
+ eng->TermDisplay ();
150
+ break ;
151
+
152
+ // Note that as of NDK r21b (21.1.6352462), APP_CMD_CONTENT_RECT_CHANGED event is never
153
+ // generated by android_native_app_glue
154
+ case APP_CMD_CONTENT_RECT_CHANGED:
134
155
{
135
- // This callback is not reliable for handling orientation changes. Depending on the
136
- // device, it may be called before or after the surface has been actually resized.
156
+ LOG_INFO_MESSAGE (" APP_CMD_CONTENT_RECT_CHANGED" );
157
+
158
+ int32_t new_window_width =
159
+ app->contentRect .right - app->contentRect .left ;
160
+ int32_t new_window_height =
161
+ app->contentRect .bottom - app->contentRect .top ;
162
+ eng->WindowResize (new_window_width, new_window_height);
137
163
break ;
138
164
}
139
165
@@ -144,45 +170,87 @@ void AndroidAppBase::HandleCmd(struct android_app* app, int32_t cmd)
144
170
// does not work either - the callback is only called once after the window has been created.
145
171
case APP_CMD_WINDOW_RESIZED:
146
172
{
147
- auto new_window_width = ANativeWindow_getWidth (app->window );
148
- auto new_window_height = ANativeWindow_getHeight (app->window );
149
- eng->WindowResize (new_window_width, new_window_height);
173
+ LOG_INFO_MESSAGE (" APP_CMD_WINDOW_RESIZED" );
174
+ if (app->window )
175
+ {
176
+ int32_t new_window_width = ANativeWindow_getWidth (app->window );
177
+ int32_t new_window_height = ANativeWindow_getHeight (app->window );
178
+ if (new_window_width && new_window_height)
179
+ {
180
+ eng->WindowResize (new_window_width, new_window_height);
181
+ }
182
+ }
150
183
break ;
151
184
}
152
185
153
- // Note that as of NDK r21b (21.1.6352462), APP_CMD_CONTENT_RECT_CHANGED event is never
154
- // generated by android_native_app_glue
155
- case APP_CMD_CONTENT_RECT_CHANGED:
156
- {
157
- auto new_window_width = app->contentRect .right - app->contentRect .left ;
158
- auto new_window_height = app->contentRect .bottom - app->contentRect .top ;
159
- eng->WindowResize (new_window_width, new_window_height);
186
+ case APP_CMD_GAINED_FOCUS:
187
+ LOG_INFO_MESSAGE (" APP_CMD_GAINED_FOCUS - HAS FOCUS" );
188
+ {
189
+ std::lock_guard<std::mutex> lock (eng->mutex );
190
+ eng->app_status_ |= APP_STATUS_FLAG_FOCUSED;
191
+ }
192
+ eng->ResumeSensors ();
193
+ break ;
194
+
195
+ case APP_CMD_LOST_FOCUS:
196
+ LOG_INFO_MESSAGE (" APP_CMD_LOST_FOCUS - LOST FOCUS" );
197
+ {
198
+ std::lock_guard<std::mutex> lock (eng->mutex );
199
+ eng->app_status_ &= ~APP_STATUS_FLAG_FOCUSED;
200
+ }
201
+ eng->SuspendSensors ();
202
+ break ;
203
+
204
+ case APP_CMD_RESUME:
205
+ LOG_INFO_MESSAGE (" APP_CMD_RESUME - IS ACTIVE" );
206
+ {
207
+ std::lock_guard<std::mutex> lock (eng->mutex );
208
+ eng->app_status_ |= APP_STATUS_FLAG_ACTIVE;
209
+ }
210
+ break ;
211
+
212
+ case APP_CMD_START:
213
+ LOG_INFO_MESSAGE (" APP_CMD_START" );
160
214
break ;
161
- }
162
215
163
- case APP_CMD_TERM_WINDOW:
164
216
case APP_CMD_PAUSE:
165
- // The window is being hidden or closed, clean it up.
166
- eng->TermDisplay ();
167
- eng->has_focus_ = false ;
217
+ LOG_INFO_MESSAGE (" APP_CMD_PAUSE - IS NOT ACTIVE" );
218
+ {
219
+ std::lock_guard<std::mutex> lock (eng->mutex );
220
+ eng->app_status_ &= ~APP_STATUS_FLAG_ACTIVE;
221
+ }
168
222
break ;
169
223
170
224
case APP_CMD_STOP:
225
+ LOG_INFO_MESSAGE (" APP_CMD_STOP" );
171
226
break ;
172
227
173
- case APP_CMD_GAINED_FOCUS:
174
- eng->ResumeSensors ();
175
- // Start animation
176
- eng->has_focus_ = true ;
228
+ case APP_CMD_CONFIG_CHANGED:
229
+ LOG_INFO_MESSAGE (" APP_CMD_CONFIG_CHANGED" );
230
+ // AConfiguration_fromAssetManager(app->config, app->activity->assetManager);
231
+ // This callback is not reliable for handling orientation changes. Depending on the
232
+ // device, it may be called before or after the surface has been actually resized.
177
233
break ;
178
234
179
- case APP_CMD_LOST_FOCUS:
180
- eng->SuspendSensors ();
181
- // Also stop animating.
182
- eng->has_focus_ = false ;
235
+ case APP_CMD_DESTROY:
236
+ LOG_INFO_MESSAGE (" APP_CMD_DESTROY - IS NOT RUNNING" );
237
+ {
238
+ std::lock_guard<std::mutex> lock (eng->mutex );
239
+ eng->app_status_ &= ~APP_STATUS_FLAG_RUNNING;
240
+ }
241
+ break ;
242
+
243
+ case APP_CMD_WINDOW_REDRAW_NEEDED:
244
+ LOG_INFO_MESSAGE (" APP_CMD_WINDOW_REDRAW_NEEDED" );
245
+ if (eng->IsReady ()) eng->DrawFrame ();
246
+ break ;
247
+
248
+ case APP_CMD_SAVE_STATE:
249
+ LOG_INFO_MESSAGE (" APP_CMD_SAVE_STATE" );
183
250
break ;
184
251
185
252
case APP_CMD_LOW_MEMORY:
253
+ LOG_INFO_MESSAGE (" APP_CMD_LOW_MEMORY" );
186
254
// Free up GL resources
187
255
eng->TrimMemory ();
188
256
break ;
@@ -244,10 +312,11 @@ void AndroidAppBase::SetState(android_app* state, const char* native_activity_cl
244
312
245
313
bool AndroidAppBase::IsReady ()
246
314
{
247
- if (has_focus_)
248
- return true ;
249
-
250
- return false ;
315
+ std::lock_guard<std::mutex> lock (mutex);
316
+ return (app_status_ & APP_STATUS_FLAG_RUNNING) != APP_STATUS_FLAG_NONE &&
317
+ (app_status_ & APP_STATUS_FLAG_ACTIVE) != APP_STATUS_FLAG_NONE &&
318
+ (app_status_ & APP_STATUS_FLAG_FOCUSED) != APP_STATUS_FLAG_NONE &&
319
+ (app_status_ & APP_STATUS_FLAG_HAS_REAL_SURFACE) != APP_STATUS_FLAG_NONE;
251
320
}
252
321
253
322
// void Engine::TransformPosition( ndk_helper::Vec2& vec )
0 commit comments