add SDL event handler -- now we can quit the emulator without using kill -9! :)

Fri, 03 Dec 2010 14:21:19 +0000

author
Philip Pemberton <philpem@philpem.me.uk>
date
Fri, 03 Dec 2010 14:21:19 +0000
changeset 47
c472ae8f44b2
parent 46
7d14fab5e4aa
child 48
d52688dad7fd

add SDL event handler -- now we can quit the emulator without using kill -9! :)

src/main.c file | annotate | diff | revisions
     1.1 --- a/src/main.c	Fri Dec 03 01:43:57 2010 +0000
     1.2 +++ b/src/main.c	Fri Dec 03 14:21:19 2010 +0000
     1.3 @@ -115,6 +115,38 @@
     1.4  	SDL_Flip(s);
     1.5  }
     1.6  
     1.7 +/**
     1.8 + * @brief	Handle events posted by SDL.
     1.9 + */
    1.10 +bool HandleSDLEvents(SDL_Surface *screen)
    1.11 +{
    1.12 +	SDL_Event event;
    1.13 +	while (SDL_PollEvent(&event))
    1.14 +	{
    1.15 +		switch (event.type) {
    1.16 +			case SDL_QUIT:
    1.17 +				// Quit button tagged. Exit.
    1.18 +				return true;
    1.19 +			case SDL_KEYDOWN:
    1.20 +				switch (event.key.keysym.sym) {
    1.21 +					case SDLK_ESCAPE:
    1.22 +						if (event.key.keysym.mod & (KMOD_LALT | KMOD_RALT))
    1.23 +							// ALT-ESC pressed; exit emulator
    1.24 +							return true;
    1.25 +						break;
    1.26 +					default:
    1.27 +						break;
    1.28 +				}
    1.29 +				break;
    1.30 +			default:
    1.31 +				break;
    1.32 +		}
    1.33 +	}
    1.34 +
    1.35 +	return false;
    1.36 +}
    1.37 +
    1.38 +
    1.39  /****************************
    1.40   * blessed be thy main()...
    1.41   ****************************/
    1.42 @@ -183,6 +215,10 @@
    1.43  			clock_cycles -= CLOCKS_PER_60HZ;
    1.44  		}
    1.45  
    1.46 +		// handle SDL events -- returns true if we need to exit
    1.47 +		if (HandleSDLEvents(screen))
    1.48 +			exitEmu = true;
    1.49 +
    1.50  		// make sure frame rate is equal to real time
    1.51  		uint32_t now = SDL_GetTicks();
    1.52  		if (now < next_timeslot) {