aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2022-10-19 13:17:08 +0200
committerBad Diode <bd@badd10de.dev>2022-10-19 13:17:08 +0200
commitdb06aef7cb6539becbbef77c8dad472e90e50770 (patch)
treeb2419bcccfd81929427731b6b6fc799462e1bd96 /src
parentf9058cc2dfa087ffcae8d6fe72d9343dcfc9cc15 (diff)
downloaduxn64-db06aef7cb6539becbbef77c8dad472e90e50770.tar.gz
uxn64-db06aef7cb6539becbbef77c8dad472e90e50770.zip
Add "mouse" support (analog stick)
Diffstat (limited to 'src')
-rw-r--r--src/main.c75
1 files changed, 66 insertions, 9 deletions
diff --git a/src/main.c b/src/main.c
index d457140..71016cb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -36,7 +36,7 @@ OSPiHandle *rom_handle;
36#include "ppu.c" 36#include "ppu.c"
37#include "uxn/src/uxn.c" 37#include "uxn/src/uxn.c"
38 38
39#include "uxn_controller_rom.c" 39#include "uxn_polycat_rom.c"
40 40
41#define CLAMP(X, MIN, MAX) ((X) <= (MIN) ? (MIN) : (X) > (MAX) ? (MAX): (X)) 41#define CLAMP(X, MIN, MAX) ((X) <= (MIN) ? (MIN) : (X) > (MAX) ? (MAX): (X))
42 42
@@ -50,9 +50,12 @@ static Device *devmouse;
50typedef struct Mouse { 50typedef struct Mouse {
51 s32 x; 51 s32 x;
52 s32 y; 52 s32 y;
53 u8 buttons;
53 // TODO: mouse timeout? 54 // TODO: mouse timeout?
54} Mouse; 55} Mouse;
55 56
57static Mouse mouse = {0};
58
56int 59int
57uxn_halt(Uxn *u, Uint8 error, Uint16 addr) { 60uxn_halt(Uxn *u, Uint8 error, Uint16 addr) {
58 (void)u; 61 (void)u;
@@ -217,59 +220,113 @@ handle_input(int i) {
217 OSContPad prev_pad = ctrl_pad[i]; 220 OSContPad prev_pad = ctrl_pad[i];
218 osContGetReadData(&ctrl_pad[i]); 221 osContGetReadData(&ctrl_pad[i]);
219 OSContPad current_pad = ctrl_pad[i]; 222 OSContPad current_pad = ctrl_pad[i];
220 // TODO: Check for controller changes. 223 if (current_pad.errno != 0) {
224 return;
225 }
226 bool update_ctrl = false;
227 bool update_mouse = false;
228
229 // Check for controller changes.
221 if (prev_pad.button != current_pad.button) { 230 if (prev_pad.button != current_pad.button) {
222 u8 *uxn_ctrl = &devctrl->dat[2]; 231 u8 *uxn_ctrl = &devctrl->dat[2];
223 if (current_pad.button & U_JPAD || current_pad.button & U_CBUTTONS) { 232 if (current_pad.button & U_JPAD || current_pad.button & U_CBUTTONS) {
224 *uxn_ctrl |= 0x10; 233 *uxn_ctrl |= 0x10;
234 update_ctrl = true;
225 } else { 235 } else {
226 *uxn_ctrl &= ~0x10; 236 *uxn_ctrl &= ~0x10;
237 update_ctrl = true;
227 } 238 }
228 if (current_pad.button & D_JPAD || current_pad.button & D_CBUTTONS) { 239 if (current_pad.button & D_JPAD || current_pad.button & D_CBUTTONS) {
229 *uxn_ctrl |= 0x20; 240 *uxn_ctrl |= 0x20;
241 update_ctrl = true;
230 } else { 242 } else {
231 *uxn_ctrl &= ~0x20; 243 *uxn_ctrl &= ~0x20;
244 update_ctrl = true;
232 } 245 }
233 if (current_pad.button & L_JPAD || current_pad.button & L_CBUTTONS) { 246 if (current_pad.button & L_JPAD || current_pad.button & L_CBUTTONS) {
234 *uxn_ctrl |= 0x40; 247 *uxn_ctrl |= 0x40;
248 update_ctrl = true;
235 } else { 249 } else {
236 *uxn_ctrl &= ~0x40; 250 *uxn_ctrl &= ~0x40;
251 update_ctrl = true;
237 } 252 }
238 if (current_pad.button & R_JPAD || current_pad.button & R_CBUTTONS) { 253 if (current_pad.button & R_JPAD || current_pad.button & R_CBUTTONS) {
239 *uxn_ctrl |= 0x80; 254 *uxn_ctrl |= 0x80;
255 update_ctrl = true;
240 } else { 256 } else {
241 *uxn_ctrl &= ~0x80; 257 *uxn_ctrl &= ~0x80;
258 update_ctrl = true;
242 } 259 }
243 if (current_pad.button & A_BUTTON) { 260 if (current_pad.button & A_BUTTON) {
244 *uxn_ctrl |= 0x01; 261 *uxn_ctrl |= 0x01;
262 update_ctrl = true;
245 } else { 263 } else {
246 *uxn_ctrl &= ~0x01; 264 *uxn_ctrl &= ~0x01;
265 update_ctrl = true;
247 } 266 }
248 if (current_pad.button & B_BUTTON) { 267 if (current_pad.button & B_BUTTON) {
249 *uxn_ctrl |= 0x02; 268 *uxn_ctrl |= 0x02;
269 update_ctrl = true;
250 } else { 270 } else {
251 *uxn_ctrl &= ~0x02; 271 *uxn_ctrl &= ~0x02;
272 update_ctrl = true;
252 } 273 }
253 if (current_pad.button & START_BUTTON) { 274 if (current_pad.button & START_BUTTON) {
254 *uxn_ctrl |= 0x08; 275 *uxn_ctrl |= 0x08;
276 update_ctrl = true;
255 } else { 277 } else {
256 *uxn_ctrl &= ~0x08; 278 *uxn_ctrl &= ~0x08;
279 update_ctrl = true;
257 } 280 }
258 if (current_pad.button & Z_TRIG) { 281 if (current_pad.button & Z_TRIG) {
259 *uxn_ctrl |= 0x04; 282 *uxn_ctrl |= 0x04;
283 update_ctrl = true;
260 } else { 284 } else {
261 *uxn_ctrl &= ~0x04; 285 *uxn_ctrl &= ~0x04;
286 update_ctrl = true;
287 }
288
289 // Check for mouse button changes (L/R on the pad).
290 if (current_pad.button & L_TRIG) {
291 mouse.buttons |= 0x01;
292 update_mouse = true;
293 } else {
294 mouse.buttons &= ~0x01;
295 update_mouse = true;
296 }
297 if (current_pad.button & R_TRIG) {
298 mouse.buttons |= 0x10;
299 update_mouse = true;
300 } else {
301 mouse.buttons &= ~0x10;
302 update_mouse = true;
262 } 303 }
304 }
305
306 // Check for "mouse" x/y changes.
307 if (current_pad.stick_x != 0 || current_pad.stick_y != 0) {
308 Device *d = devmouse;
309 mouse.x = CLAMP(mouse.x + prev_pad.stick_x / 4, 0, screen_width);
310 mouse.y = CLAMP(mouse.y - prev_pad.stick_y / 4, 0, screen_height);
311 DEVPOKE16(0x2, mouse.x);
312 DEVPOKE16(0x4, mouse.y);
313 update_mouse = true;
314 }
315
316 if (update_ctrl) {
263 uxn_eval(&u, GETVECTOR(devctrl)); 317 uxn_eval(&u, GETVECTOR(devctrl));
264 devctrl->dat[3] = 0; 318 devctrl->dat[3] = 0;
265 // TODO: L/R mouse ? || stick x || xtick y || errno?
266 } 319 }
267 // TODO: Check for "mouse" changes. 320 if (update_mouse) {
268 // if (controller_now != in.controller) { 321 devmouse->dat[6] = mouse.buttons;
269 // devctrl->dat[2] = controller_now; 322 if(mouse.buttons == 0x10 && (devmouse->dat[6] & 0x01)) {
270 // uxn_eval(&u, GETVECTOR(devctrl)); 323 devmouse->dat[7] = 0x01;
271 // in.controller = controller_now; 324 }
272 // } 325 if(mouse.buttons == 0x01 && (devmouse->dat[6] & 0x10)) {
326 devmouse->dat[7] = 0x10;
327 }
328 uxn_eval(&u, GETVECTOR(devmouse));
329 }
273} 330}
274 331
275void 332void