The game loop

The game_loop function will need to be modified to perform different logic for each screen. Here is what the new version of the game loop will look like:

void game_loop() {
current_time = SDL_GetTicks();
diff_time = current_time - last_time;
delta_time = diff_time / 1000.0;
last_time = current_time;
if( current_screen == START_SCREEN ) {
start_input();
start_render();
}
else if( current_screen == PLAY_SCREEN || current_screen ==
PLAY_TRANSITION ) {
play_input();
move();
collisions();
play_render();
if( current_screen == PLAY_TRANSITION ) {
draw_play_transition();
}
}
else if( current_screen == YOU_WIN_SCREEN || current_screen ==
GAME_OVER_SCREEN ) {
end_input();
move();
collisions();
play_render();
play_again_btn->RenderUI();
if( current_screen == YOU_WIN_SCREEN ) {
you_win_sprite->RenderUI();
}
else {
game_over_sprite->RenderUI();
}
}
}

We have new branching logic that branches based on the current screen. The first if block runs if the current screen is the start screen. It runs the start_input and start_render functions:

if( current_screen == START_SCREEN ) {
start_input();
start_render();
}

The play screen and the play transition have the same logic as the original game loop, except for the if block around PLAY_TRANSITION at the end of this block of code. This draws the play transition by calling the draw_play_transition() function that we defined earlier:

else if( current_screen == PLAY_SCREEN || current_screen == PLAY_TRANSITION ) {
play_input();
move();
collisions();
play_render();
if( current_screen == PLAY_TRANSITION ) {
draw_play_transition();
}
}

The final block of code in the function is for the game over screen. It will render you_win_sprite if the current screen is YOU_WIN_SCREEN and will render game_over_sprite if the current screen is GAME_OVER_SCREEN:

else if( current_screen == YOU_WIN_SCREEN || current_screen == 
GAME_OVER_SCREEN ) {
end_input();
move();
collisions();
play_render();
play_again_btn->RenderUI();
if( current_screen == YOU_WIN_SCREEN ) {
you_win_sprite->RenderUI();
}
else {
game_over_sprite->RenderUI();
}
}