diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..05a3de8 --- /dev/null +++ b/src/config.h @@ -0,0 +1,8 @@ +#pragma once + +#define VERSION_MAJ 0 +#define VERSION_MIN 1 +#define VERSION_PATCH 0 + +// Set in ui.c +extern char *VERSION_STR; diff --git a/src/draw.c b/src/draw.c index a81cc4c..dae9701 100644 --- a/src/draw.c +++ b/src/draw.c @@ -66,6 +66,8 @@ void draw_machines(Game *game){ } void draw_game(Game *game){ + ClearBackground(BLACK); + BeginMode2D(game->camera); draw_machines(game); draw_workers(game); diff --git a/src/game.c b/src/game.c index d97aeea..acdb42f 100644 --- a/src/game.c +++ b/src/game.c @@ -45,6 +45,7 @@ int init_game(Game *game){ srand(rand_seed); game->paused = true; + game->exit = false; game->ttime = 0; game->camera = (Camera2D){.offset = {0, 0}, .zoom = 1}; diff --git a/src/main.c b/src/main.c index 9bcccc5..88b9120 100644 --- a/src/main.c +++ b/src/main.c @@ -66,11 +66,9 @@ int main(){ } } - while(!WindowShouldClose()){ + while(!WindowShouldClose() && !game.exit){ BeginDrawing(); - ClearBackground(BLACK); - get_keys(&game); update(&game); diff --git a/src/types.h b/src/types.h index 00f8b4b..258b758 100644 --- a/src/types.h +++ b/src/types.h @@ -110,6 +110,7 @@ typedef struct { Camera2D camera; bool paused; + bool exit; float ttime; int t_per_frame; diff --git a/src/ui.c b/src/ui.c index 969a217..fc4010b 100644 --- a/src/ui.c +++ b/src/ui.c @@ -12,12 +12,44 @@ #include "game.h" #include "draw.h" #include "ui.h" +#include "config.h" + +char _ver_str[256]; +char *VERSION_STR = _ver_str; GUIInfo gui_info; +#define color_main_brown (Color){247, 239, 206, 255} +#define color_main_brown1 (Color){161, 151, 112, 255} + +void draw_text_centered(char *str, int y, int font_size, Color color){ + int width = MeasureText(str, font_size); + DrawText(str, 640-width/2, y, font_size, color); +} + +void draw_buttons(Widget *widget){ + for(int i = 0; i < widget->button_n; i++){ + Button *butt = &widget->buttons[i]; + DrawRectangleRounded(butt->box, 0.1, 3, butt->color_0); + Color outline_color = butt->curr_select ? SKYBLUE : butt->color_1; + DrawRectangleRoundedLines(butt->box, 0.1, 3, 2, outline_color); + int font_size = 0.75*butt->box.height; + int width = MeasureText(butt->txt, font_size); + int text_x = butt->box.x + butt->box.width/2 - width/2; + int text_y = butt->box.y + butt->box.height/5; + DrawText(butt->txt, text_x, text_y, font_size, butt->color_txt); + } +} + void main_menu_draw(Widget *widget, Game *game){ - DrawRectangleRec((Rectangle){.x = 0, .y = 0, .width = SCREEN_W, .height = SCREEN_H} - , WHITE); + ClearBackground(color_main_brown); + + draw_text_centered("Mineur Tycoon", 50, 100, BLACK); + draw_text_centered(VERSION_STR, 160, 30, BLACK); + + DrawRectangleRounded((Rectangle){500, 200, 280, 260}, 0.1, 3, color_main_brown1); + + draw_buttons(widget); } void main_menu_do_event(Widget *widget, Game *game, MTEvent event){ @@ -32,12 +64,46 @@ void widget_draw_game(Widget *widget, Game *game){ draw_game(game); } +void main_menu_exit(Game *game){ + game->exit = true; +} + Widget main_menu = { .box = {.x = 0, .y = 0, .width = -1, .height = -1}, .draw = &main_menu_draw, .do_event = &main_menu_do_event, .capt_flags = CF_Mouse | CF_Keyb, - .buttons = {} + .buttons = { + {.box = {520,220,240,40}, + .color_0 = WHITE, + .color_1 = PINK, + .color_txt = BLACK, + .txt = "New game", + .handler = NULL + }, + {.box = {520,270,240,40}, + .color_0 = WHITE, + .color_1 = PINK, + .color_txt = GRAY, + .txt = "Load game", + .handler = NULL + }, + {.box = {520,320,240,40}, + .color_0 = WHITE, + .color_1 = PINK, + .color_txt = GRAY, + .txt = "Settings", + .handler = NULL + }, + {.box = {520,400,240,40}, + .color_0 = WHITE, + .color_1 = PINK, + .color_txt = GRAY, + .txt = "Exit", + .handler = main_menu_exit + }, + }, + .button_n = 4 }; Widget game_widget = { @@ -45,10 +111,14 @@ Widget game_widget = { .draw = &widget_draw_game, .do_event = NULL, .capt_flags = 0, - .buttons = {} + .buttons = {}, + .button_n = 0 }; void init_ui(){ + snprintf(_ver_str, 256, "v%d.%d-%d", VERSION_MAJ, VERSION_MIN, + VERSION_PATCH); + gui_info = (GUIInfo){{&game_widget, &main_menu}, {false, true}, NULL}; } diff --git a/src/ui.h b/src/ui.h index 205d5e1..814a704 100644 --- a/src/ui.h +++ b/src/ui.h @@ -42,14 +42,16 @@ struct Widget; typedef void (widget_filler_t)(struct Widget*, Game*); typedef void (widget_event_t)(struct Widget*, Game*, MTEvent); -typedef void (button_handler_t)(void); +typedef void (button_handler_t)(Game *); typedef struct { Rectangle box; - Color color_0; - Color color_1; - int curr_select; + Color color_0; // Fill + Color color_1; // Outline + Color color_txt; + char *txt; + bool curr_select; button_handler_t *handler; } Button; @@ -61,6 +63,7 @@ typedef struct Widget { widget_event_t *do_event; int capt_flags; Button buttons[10]; + int button_n; } Widget;