Moved mouse to devices/

This commit is contained in:
neauoire 2021-12-26 21:02:24 -08:00
parent 13fd0d9724
commit 89c07988ac
4 changed files with 73 additions and 31 deletions

View File

@ -20,6 +20,8 @@ then
clang-format -i src/devices/apu.c
clang-format -i src/devices/file.h
clang-format -i src/devices/file.c
clang-format -i src/devices/mouse.h
clang-format -i src/devices/mouse.c
clang-format -i src/uxnasm.c
clang-format -i src/uxnemu.c
clang-format -i src/uxncli.c
@ -62,7 +64,7 @@ fi
echo "Building.."
${CC} ${CFLAGS} src/uxnasm.c -o bin/uxnasm
${CC} ${CFLAGS} ${CORE} src/devices/file.c src/devices/ppu.c src/devices/apu.c src/uxnemu.c ${EXTRA} ${UXNEMU_LDFLAGS} -o bin/uxnemu
${CC} ${CFLAGS} ${CORE} src/devices/file.c src/devices/mouse.c src/devices/ppu.c src/devices/apu.c src/uxnemu.c ${EXTRA} ${UXNEMU_LDFLAGS} -o bin/uxnemu
${CC} ${CFLAGS} ${CORE} src/devices/file.c src/uxncli.c -o bin/uxncli
if [ -d "$HOME/bin" ]

44
src/devices/mouse.c Normal file
View File

@ -0,0 +1,44 @@
#include "../uxn.h"
#include "mouse.h"
/*
Copyright (c) 2021 Devine Lu Linvega
Copyright (c) 2021 Andrew Alderwick
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/
void
mouse_xy(Device *d, Uint16 x, Uint16 y)
{
poke16(d->dat, 0x2, x);
poke16(d->dat, 0x4, y);
uxn_eval(d->u, d->vector);
}
void
mouse_z(Device *d, Uint8 z)
{
d->dat[7] = z;
uxn_eval(d->u, d->vector);
d->dat[7] = 0x00;
}
void
mouse_down(Device *d, Uint8 mask)
{
d->dat[6] |= mask;
uxn_eval(d->u, d->vector);
}
void
mouse_up(Device *d, Uint8 mask)
{
d->dat[6] &= (~mask);
uxn_eval(d->u, d->vector);
}

16
src/devices/mouse.h Normal file
View File

@ -0,0 +1,16 @@
/*
Copyright (c) 2021 Devine Lu Linvega
Copyright (c) 2021 Andrew Alderwick
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE.
*/
void mouse_xy(Device *d, Uint16 x, Uint16 y);
void mouse_z(Device *d, Uint8 z);
void mouse_down(Device *d, Uint8 mask);
void mouse_up(Device *d, Uint8 mask);

View File

@ -11,6 +11,7 @@
#include "devices/ppu.h"
#include "devices/apu.h"
#include "devices/file.h"
#include "devices/mouse.h"
#pragma GCC diagnostic pop
#pragma clang diagnostic pop
@ -196,33 +197,6 @@ init(void)
return 1;
}
static void
domouse(SDL_Event *event)
{
Uint8 flag = 0x00;
Uint16 x = clamp(event->motion.x - PAD, 0, ppu.width - 1);
Uint16 y = clamp(event->motion.y - PAD, 0, ppu.height - 1);
if(event->type == SDL_MOUSEWHEEL) {
devmouse->dat[7] = event->wheel.y;
return;
}
poke16(devmouse->dat, 0x2, x);
poke16(devmouse->dat, 0x4, y);
devmouse->dat[7] = 0x00;
switch(event->button.button) {
case SDL_BUTTON_LEFT: flag = 0x01; break;
case SDL_BUTTON_RIGHT: flag = 0x10; break;
}
switch(event->type) {
case SDL_MOUSEBUTTONDOWN:
devmouse->dat[6] |= flag;
break;
case SDL_MOUSEBUTTONUP:
devmouse->dat[6] &= (~flag);
break;
}
}
#pragma mark - Devices
static Uint8
@ -494,7 +468,6 @@ run(Uxn *u)
doctrl(u, &event, event.type == SDL_KEYDOWN);
uxn_eval(u, devctrl->vector);
devctrl->dat[3] = 0;
if(event.type == SDL_KEYDOWN) {
ksym = event.key.keysym.sym;
if(SDL_PeepEvents(&event, 1, SDL_PEEKEVENT, SDL_KEYUP, SDL_KEYUP) == 1 && ksym == event.key.keysym.sym)
@ -502,11 +475,18 @@ run(Uxn *u)
}
break;
case SDL_MOUSEWHEEL:
mouse_z(devmouse, event.wheel.y);
break;
case SDL_MOUSEBUTTONUP:
mouse_up(devmouse, 0x1 << (event.button.button - 1));
break;
case SDL_MOUSEBUTTONDOWN:
mouse_down(devmouse, 0x1 << (event.button.button - 1));
break;
case SDL_MOUSEMOTION:
domouse(&event);
uxn_eval(u, devmouse->vector);
mouse_xy(devmouse,
clamp(event.motion.x - PAD, 0, ppu.width - 1),
clamp(event.motion.y - PAD, 0, ppu.height - 1));
break;
case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_EXPOSED)