diff --git a/src/packages/game/IZ_app.c b/src/packages/game/IZ_app.c index bb1d47f..786deda 100644 --- a/src/packages/game/IZ_app.c +++ b/src/packages/game/IZ_app.c @@ -9,5 +9,7 @@ int IZ_InitializeApp(IZ_App* app) { app->actions[p] = 0; } + app->video_update_at = 0u; + return 0; } diff --git a/src/packages/game/IZ_app.h b/src/packages/game/IZ_app.h index 49fef2c..9ac3f5c 100644 --- a/src/packages/game/IZ_app.h +++ b/src/packages/game/IZ_app.h @@ -12,6 +12,8 @@ typedef struct { IZ_Action actions[PLAYERS]; SDL_Joystick* assigned_joysticks[PLAYERS]; + + uint64_t video_update_at; } IZ_App; int IZ_InitializeApp(IZ_App*); diff --git a/src/packages/game/main.c b/src/packages/game/main.c index 59c2316..55e5347 100644 --- a/src/packages/game/main.c +++ b/src/packages/game/main.c @@ -35,31 +35,18 @@ int main(int argc, char* args[]) { return -2; } + screen_surface = SDL_GetWindowSurface(window); + bool quit = false; SDL_Event e; - screen_surface = SDL_GetWindowSurface(window); - while (!quit) { - SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0x00, 0x00, 0x00)); + while (true) { uint64_t ticks = SDL_GetTicks64(); - for (uint8_t i = 0; i < 64; i += 1) { - const uint8_t column = (64 - i) % 32; - const uint8_t row = i / 32; - const uint64_t bitflag = (0x1lu << i); - const uint8_t size = 4; - if (ticks & bitflag) { - SDL_FillRect(screen_surface, &(SDL_Rect) { - column * size, - app.config.video.height - ((row + 1) * size), - size, - size - }, SDL_MapRGB(screen_surface->format, 0x00, 0xff, 0xff)); - } - } while (SDL_PollEvent(&e) != 0) { if (e.type == SDL_QUIT) { quit = true; + break; } // Handle joystick events @@ -137,25 +124,49 @@ int main(int argc, char* args[]) { } } - // Update window - for (uint8_t current_player = 0; current_player < PLAYERS; current_player += 1) { - for (uint8_t i = 0; i < CONTROLS; i += 1) { - const uint8_t column = i % 4; - const uint8_t row = i / 4; - const IZ_Action bitflag = (0x1 << i); + if (quit) { + break; + } + + if (ticks - app.video_update_at > 1000 / app.config.video.max_fps) { + // Update window + SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0x00, 0x00, 0x00)); + + for (uint8_t i = 0; i < 64; i += 1) { + const uint8_t column = (64 - i) % 32; + const uint8_t row = i / 32; + const uint64_t bitflag = (0x1lu << i); const uint8_t size = 4; - if (app.actions[current_player] & bitflag) { + if (ticks & bitflag) { SDL_FillRect(screen_surface, &(SDL_Rect) { column * size, - row * size, + app.config.video.height - ((row + 1) * size), size, size - }, SDL_MapRGB(screen_surface->format, 0xff, 0xff, 0x00)); + }, SDL_MapRGB(screen_surface->format, 0x00, 0xff, 0xff)); } } - } - SDL_UpdateWindowSurface(window); + for (uint8_t current_player = 0; current_player < PLAYERS; current_player += 1) { + for (uint8_t i = 0; i < CONTROLS; i += 1) { + const uint8_t column = i % 4; + const uint8_t row = i / 4; + const IZ_Action bitflag = (0x1 << i); + const uint8_t size = 4; + if (app.actions[current_player] & bitflag) { + SDL_FillRect(screen_surface, &(SDL_Rect) { + column * size, + row * size, + size, + size + }, SDL_MapRGB(screen_surface->format, 0xff, 0xff, 0x00)); + } + } + } + SDL_UpdateWindowSurface(window); + + app.video_update_at = ticks; + } } SDL_DestroyWindow(window); SDL_Quit();