diff options
author | Daniel Jones <admin@danieljon.es> | 2020-08-18 15:57:32 +0930 |
---|---|---|
committer | Daniel Jones <admin@danieljon.es> | 2020-08-18 15:57:32 +0930 |
commit | 1afab3de249c6e2740c066f4074459ed048d8306 (patch) | |
tree | bf75b0228d2131783fb00b314fbf77553097f946 | |
parent | 0fa0e22e89a697730b1876de78a0e5d38d059b70 (diff) | |
download | chip8-1afab3de249c6e2740c066f4074459ed048d8306.tar.gz chip8-1afab3de249c6e2740c066f4074459ed048d8306.zip |
move sdl event handling to own function
-rw-r--r-- | main.c | 59 |
1 files changed, 31 insertions, 28 deletions
@@ -23,6 +23,7 @@ void update_video(); void toggle_pixel(int x, int y); void quit(); void usage(char *); +int handle_sdl_events(); void handle_key_down(int keycode); void handle_key_up(int keycode); uint8_t get_chip8_key(int keycode); @@ -117,6 +118,35 @@ get_chip8_key(int keycode) } } +int +handle_sdl_events() +{ + SDL_Event e; + while (SDL_PollEvent(&e) != 0) + { + switch (e.type) + { + case SDL_QUIT: return 1; + case SDL_KEYDOWN: + { + switch(e.key.keysym.sym) + { + case SDLK_ESCAPE: return 1; + default: + handle_key_down(e.key.keysym.sym); + break; + } + break; + } + case SDL_KEYUP: + handle_key_up(e.key.keysym.sym); + break; + default: break; + } + } + return 0; +} + int main(int argc, char *argv[]) { if (argc < 2) @@ -168,39 +198,12 @@ int main(int argc, char *argv[]) chip8_draw_sprite(61, 25, 0x200, 0x6); chip8_draw_sprite(50, 30, 0x200, 0x6); - SDL_Event e; while(!do_quit) { frame_start = SDL_GetTicks(); // logic - while (SDL_PollEvent(&e) != 0) - { - switch (e.type) - { - case SDL_QUIT: - do_quit = 1; - break; - case SDL_KEYDOWN: - { - switch(e.key.keysym.sym) - { - case SDLK_ESCAPE: - do_quit = 1; - break; - default: - handle_key_down(e.key.keysym.sym); - break; - } - break; - } - case SDL_KEYUP: - handle_key_up(e.key.keysym.sym); - break; - default: break; - } - } - + do_quit = handle_sdl_events(); chip8_cycle(); if (draw_flag) |