Menu principal "fonctionel"
This commit is contained in:
parent
5d4bccfd08
commit
b03e459b83
7 changed files with 148 additions and 50 deletions
BIN
src/.ui.c.swo
Normal file
BIN
src/.ui.c.swo
Normal file
Binary file not shown.
|
@ -28,6 +28,8 @@ char *tex_files[TEX_N] = {
|
|||
|
||||
Texture2D tex_index[TEX_N];
|
||||
|
||||
extern GUIInfo gui_info;
|
||||
|
||||
int init_draw(){
|
||||
|
||||
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);
|
||||
draw_machines(game);
|
||||
draw_workers(game);
|
||||
EndMode2D();
|
||||
}
|
||||
|
||||
void draw(Game *game){
|
||||
draw_widgets(game);
|
||||
|
||||
DrawFPS(0,0);
|
||||
|
|
|
@ -12,6 +12,12 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
// Internal (game) res
|
||||
#define SCREEN_W 1280
|
||||
#define SCREEN_H 720
|
||||
|
||||
int init_draw();
|
||||
|
||||
void draw_game(Game *game);
|
||||
|
||||
void draw(Game *game);
|
||||
|
|
|
@ -44,7 +44,7 @@ int init_game(Game *game){
|
|||
|
||||
srand(rand_seed);
|
||||
|
||||
game->paused = false;
|
||||
game->paused = true;
|
||||
game->ttime = 0;
|
||||
game->camera = (Camera2D){.offset = {0, 0}, .zoom = 1};
|
||||
|
||||
|
|
26
src/main.c
26
src/main.c
|
@ -18,6 +18,7 @@
|
|||
int init(Game *game){
|
||||
int err = init_draw();
|
||||
err |= init_game(game);
|
||||
init_ui();
|
||||
return err;
|
||||
}
|
||||
|
||||
|
@ -27,31 +28,6 @@ void clean(Game *game){
|
|||
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(){
|
||||
|
||||
Game game;
|
||||
|
|
121
src/ui.c
121
src/ui.c
|
@ -13,42 +13,100 @@
|
|||
#include "draw.h"
|
||||
#include "ui.h"
|
||||
|
||||
void main_menu_draw(Widget *widget, Game *game){
|
||||
printf("main mem draw\n");
|
||||
GUIInfo gui_info;
|
||||
|
||||
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 = {
|
||||
.box = {.x = 0, .y = 0, .width = -1, .height = -1},
|
||||
.draw = &main_menu_draw,
|
||||
.do_key = &main_menu_do_key,
|
||||
.refresh = true,
|
||||
.capt_flags = CF_Mouse | CF_Keyboard,
|
||||
.do_event = &main_menu_do_event,
|
||||
.capt_flags = CF_Mouse | CF_Keyb,
|
||||
.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){
|
||||
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];
|
||||
if(w->draw && w->refresh)
|
||||
if(w->draw)
|
||||
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++){
|
||||
Widget *w = gui_info.widgets[i];
|
||||
if(!w->do_key)
|
||||
if(!gui_info.widget_active[i])
|
||||
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){
|
||||
|
@ -72,3 +130,38 @@ void try_interface(Game *game, V2d pos){
|
|||
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;
|
||||
}
|
||||
|
|
37
src/ui.h
37
src/ui.h
|
@ -13,18 +13,35 @@
|
|||
#pragma once
|
||||
|
||||
#define WIDGET_N 16
|
||||
#define MOUSE_BUTTON_NULL 7
|
||||
|
||||
enum CaptureFlags {
|
||||
CF_None = 0x0,
|
||||
CF_Arrows = 0x2,
|
||||
CF_Mouse = 0x4, //Mouse clicks
|
||||
CF_Keyboard = 0x8
|
||||
CF_None = 0x0,
|
||||
CF_Mouse = 0x2, //Mouse clicks
|
||||
CF_Keyb = 0x4
|
||||
};
|
||||
|
||||
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;
|
||||
|
||||
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 struct {
|
||||
|
@ -41,8 +58,7 @@ typedef struct Widget {
|
|||
|
||||
Rectangle box;
|
||||
widget_filler_t *draw;
|
||||
widget_dokey_t *do_key;
|
||||
bool refresh;
|
||||
widget_event_t *do_event;
|
||||
int capt_flags;
|
||||
Button buttons[10];
|
||||
|
||||
|
@ -51,14 +67,17 @@ typedef struct Widget {
|
|||
typedef struct {
|
||||
|
||||
Widget *widgets[WIDGET_N];
|
||||
int widget_n;
|
||||
bool widget_active[WIDGET_N];
|
||||
|
||||
int active_widget;
|
||||
float *zoom;
|
||||
|
||||
} GUIInfo;
|
||||
|
||||
void init_ui();
|
||||
|
||||
void draw_widgets(Game *game);
|
||||
|
||||
// World pos
|
||||
void try_interface(Game *game, V2d pos);
|
||||
|
||||
void get_keys(Game *game);
|
||||
|
|
Loading…
Add table
Reference in a new issue