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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
#include <SDL3/SDL_log.h>
#include "input.h"
#include "menu.h"
static bool load_cart(struct pw_menu *menu, size_t i) {
struct pw_cart_file *file = &menu->carts->files[i];
menu->selection.valid = pw_cart_png_load(file, menu->png);
if (!menu->selection.valid) {
SDL_LogWarn(0, "malformed cart '%s': %s", file->name, SDL_GetError());
if (!pw_cart_png_reset(menu->png)) {
return false;
}
}
return true;
}
struct pw_menu *pw_menu_init(SDL_Renderer *renderer, char const *carts,
enum pw_scale_mode mode) {
if (!renderer) {
SDL_InvalidParamError("renderer");
return NULL;
}
if (!carts) {
SDL_InvalidParamError("carts");
return NULL;
}
struct pw_menu *menu = SDL_calloc(1, sizeof(*menu));
if (!menu) {
return NULL;
}
menu->carts = pw_carts_list(carts);
if (!menu->carts) {
goto error;
}
menu->png = pw_cart_png_init(renderer);
if (!menu->png) {
goto error;
}
if (!menu->carts->count) {
SDL_LogWarn(0, "empty or missing carts directory");
menu->selection.valid = false;
} else if (!load_cart(menu, 0)) {
goto error;
}
switch (mode) {
case PW_SCALE_MODE_STRETCH:
// Stretching the cart PNGs in the menu looks silly, so in case PICO-w is
// running in stretch mode, then use fill mode for the cart selection.
menu->mode = PW_SCALE_MODE_FILL;
break;
default:
menu->mode = mode;
break;
}
return menu;
error:
if (menu->carts) {
pw_carts_free(menu->carts);
}
if (menu->png) {
pw_cart_png_free(menu->png);
}
SDL_free(menu);
return NULL;
}
void pw_menu_free(struct pw_menu *menu) {
if (!menu) {
SDL_InvalidParamError("menu");
return;
}
pw_carts_free(menu->carts);
pw_cart_png_free(menu->png);
SDL_free(menu);
}
static size_t saturating_add(size_t a, size_t b) {
size_t r = a + b;
if (r < a) {
return SIZE_MAX;
}
return r;
}
static size_t saturating_sub(size_t a, size_t b) {
size_t r = a - b;
if (r > a) {
return 0;
}
return r;
}
bool pw_menu_event(struct pw_menu *menu, SDL_Event *event) {
if (!menu) {
SDL_InvalidParamError("menu");
return false;
}
if (!event) {
SDL_InvalidParamError("event");
return false;
}
if (!menu->carts->count) {
return true;
}
size_t next = menu->selection.next;
switch (event->type) {
case SDL_EVENT_GAMEPAD_AXIS_MOTION:
switch (event->gaxis.axis) {
case SDL_GAMEPAD_AXIS_LEFTX:
if (event->gaxis.value < -PW_AXIS_THRESHOLD) {
next = saturating_sub(next, 1);
} else if (event->gaxis.value > PW_AXIS_THRESHOLD) {
next = saturating_add(next, 1);
}
break;
case SDL_GAMEPAD_AXIS_LEFTY:
if (event->gaxis.value < -PW_AXIS_THRESHOLD) {
next = saturating_sub(next, 10);
} else if (event->gaxis.value > PW_AXIS_THRESHOLD) {
next = saturating_add(next, 10);
}
break;
}
break;
case SDL_EVENT_GAMEPAD_BUTTON_DOWN:
switch (event->gbutton.button) {
case SDL_GAMEPAD_BUTTON_NORTH:
case SDL_GAMEPAD_BUTTON_SOUTH:
case SDL_GAMEPAD_BUTTON_EAST:
case SDL_GAMEPAD_BUTTON_WEST:
case SDL_GAMEPAD_BUTTON_START:
case SDL_GAMEPAD_BUTTON_BACK:
menu->selection.selected = true;
break;
case SDL_GAMEPAD_BUTTON_DPAD_UP:
next = saturating_sub(next, 10);
break;
case SDL_GAMEPAD_BUTTON_DPAD_DOWN:
next = saturating_add(next, 10);
break;
case SDL_GAMEPAD_BUTTON_DPAD_LEFT:
next = saturating_sub(next, 1);
break;
case SDL_GAMEPAD_BUTTON_DPAD_RIGHT:
next = saturating_add(next, 1);
break;
}
break;
case SDL_EVENT_KEY_DOWN:
switch (event->key.key) {
case SDLK_Z:
case SDLK_X:
case SDLK_RETURN:
menu->selection.selected = true;
break;
case SDLK_UP:
case SDLK_K:
next = saturating_sub(next, 10);
break;
case SDLK_DOWN:
case SDLK_J:
next = saturating_add(next, 10);
break;
case SDLK_LEFT:
case SDLK_H:
next = saturating_sub(next, 1);
break;
case SDLK_RIGHT:
case SDLK_L:
next = saturating_add(next, 1);
break;
}
break;
}
menu->selection.next = SDL_min(next, menu->carts->count - 1);
return true;
}
bool pw_menu_iterate(struct pw_menu *menu, struct pw_cart **cart) {
if (!menu) {
SDL_InvalidParamError("menu");
return false;
}
if (!cart) {
SDL_InvalidParamError("cartdat");
return false;
}
if (menu->selection.current != menu->selection.next) {
if (!load_cart(menu, menu->selection.next)) {
return false;
}
menu->selection.current = menu->selection.next;
}
if (menu->selection.valid && menu->selection.selected) {
*cart =
pw_cart_load(&menu->carts->files[menu->selection.current], menu->png);
if (!*cart) {
return false;
}
} else {
*cart = NULL;
}
menu->selection.selected = false;
return true;
}
bool pw_menu_render(struct pw_menu *menu) {
SDL_Texture *png = menu->png->texture;
SDL_Renderer *renderer = SDL_GetRendererFromTexture(png);
if (!renderer) {
return false;
}
SDL_FRect target;
if (!pw_scale(menu->mode, png, &target)) {
return false;
}
return SDL_RenderTexture(renderer, png, NULL, &target);
}
|