commit a1ade4c

neauoire  ·  2022-03-27 04:03:03 +0000 UTC
parent f494421
Implemented mouse
6 files changed,  +77, -122
+2, -2
 1@@ -9,10 +9,10 @@ echo "Building.."
 2 mkdir -p bin
 3 
 4 # Build(debug)
 5-gcc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined src/uxn.c src/devices/system.c src/devices/screen.c src/devices/controller.c src/uxn11.c -o bin/uxn11 -lX11
 6+gcc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined src/uxn.c src/devices/system.c src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/devices/datetime.c src/uxn11.c -o bin/uxn11 -lX11
 7 
 8 # Build(release)
 9-# gcc src/uxn.c src/devices/system.c src/devices/screen.c src/devices/controller.c src/uxn11.c -o bin/uxn11 -lX11
10+# gcc src/uxn.c src/devices/system.c src/devices/screen.c src/devices/controller.c src/devices/mouse.c src/devices/datetime.c src/uxn11.c -o bin/uxn11 -lX11
11 
12 echo "Running.."
13 bin/uxn11 etc/controller.rom
+0, -0
+0, -97
 1@@ -1,97 +0,0 @@
 2-#include "../uxn.h"
 3-#include "apu.h"
 4-
 5-/*
 6-Copyright (c) 2021 Devine Lu Linvega
 7-Copyright (c) 2021 Andrew Alderwick
 8-
 9-Permission to use, copy, modify, and distribute this software for any
10-purpose with or without fee is hereby granted, provided that the above
11-copyright notice and this permission notice appear in all copies.
12-
13-THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14-WITH REGARD TO THIS SOFTWARE.
15-*/
16-
17-#define NOTE_PERIOD (SAMPLE_FREQUENCY * 0x4000 / 11025)
18-#define ADSR_STEP (SAMPLE_FREQUENCY / 0xf)
19-
20-/* clang-format off */
21-
22-static Uint32 advances[12] = {
23-	0x80000, 0x879c8, 0x8facd, 0x9837f, 0xa1451, 0xaadc1,
24-	0xb504f, 0xbfc88, 0xcb2ff, 0xd7450, 0xe411f, 0xf1a1c
25-};
26-
27-/* clang-format on */
28-
29-static Sint32
30-envelope(Apu *c, Uint32 age)
31-{
32-	if(!c->r) return 0x0888;
33-	if(age < c->a) return 0x0888 * age / c->a;
34-	if(age < c->d) return 0x0444 * (2 * c->d - c->a - age) / (c->d - c->a);
35-	if(age < c->s) return 0x0444;
36-	if(age < c->r) return 0x0444 * (c->r - age) / (c->r - c->s);
37-	c->advance = 0;
38-	return 0x0000;
39-}
40-
41-int
42-apu_render(Apu *c, Sint16 *sample, Sint16 *end)
43-{
44-	Sint32 s;
45-	if(!c->advance || !c->period) return 0;
46-	while(sample < end) {
47-		c->count += c->advance;
48-		c->i += c->count / c->period;
49-		c->count %= c->period;
50-		if(c->i >= c->len) {
51-			if(!c->repeat) {
52-				c->advance = 0;
53-				break;
54-			}
55-			c->i %= c->len;
56-		}
57-		s = (Sint8)(c->addr[c->i] + 0x80) * envelope(c, c->age++);
58-		*sample++ += s * c->volume[0] / 0x180;
59-		*sample++ += s * c->volume[1] / 0x180;
60-	}
61-	if(!c->advance) apu_finished_handler(c);
62-	return 1;
63-}
64-
65-void
66-apu_start(Apu *c, Uint16 adsr, Uint8 pitch)
67-{
68-	if(pitch < 108 && c->len)
69-		c->advance = advances[pitch % 12] >> (8 - pitch / 12);
70-	else {
71-		c->advance = 0;
72-		return;
73-	}
74-	c->a = ADSR_STEP * (adsr >> 12);
75-	c->d = ADSR_STEP * (adsr >> 8 & 0xf) + c->a;
76-	c->s = ADSR_STEP * (adsr >> 4 & 0xf) + c->d;
77-	c->r = ADSR_STEP * (adsr >> 0 & 0xf) + c->s;
78-	c->age = 0;
79-	c->i = 0;
80-	if(c->len <= 0x100) /* single cycle mode */
81-		c->period = NOTE_PERIOD * 337 / 2 / c->len;
82-	else /* sample repeat mode */
83-		c->period = NOTE_PERIOD;
84-}
85-
86-Uint8
87-apu_get_vu(Apu *c)
88-{
89-	int i;
90-	Sint32 sum[2] = {0, 0};
91-	if(!c->advance || !c->period) return 0;
92-	for(i = 0; i < 2; ++i) {
93-		if(!c->volume[i]) continue;
94-		sum[i] = 1 + envelope(c, c->age) * c->volume[i] / 0x800;
95-		if(sum[i] > 0xf) sum[i] = 0xf;
96-	}
97-	return (sum[0] << 4) | sum[1];
98-}
+46, -0
 1@@ -0,0 +1,46 @@
 2+#include "../uxn.h"
 3+#include "mouse.h"
 4+
 5+/*
 6+Copyright (c) 2021 Devine Lu Linvega
 7+Copyright (c) 2021 Andrew Alderwick
 8+
 9+Permission to use, copy, modify, and distribute this software for any
10+purpose with or without fee is hereby granted, provided that the above
11+copyright notice and this permission notice appear in all copies.
12+
13+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14+WITH REGARD TO THIS SOFTWARE.
15+*/
16+
17+void
18+mouse_down(Device *d, Uint8 mask)
19+{
20+	d->dat[6] |= mask;
21+	uxn_eval(d->u, GETVECTOR(d));
22+}
23+
24+void
25+mouse_up(Device *d, Uint8 mask)
26+{
27+	d->dat[6] &= (~mask);
28+	uxn_eval(d->u, GETVECTOR(d));
29+}
30+
31+void
32+mouse_pos(Device *d, Uint16 x, Uint16 y)
33+{
34+	DEVPOKE16(0x2, x);
35+	DEVPOKE16(0x4, y);
36+	uxn_eval(d->u, GETVECTOR(d));
37+}
38+
39+void
40+mouse_scroll(Device *d, Uint16 x, Uint16 y)
41+{
42+	DEVPOKE16(0xa, x);
43+	DEVPOKE16(0xc, -y);
44+	uxn_eval(d->u, GETVECTOR(d));
45+	DEVPOKE16(0xa, 0);
46+	DEVPOKE16(0xc, 0);
47+}
R src/devices/apu.h => src/devices/mouse.h
+4, -16
 1@@ -10,19 +10,7 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 2 WITH REGARD TO THIS SOFTWARE.
 3 */
 4 
 5-typedef signed int Sint32;
 6-
 7-#define SAMPLE_FREQUENCY 44100
 8-
 9-typedef struct {
10-	Uint8 *addr;
11-	Uint32 count, advance, period, age, a, d, s, r;
12-	Uint16 i, len;
13-	Sint8 volume[2];
14-	Uint8 pitch, repeat;
15-} Apu;
16-
17-int apu_render(Apu *c, Sint16 *sample, Sint16 *end);
18-void apu_start(Apu *c, Uint16 adsr, Uint8 pitch);
19-Uint8 apu_get_vu(Apu *c);
20-void apu_finished_handler(Apu *c);
21+void mouse_down(Device *d, Uint8 mask);
22+void mouse_up(Device *d, Uint8 mask);
23+void mouse_pos(Device *d, Uint16 x, Uint16 y);
24+void mouse_scroll(Device *d, Uint16 x, Uint16 y);
+25, -7
 1@@ -13,8 +13,10 @@
 2 #include "devices/system.h"
 3 #include "devices/screen.h"
 4 #include "devices/controller.h"
 5+#include "devices/mouse.h"
 6+#include "devices/datetime.h"
 7 
 8-static Device *devctrl;
 9+static Device *devscreen, *devctrl, *devmouse;
