aboutsummaryrefslogtreecommitdiff
path: root/src/input.h
blob: 781091843fd4155078ba6ba1a72fe68e3fabebc4 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#ifndef PW_INPUT_H
#define PW_INPUT_H

#include <SDL3/SDL_events.h>

#include "fb.h"
#include "player.h"

#define PW_AXIS_THRESHOLD 9830
#define PW_TRIGGER_THRESHOLD 16384
#define PW_KEYBOARD_BUFFER_SIZE 256

/**
 * 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 keyboard modifier.
 */
enum pw_keymod {
  PW_KEYMOD_NONE = 0,
  PW_KEYMOD_LSHIFT = 1 << 0,
  PW_KEYMOD_RSHIFT = 1 << 1,
  PW_KEYMOD_LCTRL = 1 << 6,
  PW_KEYMOD_RCTRL = 1 << 7,
  PW_KEYMOD_LALT = 1 << 8,
  PW_KEYMOD_RALT = 1 << 9,
};

/**
 * A queue used for storing queued key events and text input.
 */
struct pw_keyboard_queue {
  uint32_t items[PW_KEYBOARD_BUFFER_SIZE];
  size_t start;
  size_t end;
};

/**
 * The PICO-8 keyboard. This is used both for collecting keyboard events that
 * are used by PICO-8 when no gamepads are connected, but also for buffering
 * text for the `devkit` mode.
 */
struct pw_keyboard {
  enum pw_keymod mod;
  struct pw_keyboard_queue scancodes;
  struct pw_keyboard_queue text;
};

/**
 * A PICO-8 mouse button.
 */
enum pw_mouse_button {
  PW_MOUSE_BUTTON_NONE = 0,
  PW_MOUSE_BUTTON_LEFT = 1 << 0,
  PW_MOUSE_BUTTON_MIDDLE = 1 << 1,
  PW_MOUSE_BUTTON_RIGHT = 1 << 2,
};

/**
 * PICO-8 mouse wheel motion.
 */
struct pw_mouse_wheel {
  int32_t dx;
  int32_t dy;
};

/**
 * The PICO-8 mouse state.
 */
struct pw_mouse {
  enum pw_mouse_button button;
  struct pw_mouse_wheel wheel;
  struct pw_coordinates pos;
  int32_t xrel;
  int32_t yrel;
};

/**
 * A PICO-8 input event.
 *
 * This is only a subset of SDL events that the PICO-8 engine uses.
 */
union pw_input_event {
  enum pw_input_event_type {
    PW_INPUT_EVENT_KEY,
    PW_INPUT_EVENT_MOUSE,
  } type;

  struct pw_input_event_key {
    enum pw_input_event_type type;
    bool pressed;
    bool repeat;
    uint32_t scancode;
    uint32_t keycode;
    enum pw_keymod mod;
  } key;
  struct pw_input_event_mouse {
    enum pw_input_event_type type;
    enum pw_mouse_button button;
    int32_t x;
    int32_t y;
    int32_t xrel;
    int32_t yrel;
  } mouse;
};

/**
 * PICO-8 input.
 */
struct pw_input {
  SDL_Window *window;
  size_t nplayers;
  enum pw_button players[PW_PLAYERS_MAX];
  struct pw_keyboard keyboard;
  struct pw_mouse mouse;
};

/**
 * Allocates and initializes the PICO-8 input state.
 */
struct pw_input *pw_input_init(SDL_Window *window);

/**
 * Cleanup and free the PICO-8 input state.
 */
void pw_input_free(struct pw_input *input);

/**
 * Handles an SDL events for PICO-8 input.
 */
bool pw_input_event(struct pw_input *input, struct pw_players *players,
                    SDL_Event *event);

/**
 * Updates PICO-8 input state, once per frame.
 */
bool pw_input_iterate(struct pw_input *input, struct pw_framebuffer *fb);

/**
 * Gets the number of connected players.
 */
size_t pw_input_players(struct pw_input *input);

/**
 * Gets the button state for a player.
 */
enum pw_button pw_input_btn(struct pw_input *input, size_t player);

/**
 * Gets the current keyboard modifier state.
 */
enum pw_keymod pw_input_keymod(struct pw_input *input);

/**
 * Gets the current mouse button state.
 */
enum pw_mouse_button pw_input_mouse(struct pw_input *input,
                                    struct pw_coordinates *pos);

/**
 * Gets the accumulated mouse wheel change, resetting the accumulator.
 *
 * Can specify `NULL` to ignore coordinate.
 */
void pw_input_wheel(struct pw_input *input, int32_t *dx, int32_t *dy);

/**
 * Polls for the next input event for the PICO-8 engine.
 */
bool pw_input_poll(struct pw_input *input, union pw_input_event *event);

/**
 * Sets the mouse pointer lock.
 *
 * Returns `false` in case of an error.
 */
bool pw_input_set_pointer_lock(struct pw_input *input, bool lock);

/**
 * Gets whether or not the pointer is currently locked.
 */
bool pw_input_get_pointer_lock(struct pw_input *input);

#endif