From 9d4da63e36435e7ef4813fcca66fa5b8c04f614f Mon Sep 17 00:00:00 2001 From: Vlad Date: Sun, 19 Apr 2026 13:46:44 +0600 Subject: [PATCH] added menu --- resources/config2/about.json | 33 +++++++ resources/config2/help.json | 33 +++++++ resources/config2/main_menu.json | 55 +++++++++++ resources/config2/navigation2.json | 1 - src/Game.cpp | 151 ++++++++++++++++++----------- src/MenuManager.cpp | 58 +++++++++++ src/MenuManager.h | 13 ++- 7 files changed, 282 insertions(+), 62 deletions(-) create mode 100644 resources/config2/about.json create mode 100644 resources/config2/help.json create mode 100644 resources/config2/main_menu.json diff --git a/resources/config2/about.json b/resources/config2/about.json new file mode 100644 index 0000000..2ea3954 --- /dev/null +++ b/resources/config2/about.json @@ -0,0 +1,33 @@ +{ + "root": { + "type": "LinearLayout", + "orientation": "vertical", + "horizontal_align": "center", + "vertical_align": "center", + "spacing": 30, + "x": 0, + "y": 0, + "width": "match_parent", + "height": "match_parent", + "children": [ + { + "type": "StaticImage", + "name": "aboutImage", + "width": 400, + "height": 300, + "texture": "resources/loading.png" + }, + { + "type": "Button", + "name": "backButton", + "width": 200, + "height": 60, + "textures": { + "normal": "resources/loading.png", + "hover": "resources/loading.png", + "pressed": "resources/loading.png" + } + } + ] + } +} \ No newline at end of file diff --git a/resources/config2/help.json b/resources/config2/help.json new file mode 100644 index 0000000..63553f7 --- /dev/null +++ b/resources/config2/help.json @@ -0,0 +1,33 @@ +{ + "root": { + "type": "LinearLayout", + "orientation": "vertical", + "horizontal_align": "center", + "vertical_align": "center", + "spacing": 30, + "x": 0, + "y": 0, + "width": "match_parent", + "height": "match_parent", + "children": [ + { + "type": "StaticImage", + "name": "helpImage", + "width": 400, + "height": 300, + "texture": "resources/loading.png" + }, + { + "type": "Button", + "name": "backButton", + "width": 200, + "height": 60, + "textures": { + "normal": "resources/loading.png", + "hover": "resources/loading.png", + "pressed": "resources/loading.png" + } + } + ] + } +} \ No newline at end of file diff --git a/resources/config2/main_menu.json b/resources/config2/main_menu.json new file mode 100644 index 0000000..2b8e036 --- /dev/null +++ b/resources/config2/main_menu.json @@ -0,0 +1,55 @@ +{ + "root": { + "type": "LinearLayout", + "orientation": "vertical", + "horizontal_align": "center", + "vertical_align": "center", + "spacing": 20, + "x": 0, + "y": 0, + "width": "match_parent", + "height": "match_parent", + "children": [ + { + "type": "StaticImage", + "name": "title", + "width": 300, + "height": 100, + "texture": "resources/loading.png" + }, + { + "type": "Button", + "name": "startButton", + "width": 300, + "height": 60, + "textures": { + "normal": "resources/loading.png", + "hover": "resources/loading.png", + "pressed": "resources/loading.png" + } + }, + { + "type": "Button", + "name": "helpButton", + "width": 300, + "height": 60, + "textures": { + "normal": "resources/loading.png", + "hover": "resources/loading.png", + "pressed": "resources/loading.png" + } + }, + { + "type": "Button", + "name": "aboutButton", + "width": 300, + "height": 60, + "textures": { + "normal": "resources/loading.png", + "hover": "resources/loading.png", + "pressed": "resources/loading.png" + } + } + ] + } +} \ No newline at end of file diff --git a/resources/config2/navigation2.json b/resources/config2/navigation2.json index b705b71..59f56ee 100644 --- a/resources/config2/navigation2.json +++ b/resources/config2/navigation2.json @@ -114,7 +114,6 @@ [-0.2, -5.5] ] }, - }, { "name": "main_corridor10x", "available": true, diff --git a/src/Game.cpp b/src/Game.cpp index 67b6554..8da2a20 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -222,6 +222,9 @@ namespace ZL std::cout << "Audio initialization failed" << std::endl; } + menuManager.setupMainMenu(); + + SDL_ShowCursor(SDL_ENABLE); } void Game::drawUI() @@ -235,7 +238,22 @@ namespace ZL renderer.RenderUniform1i(textureUniformName, 0); glEnable(GL_BLEND); - menuManager.uiManager.draw(renderer); + if (menuManager.getState() == GameState::MainMenu || + menuManager.getState() == GameState::HelpScreen || + menuManager.getState() == GameState::AboutMenu) + { + SDL_ShowCursor(SDL_ENABLE); + } + else if (menuManager.getState() == GameState::Gameplay) + { + SDL_ShowCursor(SDL_DISABLE); + } + + if (menuManager.getState() != GameState::Gameplay) + { + menuManager.uiManager.draw(renderer); + } + if (currentLocation) { currentLocation->dialogueSystem.draw(renderer); @@ -252,32 +270,28 @@ namespace ZL void Game::drawScene() { glViewport(0, 0, Environment::width, Environment::height); + if (!loadingCompleted) { drawLoading(); + return; } - else + + if (menuManager.getState() == GameState::MainMenu || + menuManager.getState() == GameState::HelpScreen || + menuManager.getState() == GameState::AboutMenu) { - if (currentLocation) - { - /* - if (currentLocation->shadowMap) { - CheckGlError(__FILE__, __LINE__); - currentLocation->drawShadowDepthPass(); - CheckGlError(__FILE__, __LINE__); - currentLocation->drawGameWithShadows(); - CheckGlError(__FILE__, __LINE__); - } - else {*/ - currentLocation->drawGame(); - CheckGlError(__FILE__, __LINE__); - //} - } - else - { - // ??? Main menu??? - } + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); drawUI(); + return; } + + if (currentLocation) + { + currentLocation->drawGame(); + CheckGlError(__FILE__, __LINE__); + } + + drawUI(); CheckGlError(__FILE__, __LINE__); } @@ -376,7 +390,11 @@ namespace ZL } if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) { - SDL_SetRelativeMouseMode(SDL_TRUE); + if (menuManager.getState() == GameState::Gameplay) { + SDL_SetRelativeMouseMode(SDL_TRUE); + } else { + SDL_SetRelativeMouseMode(SDL_FALSE); + } } if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_FOCUS_LOST) { SDL_SetRelativeMouseMode(SDL_FALSE); @@ -435,8 +453,9 @@ namespace ZL std::cout << "\n========== MOUSE DOWN EVENT ==========" << std::endl; handleDown(ZL::UiManager::MOUSE_FINGER_ID, mx, my); - if (menuManager.uiManager.isUiInteractionForFinger(ZL::UiManager::MOUSE_FINGER_ID)) { - std::cout << "[CLICK] UI handled, skipping character movement" << std::endl; + if (menuManager.getState() != GameState::Gameplay || + menuManager.uiManager.isUiInteractionForFinger(ZL::UiManager::MOUSE_FINGER_ID)) { + std::cout << "[CLICK] UI handled or in menu, skipping game logic" << std::endl; continue; } @@ -450,22 +469,25 @@ namespace ZL } } else if (event.button.button == SDL_BUTTON_RIGHT) { - if (event.type == SDL_MOUSEBUTTONDOWN) { - rightMouseDown = true; - lastMouseX = event.button.x; - lastMouseY = event.button.y; - if (currentLocation) - { - currentLocation->rightMouseDown = true; - currentLocation->lastMouseX = event.button.x; - currentLocation->lastMouseY = event.button.y; + if (menuManager.getState() == GameState::Gameplay) + { + if (event.type == SDL_MOUSEBUTTONDOWN) { + rightMouseDown = true; + lastMouseX = event.button.x; + lastMouseY = event.button.y; + if (currentLocation) + { + currentLocation->rightMouseDown = true; + currentLocation->lastMouseX = event.button.x; + currentLocation->lastMouseY = event.button.y; + } } - } - else { - rightMouseDown = false; - if (currentLocation) - { - currentLocation->rightMouseDown = false; + else { + rightMouseDown = false; + if (currentLocation) + { + currentLocation->rightMouseDown = false; + } } } } @@ -474,22 +496,25 @@ namespace ZL int mx = static_cast((float)event.motion.x / Environment::width * Environment::projectionWidth); int my = static_cast((float)event.motion.y / Environment::height * Environment::projectionHeight); handleMotion(ZL::UiManager::MOUSE_FINGER_ID, mx, my); - if (currentLocation) + + if (menuManager.getState() == GameState::Gameplay && currentLocation) { currentLocation->handleMotion(ZL::UiManager::MOUSE_FINGER_ID, event.motion.xrel, event.motion.yrel, mx, my); } } if (event.type == SDL_MOUSEWHEEL) { - static const float zoomstep = 2.0f; - if (event.wheel.y > 0) { - Environment::zoom -= zoomstep; - } - else if (event.wheel.y < 0) { - Environment::zoom += zoomstep; - } - if (Environment::zoom < zoomstep) { - Environment::zoom = zoomstep; + if (menuManager.getState() == GameState::Gameplay) { + static const float zoomstep = 2.0f; + if (event.wheel.y > 0) { + Environment::zoom -= zoomstep; + } + else if (event.wheel.y < 0) { + Environment::zoom += zoomstep; + } + if (Environment::zoom < zoomstep) { + Environment::zoom = zoomstep; + } } } @@ -506,21 +531,25 @@ namespace ZL break; case SDLK_c: { - std::cout << "[GAME] Location change triggered by key press" << std::endl; - static size_t locIndex = 0; - static std::vector locations = {"forest", "default"}; - locIndex = (locIndex + 1) % locations.size(); - changeLocation(locations[locIndex]); + if (menuManager.getState() == GameState::Gameplay) { + std::cout << "[GAME] Location change triggered by key press" << std::endl; + static size_t locIndex = 0; + static std::vector locations = {"forest", "default"}; + locIndex = (locIndex + 1) % locations.size(); + changeLocation(locations[locIndex]); + } break; } case SDLK_f: - currentLocation->dialogueSystem.startDialogue("dialogue_gas1"); + if (currentLocation) currentLocation->dialogueSystem.startDialogue("dialogue_gas1"); break; case SDLK_e: //currentLocation->dialogueSystem.startDialogue("test_cutscene_pan_dialogue"); - currentLocation->navigation.build({}, "resources/config2/navigation2.json", CONST_ZIP_FILE); - currentLocation->buildDebugNavMeshes(); + if (currentLocation) { + currentLocation->navigation.build({}, "resources/config2/navigation2.json", CONST_ZIP_FILE); + currentLocation->buildDebugNavMeshes(); + } break; case SDLK_p: @@ -538,7 +567,9 @@ namespace ZL case SDLK_d: case SDLK_u: case SDLK_i: - if (currentLocation) currentLocation->handleKeyDown(event.key.keysym.sym); + if (menuManager.getState() == GameState::Gameplay && currentLocation) { + currentLocation->handleKeyDown(event.key.keysym.sym); + } break; case SDLK_RETURN: @@ -575,7 +606,9 @@ namespace ZL case SDLK_a: case SDLK_d: case SDLK_i: - if (currentLocation) currentLocation->handleKeyUp(event.key.keysym.sym); + if (menuManager.getState() == GameState::Gameplay && currentLocation) { + currentLocation->handleKeyUp(event.key.keysym.sym); + } break; default: break; } diff --git a/src/MenuManager.cpp b/src/MenuManager.cpp index ef77a4e..352b548 100644 --- a/src/MenuManager.cpp +++ b/src/MenuManager.cpp @@ -15,6 +15,64 @@ namespace ZL { { } + void MenuManager::setupMainMenu() + { + currentState = GameState::MainMenu; + uiManager.loadFromFile("resources/config2/main_menu.json", renderer, CONST_ZIP_FILE); + + uiManager.setButtonCallback("startButton", [this](const std::string&) { + startGame(); + }); + + uiManager.setButtonCallback("helpButton", [this](const std::string&) { + enterHelpMenu(); + }); + + uiManager.setButtonCallback("aboutButton", [this](const std::string&) { + enterAboutMenu(); + }); + } + + void MenuManager::setupHelpMenu() + { + currentState = GameState::HelpScreen; + uiManager.loadFromFile("resources/config2/help.json", renderer, CONST_ZIP_FILE); + + uiManager.setButtonCallback("backButton", [this](const std::string&) { + enterMainMenu(); + }); + } + + void MenuManager::setupAboutMenu() + { + currentState = GameState::AboutMenu; + uiManager.loadFromFile("resources/config2/about.json", renderer, CONST_ZIP_FILE); + + uiManager.setButtonCallback("backButton", [this](const std::string&) { + enterMainMenu(); + }); + } + + void MenuManager::enterMainMenu() + { + setupMainMenu(); + } + + void MenuManager::enterHelpMenu() + { + setupHelpMenu(); + } + + void MenuManager::enterAboutMenu() + { + setupAboutMenu(); + } + + void MenuManager::startGame() + { + currentState = GameState::Gameplay; + } + /* void MenuManager::setupMenu() { diff --git a/src/MenuManager.h b/src/MenuManager.h index e88912e..d012ec8 100644 --- a/src/MenuManager.h +++ b/src/MenuManager.h @@ -56,10 +56,19 @@ namespace ZL { */ public: UiManager uiManager; + GameState currentState = GameState::MainMenu; MenuManager(Renderer& iRenderer); - - //void setupMenu(); + + void setupMainMenu(); + void setupHelpMenu(); + void setupAboutMenu(); + void enterMainMenu(); + void enterHelpMenu(); + void enterAboutMenu(); + void startGame(); + + GameState getState() const { return currentState; } /* // Returns true for states where Space should render and run (Gameplay, GameOver, ConnectionLost)