From 599d5295b7fb5385aa9ad4c613d36b8700b4f6e7 Mon Sep 17 00:00:00 2001 From: attilavs2 Date: Wed, 9 Oct 2024 16:43:10 +0200 Subject: [PATCH] Logique delta time --- CMakeLists.txt | 3 +++ eng/game.c | 48 +++++++++++++++++++++++++++++++++++++++++++ eng/moteur.c | 6 ++---- include/C3D/config.h | 15 +++++++++++++- include/C3D/game.h | 23 +++++++++++++++++++++ include/C3D/map.h | 1 + include/C3D/moteur.h | 1 + include/C3D/sprites.h | 1 + include/C3D/utils.h | 1 + src/main.c | 32 +++++++++++++++++++++-------- 10 files changed, 117 insertions(+), 14 deletions(-) create mode 100644 eng/game.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 963ca21..a13d0c0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,10 +16,13 @@ set(NAMEOFGAME "Test3D") set(AUTHOR "Fcalva") set(SOURCES + #sources de votre jeu src/main.c + #sources du moteur eng/moteur.c eng/map.c eng/sprites.c + eng/game.c ) set(ASSETS diff --git a/eng/game.c b/eng/game.c new file mode 100644 index 0000000..33317f1 --- /dev/null +++ b/eng/game.c @@ -0,0 +1,48 @@ +#include +#include +#include + +#include +#include + +#include "fixed.h" +#include "map.h" +#include "game.h" +#include "utils.h" +#include "moteur.h" +#include "sprites.h" + +int lhook_c = 0; +RcLogicFunc *logic_hooks[HINDEX_S]; + +int add_logic_hook(RcLogicFunc *func){ + if(lhook_c >= HINDEX_S) + return 1; + logic_hooks[lhook_c++] = func; + return 0; +} + +//TODO +int remove_logic_hook(RcLogicFunc *func){ + +} + +void clear_logic_hooks(){ + lhook_c = 0; +} + +//Time per tick, in µs +#define LOGIC_TIME (int)((1.0/TPS) * 1000000) + +void do_logic(RcGame *game, int delta){ + game->logic_time += delta; + for(; game->logic_time > LOGIC_TIME; game->logic_time -= LOGIC_TIME){ + for(int i = 0; i < lhook_c; i++){ + //TODO : Be less dramatic + if(logic_hooks[i](game)){ + game->flags.exit = 1; + return; + } + } + } +} diff --git a/eng/moteur.c b/eng/moteur.c index 3a5d6bc..9d5a7de 100644 --- a/eng/moteur.c +++ b/eng/moteur.c @@ -231,14 +231,12 @@ void draw_walls( fixed_t deltaDistX; fixed_t deltaDistY; fixed_t perpWallDist; - fixed_t texSize; int x; - int i; int mapX; int mapY; int stepX; //what direction to step in x or y-direction (either +1 or -1) int stepY; - int side; //was a NS or a EW wall hit? + int side = 0; //was a NS or a EW wall hit? int lineHeight; int texX; int texSample; @@ -246,7 +244,7 @@ void draw_walls( dgray_getvram(&light,&dark); - int v_offset = 0; + //int v_offset = 0; fixed_t h_offset = 0; //struct image_linear_map temp; diff --git a/include/C3D/config.h b/include/C3D/config.h index 1af00fe..601cdde 100644 --- a/include/C3D/config.h +++ b/include/C3D/config.h @@ -1,6 +1,10 @@ +// Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3 +// See README.md for the exact licensing, by ... + #pragma once -//param. graphiques +//===== Paramètres graphiques ===== + #define screen_w 128 #define screen_h 64 #define viewport_w 128 @@ -11,10 +15,19 @@ #define TSIZE 32 +//===== Paramètres de jeu ===== + +//Ticks par seconde +#define TPS 30 + //Tex index #define TINDEX_S 16 //Sprite index #define SINDEX_S 64 +//Game logic hooks +#define HINDEX_S 16 + +//===== Paramètres de debug ===== #define debug 1 //pour afficher les infos de debug diff --git a/include/C3D/game.h b/include/C3D/game.h index 17021e9..ea5c32c 100644 --- a/include/C3D/game.h +++ b/include/C3D/game.h @@ -1,4 +1,5 @@ // Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3 +// See README.md for the exact licensing, by ... #ifndef game__h #define game__h @@ -14,12 +15,34 @@ typedef struct { } RcActor; +typedef struct { + + uint8_t exit; + +} RcFlags; + typedef struct { RcActor player; + int logic_time; + uint8_t *current_map; + RcFlags flags; + } RcGame; +typedef int (RcLogicFunc)(RcGame *); + +//Returns non-zero on failure (likely max hook count was reached) +int add_logic_hook(RcLogicFunc *func); + +//Retruns non-zero on failure +int remove_logic_hook(RcLogicFunc *func); + +void clear_logic_hooks(); + +void do_logic(RcGame *game, int delta); + #endif diff --git a/include/C3D/map.h b/include/C3D/map.h index 6ce2bba..86d8f31 100644 --- a/include/C3D/map.h +++ b/include/C3D/map.h @@ -1,4 +1,5 @@ // Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3 +// See README.md for the exact licensing, by ... #include "fixed.h" #ifndef map__h diff --git a/include/C3D/moteur.h b/include/C3D/moteur.h index 093a582..7161b00 100644 --- a/include/C3D/moteur.h +++ b/include/C3D/moteur.h @@ -1,4 +1,5 @@ // Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3 +// See README.md for the exact licensing, by ... #ifndef moteur_h #define moteur_h diff --git a/include/C3D/sprites.h b/include/C3D/sprites.h index aed5543..63c8eda 100644 --- a/include/C3D/sprites.h +++ b/include/C3D/sprites.h @@ -1,4 +1,5 @@ // Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3 +// See README.md for the exact licensing, by ... #ifndef sprites__h #define sprites__h diff --git a/include/C3D/utils.h b/include/C3D/utils.h index e17cfd8..006be38 100644 --- a/include/C3D/utils.h +++ b/include/C3D/utils.h @@ -1,4 +1,5 @@ // Voir README.md pour license précise, par Fcalva 2023-2024 et est sous GPLv3 +// See README.md for the exact licensing, by ... #ifndef utils__h #define utils__h diff --git a/src/main.c b/src/main.c index 9c9626c..3c020ca 100644 --- a/src/main.c +++ b/src/main.c @@ -39,7 +39,6 @@ extern bopti_image_t briques0; extern bopti_image_t buisson0; extern bopti_image_t sprite_tst; -char exit_game = 0; char disp_frame_time = 0; char first_frame = 0; int frame_time_timer = 1; @@ -50,7 +49,8 @@ RcGame game = { .dir = {fix(0), fix(1)}, .plane = {fix(0.66), fix(0)} }, - .current_map = (void*)&map_test + .current_map = (void*)&map_test, + .flags = {0} }; int frame_time = 1; @@ -71,7 +71,7 @@ void keys_get(){ } } frame_time_timer--; - if (keydown(KEY_EXIT)) exit_game = 1; + if (keydown(KEY_EXIT)) game.flags.exit = 1; #ifdef debug //if (keydown(KEY_TAN)) end_screen(); @@ -87,6 +87,21 @@ void main_menu(){ getkey(); } +Sprite tsprite = { + .pos = {200000, 300000}, + .tex = 4 +}; + +int movdir = 1; + +int test_logic(RcGame *game){ + tsprite.pos.x += movdir*500; + if(tsprite.pos.x > fix(4) || tsprite.pos.x < fix(2)) + movdir = -movdir; + + return 0; +} + int main(){ dclear(C_WHITE); @@ -117,14 +132,11 @@ int main(){ //gdb_start_on_exception(); #endif - Sprite tsprite = { - .pos = {200000, 300000}, - .tex = 4 - }; - add_sprite(&tsprite); - while (!exit_game) { + add_logic_hook((RcLogicFunc*)&test_logic); + + while (!game.flags.exit) { prof_t frame = prof_make(); prof_enter(frame); @@ -135,6 +147,8 @@ int main(){ } keys_get(); + + do_logic(&game, frame_time*1000); draw_walls( #if debug