10 
11 static int
12 error(char *msg, const char *err)
13@@ -79,6 +81,8 @@ redraw(Display *display, Visual *visual, Window window)
14 
15 /* /usr/include/X11/keysymdef.h */
16 
17+#define XK_Escape 0xff1b
18+
19 #define XK_Left 0xff51
20 #define XK_Up 0xff52
21 #define XK_Right 0xff53
22@@ -100,6 +104,7 @@ processEvent(Display *display, Visual *visual, Window window)
23 		break;
24 	case KeyPress: {
25 		XKeyPressedEvent *e = (XKeyPressedEvent *)&ev;
26+		if(e->keycode == XKeysymToKeycode(display, XK_Escape)) exit(0);
27 		if(e->keycode == XKeysymToKeycode(display, XK_Up)) controller_down(devctrl, 0x10);
28 		if(e->keycode == XKeysymToKeycode(display, XK_Down)) controller_down(devctrl, 0x20);
29 		if(e->keycode == XKeysymToKeycode(display, XK_Left)) controller_down(devctrl, 0x40);
30@@ -120,8 +125,19 @@ processEvent(Display *display, Visual *visual, Window window)
31 		if(e->keycode == XKeysymToKeycode(display, XK_Shift)) controller_up(devctrl, 0x04);
32 		if(e->keycode == XKeysymToKeycode(display, XK_Home)) controller_up(devctrl, 0x08);
33 	} break;
34-	case ButtonPress:
35-		exit(0);
36+	case ButtonPress: {
37+		XButtonPressedEvent *e = (XButtonPressedEvent *)&ev;
38+		mouse_down(devmouse, e->button);
39+	} break;
40+	case ButtonRelease: {
41+		XButtonPressedEvent *e = (XButtonPressedEvent *)&ev;
42+		mouse_up(devmouse, e->button);
43+	} break;
44+
45+	case MotionNotify: {
46+		XMotionEvent *e = (XMotionEvent *)&ev;
47+		mouse_pos(devmouse, e->x, e->y);
48+	} break;
49 	}
50 	if(uxn_screen.fg.changed || uxn_screen.bg.changed) {
51 		redraw(display, visual, window);
52@@ -135,17 +151,17 @@ start(Uxn *u)
53 		return error("Boot", "Failed");
54 	/* system   */ uxn_port(u, 0x0, system_dei, system_deo);
55 	/* console  */ uxn_port(u, 0x1, nil_dei, console_deo);
56-	/* screen   */ uxn_port(u, 0x2, screen_dei, screen_deo);
57+	/* screen   */ devscreen = uxn_port(u, 0x2, screen_dei, screen_deo);
58 	/* empty    */ uxn_port(u, 0x3, nil_dei, nil_deo);
59 	/* empty    */ uxn_port(u, 0x4, nil_dei, nil_deo);
60 	/* empty    */ uxn_port(u, 0x5, nil_dei, nil_deo);
61 	/* empty    */ uxn_port(u, 0x6, nil_dei, nil_deo);
62 	/* empty    */ uxn_port(u, 0x7, nil_dei, nil_deo);
63 	/* control  */ devctrl = uxn_port(u, 0x8, nil_dei, nil_deo);
64-	/* empty    */ uxn_port(u, 0x9, nil_dei, nil_deo);
65+	/* mouse    */ devmouse = uxn_port(u, 0x9, nil_dei, nil_deo);
66 	/* file     */ uxn_port(u, 0xa, nil_dei, nil_deo);
67 	/* datetime */ uxn_port(u, 0xb, nil_dei, nil_deo);
68-	/* empty    */ uxn_port(u, 0xc, nil_dei, nil_deo);
69+	/* empty    */ uxn_port(u, 0xc, datetime_dei, nil_deo);
70 	/* empty    */ uxn_port(u, 0xd, nil_dei, nil_deo);
71 	/* empty    */ uxn_port(u, 0xe, nil_dei, nil_deo);
72 	/* empty    */ uxn_port(u, 0xf, nil_dei, nil_deo);
73@@ -177,10 +193,12 @@ main(int argc, char **argv)
74 		exit(1);
75 	}
76 
77-	XSelectInput(display, window, ButtonPressMask | ExposureMask | KeyPressMask | KeyReleaseMask);
78+	XSelectInput(display, window, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | KeyPressMask | KeyReleaseMask);
79 	XMapWindow(display, window);
80 	while(1) {
81 		processEvent(display, visual, window);
82+		uxn_eval(&u, GETVECTOR(devscreen));
83+		/* sleep(0.01); */
84 	}
85 	return 0;
86 }