From 825cc88e1782de2ef5a9860a7d5ec418682f4459 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 8 Oct 2024 09:36:15 -0500 Subject: [PATCH] Moving some logic into separate files --- lib/AudioFile | 1 - src/dawn/display/draw/CMakeLists.txt | 1 + src/dawn/display/draw/drawstatemainmenu.c | 14 ++++++++ src/dawn/display/draw/drawstatemainmenu.h | 14 ++++++++ src/dawn/display/draw/drawstateoverworld.c | 16 ++++----- src/dawn/display/frame.c | 4 +++ src/dawn/game/CMakeLists.txt | 1 + src/dawn/game/game.c | 24 +++++++------ src/dawn/game/game.h | 3 +- src/dawn/game/state/CMakeLists.txt | 14 ++++++++ src/dawn/game/state/mainmenu.c | 14 ++++++++ src/dawn/game/state/mainmenu.h | 14 ++++++++ src/dawn/game/state/mapchange.c | 17 +++++++++ src/dawn/game/state/mapchange.h | 14 ++++++++ src/dawn/game/state/overworld.c | 19 ++++++++++ src/dawn/game/state/overworld.h | 14 ++++++++ src/dawn/ui/CMakeLists.txt | 1 + src/dawn/ui/mainmenu.c | 38 ++++++++++++++++++++ src/dawn/ui/mainmenu.h | 40 ++++++++++++++++++++++ src/dawn/ui/menu.c | 7 ++++ src/dawn/ui/menu.h | 12 ++++++- 21 files changed, 261 insertions(+), 21 deletions(-) delete mode 160000 lib/AudioFile create mode 100644 src/dawn/display/draw/drawstatemainmenu.c create mode 100644 src/dawn/display/draw/drawstatemainmenu.h create mode 100644 src/dawn/game/state/CMakeLists.txt create mode 100644 src/dawn/game/state/mainmenu.c create mode 100644 src/dawn/game/state/mainmenu.h create mode 100644 src/dawn/game/state/mapchange.c create mode 100644 src/dawn/game/state/mapchange.h create mode 100644 src/dawn/game/state/overworld.c create mode 100644 src/dawn/game/state/overworld.h create mode 100644 src/dawn/ui/mainmenu.c create mode 100644 src/dawn/ui/mainmenu.h diff --git a/lib/AudioFile b/lib/AudioFile deleted file mode 160000 index bcb61a47..00000000 --- a/lib/AudioFile +++ /dev/null @@ -1 +0,0 @@ -Subproject commit bcb61a47e64fe4e7fc3d8a8664b8a23a192bddb7 diff --git a/src/dawn/display/draw/CMakeLists.txt b/src/dawn/display/draw/CMakeLists.txt index 75dadbec..17cfd2e1 100644 --- a/src/dawn/display/draw/CMakeLists.txt +++ b/src/dawn/display/draw/CMakeLists.txt @@ -8,6 +8,7 @@ # Sources target_sources(${DAWN_TARGET_NAME} PRIVATE + drawstatemainmenu.c drawstateoverworld.c drawmap.c drawtext.c diff --git a/src/dawn/display/draw/drawstatemainmenu.c b/src/dawn/display/draw/drawstatemainmenu.c new file mode 100644 index 00000000..df8c9cb1 --- /dev/null +++ b/src/dawn/display/draw/drawstatemainmenu.c @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2024 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "drawstatemainmenu.h" +#include "display/draw/drawtext.h" + +void drawStateMainMenu() { + // Draw the logo + drawText("Dawn", 4, 0, 0, COLOR_WHITE); +} \ No newline at end of file diff --git a/src/dawn/display/draw/drawstatemainmenu.h b/src/dawn/display/draw/drawstatemainmenu.h new file mode 100644 index 00000000..7ae91588 --- /dev/null +++ b/src/dawn/display/draw/drawstatemainmenu.h @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2024 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "display/frame.h" + +/** + * Draws the main menu state. + */ +void drawStateMainMenu(); \ No newline at end of file diff --git a/src/dawn/display/draw/drawstateoverworld.c b/src/dawn/display/draw/drawstateoverworld.c index 36ff21a9..5b148fc1 100644 --- a/src/dawn/display/draw/drawstateoverworld.c +++ b/src/dawn/display/draw/drawstateoverworld.c @@ -27,14 +27,14 @@ void drawStateOverworld() { cameraPositionY = player->y; } - // drawMap( - // GAME.currentMap, - // cameraPositionX, cameraPositionY, - // 0, 0, - // FRAME_WIDTH, FRAME_HEIGHT - // ); + drawMap( + GAME.currentMap, + cameraPositionX, cameraPositionY, + 0, 0, + FRAME_WIDTH, FRAME_HEIGHT + ); // Draw UI - // drawUITextbox(); - drawUITestMenu(); + drawUITextbox(); + // drawUITestMenu(); } \ No newline at end of file diff --git a/src/dawn/display/frame.c b/src/dawn/display/frame.c index 3ec8ba0b..2e7e88ca 100644 --- a/src/dawn/display/frame.c +++ b/src/dawn/display/frame.c @@ -11,6 +11,7 @@ #include "display/draw/drawshape.h" #include "display/draw/drawtext.h" #include "display/draw/drawstateoverworld.h" +#include "display/draw/drawstatemainmenu.h" char_t FRAME_BUFFER[FRAME_HEIGHT * FRAME_WIDTH]; uint8_t FRAME_COLOR[FRAME_HEIGHT * FRAME_WIDTH]; @@ -37,6 +38,9 @@ void frameUpdate() { drawStateOverworld(); break; + case GAME_STATE_MAIN_MENU: + drawStateMainMenu(); + default: break; } diff --git a/src/dawn/game/CMakeLists.txt b/src/dawn/game/CMakeLists.txt index 3726ed9e..8bd09f58 100644 --- a/src/dawn/game/CMakeLists.txt +++ b/src/dawn/game/CMakeLists.txt @@ -4,6 +4,7 @@ # https://opensource.org/licenses/MIT # Subdirs +add_subdirectory(state) # Sources target_sources(${DAWN_TARGET_NAME} diff --git a/src/dawn/game/game.c b/src/dawn/game/game.c index 516f770f..38ee3a82 100644 --- a/src/dawn/game/game.c +++ b/src/dawn/game/game.c @@ -13,6 +13,11 @@ #include "asset/assetmap.h" #include "ui/textbox.h" #include "ui/testmenu.h" +#include "ui/mainmenu.h" + +#include "game/state/mainmenu.h" +#include "game/state/mapchange.h" +#include "game/state/overworld.h" game_t GAME; @@ -26,9 +31,10 @@ void gameInit() { textboxInit(); testMenuInit(); + mainMenuInit(); GAME.mapNext = MAP_LIST_TEST; - GAME.state = GAME_STATE_MAP_CHANGE; + GAME.state = GAME_STATE_INITIAL; } gameupdateresult_t gameUpdate(const float_t delta) { @@ -37,14 +43,11 @@ gameupdateresult_t gameUpdate(const float_t delta) { switch(GAME.state) { case GAME_STATE_INITIAL: - GAME.state = GAME_STATE_OVERWORLD; + GAME.state = GAME_STATE_MAIN_MENU; break; case GAME_STATE_OVERWORLD: - textboxUpdate(); - testMenuUpdate(); - if(GAME.currentMap) mapUpdate(GAME.currentMap); - if(inputWasPressed(INPUT_BIND_PAUSE)) GAME.state = GAME_STATE_PAUSED; + gameStateOverworldUpdate(); break; case GAME_STATE_PAUSED: @@ -52,10 +55,11 @@ gameupdateresult_t gameUpdate(const float_t delta) { break; case GAME_STATE_MAP_CHANGE: - assetMapLoad(MAP_LIST_PATHS[GAME.mapNext], &MAP_MAIN); - GAME.state = GAME_STATE_OVERWORLD; - GAME.currentMap = &MAP_MAIN; - map_t *test = &MAP_MAIN; + gameStateMapChangeUpdate(); + break; + + case GAME_STATE_MAIN_MENU: + gameStateMainMenuUpdate(); break; } diff --git a/src/dawn/game/game.h b/src/dawn/game/game.h index 444d98bc..0f24c676 100644 --- a/src/dawn/game/game.h +++ b/src/dawn/game/game.h @@ -17,7 +17,8 @@ typedef enum { GAME_STATE_INITIAL = 0, GAME_STATE_OVERWORLD = 1, GAME_STATE_PAUSED = 2, - GAME_STATE_MAP_CHANGE = 3 + GAME_STATE_MAP_CHANGE = 3, + GAME_STATE_MAIN_MENU = 4 } gamestate_t; typedef struct { diff --git a/src/dawn/game/state/CMakeLists.txt b/src/dawn/game/state/CMakeLists.txt new file mode 100644 index 00000000..ab907a8d --- /dev/null +++ b/src/dawn/game/state/CMakeLists.txt @@ -0,0 +1,14 @@ +# Copyright (c) 2024 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Subdirs + +# Sources +target_sources(${DAWN_TARGET_NAME} + PRIVATE + mainmenu.c + mapchange.c + overworld.c +) \ No newline at end of file diff --git a/src/dawn/game/state/mainmenu.c b/src/dawn/game/state/mainmenu.c new file mode 100644 index 00000000..63fbfa1d --- /dev/null +++ b/src/dawn/game/state/mainmenu.c @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2024 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "mainmenu.h" +#include "ui/mainmenu.h" + +void gameStateMainMenuUpdate() { + // Update the main menu + mainMenuUpdate(); +} \ No newline at end of file diff --git a/src/dawn/game/state/mainmenu.h b/src/dawn/game/state/mainmenu.h new file mode 100644 index 00000000..acb0ed21 --- /dev/null +++ b/src/dawn/game/state/mainmenu.h @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2024 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "dawn.h" + +/** + * Updates the main menu state. + */ +void gameStateMainMenuUpdate(); \ No newline at end of file diff --git a/src/dawn/game/state/mapchange.c b/src/dawn/game/state/mapchange.c new file mode 100644 index 00000000..1060ad15 --- /dev/null +++ b/src/dawn/game/state/mapchange.c @@ -0,0 +1,17 @@ +/** + * Copyright (c) 2024 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "mapchange.h" +#include "asset/assetmap.h" +#include "game/game.h" + +void gameStateMapChangeUpdate() { + assetMapLoad(MAP_LIST_PATHS[GAME.mapNext], &MAP_MAIN); + GAME.state = GAME_STATE_OVERWORLD; + GAME.currentMap = &MAP_MAIN; + map_t *test = &MAP_MAIN; +} \ No newline at end of file diff --git a/src/dawn/game/state/mapchange.h b/src/dawn/game/state/mapchange.h new file mode 100644 index 00000000..7a63fd8d --- /dev/null +++ b/src/dawn/game/state/mapchange.h @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2024 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "dawn.h" + +/** + * Updates the map change game state. + */ +void gameStateMapChangeUpdate(); \ No newline at end of file diff --git a/src/dawn/game/state/overworld.c b/src/dawn/game/state/overworld.c new file mode 100644 index 00000000..cec81808 --- /dev/null +++ b/src/dawn/game/state/overworld.c @@ -0,0 +1,19 @@ +/** + * Copyright (c) 2024 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "overworld.h" +#include "game/game.h" +#include "input.h" +#include "ui/textbox.h" +#include "ui/testmenu.h" + +void gameStateOverworldUpdate() { + textboxUpdate(); + testMenuUpdate(); + if(GAME.currentMap) mapUpdate(GAME.currentMap); + if(inputWasPressed(INPUT_BIND_PAUSE)) GAME.state = GAME_STATE_PAUSED; +} \ No newline at end of file diff --git a/src/dawn/game/state/overworld.h b/src/dawn/game/state/overworld.h new file mode 100644 index 00000000..eecce679 --- /dev/null +++ b/src/dawn/game/state/overworld.h @@ -0,0 +1,14 @@ +/** + * Copyright (c) 2024 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "dawn.h" + +/** + * Updates the overworld game state. + */ +void gameStateOverworldUpdate(); \ No newline at end of file diff --git a/src/dawn/ui/CMakeLists.txt b/src/dawn/ui/CMakeLists.txt index 662b6713..d23bd18a 100644 --- a/src/dawn/ui/CMakeLists.txt +++ b/src/dawn/ui/CMakeLists.txt @@ -11,4 +11,5 @@ target_sources(${DAWN_TARGET_NAME} menu.c textbox.c testmenu.c + mainmenu.c ) \ No newline at end of file diff --git a/src/dawn/ui/mainmenu.c b/src/dawn/ui/mainmenu.c new file mode 100644 index 00000000..f4fe9a28 --- /dev/null +++ b/src/dawn/ui/mainmenu.c @@ -0,0 +1,38 @@ +/** + * Copyright (c) 2024 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "mainmenu.h" + +mainmenu_t MAIN_MENU; + +void mainMenuInit() { + const char_t* strings[] = { + "New Game", + "Load Game", + "Options", + "Exit" + }; + + uint16_t columns = 1; + uint16_t rows = 4; + menuInit(&MAIN_MENU.menu, columns, rows); + memcpy(MAIN_MENU.menu.strings, strings, sizeof(strings)); + MAIN_MENU.menu.selectCallback = mainMenuSelectCallback; +} + +void mainMenuUpdate() { + menuUpdate(&MAIN_MENU.menu); +} + +void mainMenuSelectCallback( + const menu_t *menu, + const uint16_t x, + const uint16_t y, + const char_t *str +) { + printf("Selected: %s\n", str); +} \ No newline at end of file diff --git a/src/dawn/ui/mainmenu.h b/src/dawn/ui/mainmenu.h new file mode 100644 index 00000000..b66b3a63 --- /dev/null +++ b/src/dawn/ui/mainmenu.h @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2024 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#pragma once +#include "ui/menu.h" + +typedef struct { + menu_t menu; +} mainmenu_t; + +extern mainmenu_t MAIN_MENU; + +/** + * Initializes the main menu. + */ +void mainMenuInit(); + +/** + * Updates the main menu. + */ +void mainMenuUpdate(); + +/** + * Callback for when a main menu item is selected. + * + * @param menu The menu that was selected. + * @param x The x position of the selected item. + * @param y The y position of the selected item. + * @param str The string of the selected item. + */ +void mainMenuSelectCallback( + const menu_t *menu, + const uint16_t x, + const uint16_t y, + const char_t *str +); \ No newline at end of file diff --git a/src/dawn/ui/menu.c b/src/dawn/ui/menu.c index e22e2e7d..d57f69d4 100644 --- a/src/dawn/ui/menu.c +++ b/src/dawn/ui/menu.c @@ -36,6 +36,13 @@ void menuUpdate(menu_t *menu) { } else if(inputWasPressed(INPUT_BIND_RIGHT)) { menu->repeatHeld = 0.0f; return menuPositionMove(menu, MENU_DIRECTION_RIGHT); + } else if(inputWasPressed(INPUT_BIND_ACCEPT) && menu->selectCallback) { + menu->selectCallback( + menu, + menu->x, menu->y, + menu->strings[(menu->y * menu->columns) + menu->x] + ); + return; } // Handle repeat diff --git a/src/dawn/ui/menu.h b/src/dawn/ui/menu.h index dc4be137..11c922a0 100644 --- a/src/dawn/ui/menu.h +++ b/src/dawn/ui/menu.h @@ -19,7 +19,9 @@ typedef enum { MENU_DIRECTION_RIGHT = 3 } menudirection_t; -typedef struct { +typedef struct _menu_t menu_t; + +typedef struct _menu_t { uint16_t x, y; uint16_t rows, columns; float_t repeatHeld; @@ -27,6 +29,14 @@ typedef struct { // Visual things uint16_t renderOffsetX, renderOffsetY; + + // Callbacks + void (*selectCallback)( + const menu_t *menu, + const uint16_t x, + const uint16_t y, + const char_t *str + ); } menu_t; /**