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).
This commit is contained in:
parent
5ac01ff845
commit
992e7fb935
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -25,6 +25,7 @@ union peripheral
|
|||
struct peripheral_common
|
||||
{
|
||||
enum peripheral_type type;
|
||||
bool exit;
|
||||
} common;
|
||||
|
||||
struct peripheral_pad
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue