From b8a0ab5468d3d3f53b6d5864c90e8ce56f980306 Mon Sep 17 00:00:00 2001 From: Kevin Trogant Date: Mon, 17 Oct 2022 18:45:09 +0200 Subject: [PATCH] Handling state --- README.md | 9 +++++++-- app/src/main/cpp/CMakeLists.txt | 28 +++++++++++++++++----------- app/src/main/cpp/GameState.h | 13 +++++++++++++ app/src/main/cpp/NativeEngine.cpp | 20 ++++++++++++++------ app/src/main/cpp/NativeEngine.h | 8 ++------ 5 files changed, 53 insertions(+), 25 deletions(-) create mode 100644 app/src/main/cpp/GameState.h diff --git a/README.md b/README.md index 7190e43..7c9f2b8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,10 @@ # Krimi Dinner Engine Source code für die Krimi Dinner Engine. -In app/src/main/cpp/NativeEngine.cpp sind Kommentare (beginnend mit INTEGRATION), die erklären, wie -das Spiel integriert werden kann. \ No newline at end of file + +In +[app/src/main/cpp/NativeEngine.cpp](app/src/main/cpp/NativeEngine.cpp), +[app/src/main/cpp/CMakeLists.txt](app/src/main/cpp/CMakeLists.txt) +und [app/src/main/cpp/GameState.h](app/src/main/cpp/GameState.h) +sind Kommentare +(beginnend mit INTEGRATION), die Hinweise geben, wie das Spiel integriert werden kann. \ No newline at end of file diff --git a/app/src/main/cpp/CMakeLists.txt b/app/src/main/cpp/CMakeLists.txt index 3d0867b..d342bf7 100644 --- a/app/src/main/cpp/CMakeLists.txt +++ b/app/src/main/cpp/CMakeLists.txt @@ -1,14 +1,11 @@ cmake_minimum_required(VERSION 3.18.1) project("kde") -find_library(log-lib log ) +find_library(log-lib log) find_package(game-activity REQUIRED CONFIG) find_package(games-frame-pacing REQUIRED CONFIG) add_library(kde SHARED - kde.cpp - NativeEngine.cpp - NativeEngine.h Renderer.cpp Renderer.h Texture.cpp @@ -18,10 +15,19 @@ add_library(kde SHARED StringRepository.h StringRepository.cpp Hash.h - Hash.cpp) -target_link_libraries(kde ${log-lib} - EGL - GLESv3 - android - game-activity::game-activity - games-frame-pacing::swappy_static) + Hash.cpp + GameState.h) + +if (ANDROID) + message(STATUS "Building for Android") + target_sources(kde PUBLIC + kde.cpp + NativeEngine.cpp + NativeEngine.h) + target_link_libraries(kde ${log-lib} + EGL + GLESv3 + android + game-activity::game-activity + games-frame-pacing::swappy_static) +endif() \ No newline at end of file diff --git a/app/src/main/cpp/GameState.h b/app/src/main/cpp/GameState.h new file mode 100644 index 0000000..b8105a2 --- /dev/null +++ b/app/src/main/cpp/GameState.h @@ -0,0 +1,13 @@ +#ifndef KRIMI_DINNER_ENGINE_GAMESTATE_H +#define KRIMI_DINNER_ENGINE_GAMESTATE_H + +/// @brief State that is saved between app resumes +struct GameState +{ + bool has_focus; + + // INTEGRATION Füge deinen State hier hinzu + int foo; +}; + +#endif \ No newline at end of file diff --git a/app/src/main/cpp/NativeEngine.cpp b/app/src/main/cpp/NativeEngine.cpp index 89fb580..708926b 100644 --- a/app/src/main/cpp/NativeEngine.cpp +++ b/app/src/main/cpp/NativeEngine.cpp @@ -13,7 +13,7 @@ #include #include -static NativeEngineState g_app_state = { false }; +static GameState g_app_state = { false }; static void _handle_cmd_proxy(struct android_app* app, int32_t cmd) { @@ -160,14 +160,14 @@ void NativeEngine::gameLoop() if (isAnimating() && Renderer::ptr) { // INTEGRATION Rufe hier deine "gameloop"/"update" funktion auf, die als Parameter - // input_events und input_event_count bekommen sollte. - // Außerdem stehen m_display_width und m_display_height zur Verfügung: + // den GameState, input_events und input_event_count und die Displaygröße bekommen sollte: // - // kUpdate(input_events, input_event_count, m_display_width, m_display_height) + // kUpdate(&m_state, input_events, input_event_count, m_display_width, m_display_height) // // // Die Funktion könnte folgende Definition haben: - // void kUpdate(const TouchInputEvent* touch_events, + // void kUpdate(GameState* state, + // const TouchInputEvent* touch_events, // unsigned int touch_event_count, // float display_width, // float display_height) @@ -210,6 +210,11 @@ void NativeEngine::handleAppCmd(int32_t cmd) ALOGV("NativeEngine: Handling command %d.", cmd); switch (cmd) { case APP_CMD_SAVE_STATE: + ALOGV("NativeEngine:: APP_CMD_SAVE_STATE"); + m_state.has_focus = m_has_focus; + m_app->savedState = malloc(sizeof(m_state)); + *reinterpret_cast(m_app->savedState) = m_state; + m_app->savedStateSize = sizeof(m_state); break; case APP_CMD_INIT_WINDOW: ALOGV("NativeEngine:: APP_CMD_INIT_WINDOW"); @@ -217,7 +222,7 @@ void NativeEngine::handleAppCmd(int32_t cmd) m_has_window = true; SwappyGL_setWindow(m_app->window); if (m_app->savedStateSize == sizeof(m_state) && m_app->savedState) { - m_state = *reinterpret_cast(m_app->savedState); + m_state = *reinterpret_cast(m_app->savedState); m_has_focus = m_state.has_focus; } else { @@ -252,6 +257,9 @@ void NativeEngine::handleAppCmd(int32_t cmd) ALOGV("NativeEngine: APP_CMD_STOP"); m_is_visible = false; break; + case APP_CMD_RESUME: + ALOGV("NativeEngine: APP_CMD_RESUME"); + break; default: ALOGW("Unhandled command."); break; diff --git a/app/src/main/cpp/NativeEngine.h b/app/src/main/cpp/NativeEngine.h index f695de5..c906cfd 100644 --- a/app/src/main/cpp/NativeEngine.h +++ b/app/src/main/cpp/NativeEngine.h @@ -6,15 +6,11 @@ #include "Position.h" #include "StringRepository.h" +#include "GameState.h" #include #include -/// @brief State that is saved between app resumes -struct NativeEngineState -{ - bool has_focus; -}; /// @brief Interface to the android game activity class NativeEngine { @@ -52,7 +48,7 @@ private: bool m_has_focus; bool m_is_visible; - NativeEngineState m_state; + GameState m_state; EGLDisplay m_egl_display; EGLSurface m_egl_surface;