frillrun/src/game/game.c
2024-08-24 00:47:58 -04:00

126 lines
2.3 KiB
C

#include "game.h"
Game game;
static void _game_tick(Game* self);
static void _game_update(Game* self);
static void _game_draw(Game* self);
static void _game_quit(Game* self);
static void
_game_quit(Game* self)
{
state_free(&self->state);
ecs_free(&self->ecs);
window_free(&self->window);
resources_free(&self->resources);
sdl_quit();
memset(self, '\0', sizeof(Game));
printf("%s\n", STRING_GAME_EXIT);
exit(EXIT_SUCCESS);
}
static void
_game_tick(Game* self)
{
input_tick(&self->input);
control_tick(&self->control, &self->input);
state_tick(&self->state);
ecs_tick(&self->ecs);
if (event_press(&self->input.event, EVENT_QUIT))
_game_quit(self);
}
static void
_game_update(Game* self)
{
renderer_update(&self->renderer);
ecs_update(&self->ecs);
}
static void
_game_draw(Game* self)
{
renderer_clear_color_set(&self->renderer, (f32*)RENDERER_CLEAR_COLOR);
renderer_clear(&self->renderer);
for (s32 i = 0; i < RENDERER_BUFFER_COUNT; i++)
renderer_buffer_init(&self->renderer, (RendererBuffer)i);
ecs_draw(&self->ecs);
for (s32 i = 0; i < RENDERER_BUFFER_COUNT; i++)
{
buffer_draw
(
&self->renderer,
&self->resources.shaders[SHADER_TEXTURE_QUAD],
(RendererBuffer)i,
&self->postprocessing[i]
);
renderer_buffer_free(&self->renderer, (RendererBuffer)i);
}
renderer_present(&self->renderer);
}
void
game_init(Game* self)
{
printf("%s\n", STRING_GAME_INIT);
memset(self, '\0', sizeof(Game));
RANDOM_SEED_SET(time(NULL));
resource_shader_read(&self->resources);
sdl_init();
window_init(&self->window, STRING_WINDOW_TITLE, (s32*)WINDOW_SIZE, WINDOW_FLAGS);
renderer_init(&self->renderer, &self->window, CAMERA_ORTHOGRAPHIC, BUFFER_SIZES);
renderer_clear_color_set(&self->renderer, (f32*)RENDERER_CLEAR_COLOR);
for (s32 i = 0; i < RENDERER_BUFFER_COUNT; i++)
postprocessing_init(&self->postprocessing[i], OPAQUE, TRANSPARENT);
if (!resources_init(&self->resources))
{
printf("%s\n", STRING_RESOURCE_ERROR);
_game_quit(self);
}
ecs_init(&self->ecs, &self->renderer, &self->control, &self->input, &self->resources, self->postprocessing);
state_init(&self->state, &self->ecs, GAME_STATE_START);
}
void
game_loop(Game* self)
{
tick_update(&self->tick);
while (self->tick.cumulative > TICK_DELAY)
{
_game_tick(self);
self->tick.cumulative -= TICK_DELAY;
}
_game_update(self);
_game_draw(self);
}