aboutsummaryrefslogtreecommitdiff
path: root/src/input.h
blob: 996daf946a91b94354a7a9a7d91b6c4f037c4cf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#ifndef PW_INPUT_H
#define PW_INPUT_H

#include <SDL3/SDL_events.h>
#include <SDL3/SDL_gamepad.h>

#define PW_PLAYERS_MAX 8

/**
 * PICO-8 controller buttons.
 */
enum pw_button {
  PW_BUTTON_NONE = 0,
  PW_BUTTON_LEFT = 1 << 0,
  PW_BUTTON_RIGHT = 1 << 1,
  PW_BUTTON_UP = 1 << 2,
  PW_BUTTON_DOWN = 1 << 3,
  PW_BUTTON_O = 1 << 4,
  PW_BUTTON_X = 1 << 5,
  PW_BUTTON_MENU = 1 << 6,
  PW_BUTTON_ALL = 0x7f,
};

/**
 * A PICO-8 controller.
 */
struct pw_controller {
  SDL_Gamepad *gamepad;
  enum pw_button button;
};

/**
 * PICO-8 controllers.
 */
struct pw_controllers {
  struct pw_controller players[PW_PLAYERS_MAX];
};

/**
 * Initializes the controller state.
 */
bool pw_controllers_init(struct pw_controllers *controllers);

/**
 * Cleanup controller gamepad handles.
 */
void pw_controllers_destroy(struct pw_controllers *controllers);

/**
 * Handles an SDL gamepad events for controller input. Converts the SDL event
 * into a PICO-w controller update.
 */
bool pw_controllers_update(struct pw_controllers *controllers,
                           SDL_Event *event);

/**
 * Gets the button state for a player.
 */
uint8_t pw_controller_button(struct pw_controllers *controllers, size_t player);

#endif