Implement main menu

This commit is contained in:
Xavier Del Campo Romero 2022-06-24 17:53:46 +02:00
parent c0887a44fb
commit 19d9a571af
8 changed files with 106 additions and 12 deletions

View File

@ -43,6 +43,7 @@ set(components
header
instance
keyboard
menu
mouse
pad
peripheral
@ -61,7 +62,7 @@ set(interfaces
target_compile_options(${PROJECT_NAME} PUBLIC ${cflags})
# Dependencies for main.c
target_link_libraries(${PROJECT_NAME} PRIVATE system game)
target_link_libraries(${PROJECT_NAME} PRIVATE system menu)
foreach(c ${components})
add_subdirectory("src/${c}")

View File

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

View File

@ -6,8 +6,7 @@ extern "C"
{
#endif
int game_resinit(void);
void game_free(void);
#ifdef __cplusplus
}

View File

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

View File

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

5
src/menu/CMakeLists.txt Normal file
View File

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

15
src/menu/inc/menu.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef MENU_H
#define MENU_H
#ifdef __cplusplus
extern "C"
{
#endif
int menu(void);
#ifdef __cplusplus
}
#endif
#endif /* MENU_H */

71
src/menu/src/menu.c Normal file
View File

@ -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);
}