1
0
mirror of https://github.com/pevans/erc-c.git synced 2024-12-21 08:30:55 +00:00

Add event system; move event code into vm_event.c

This commit is contained in:
Peter Evans 2018-02-05 00:28:22 -06:00
parent e691d33d42
commit 72c4111458
4 changed files with 76 additions and 44 deletions

14
include/vm_event.h Normal file
View File

@ -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

View File

@ -24,6 +24,7 @@ set(erc_sources
option.c
vm_area.c
vm_bitfont.c
vm_event.c
vm_screen.c
vm_segment.c
)

59
src/vm_event.c Normal file
View File

@ -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;
}
}

View File

@ -12,6 +12,7 @@
#include <sys/time.h>
#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;
}