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 | |
| parent | c9e6ae44a9aeb89b3f48f3443d6baa80103f7445 (diff) | |
| download | jancity-6b9f686913efc3725b2690033cd4f398e07076ba.tar.gz | |
Add project source code
Diffstat (limited to 'src/pad')
| -rw-r--r-- | src/pad/CMakeLists.txt | 10 | ||||
| -rw-r--r-- | src/pad/inc/pad.h | 49 | ||||
| -rw-r--r-- | src/pad/privinc/pad_private.h | 17 | ||||
| -rw-r--r-- | src/pad/ps1/inc/pad/port.h | 20 | ||||
| -rw-r--r-- | src/pad/ps1/src/pad.c | 41 | ||||
| -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 | ||||
| -rw-r--r-- | src/pad/src/pad.c | 37 |
8 files changed, 274 insertions, 0 deletions
diff --git a/src/pad/CMakeLists.txt b/src/pad/CMakeLists.txt new file mode 100644 index 0000000..4862c2b --- /dev/null +++ b/src/pad/CMakeLists.txt @@ -0,0 +1,10 @@ +add_library(pad "src/pad.c") +target_include_directories(pad PUBLIC "inc" PRIVATE "privinc") + +if(PS1_BUILD) + target_include_directories(pad PUBLIC "ps1/inc") + target_sources(pad PRIVATE "ps1/src/pad.c") +elseif(SDL1_2_BUILD) + target_include_directories(pad PUBLIC "sdl-1.2/inc") + target_sources(pad PRIVATE "sdl-1.2/src/pad.c") +endif() diff --git a/src/pad/inc/pad.h b/src/pad/inc/pad.h new file mode 100644 index 0000000..2579c84 --- /dev/null +++ b/src/pad/inc/pad.h @@ -0,0 +1,49 @@ +#ifndef PAD_H +#define PAD_H + +#include <pad/port.h> +#include <stdbool.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + +#define PAD_KEYS \ + X(PAD_KEY_LEFT) \ + X(PAD_KEY_RIGHT) \ + X(PAD_KEY_UP) \ + X(PAD_KEY_DOWN) \ + X(PAD_KEY_A) \ + X(PAD_KEY_B) \ + X(PAD_KEY_C) \ + X(PAD_KEY_D) \ + X(PAD_KEY_E) \ + X(PAD_KEY_OPTIONS) + +enum pad_key +{ +#define X(x) x, + PAD_KEYS +#undef X +}; + +struct pad +{ + int player; + int mask, oldmask; + struct pad_port port; +}; + +void pad_init(int player, struct pad *p); +void pad_update(struct pad *p); +bool pad_pressed(const struct pad *p, enum pad_key k); +bool pad_justpressed(const struct pad *p, enum pad_key k); +bool pad_released(const struct pad *p, enum pad_key k); +const char *pad_str(enum pad_key k); + +#ifdef __cplusplus +} +#endif + +#endif /* PAD_H */ diff --git a/src/pad/privinc/pad_private.h b/src/pad/privinc/pad_private.h new file mode 100644 index 0000000..095c37e --- /dev/null +++ b/src/pad/privinc/pad_private.h @@ -0,0 +1,17 @@ +#ifndef PAD_PRIVATE_H +#define PAD_PRIVATE_H + +#include <pad.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + +void pad_port_update(struct pad *p); + +#ifdef __cplusplus +} +#endif + +#endif /* PAD_PRIVATE_H */ diff --git a/src/pad/ps1/inc/pad/port.h b/src/pad/ps1/inc/pad/port.h new file mode 100644 index 0000000..14dc784 --- /dev/null +++ b/src/pad/ps1/inc/pad/port.h @@ -0,0 +1,20 @@ +#ifndef PAD_PS1_H +#define PAD_PS1_H + +#include <psx.h> + +#ifdef __cplusplus +extern "C" +{ +#endif + +struct pad_port +{ + psx_pad_state pps; +}; + +#ifdef __cplusplus +} +#endif + +#endif /* PAD_PS1_H */ diff --git a/src/pad/ps1/src/pad.c b/src/pad/ps1/src/pad.c new file mode 100644 index 0000000..eea56ed --- /dev/null +++ b/src/pad/ps1/src/pad.c @@ -0,0 +1,41 @@ +#include <pad.h> +#include <pad_private.h> +#include <pad/port.h> +#include <stddef.h> +#include <string.h> + +void pad_init(const int player, struct pad *const p) +{ + memset(p, 0, sizeof *p); + p->player = player; +} + +void pad_port_update(struct pad *const p) +{ + static const int psx_keys[] = + { + [PAD_KEY_LEFT] = PAD_LEFT, + [PAD_KEY_RIGHT] = PAD_RIGHT, + [PAD_KEY_UP] = PAD_UP, + [PAD_KEY_DOWN] = PAD_DOWN, + [PAD_KEY_A] = PAD_CROSS, + [PAD_KEY_B] = PAD_CIRCLE, + [PAD_KEY_C] = PAD_TRIANGLE, + [PAD_KEY_D] = PAD_SQUARE, + [PAD_KEY_E] = PAD_L1, + [PAD_KEY_OPTIONS] = PAD_START + }; + + p->mask = 0; + PSX_PollPad(p->player, &p->port.pps); + + for (size_t i = 0; i < sizeof psx_keys / sizeof *psx_keys; i++) + { + const int mask = 1 << i; + + if (p->port.pps.buttons & psx_keys[i]) + p->mask |= mask; + else + p->mask &= ~mask; + } +} 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) +{ +} diff --git a/src/pad/src/pad.c b/src/pad/src/pad.c new file mode 100644 index 0000000..b95a43e --- /dev/null +++ b/src/pad/src/pad.c @@ -0,0 +1,37 @@ +#include <pad.h> +#include <pad_private.h> +#include <pad/port.h> +#include <stdbool.h> + +bool pad_pressed(const struct pad *const p, const enum pad_key k) +{ + return p->mask & (1 << k); +} + +bool pad_justpressed(const struct pad *const p, const enum pad_key k) +{ + return p->mask & (1 << k) && !(p->oldmask & (1 << k)); +} + +bool pad_released(const struct pad *const p, const enum pad_key k) +{ + return !(p->mask & (1 << k)) && p->oldmask & (1 << k); +} + +void pad_update(struct pad *const p) +{ + p->oldmask = p->mask; + pad_port_update(p); +} + +const char *pad_str(const enum pad_key k) +{ + static const char *const s[] = + { +#define X(x) [x] = #x, + PAD_KEYS +#undef X + }; + + return s[k]; +} |
