aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-06-24 17:53:46 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2022-06-26 21:51:47 +0200
commitc401b7663d0854a7a3f5c35b6809faf65dc1fd66 (patch)
tree68e2f9af0115bc48fcc34ba41ceb1461bccf5f1d /src
parent70bc98f96c3e4ae7a45ff095c5f539711aaf7fb1 (diff)
Implement main menu
Diffstat (limited to 'src')
-rw-r--r--src/game/inc/game.h10
-rw-r--r--src/game/privinc/game_private.h3
-rw-r--r--src/game/src/game.c7
-rw-r--r--src/main.c4
-rw-r--r--src/menu/CMakeLists.txt5
-rw-r--r--src/menu/inc/menu.h15
-rw-r--r--src/menu/src/menu.c71
7 files changed, 104 insertions, 11 deletions
diff --git a/src/game/inc/game.h b/src/game/inc/game.h
index 8275d54..5298b9f 100644
--- a/src/game/inc/game.h
+++ b/src/game/inc/game.h
@@ -1,12 +1,20 @@
#ifndef GAME_H
#define GAME_H
+#include <stddef.h>
+
#ifdef __cplusplus
extern "C"
{
#endif
-int game(void);
+struct game_cfg
+{
+};
+
+int game_resinit(void);
+int game(const struct game_cfg *cfg);
+void game_free(void);
#ifdef __cplusplus
}
diff --git a/src/game/privinc/game_private.h b/src/game/privinc/game_private.h
index 1b9449f..3ca8d62 100644
--- a/src/game/privinc/game_private.h
+++ b/src/game/privinc/game_private.h
@@ -6,8 +6,7 @@ extern "C"
{
#endif
-int game_resinit(void);
-void game_free(void);
+
#ifdef __cplusplus
}
diff --git a/src/game/src/game.c b/src/game/src/game.c
index bcab5f2..83be2fd 100644
--- a/src/game/src/game.c
+++ b/src/game/src/game.c
@@ -8,13 +8,9 @@
#include <terrain.h>
#include <stddef.h>
-int game(void)
+int game(const struct game_cfg *const cfg)
{
int ret = -1;
-
- if (game_resinit())
- goto end;
-
enum {HUMAN_PLAYERS = 1, MAP_RESOURCES = 3};
struct human_player humans[HUMAN_PLAYERS];
struct terrain_map map;
@@ -109,6 +105,5 @@ int game(void)
ret = 0;
end:
- game_free();
return ret;
}
diff --git a/src/main.c b/src/main.c
index fd0759f..8933639 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,4 +1,4 @@
-#include <game.h>
+#include <menu.h>
#include <system.h>
#include <stdlib.h>
@@ -6,7 +6,7 @@ int main(void)
{
int ret = EXIT_SUCCESS;
- if (system_init() || game())
+ if (system_init() || menu())
ret = EXIT_FAILURE;
system_deinit();
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 <menu.h>
+#include <camera.h>
+#include <game.h>
+#include <gfx.h>
+#include <gui.h>
+#include <gui/button.h>
+#include <system.h>
+#include <stdbool.h>
+
+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);
+}