Menu principal "fonctionel"

This commit is contained in:
attilavs2 2025-02-26 12:02:13 +01:00
parent 5d4bccfd08
commit b03e459b83
7 changed files with 148 additions and 50 deletions

BIN
src/.ui.c.swo Normal file

Binary file not shown.

View file

@ -28,6 +28,8 @@ char *tex_files[TEX_N] = {
Texture2D tex_index[TEX_N]; Texture2D tex_index[TEX_N];
extern GUIInfo gui_info;
int init_draw(){ int init_draw(){
InitWindow(1280, 720, "Mineur Tycoon"); InitWindow(1280, 720, "Mineur Tycoon");
@ -63,12 +65,14 @@ void draw_machines(Game *game){
} }
} }
void draw(Game *game){ void draw_game(Game *game){
BeginMode2D(game->camera); BeginMode2D(game->camera);
draw_machines(game); draw_machines(game);
draw_workers(game); draw_workers(game);
EndMode2D(); EndMode2D();
}
void draw(Game *game){
draw_widgets(game); draw_widgets(game);
DrawFPS(0,0); DrawFPS(0,0);

View file

@ -12,6 +12,12 @@
#pragma once #pragma once
// Internal (game) res
#define SCREEN_W 1280
#define SCREEN_H 720
int init_draw(); int init_draw();
void draw_game(Game *game);
void draw(Game *game); void draw(Game *game);

View file

@ -44,7 +44,7 @@ int init_game(Game *game){
srand(rand_seed); srand(rand_seed);
game->paused = false; game->paused = true;
game->ttime = 0; game->ttime = 0;
game->camera = (Camera2D){.offset = {0, 0}, .zoom = 1}; game->camera = (Camera2D){.offset = {0, 0}, .zoom = 1};

View file

@ -18,6 +18,7 @@
int init(Game *game){ int init(Game *game){
int err = init_draw(); int err = init_draw();
err |= init_game(game); err |= init_game(game);
init_ui();
return err; return err;
} }
@ -27,31 +28,6 @@ void clean(Game *game){
CloseWindow(); CloseWindow();
} }
bool keydown(int key){
return IsKeyDown(key);
}
void get_keys(Game *game){
V2d mouse = GetMousePosition();
if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
try_interface(game, GetScreenToWorld2D(mouse,game->camera));
if(keydown(KEY_W))
game->camera.target.y -= 3;
if(keydown(KEY_S))
game->camera.target.y += 3;
if(keydown(KEY_A))
game->camera.target.x -= 3;
if(keydown(KEY_D))
game->camera.target.x += 3;
if(keydown(KEY_UP))
game->camera.zoom *= 1.1;
if(keydown(KEY_DOWN))
game->camera.zoom *= 1/1.1;
if(IsKeyPressed(KEY_P))
game->paused = !game->paused;
}
int main(){ int main(){
Game game; Game game;

121
src/ui.c
View file

@ -13,42 +13,100 @@
#include "draw.h" #include "draw.h"
#include "ui.h" #include "ui.h"
void main_menu_draw(Widget *widget, Game *game){ GUIInfo gui_info;
printf("main mem draw\n");
widget->refresh = false; void main_menu_draw(Widget *widget, Game *game){
DrawRectangleRec((Rectangle){.x = 0, .y = 0, .width = SCREEN_W, .height = SCREEN_H}
, WHITE);
} }
void main_menu_do_key(Widget *widget, Game *game, int key){ void main_menu_do_event(Widget *widget, Game *game, MTEvent event){
if(event.type == EV_Mouse && event.click == MOUSE_BUTTON_LEFT){
gui_info.widget_active[0] = true;
gui_info.widget_active[1] = false;
game->paused = false;
}
}
void widget_draw_game(Widget *widget, Game *game){
draw_game(game);
} }
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_key = &main_menu_do_key, .do_event = &main_menu_do_event,
.refresh = true, .capt_flags = CF_Mouse | CF_Keyb,
.capt_flags = CF_Mouse | CF_Keyboard,
.buttons = {} .buttons = {}
}; };
GUIInfo gui_info = {{&main_menu}, 1, 0, NULL}; Widget game_widget = {
.box = {0,0,0,0},
.draw = &widget_draw_game,
.do_event = NULL,
.capt_flags = 0,
.buttons = {}
};
void init_ui(){
gui_info = (GUIInfo){{&game_widget, &main_menu}, {false, true}, NULL};
}
void draw_widgets(Game *game){ void draw_widgets(Game *game){
for(int i = 0; i < gui_info.widget_n; i++){ for(int i = 0; i < WIDGET_N; i++){
if(!gui_info.widget_active[i])
continue;
Widget *w = gui_info.widgets[i]; Widget *w = gui_info.widgets[i];
if(w->draw && w->refresh) if(w->draw)
w->draw(w, game); w->draw(w, game);
} }
} }
void widgets_treat_key(Game *game, int key){ bool widget_coll(Rectangle widget, V2d point){
if(widget.width < 0)
widget.width = SCREEN_W;
if(widget.height < 0)
widget.height = SCREEN_H;
return CheckCollisionPointRec(point, widget);
}
bool widgets_treat_event(Game *game, MTEvent event){
bool treated = true;
for(int i = 0; i < WIDGET_N; i++){ for(int i = 0; i < WIDGET_N; i++){
Widget *w = gui_info.widgets[i]; if(!gui_info.widget_active[i])
if(!w->do_key)
continue; continue;
w->do_key(w, game, key); Widget *w = gui_info.widgets[i];
if(!w || !w->do_event)
continue;
if(!widget_coll(w->box, event.pos))
continue;
// Only actually call do_key if the event type matches event flags
switch(event.type){
default:
continue;
case EV_Mouse:
if(w->capt_flags & ~CF_Mouse)
break;
continue;
case EV_Keyb:
if(w->capt_flags & ~CF_Keyb)
break;
continue;
}
treated = false;
w->do_event(w, game, event);
} }
return treated;
}
bool ui_do_key(Game *game, int key){
MTEvent event = {.type = EV_Keyb, {.key = key}};
return widgets_treat_event(game, event);
}
bool ui_do_mouse(Game *game, V2d pos, int click){
MTEvent event = {.type = EV_Mouse, {.click = click}, .pos = pos};
return widgets_treat_event(game, event);
} }
void try_interface(Game *game, V2d pos){ void try_interface(Game *game, V2d pos){
@ -72,3 +130,38 @@ void try_interface(Game *game, V2d pos){
return; return;
} }
bool keydown(int key){
return IsKeyDown(key);
}
int get_mouse(){
for(int i = 0; i < MOUSE_BUTTON_NULL; i++){
if(IsMouseButtonPressed(i))
return i;
}
return MOUSE_BUTTON_NULL;
}
void get_keys(Game *game){
V2d mouse_pos = GetMousePosition();
int mouse_click = get_mouse();
if(ui_do_mouse(game, mouse_pos, mouse_click)){
if(mouse_click == MOUSE_BUTTON_LEFT)
try_interface(game, GetScreenToWorld2D(mouse_pos,game->camera));
}
if(keydown(KEY_W))
game->camera.target.y -= 3;
if(keydown(KEY_S))
game->camera.target.y += 3;
if(keydown(KEY_A))
game->camera.target.x -= 3;
if(keydown(KEY_D))
game->camera.target.x += 3;
if(keydown(KEY_UP))
game->camera.zoom *= 1.1;
if(keydown(KEY_DOWN))
game->camera.zoom *= 1/1.1;
if(IsKeyPressed(KEY_P))
game->paused = !game->paused;
}

View file

@ -13,18 +13,35 @@
#pragma once #pragma once
#define WIDGET_N 16 #define WIDGET_N 16
#define MOUSE_BUTTON_NULL 7
enum CaptureFlags { enum CaptureFlags {
CF_None = 0x0, CF_None = 0x0,
CF_Arrows = 0x2, CF_Mouse = 0x2, //Mouse clicks
CF_Mouse = 0x4, //Mouse clicks CF_Keyb = 0x4
CF_Keyboard = 0x8
}; };
enum EventTypes {
EV_None = 0,
EV_Mouse = 1,
EV_Keyb = 2
};
typedef struct {
int type;
union {
int key;
int click;
};
V2d pos;
} MTEvent;
struct Widget; struct Widget;
typedef void (widget_filler_t)(struct Widget*, Game*); typedef void (widget_filler_t)(struct Widget*, Game*);
typedef void (widget_dokey_t)(struct Widget*, Game*, int); typedef void (widget_event_t)(struct Widget*, Game*, MTEvent);
typedef void (button_handler_t)(void); typedef void (button_handler_t)(void);
typedef struct { typedef struct {
@ -41,8 +58,7 @@ typedef struct Widget {
Rectangle box; Rectangle box;
widget_filler_t *draw; widget_filler_t *draw;
widget_dokey_t *do_key; widget_event_t *do_event;
bool refresh;
int capt_flags; int capt_flags;
Button buttons[10]; Button buttons[10];
@ -51,14 +67,17 @@ typedef struct Widget {
typedef struct { typedef struct {
Widget *widgets[WIDGET_N]; Widget *widgets[WIDGET_N];
int widget_n; bool widget_active[WIDGET_N];
int active_widget;
float *zoom; float *zoom;
} GUIInfo; } GUIInfo;
void init_ui();
void draw_widgets(Game *game); void draw_widgets(Game *game);
// World pos // World pos
void try_interface(Game *game, V2d pos); void try_interface(Game *game, V2d pos);
void get_keys(Game *game);