From be5479e73c1c338cd369ddd99d19563c7349cda6 Mon Sep 17 00:00:00 2001 From: Menase Teshale Date: Tue, 18 Aug 2026 22:21:29 +0300 Subject: Initial Commit --- src/main.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/main.c (limited to 'src/main.c') diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..726e781 --- /dev/null +++ b/src/main.c @@ -0,0 +1,92 @@ +#include +#include +#include +#include +#define SDL_MAIN_USE_CALLBACKS +#include + +#include "app.h" + +#define WINDOW_WIDTH 800 +#define WINDOW_HEIGHT 600 +#define WINDOW_TITLE "Match 3" +#define MAX_DELTATIME (1000.0 / 30) + +SDL_Window* window; +SDL_Renderer* renderer; + +application_state app_state; + +SDL_AppResult SDL_AppInit(void**, int, char**) { + SDL_Init(SDL_INIT_VIDEO); + srand(time(NULL)); + + window = SDL_CreateWindow(WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT, 0); + renderer = SDL_CreateRenderer(window, NULL); +#ifndef SDL_PLATFORM_EMSCRIPTEN + SDL_SetRenderVSync(renderer, true); +#endif + + init_app(renderer); + + return SDL_APP_CONTINUE; +} + +SDL_AppResult SDL_AppEvent(void*, SDL_Event* e) { + if (e->type == SDL_EVENT_QUIT) { + return SDL_APP_SUCCESS; + } + + switch (app_state) { + case game_state: + game_event(e); + break; + + default: + break; + } + + return SDL_APP_CONTINUE; +} + +SDL_AppResult SDL_AppIterate(void*) { + static uint64_t then = 0, now = 0; + + then = now; + now = SDL_GetTicks(); + + float dt = (now - then) / 1000.0f; + + switch (app_state) { + + case game_state: + update_game(SDL_min(dt, MAX_DELTATIME)); + draw_game(renderer); + break; + + default: + break; + + } + + return SDL_APP_CONTINUE; +} + +void SDL_AppQuit(void*, SDL_AppResult) { + SDL_DestroyWindow(window); + SDL_DestroyRenderer(renderer); + + free_app(); + + SDL_Quit(); +} + +void init_app(SDL_Renderer* renderer) { + init_game(renderer); + + app_state = game_state; +} + +void free_app() { + free_game(); +} -- cgit v1.2.3