diff --git a/include/vm_event.h b/include/vm_event.h new file mode 100644 index 0000000..e039907 --- /dev/null +++ b/include/vm_event.h @@ -0,0 +1,14 @@ +#ifndef _VM_EVENT_H_ +#define _VM_EVENT_H_ + +#include "vm_screen.h" + +typedef struct { + SDL_Event event; + vm_screen *screen; +} vm_event; + +extern void vm_event_keyboard(vm_event *); +extern void vm_event_poll(vm_screen *); + +#endif diff --git a/sources.cmake b/sources.cmake index 1f2b6a5..2fdb3e0 100644 --- a/sources.cmake +++ b/sources.cmake @@ -24,6 +24,7 @@ set(erc_sources option.c vm_area.c vm_bitfont.c + vm_event.c vm_screen.c vm_segment.c ) diff --git a/src/vm_event.c b/src/vm_event.c new file mode 100644 index 0000000..1ca8e63 --- /dev/null +++ b/src/vm_event.c @@ -0,0 +1,59 @@ +/* + * vm_event.c + */ + +#include "vm_event.h" + +void +vm_event_poll(vm_screen *scr) +{ + vm_event ev; + + ev.screen = scr; + while (SDL_PollEvent(&ev.event)) { + if (ev.event.type == SDL_KEYDOWN || ev.event.type == SDL_KEYUP) { + vm_event_keyboard(&ev); + } + } +} + +void +vm_event_keyboard(vm_event *ev) +{ + char ch; + + ch = '\0'; + + // The sym field is of type SDL_Keycode; this type, however, + // maps roughly to Unicode, which of course maps roughly to + // ASCII in the low range. + ch = (char)ev->event.key.keysym.sym; + + // If we had shift pressed, we need to uppercase the + // character. + if (ev->event.key.keysym.mod & KMOD_LSHIFT || + ev->event.key.keysym.mod & KMOD_RSHIFT + ) { + ch = toupper(ch); + } + + switch (ev->event.type) { + case SDL_KEYDOWN: + ev->screen->dirty = true; + ev->screen->key_pressed = true; + ev->screen->last_key = ch; + break; + + case SDL_KEYUP: + // Note we do not erase the last_key value. + ev->screen->key_pressed = false; + + if (ch == SDLK_ESCAPE) { + } + + break; + + default: + break; + } +} diff --git a/src/vm_screen.c b/src/vm_screen.c index 54358d3..a5a86a9 100644 --- a/src/vm_screen.c +++ b/src/vm_screen.c @@ -12,6 +12,7 @@ #include #include "log.h" +#include "vm_event.h" #include "vm_screen.h" /* @@ -168,50 +169,7 @@ vm_screen_free(vm_screen *screen) bool vm_screen_active(vm_screen *scr) { - SDL_Event event; - char ch; - - // There may be _many_ events in the queue; for example, you may be - // facerolling on Zork because it feels good. And good for you if - // so--but still we have to handle all those keyboard events! - while (SDL_PollEvent(&event)) { - ch = '\0'; - - // It seems we may have pressed a key... - if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) { - // The sym field is of type SDL_Keycode; this type, however, - // maps roughly to Unicode, which of course maps roughly to - // ASCII in the low range. - ch = (char)event.key.keysym.sym; - - // If we had shift pressed, we need to uppercase the - // character. - if (event.key.keysym.mod & KMOD_LSHIFT || - event.key.keysym.mod & KMOD_RSHIFT - ) { - ch = toupper(ch); - } - } - - switch (event.type) { - case SDL_KEYDOWN: - scr->dirty = true; - scr->key_pressed = true; - scr->last_key = ch; - break; - - case SDL_KEYUP: - // Note we do not erase the last_key value. - scr->key_pressed = false; - - if (ch == SDLK_ESCAPE) { - return false; - } - - break; - } - } - + vm_event_poll(scr); return true; }