rework event system

This commit is contained in:
KikooDX 2022-03-06 02:58:07 +01:00
parent 73f8d58c1a
commit c61cd287a5
2 changed files with 134 additions and 60 deletions

144
inc/lzy.h
View File

@ -31,16 +31,58 @@ extern "C" {
#define LZY_UNUSED(x) (void)(x)
#ifdef FXCG50
#include <gint/keycodes.h>
typedef void LZY_Music;
typedef void LZY_Sound;
#else
enum LZY_ScanCode {
LZYK_LEFT = KEY_LEFT,
LZYK_RIGHT = KEY_RIGHT,
LZYK_UP = KEY_UP,
LZYK_DOWN = KEY_DOWN,
LZYK_O = KEY_SHIFT,
LZYK_X = KEY_OPTN,
};
#else /* end FXCG50, begin SDL2 */
#ifndef LZY_SDL_INCLUDE
#define LZY_SDL_INCLUDE <SDL.h>
#endif
#include LZY_SDL_INCLUDE
#ifndef LZY_SDL_MIXER_INCLUDE
#define LZY_SDL_MIXER_INCLUDE <SDL_mixer.h>
#endif
#include LZY_SDL_MIXER_INCLUDE
enum LZY_ScanCode {
LZYK_LEFT,
LZYK_RIGHT,
LZYK_UP,
LZYK_DOWN,
LZYK_O,
LZYK_X,
LZYK_COUNT,
};
typedef Mix_Music LZY_Music;
typedef Mix_Chunk LZY_Sound;
#endif
#endif /* SDL2 */
enum LZY_EventType {
LZY_QUIT,
LZY_KEYDOWN,
LZY_KEYUP,
};
typedef struct LZY_KeyEvent {
unsigned int scancode;
int down;
} LZY_KeyEvent;
typedef union LZY_EventUnion {
LZY_KeyEvent key;
} LZY_EventUnion;
typedef struct LZY_Event {
int type;
LZY_EventUnion u;
} LZY_Event;
int LZY_Init(const char *title, int target_fps, const char *tset_path,
const char *font_path);
@ -63,37 +105,13 @@ int LZY_MusicPlay(LZY_Music *music, int loops);
LZY_Sound *LZY_SoundLoad(const char *path);
int LZY_SoundDestroy(LZY_Sound *sound);
int LZY_SoundPlay(LZY_Sound *sound, int loops);
int LZY_PollEvent(LZY_Event *);
void LZY_CycleEvents(void);
int LZY_KeyDown(unsigned int key);
int LZY_ShouldQuit(void);
void LZY_Log(const char *msg);
const char *LZY_GetError(void);
#ifdef FXCG50
#include <gint/keycodes.h>
enum LZY_ScanCode {
LZYK_LEFT = KEY_LEFT,
LZYK_RIGHT = KEY_RIGHT,
LZYK_UP = KEY_UP,
LZYK_DOWN = KEY_DOWN,
LZYK_O = KEY_SHIFT,
LZYK_X = KEY_OPTN,
};
#else /* end FXCG50, begin SDL2 */
#ifndef LZY_SDL_INCLUDE
#define LZY_SDL_INCLUDE <SDL.h>
#endif
#include LZY_SDL_INCLUDE
enum LZY_ScanCode {
LZYK_LEFT,
LZYK_RIGHT,
LZYK_UP,
LZYK_DOWN,
LZYK_O,
LZYK_X,
LZYK_COUNT,
};
#endif /* SDL2 */
#ifdef __cplusplus
}
#endif
@ -363,6 +381,27 @@ int LZY_SoundPlay(LZY_Sound *sound, int loops) {
return -1;
}
int LZY_PollEvent(LZY_Event *e) {
for (;;) {
const key_event_t gk_e = pollevent();
switch (gk_e.type) {
case KEYEV_NONE:
return 0;
case KEYEV_DOWN:
e->type = LZY_KEYDOWN;
e->u.key.scancode = gk_e.key;
if (gk_e.key == KEY_EXIT)
should_quit = 1;
return 1;
case KEYEV_UP:
e->type = LZY_KEYUP;
e->u.key.scancode = gk_e.key;
return 1;
}
}
}
void LZY_CycleEvents(void) {
clearevents();
should_quit = should_quit || keydown(KEY_EXIT);
@ -390,6 +429,10 @@ const char *LZY_GetError(void) {
#include LZY_SDL_MIXER_INCLUDE
#include <stdint.h>
static const SDL_Scancode sc[LZYK_COUNT] = {
SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_UP,
SDL_SCANCODE_DOWN, SDL_SCANCODE_Z, SDL_SCANCODE_X,
};
static const char *error = NULL;
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
@ -754,40 +797,51 @@ int LZY_SoundDestroy(LZY_Sound *sound) {
return 0;
}
void LZY_CycleEvents(void) {
static const SDL_Scancode sc[LZYK_COUNT] = {
SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_UP,
SDL_SCANCODE_DOWN, SDL_SCANCODE_Z, SDL_SCANCODE_X,
};
SDL_Event e;
int LZY_PollEvent(LZY_Event *e) {
SDL_Event sdl_e;
while (SDL_PollEvent(&e) != 0) {
switch (e.type) {
if (SDL_PollEvent(&sdl_e) != 0) {
switch (sdl_e.type) {
case SDL_QUIT:
e->type = LZY_QUIT;
should_quit = 1;
break;
return 1;
case SDL_KEYDOWN: {
int i = LZYK_COUNT;
while (i-- > 0) {
if (sc[i] == e.key.keysym.scancode) {
input[i] = 1;
break;
if (!sdl_e.key.repeat) {
int i = LZYK_COUNT;
while (i-- > 0) {
if (sc[i] ==
sdl_e.key.keysym.scancode) {
e->type = LZY_KEYDOWN;
e->u.key.scancode = i;
input[i] = 1;
return 1;
}
}
}
} break;
case SDL_KEYUP: {
int i = LZYK_COUNT;
while (i-- > 0) {
if (sc[i] == e.key.keysym.scancode) {
if (sc[i] == sdl_e.key.keysym.scancode) {
e->type = LZY_KEYUP;
e->u.key.scancode = i;
input[i] = 0;
break;
return 1;
}
}
} break;
default:
break;
}
}
return 0;
}
void LZY_CycleEvents(void) {
LZY_Event e;
while (LZY_PollEvent(&e) != 0)
;
}
int LZY_KeyDown(unsigned int key) {

View File

@ -27,18 +27,36 @@ int main(void) {
LZY_MusicPlay(music, 0);
do {
const int speed = 2;
static const int speed = 16;
LZY_Event e;
/* update */
LZY_CycleEvents();
if (LZY_KeyDown(LZYK_LEFT))
x -= speed;
if (LZY_KeyDown(LZYK_RIGHT))
x += speed;
if (LZY_KeyDown(LZYK_UP))
y -= speed;
if (LZY_KeyDown(LZYK_DOWN))
y += speed;
while (LZY_PollEvent(&e)) {
switch (e.type) {
case LZY_QUIT:
LZY_Log("get back");
break;
case LZY_KEYDOWN:
LZY_Log("under pressure");
switch (e.u.key.scancode) {
case LZYK_LEFT:
x -= speed;
break;
case LZYK_RIGHT:
x += speed;
break;
case LZYK_UP:
y -= speed;
break;
case LZYK_DOWN:
y += speed;
break;
};
break;
case LZY_KEYUP:
LZY_Log("let it be");
break;
}
}
/* draw */
LZY_DrawBegin();
@ -62,13 +80,15 @@ int main(void) {
static void draw_player(int x, int y) {
const int cx = x + 8;
const int cy = y + 8;
int i;
if (LZY_DrawTile(14, x, y))
LZY_Log(LZY_GetError());
LZY_DrawSetColor(0x00, 0xff, 0xff);
LZY_DrawLine(0, 0, cx - 1, cy - 1);
LZY_DrawLine(395, 0, cx, cy - 1);
LZY_DrawLine(0, 223, cx - 1, cy);
LZY_DrawLine(395, 223, cx, cy);
for (i = 0; i < 128; i++) {
LZY_DrawSetColor(0x00, i * 2, i * 2);
LZY_DrawLine(i + 140, 0, cx, cy);
LZY_DrawLine(i + 140, 223, cx, cy);
}
}