Logique delta time

This commit is contained in:
attilavs2 2024-10-09 16:43:10 +02:00
parent 39a4bd035a
commit 599d5295b7
10 changed files with 117 additions and 14 deletions

View file

@ -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

48
eng/game.c Normal file
View file

@ -0,0 +1,48 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <gint/display.h>
#include <gint/keyboard.h>
#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;
}
}
}
}

View file

@ -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;

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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