initial commit with demo in main.c

This commit is contained in:
2026-10-04 20:29:49 +02:00
commit 7e909ac7eb
8 changed files with 419 additions and 0 deletions
+59
View File
@@ -0,0 +1,59 @@
#include "engine.h"
engine_internal_state_t *internal_state = NULL;
game_state_t *game_state = NULL;
void
engine_start() {
// main game loop
while(game_state->is_running) {
uint64_t start = SDL_GetPerformanceCounter();
// poll events
while (SDL_PollEvent(&game_state->event)) {
internal_state->engine_poll_events();
}
// call the user-defined framebuffer manipulation function
internal_state->engine_update_framebuffer();
// update the texture with the modified framebuffer
SDL_UpdateTexture(
internal_state->sdl_state->texture,
NULL, game_state->framebuffer,
internal_state->sdl_state->width * sizeof(uint32_t)
);
SDL_RenderClear(
internal_state->sdl_state->renderer
);
SDL_RenderTexture(
internal_state->sdl_state->renderer,
internal_state->sdl_state->texture,
NULL, NULL
);
SDL_RenderPresent(
internal_state->sdl_state->renderer
);
uint64_t end = SDL_GetPerformanceCounter();
double elapsed =
(double)(end - start) /
(double)SDL_GetPerformanceFrequency();
// cap the framerate at '1.0/TARGET_FRAME' fps
if (elapsed < TARGET_FRAME) {
SDL_Delay((TARGET_FRAME - elapsed) * 1000);
}
}
}
void
engine_stop() {
engine_sdl_destroy(internal_state->sdl_state);
free(internal_state);
free(game_state);
}