diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2021-07-03 00:49:03 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-03-30 08:20:20 +0200 |
| commit | 6b9f686913efc3725b2690033cd4f398e07076ba (patch) | |
| tree | e9aa91a6b9f617d78123ebe7ad272fc42a60d306 /src/pad/sdl-1.2 | |
| parent | c9e6ae44a9aeb89b3f48f3443d6baa80103f7445 (diff) | |
| download | jancity-6b9f686913efc3725b2690033cd4f398e07076ba.tar.gz | |
Add project source code
Diffstat (limited to 'src/pad/sdl-1.2')
| -rw-r--r-- | src/pad/sdl-1.2/inc/pad/port.h | 20 | ||||
| -rw-r--r-- | src/pad/sdl-1.2/src/pad.c | 80 |
2 files changed, 100 insertions, 0 deletions
diff --git a/src/pad/sdl-1.2/inc/pad/port.h b/src/pad/sdl-1.2/inc/pad/port.h new file mode 100644 index 0000000..b6ae85a --- /dev/null +++ b/src/pad/sdl-1.2/inc/pad/port.h @@ -0,0 +1,20 @@ +#ifndef PAD_SDL_1_2_H +#define PAD_SDL_1_2_H + +#include <stdbool.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct pad_port +{ + bool exit; +}; + +#ifdef __cplusplus +} +#endif + +#endif /* PAD_SDL_1_2_H */ diff --git a/src/pad/sdl-1.2/src/pad.c b/src/pad/sdl-1.2/src/pad.c new file mode 100644 index 0000000..a062758 --- /dev/null +++ b/src/pad/sdl-1.2/src/pad.c @@ -0,0 +1,80 @@ +#include <pad.h> +#include <SDL/SDL.h> +#include <stdio.h> + +static void key_event(const SDL_KeyboardEvent *const key, struct pad *const p) +{ + static const int keys[] = + { + [PAD_KEY_LEFT] = SDLK_LEFT, + [PAD_KEY_RIGHT] = SDLK_RIGHT, + [PAD_KEY_UP] = SDLK_UP, + [PAD_KEY_DOWN] = SDLK_DOWN, + [PAD_KEY_A] = SDLK_x, + [PAD_KEY_B] = SDLK_d, + [PAD_KEY_C] = SDLK_w, + [PAD_KEY_D] = SDLK_a, + [PAD_KEY_E] = SDLK_q, + [PAD_KEY_OPTIONS] = SDLK_ESCAPE + }; + + for (size_t i = 0; i < sizeof keys / sizeof *keys; i++) + { + const SDLKey k = key->keysym.sym; + + if (k == keys[i]) + { + const int mask = 1 << i; + + if (key->type == SDL_KEYDOWN) + { + printf("%s pressed\n", pad_str(i)); + p->mask |= mask; + } + else + { + printf("%s released\n", pad_str(i)); + p->mask &= ~mask; + } + } + } +} + +void pad_port_update(struct pad *const p) +{ + SDL_Event ev; + static const int masks[] = {SDL_KEYEVENTMASK}; + int n; + + while ((n = SDL_PeepEvents(&ev, 1, SDL_GETEVENT, + masks[p->player] | SDL_QUITMASK)) > 0) + { + switch (ev.type) + { + case SDL_KEYDOWN: + /* Fall through. */ + case SDL_KEYUP: + key_event(&ev.key, p); + break; + + case SDL_QUIT: + p->port.exit = true; + break; + + default: + fprintf(stderr, "%s: unexpected SDL_Event %d\n", + __func__, ev.type); + break; + } + } + + if (n < 0) + { + fprintf(stderr, "%s: SDL_PeepEvents: %s\n", __func__, SDL_GetError()); + return; + } +} + +void pad_init(const int player, struct pad *const p) +{ +} |
