Handling state

This commit is contained in:
Kevin Trogant 2022-10-17 18:45:09 +02:00
parent dd8fbe4c2f
commit b8a0ab5468
5 changed files with 53 additions and 25 deletions

View File

@ -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.
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.

View File

@ -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()

View File

@ -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

View File

@ -13,7 +13,7 @@
#include <swappy/swappyGL.h>
#include <GLES3/gl3.h>
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<GameState*>(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<NativeEngineState*>(m_app->savedState);
m_state = *reinterpret_cast<GameState*>(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;

View File

@ -6,15 +6,11 @@
#include "Position.h"
#include "StringRepository.h"
#include "GameState.h"
#include <game-activity/native_app_glue/android_native_app_glue.h>
#include <EGL/egl.h>
/// @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;