summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c43
1 files changed, 16 insertions, 27 deletions
diff --git a/main.c b/main.c
index 210fd3f..bc99a45 100644
--- a/main.c
+++ b/main.c
@@ -62,8 +62,6 @@ int main(int argc, char *argv[])
/// Menu
printf(" ==== Entered menu\n");
- // Events are variables used to store user input
- SDL_Event menuevent;
// Load the menu graphic into memory
SDL_Surface *surf_title = IMG_Load( "titlescreen.png" );
if (!surf_title) {
@@ -74,39 +72,30 @@ int main(int argc, char *argv[])
error_sdl("could not get title texture from surface\n");
}
- /* We loop the menu continously for a number of reasons:
- * 1) To make sure we capture and respond to user input
- * 2) To continue redrawing the menu graphic.
- *
- * If we did not do number 2 then the window would appear blank if minimised
- * and then de-minimised again, amongst other situations
- */
- while ( 1 )
- {
+ // Need to draw before the while loop otherwise we only do the
+ // initial drawing after a keydown-event
+ SDL_RenderClear(renderer);
+ SDL_RenderCopy(renderer, surf_title_tex, NULL, NULL);
+ SDL_RenderPresent(renderer);
+
+ // Events are variables used to store user input
+ SDL_Event menuevent;
+ while ( SDL_WaitEvent( &menuevent ) >= 0)
+ {
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, surf_title_tex, NULL, NULL);
SDL_RenderPresent(renderer);
- // The while statement attempts to go through every key the user has pressed
- // it stops when the user has not pressed any more keys
- // this is useful incase the user presses keys fast or multiple at once
- while ( SDL_PollEvent( &menuevent ) )
+ if ( menuevent.type == SDL_KEYDOWN )
{
- if ( menuevent.type == SDL_KEYDOWN )
- {
- // Menu choice selection
- if ( menuevent.key.keysym.sym == SDLK_p ) game_loop(renderer);
- else if ( menuevent.key.keysym.sym == SDLK_h ) help(renderer);
- else if ( menuevent.key.keysym.sym == SDLK_q || menuevent.key.keysym.sym == SDLK_ESCAPE ) exit(0);
- }
+ // Menu choice selection
+ if ( menuevent.key.keysym.sym == SDLK_p ) game_loop(renderer);
+ else if ( menuevent.key.keysym.sym == SDLK_h ) help(renderer);
+ else if ( menuevent.key.keysym.sym == SDLK_q || menuevent.key.keysym.sym == SDLK_ESCAPE ) exit(0);
}
-
- // If we do not 'wait' or sleep in every ieteration of the loop
- // the game will loop as fast as it can, utilising 100% of the CPU
- SDL_Delay( 200 );
}
-
+
/* Technically the computer will never reach this point, because the while statement
* above will run forever until the user quits the game.
*