Browse Source

Reorganize game loop

Honor the FPS cap with updating video. Now the update occurs after
processing all the input actions.
master
TheoryOfNekomata 2 years ago
parent
commit
3f1757574a
3 changed files with 43 additions and 28 deletions
  1. +2
    -0
      src/packages/game/IZ_app.c
  2. +2
    -0
      src/packages/game/IZ_app.h
  3. +39
    -28
      src/packages/game/main.c

+ 2
- 0
src/packages/game/IZ_app.c View File

@@ -9,5 +9,7 @@ int IZ_InitializeApp(IZ_App* app) {
app->actions[p] = 0; app->actions[p] = 0;
} }


app->video_update_at = 0u;

return 0; return 0;
} }

+ 2
- 0
src/packages/game/IZ_app.h View File

@@ -12,6 +12,8 @@ typedef struct {
IZ_Action actions[PLAYERS]; IZ_Action actions[PLAYERS];


SDL_Joystick* assigned_joysticks[PLAYERS]; SDL_Joystick* assigned_joysticks[PLAYERS];

uint64_t video_update_at;
} IZ_App; } IZ_App;


int IZ_InitializeApp(IZ_App*); int IZ_InitializeApp(IZ_App*);


+ 39
- 28
src/packages/game/main.c View File

@@ -35,31 +35,18 @@ int main(int argc, char* args[]) {
return -2; return -2;
} }


screen_surface = SDL_GetWindowSurface(window);

bool quit = false; bool quit = false;
SDL_Event e; 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(); 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) { while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) { if (e.type == SDL_QUIT) {
quit = true; quit = true;
break;
} }


// Handle joystick events // 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; const uint8_t size = 4;
if (app.actions[current_player] & bitflag) {
if (ticks & bitflag) {
SDL_FillRect(screen_surface, &(SDL_Rect) { SDL_FillRect(screen_surface, &(SDL_Rect) {
column * size, column * size,
row * size,
app.config.video.height - ((row + 1) * size),
size, 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_DestroyWindow(window);
SDL_Quit(); SDL_Quit();


Loading…
Cancel
Save