From 5d4bccfd08695e3e45e7499e215e51fed910f145 Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Tue, 25 Feb 2025 21:35:19 +0100 Subject: [PATCH] =?UTF-8?q?Possibilit=C3=A9=20de=20pauser=20(P)=20+=20prog?= =?UTF-8?q?r=C3=A8s=20UI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- src/draw.c | 6 +++++ src/game.c | 48 +++++++++++++++++++++++----------- src/main.c | 30 +++++++--------------- src/types.h | 4 +++ src/ui.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ui.h | 64 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 191 insertions(+), 37 deletions(-) create mode 100644 src/ui.c create mode 100644 src/ui.h diff --git a/Makefile b/Makefile index ce0d343..26dcab3 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ OUTNAME = mtycoon -CFLAGS = -O2 -g -Wall -Wextra -I ./raylib/include -pipe +CFLAGS = -O0 -g -Wall -Wextra -I ./raylib/include -pipe #linux #LDFLAGS = -static -L./raylib/lib -lraylib -lm #windows diff --git a/src/draw.c b/src/draw.c index fd748fc..5024bc7 100644 --- a/src/draw.c +++ b/src/draw.c @@ -10,6 +10,7 @@ #include "types.h" #include "game.h" #include "draw.h" +#include "ui.h" #define TEX_N 9 @@ -63,7 +64,12 @@ void draw_machines(Game *game){ } void draw(Game *game){ + BeginMode2D(game->camera); draw_machines(game); draw_workers(game); + EndMode2D(); + + draw_widgets(game); + DrawFPS(0,0); } diff --git a/src/game.c b/src/game.c index ec19bb6..abc95e7 100644 --- a/src/game.c +++ b/src/game.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -43,6 +44,7 @@ int init_game(Game *game){ srand(rand_seed); + game->paused = false; game->ttime = 0; game->camera = (Camera2D){.offset = {0, 0}, .zoom = 1}; @@ -133,23 +135,32 @@ int get_machine_localres(Machine *machine, int globalres){ return -1; } -int mach_work_pos_cmpfunc(const void *a_, const void *b_){ - const Worker *a = a_; - const Worker *b = b_; - return (int)(Vector2Distance(a->pos, b->pos)*1000); -} - Worker *get_worker_from_pos(Workers *workers, V2d pos){ - Worker tmp = {.pos = pos}; - Worker *match = bsearch(&tmp, workers->worker_stack, workers->worker_n, - sizeof(Worker), &mach_work_pos_cmpfunc); + Worker *match = NULL; + + for(int i = 0; i < workers->worker_n; i++){ + Worker *curr = &workers->worker_stack[i]; + if(CheckCollisionPointRec(pos, + (Rectangle){.x=curr->pos.x,.y=curr->pos.y,.width=32,.height=32})){ + match = curr; + break; + } + } + return match; } Machine *get_machine_from_pos(Machines *machines, V2d pos){ - Machine tmp = {.pos = pos}; - Machine *match = bsearch(&tmp, machines->machine_stack, machines->machine_n, - sizeof(Machine), &mach_work_pos_cmpfunc); + Machine *match = NULL; + + for(int i = 0; i < machines->machine_n; i++){ + Machine *curr = &machines->machine_stack[i]; + if(CheckCollisionPointRec(pos, + (Rectangle){.x=curr->pos.x,.y=curr->pos.y,.width=64,.height=64})){ + match = curr; + break; + } + } return match; } @@ -179,6 +190,9 @@ void update_worker(Game *game, Worker *worker){ if(ai->assigned_machine == MACH_NULL && ai->assigned_machine1 == MACH_NULL) ai->status = S_Idle; + int localres; + Vector2 mov; + //TODO : Move ! switch(ai->status){ default: @@ -200,7 +214,7 @@ void update_worker(Game *game, Worker *worker){ } break; case S_Unloading: - int localres = get_machine_localres(mach[1], ai->carry_type); + localres = get_machine_localres(mach[1], ai->carry_type); if(localres != (int)ai->carry_type){ ai->status = S_Idle; break; @@ -224,7 +238,7 @@ void update_worker(Game *game, Worker *worker){ break; case S_Transporting: case S_Walking: - Vector2 mov = Vector2Scale( + mov = Vector2Scale( Vector2Normalize(Vector2Subtract(ai->dest, worker->pos)), 2 ); @@ -275,9 +289,13 @@ void update_machines(Game *game){ } void update(Game *game){ - game->ttime += GetFrameTime(); game->t_per_frame = 0; + if(game->paused) + return; + + game->ttime += GetFrameTime(); + while(game->ttime > TPS_TIME){ update_machines(game); update_workers(game); diff --git a/src/main.c b/src/main.c index 6ee9366..a1de1e5 100644 --- a/src/main.c +++ b/src/main.c @@ -10,27 +10,11 @@ #include "types.h" #include "game.h" #include "draw.h" +#include "ui.h" //Idées : // - Drogues -void try_interface(Game *game, V2d pos){ - DrawRectangleV(GetScreenToWorld2D(pos,game->camera), (V2d){16,16},PINK); - - Machine *mach = get_machine_from_pos(&game->machines, pos); - if(mach){ - - return; - } - - Worker *work = get_worker_from_pos(&game->workers, pos); - - if(!work) - return; - - return; -} - int init(Game *game){ int err = init_draw(); err |= init_game(game); @@ -50,7 +34,7 @@ bool keydown(int key){ void get_keys(Game *game){ V2d mouse = GetMousePosition(); if(IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) - try_interface(game, mouse); + try_interface(game, GetScreenToWorld2D(mouse,game->camera)); if(keydown(KEY_W)) game->camera.target.y -= 3; @@ -64,12 +48,18 @@ void get_keys(Game *game){ 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; + extern GUIInfo gui_info; + + gui_info.zoom = &game.camera.zoom; + if(init(&game)){ printf("Failed to init game ! Exiting...\n"); clean(&game); @@ -79,7 +69,7 @@ int main(){ int c_worker = 0; for(int k = 0; k < 42; k++){ - for(int j = 0; j < 256; j++){ + for(int j = 0; j < 1; j++){ Machine *coal_mine = add_machine(&game.machines, (V2d){k*250+40,j*400+40}); coal_mine->type = MT_CoalMine; Machine *iron_mine = add_machine(&game.machines, (V2d){k*250+40,j*400+240}); @@ -102,7 +92,6 @@ int main(){ while(!WindowShouldClose()){ BeginDrawing(); - BeginMode2D(game.camera); ClearBackground(BLACK); @@ -112,7 +101,6 @@ int main(){ draw(&game); - EndMode2D(); EndDrawing(); } diff --git a/src/types.h b/src/types.h index 9ac9d63..00f8b4b 100644 --- a/src/types.h +++ b/src/types.h @@ -11,6 +11,8 @@ #define V2d Vector2 +#define uint unsigned int + enum Status { S_Idle = 0, S_Transporting = 1, @@ -107,6 +109,8 @@ typedef struct { Camera2D camera; + bool paused; + float ttime; int t_per_frame; diff --git a/src/ui.c b/src/ui.c new file mode 100644 index 0000000..19ad875 --- /dev/null +++ b/src/ui.c @@ -0,0 +1,74 @@ +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "types.h" +#include "game.h" +#include "draw.h" +#include "ui.h" + +void main_menu_draw(Widget *widget, Game *game){ + printf("main mem draw\n"); + + widget->refresh = false; +} + +void main_menu_do_key(Widget *widget, Game *game, int key){ + +} + +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, + .buttons = {} +}; + +GUIInfo gui_info = {{&main_menu}, 1, 0, NULL}; + +void draw_widgets(Game *game){ + for(int i = 0; i < gui_info.widget_n; i++){ + Widget *w = gui_info.widgets[i]; + if(w->draw && w->refresh) + w->draw(w, game); + } +} + +void widgets_treat_key(Game *game, int key){ + for(int i = 0; i < WIDGET_N; i++){ + Widget *w = gui_info.widgets[i]; + if(!w->do_key) + continue; + w->do_key(w, game, key); + } +} + +void try_interface(Game *game, V2d pos){ + + DrawRectangleV(pos, (V2d){16,16},PINK); + + Machine *mach = get_machine_from_pos(&game->machines, pos); + + if(mach){ + printf("found machine %d\n",mach->id); + return; + } + + Worker *work = get_worker_from_pos(&game->workers, pos); + + if(!work) + return; + + printf("found worker\n"); + + return; +} + diff --git a/src/ui.h b/src/ui.h new file mode 100644 index 0000000..252b6a2 --- /dev/null +++ b/src/ui.h @@ -0,0 +1,64 @@ +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "types.h" + +#pragma once + +#define WIDGET_N 16 + +enum CaptureFlags { + CF_None = 0x0, + CF_Arrows = 0x2, + CF_Mouse = 0x4, //Mouse clicks + CF_Keyboard = 0x8 +}; + +struct Widget; + +typedef void (widget_filler_t)(struct Widget*, Game*); +typedef void (widget_dokey_t)(struct Widget*, Game*, int); +typedef void (button_handler_t)(void); + +typedef struct { + + Rectangle box; + Color color_0; + Color color_1; + int curr_select; + button_handler_t *handler; + +} Button; + +typedef struct Widget { + + Rectangle box; + widget_filler_t *draw; + widget_dokey_t *do_key; + bool refresh; + int capt_flags; + Button buttons[10]; + +} Widget; + +typedef struct { + + Widget *widgets[WIDGET_N]; + int widget_n; + + int active_widget; + float *zoom; + +} GUIInfo; + +void draw_widgets(Game *game); + +// World pos +void try_interface(Game *game, V2d pos);