google-game-sdk: only notify resize event after window's size(lastWindowWidth) has been initialized (#265)

This commit is contained in:
bofeng-song 2022-06-23 15:24:06 +08:00 committed by GitHub
parent ae7ffe5396
commit 6bc7be8a80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 21 deletions

View File

@ -353,8 +353,8 @@ struct NativeCode : public GameActivity {
std::string obbPathObj; std::string obbPathObj;
ANativeWindow *nativeWindow; ANativeWindow *nativeWindow;
int32_t lastWindowWidth; int32_t lastWindowWidth{-1};
int32_t lastWindowHeight; int32_t lastWindowHeight{-1};
// These are used to wake up the main thread to process work. // These are used to wake up the main thread to process work.
int mainWorkRead; int mainWorkRead;
@ -743,6 +743,19 @@ static void onSurfaceCreated_native(JNIEnv *env, jobject javaGameActivity,
} }
} }
static void notify_window_resize(NativeCode *code, int32_t width, int32_t height) {
if (width != code->lastWindowWidth ||
height != code->lastWindowHeight) {
bool hasSizeInitialized = code->lastWindowWidth > 0;
code->lastWindowWidth = width;
code->lastWindowHeight = height;
if (hasSizeInitialized && code->callbacks.onNativeWindowResized != NULL) {
code->callbacks.onNativeWindowResized(
code, code->nativeWindow, width, height);
}
}
}
static void onSurfaceChanged_native(JNIEnv *env, jobject javaGameActivity, static void onSurfaceChanged_native(JNIEnv *env, jobject javaGameActivity,
jlong handle, jobject surface, jint format, jlong handle, jobject surface, jint format,
jint width, jint height) { jint width, jint height) {
@ -766,30 +779,13 @@ static void onSurfaceChanged_native(JNIEnv *env, jobject javaGameActivity,
code->callbacks.onNativeWindowCreated(code, code->callbacks.onNativeWindowCreated(code,
code->nativeWindow); code->nativeWindow);
} }
notify_window_resize(code, width, height);
if (width != code->lastWindowWidth ||
height != code->lastWindowHeight) {
code->lastWindowWidth = width;
code->lastWindowHeight = height;
if (code->callbacks.onNativeWindowResized != NULL) {
code->callbacks.onNativeWindowResized(
code, code->nativeWindow, width, height);
}
}
} }
} else { } else {
// Maybe it was resized? // Maybe it was resized?
int32_t newWidth = ANativeWindow_getWidth(code->nativeWindow); int32_t newWidth = ANativeWindow_getWidth(code->nativeWindow);
int32_t newHeight = ANativeWindow_getHeight(code->nativeWindow); int32_t newHeight = ANativeWindow_getHeight(code->nativeWindow);
if (newWidth != code->lastWindowWidth || notify_window_resize(code, newWidth, newHeight);
newHeight != code->lastWindowHeight) {
code->lastWindowWidth = newWidth;
code->lastWindowHeight = newHeight;
if (code->callbacks.onNativeWindowResized != NULL) {
code->callbacks.onNativeWindowResized(
code, code->nativeWindow, newWidth, newHeight);
}
}
} }
// Release the window we acquired earlier. // Release the window we acquired earlier.
if (oldNativeWindow != NULL) { if (oldNativeWindow != NULL) {