summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorMenase Teshale <osome3717@gmail.com>2026-08-18 22:21:29 +0300
committerMenase Teshale <osome3717@gmail.com>2026-08-18 22:21:29 +0300
commitbe5479e73c1c338cd369ddd99d19563c7349cda6 (patch)
treeea32a099d25e21c85d8e53455fa925c2254f8e85 /src/main.c
Initial CommitHEADmain
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c92
1 files changed, 92 insertions, 0 deletions
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 <stdbool.h>
+#include <SDL3/SDL.h>
+#include <time.h>
+#include <stdlib.h>
+#define SDL_MAIN_USE_CALLBACKS
+#include <SDL3/SDL_main.h>
+
+#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();
+}