diff options
| author | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-06-24 17:28:38 +0200 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi.dcr@tutanota.com> | 2022-06-24 17:28:38 +0200 |
| commit | 992e7fb9358a0d0a5d99ba119cf584477bda8d72 (patch) | |
| tree | 1cd753a73b560e60e0de353b9a1b7d0e15b002d6 /src | |
| parent | 5ac01ff845f37fadd1822f99149f1b610f510bba (diff) | |
| download | rts-992e7fb9358a0d0a5d99ba119cf584477bda8d72.tar.gz | |
peripheral: provide common actions
Whereas some actions are context-specific (e.g.: selecting a player),
some are context-independent and can be executed for all screens
(e.g.: exiting the game).
Diffstat (limited to 'src')
| -rw-r--r-- | src/game/src/game.c | 3 | ||||
| -rw-r--r-- | src/peripheral/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/peripheral/inc/peripheral.h | 1 | ||||
| -rw-r--r-- | src/peripheral/src/peripheral.c | 25 | ||||
| -rw-r--r-- | src/player/inc/human_player.h | 2 | ||||
| -rw-r--r-- | src/player/src/human_player.c | 43 |
6 files changed, 38 insertions, 38 deletions
diff --git a/src/game/src/game.c b/src/game/src/game.c index dc3d55b..bcab5f2 100644 --- a/src/game/src/game.c +++ b/src/game/src/game.c @@ -88,7 +88,8 @@ int game(void) .n_res = sizeof res / sizeof *res }; - exit |= human_player_update(&humans[i], &o); + human_player_update(&humans[i], &o); + exit |= humans[i].periph.common.exit; terrain_update(&map); if (terrain_render(&map, &h->cam) diff --git a/src/peripheral/CMakeLists.txt b/src/peripheral/CMakeLists.txt index 9666a5c..54f5f1b 100644 --- a/src/peripheral/CMakeLists.txt +++ b/src/peripheral/CMakeLists.txt @@ -1,3 +1,3 @@ add_library(peripheral "src/peripheral.c") target_include_directories(peripheral PUBLIC "inc") -target_link_libraries(peripheral PUBLIC pad mouse keyboard util) +target_link_libraries(peripheral PUBLIC pad mouse keyboard util PRIVATE gfx) diff --git a/src/peripheral/inc/peripheral.h b/src/peripheral/inc/peripheral.h index c405c3c..956d965 100644 --- a/src/peripheral/inc/peripheral.h +++ b/src/peripheral/inc/peripheral.h @@ -25,6 +25,7 @@ union peripheral struct peripheral_common { enum peripheral_type type; + bool exit; } common; struct peripheral_pad diff --git a/src/peripheral/src/peripheral.c b/src/peripheral/src/peripheral.c index a1f61be..c461573 100644 --- a/src/peripheral/src/peripheral.c +++ b/src/peripheral/src/peripheral.c @@ -1,8 +1,26 @@ #include <peripheral.h> +#include <gfx.h> #include <keyboard.h> #include <mouse.h> #include <pad.h> +static void update_pad_common(union peripheral *const p) +{ + if (pad_justpressed(&p->pad.pad, PAD_KEY_EXIT)) + p->common.exit = true; +} + +static void update_kbm_common(union peripheral *const p) +{ + struct peripheral_kbm *const kbm = &p->kbm; + struct keyboard *const k = &kbm->keyboard; + + if (keyboard_justpressed(k, &KEYBOARD_COMBO(KEYBOARD_KEY_EXIT))) + p->common.exit = true; + else if (keyboard_justreleased(k, &KEYBOARD_COMBO(KEYBOARD_KEY_F11))) + gfx_toggle_fullscreen(); +} + void peripheral_update(union peripheral *const p) { switch (p->common.type) @@ -12,10 +30,12 @@ void peripheral_update(union peripheral *const p) case PERIPHERAL_TYPE_KEYBOARD_MOUSE: mouse_update(&p->kbm.mouse); keyboard_update(&p->kbm.keyboard); + update_kbm_common(p); break; case PERIPHERAL_TYPE_PAD: pad_update(&p->pad.pad); + update_pad_common(p); break; } } @@ -39,3 +59,8 @@ void peripheral_init(const struct peripheral_cfg *const cfg, break; } } + +int peripheral_get_default(struct peripheral_cfg *const cfg) +{ + return -1; +} diff --git a/src/player/inc/human_player.h b/src/player/inc/human_player.h index ca52112..39a0d6d 100644 --- a/src/player/inc/human_player.h +++ b/src/player/inc/human_player.h @@ -64,7 +64,7 @@ struct human_player }; int human_player_init(const struct human_player_cfg *cfg, struct human_player *h); -bool human_player_update(struct human_player *h, struct player_others *o); +void human_player_update(struct human_player *h, struct player_others *o); int human_player_render(const struct human_player *h, const struct player_others *o); #ifdef __cplusplus diff --git a/src/player/src/human_player.c b/src/player/src/human_player.c index 956b692..a70eddf 100644 --- a/src/player/src/human_player.c +++ b/src/player/src/human_player.c @@ -524,16 +524,12 @@ static void update_target(struct human_player *const h) } } -static bool update_from_pad(struct human_player *const h, +static void update_from_pad(struct human_player *const h, struct player_others *const o) { - bool ret = false; struct pad *const p = &h->periph.pad.pad; - if (pad_justpressed(p, PAD_KEY_OPTIONS) - || pad_justpressed(p, PAD_KEY_EXIT)) - ret = true; - else if (pad_justpressed(p, PAD_KEY_A)) + if (pad_justpressed(p, PAD_KEY_A)) select_instances(h, o, false, false); else if (pad_justpressed(p, PAD_KEY_B)) move_units(h, o); @@ -541,28 +537,12 @@ static bool update_from_pad(struct human_player *const h, deselect_instances(h); else if (pad_justpressed(p, PAD_KEY_E)) h->top_gui ^= true; - - return ret; -} - -static bool update_keyboard_mouse_common(const struct mouse *const m, - const struct keyboard *const k) -{ - bool ret = false; - - if (keyboard_justpressed(k, &KEYBOARD_COMBO(KEYBOARD_KEY_EXIT))) - ret = true; - else if (keyboard_justreleased(k, &KEYBOARD_COMBO(KEYBOARD_KEY_F11))) - gfx_toggle_fullscreen(); - - return ret; } -static bool update_from_touch(struct human_player *const h, +static void update_from_touch(struct human_player *const h, struct player_others *const o) { struct mouse *const m = &h->periph.kbm.mouse; - struct keyboard *const k = &h->periph.kbm.keyboard; struct peripheral_kbm *const kbm = &h->periph.kbm; bool *const pan = &h->cam.pan; @@ -587,11 +567,9 @@ static bool update_from_touch(struct human_player *const h, kbm->long_press = false; kbm->lp_t = 0; } - - return update_keyboard_mouse_common(m, k); } -static bool update_from_keyboard_mouse(struct human_player *const h, +static void update_from_keyboard_mouse(struct human_player *const h, struct player_others *const o) { struct mouse *const m = &h->periph.kbm.mouse; @@ -608,14 +586,11 @@ static bool update_from_keyboard_mouse(struct human_player *const h, } else if (mouse_justreleased(m, MOUSE_BUTTON_RIGHT)) move_units(h, o); - - return update_keyboard_mouse_common(m, k); } -bool human_player_update(struct human_player *const h, +void human_player_update(struct human_player *const h, struct player_others *const o) { - bool ret = false; struct player *const p = &h->pl; if (p->alive) @@ -628,23 +603,21 @@ bool human_player_update(struct human_player *const h, switch (h->periph.common.type) { case PERIPHERAL_TYPE_PAD: - ret = update_from_pad(h, o); + update_from_pad(h, o); break; case PERIPHERAL_TYPE_TOUCH: - ret = update_from_touch(h, o); + update_from_touch(h, o); break; case PERIPHERAL_TYPE_KEYBOARD_MOUSE: - ret = update_from_keyboard_mouse(h, o); + update_from_keyboard_mouse(h, o); break; } camera_update(&h->cam, &h->periph); player_update(p); } - - return ret; } static int render_target(const struct human_player *const h) |
