Avancées UI, menu principal dessiné
This commit is contained in:
parent
8d3f17c7e2
commit
50f6376bbf
7 changed files with 94 additions and 11 deletions
8
src/config.h
Normal file
8
src/config.h
Normal file
|
@ -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;
|
|
@ -66,6 +66,8 @@ void draw_machines(Game *game){
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_game(Game *game){
|
void draw_game(Game *game){
|
||||||
|
ClearBackground(BLACK);
|
||||||
|
|
||||||
BeginMode2D(game->camera);
|
BeginMode2D(game->camera);
|
||||||
draw_machines(game);
|
draw_machines(game);
|
||||||
draw_workers(game);
|
draw_workers(game);
|
||||||
|
|
|
@ -45,6 +45,7 @@ int init_game(Game *game){
|
||||||
srand(rand_seed);
|
srand(rand_seed);
|
||||||
|
|
||||||
game->paused = true;
|
game->paused = true;
|
||||||
|
game->exit = false;
|
||||||
game->ttime = 0;
|
game->ttime = 0;
|
||||||
game->camera = (Camera2D){.offset = {0, 0}, .zoom = 1};
|
game->camera = (Camera2D){.offset = {0, 0}, .zoom = 1};
|
||||||
|
|
||||||
|
|
|
@ -66,11 +66,9 @@ int main(){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while(!WindowShouldClose()){
|
while(!WindowShouldClose() && !game.exit){
|
||||||
BeginDrawing();
|
BeginDrawing();
|
||||||
|
|
||||||
ClearBackground(BLACK);
|
|
||||||
|
|
||||||
get_keys(&game);
|
get_keys(&game);
|
||||||
|
|
||||||
update(&game);
|
update(&game);
|
||||||
|
|
|
@ -110,6 +110,7 @@ typedef struct {
|
||||||
Camera2D camera;
|
Camera2D camera;
|
||||||
|
|
||||||
bool paused;
|
bool paused;
|
||||||
|
bool exit;
|
||||||
|
|
||||||
float ttime;
|
float ttime;
|
||||||
int t_per_frame;
|
int t_per_frame;
|
||||||
|
|
78
src/ui.c
78
src/ui.c
|
@ -12,12 +12,44 @@
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "draw.h"
|
#include "draw.h"
|
||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
char _ver_str[256];
|
||||||
|
char *VERSION_STR = _ver_str;
|
||||||
|
|
||||||
GUIInfo gui_info;
|
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){
|
void main_menu_draw(Widget *widget, Game *game){
|
||||||
DrawRectangleRec((Rectangle){.x = 0, .y = 0, .width = SCREEN_W, .height = SCREEN_H}
|
ClearBackground(color_main_brown);
|
||||||
, WHITE);
|
|
||||||
|
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){
|
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);
|
draw_game(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void main_menu_exit(Game *game){
|
||||||
|
game->exit = true;
|
||||||
|
}
|
||||||
|
|
||||||
Widget main_menu = {
|
Widget main_menu = {
|
||||||
.box = {.x = 0, .y = 0, .width = -1, .height = -1},
|
.box = {.x = 0, .y = 0, .width = -1, .height = -1},
|
||||||
.draw = &main_menu_draw,
|
.draw = &main_menu_draw,
|
||||||
.do_event = &main_menu_do_event,
|
.do_event = &main_menu_do_event,
|
||||||
.capt_flags = CF_Mouse | CF_Keyb,
|
.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 = {
|
Widget game_widget = {
|
||||||
|
@ -45,10 +111,14 @@ Widget game_widget = {
|
||||||
.draw = &widget_draw_game,
|
.draw = &widget_draw_game,
|
||||||
.do_event = NULL,
|
.do_event = NULL,
|
||||||
.capt_flags = 0,
|
.capt_flags = 0,
|
||||||
.buttons = {}
|
.buttons = {},
|
||||||
|
.button_n = 0
|
||||||
};
|
};
|
||||||
|
|
||||||
void init_ui(){
|
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};
|
gui_info = (GUIInfo){{&game_widget, &main_menu}, {false, true}, NULL};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
src/ui.h
11
src/ui.h
|
@ -42,14 +42,16 @@ struct Widget;
|
||||||
|
|
||||||
typedef void (widget_filler_t)(struct Widget*, Game*);
|
typedef void (widget_filler_t)(struct Widget*, Game*);
|
||||||
typedef void (widget_event_t)(struct Widget*, Game*, MTEvent);
|
typedef void (widget_event_t)(struct Widget*, Game*, MTEvent);
|
||||||
typedef void (button_handler_t)(void);
|
typedef void (button_handler_t)(Game *);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
|
||||||
Rectangle box;
|
Rectangle box;
|
||||||
Color color_0;
|
Color color_0; // Fill
|
||||||
Color color_1;
|
Color color_1; // Outline
|
||||||
int curr_select;
|
Color color_txt;
|
||||||
|
char *txt;
|
||||||
|
bool curr_select;
|
||||||
button_handler_t *handler;
|
button_handler_t *handler;
|
||||||
|
|
||||||
} Button;
|
} Button;
|
||||||
|
@ -61,6 +63,7 @@ typedef struct Widget {
|
||||||
widget_event_t *do_event;
|
widget_event_t *do_event;
|
||||||
int capt_flags;
|
int capt_flags;
|
||||||
Button buttons[10];
|
Button buttons[10];
|
||||||
|
int button_n;
|
||||||
|
|
||||||
} Widget;
|
} Widget;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue