From c401b7663d0854a7a3f5c35b6809faf65dc1fd66 Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Fri, 24 Jun 2022 17:53:46 +0200 Subject: Implement main menu --- src/menu/CMakeLists.txt | 5 ++++ src/menu/inc/menu.h | 15 +++++++++++ src/menu/src/menu.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 src/menu/CMakeLists.txt create mode 100644 src/menu/inc/menu.h create mode 100644 src/menu/src/menu.c (limited to 'src/menu') diff --git a/src/menu/CMakeLists.txt b/src/menu/CMakeLists.txt new file mode 100644 index 0000000..c4adb09 --- /dev/null +++ b/src/menu/CMakeLists.txt @@ -0,0 +1,5 @@ +add_library(menu + "src/menu.c" +) +target_include_directories(menu PUBLIC "inc") +target_link_libraries(menu PRIVATE camera game gfx gui system) diff --git a/src/menu/inc/menu.h b/src/menu/inc/menu.h new file mode 100644 index 0000000..4e92a3f --- /dev/null +++ b/src/menu/inc/menu.h @@ -0,0 +1,15 @@ +#ifndef MENU_H +#define MENU_H + +#ifdef __cplusplus +extern "C" +{ +#endif + +int menu(void); + +#ifdef __cplusplus +} +#endif + +#endif /* MENU_H */ diff --git a/src/menu/src/menu.c b/src/menu/src/menu.c new file mode 100644 index 0000000..12da1a8 --- /dev/null +++ b/src/menu/src/menu.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +static void on_pressed(void *const arg) +{ + *(bool *)arg = true; +} + +int menu(void) +{ + struct camera cam = {0}; + struct gui_button play; + union peripheral p; + bool start = false; + + if (game_resinit()) + return -1; + + cursor_init(&cam.cursor); + gui_button_init(&play); + play.on_pressed = on_pressed; + play.arg = &start; + play.w = 140; + play.label.text = "Play"; + + { + const struct peripheral_cfg cfg = + { + .type = PERIPHERAL_TYPE_KEYBOARD_MOUSE + }; + + peripheral_init(&cfg, &p); + } + + while (!start) + { + system_loop(); + peripheral_update(&p); + camera_update(&cam, &p); + + play.common.x = screen_w / 2 - play.w / 2; + play.common.y = screen_h / 2 - 20; + play.label.common.x = play.w / 2 - 20; + play.label.common.y = 4; + + if (gui_update(&play.common, &p, &cam)) + return -1; + + rect_get_or_ret(r, -1); + rect_init(r); + r->w = screen_w; + r->h = screen_h; + rect_sort(r); + + if (p.common.exit) + return 0; + + if (gui_render(&play.common) + || cursor_render(&cam.cursor) + || gfx_draw()) + return -1; + } + + return game(NULL); +} -- cgit v1.2.3