Cleaning code in game.c and documented game.h

This commit is contained in:
Ulysse Cura 2025-08-11 18:35:08 +02:00
parent 9a36a64c7c
commit 8d7813bb53
3 changed files with 41 additions and 17 deletions

View File

@ -29,7 +29,9 @@
"libimg.h": "c",
"stdint.h": "c",
"transform_component.h": "c",
"sprite_component.h": "c"
"sprite_component.h": "c",
"texture_manager.h": "c",
"image.h": "c"
},
"C_Cpp.default.compilerPath": "/home/ulysse-cura/.local/bin/sh-elf-gcc"
}

View File

@ -10,10 +10,6 @@ void game_init(void)
entity_manager_init(&game.entity_manager);
game.last_clock_state = clock();
game.is_running = true;
entity_t *player = entity_manager_add_entity(&game.entity_manager, "player");
rect_t player_bounds = {10.0f, 10.0f, 32.0f, 32.0f};
@ -23,14 +19,7 @@ void game_init(void)
add_component(player, create_component(ANIMATION_SYSTEM), 4, 0, PLAYER_DEFAULT_IDLE_ANIMATION_SPEED, true, true, false);
add_component(player, create_component(PLAYER_SYSTEM));
//entity_t *enemy = entity_manager_add_entity(&game.entity_manager, "enemy0");
//rect_t enemy_bounds = {50.0f, 50.0f, 32.0f, 32.0f};
//add_component(enemy, create_component(TRANSFORM_COMPONENT), enemy_bounds, ENEMY_DEFAULT_SPEED);
//add_component(enemy, create_component(SPRITE_COMPONENT), "enemy_idle_sheet");
//add_component(enemy, create_component(ANIMATION_SYSTEM), 4, 0, ENEMY_DEFAULT_IDLE_ANIMATION_SPEED, true, true, false);
//add_component(enemy, create_component(ENEMY_SYSTEM));
game.is_running = true;
}
void game_handle_event(void)
@ -52,11 +41,17 @@ void game_handle_event(void)
while(key_event.type != KEYEV_NONE);
}
static inline void update_time(void)
{
clock_t last_clock_state = clock();
clock_t start_clock = clock();
game.delta_time_ms = (double)(start_clock - last_clock_state) * 1000.0f / CLOCKS_PER_SEC;
last_clock_state = start_clock;
}
void game_update(void)
{
clock_t start_clock = clock();
game.delta_time_ms = (double)(start_clock - game.last_clock_state) * 1000.0f / CLOCKS_PER_SEC;
game.last_clock_state = start_clock;
update_time();
entity_manager_update(&game.entity_manager);
}

View File

@ -6,26 +6,53 @@
#include "texture_manager.h"
#include "ecs/ecs.h"
/**
* @brief Game data.
* There should be only one instance of this struct.
*
* @param event Events data
* @param texture_manager Texture manager instance
* @param entity_manager Entity manager instance
* @param is_running Game running state
* @param delta_time_ms Frame time
*/
typedef struct game_t {
event_t event;
texture_manager_t texture_manager;
entity_manager_t entity_manager;
bool is_running;
clock_t last_clock_state;
double delta_time_ms;
} game_t;
/**
* @brief Global game instance
*/
extern game_t game;
/**
* @brief Initialise game
*/
void game_init(void);
/**
* @brief Handle event like inputs
*/
void game_handle_event(void);
/**
* @brief Update game
*/
void game_update(void);
/**
* @brief Render game
*/
void game_render(void);
/**
* @brief Free game instance
*/
void game_exit(void);
#endif // GAME_H