aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.c
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-05-21 09:23:11 +0200
committerBad Diode <bd@badd10de.dev>2021-05-21 09:23:11 +0200
commit65cf69fab1c90b5581f277761bd9342f1c7b4d84 (patch)
tree44aac6d213476589c3759d8d13baa0e98f4b9367 /src/main.c
parentb3f4342b584612f95938f1f00ca2d026a3f3c304 (diff)
downloaduxngba-65cf69fab1c90b5581f277761bd9342f1c7b4d84.tar.gz
uxngba-65cf69fab1c90b5581f277761bd9342f1c7b4d84.zip
Add mouse device support with SELECT toggle
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c177
1 files changed, 112 insertions, 65 deletions
diff --git a/src/main.c b/src/main.c
index c8a15af..8a694a8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1,9 +1,7 @@
1#include <string.h> 1#include <string.h>
2#include <time.h>
3 2
4#include "common.h" 3#include "common.h"
5#include "bitmap.h" 4#include "bitmap.h"
6// #include "small-font.c"
7#include "bd-font.c" 5#include "bd-font.c"
8 6
9#include "uxn/uxn.h" 7#include "uxn/uxn.h"
@@ -24,10 +22,25 @@ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
24WITH REGARD TO THIS SOFTWARE. 22WITH REGARD TO THIS SOFTWARE.
25*/ 23*/
26 24
25typedef enum {
26 CONTROL_CONTROLLER,
27 CONTROL_MOUSE,
28} ControlMethod;
29
30static ControlMethod control_method = CONTROL_CONTROLLER;
31
32#define MOUSE_DELTA 1
33typedef struct Mouse {
34 int x;
35 int y;
36} Mouse;
37
27static Ppu ppu; 38static Ppu ppu;
28static Device *devscreen; 39static Device *devscreen;
29static Device *devctrl; 40static Device *devctrl;
30time_t time_seconds; 41static Device *devmouse;
42
43static Mouse mouse = {SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2};
31 44
32void 45void
33nil_talk(Device *d, Uint8 b0, Uint8 w) { 46nil_talk(Device *d, Uint8 b0, Uint8 w) {
@@ -82,17 +95,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w) {
82 95
83void 96void
84datetime_talk(Device *d, Uint8 b0, Uint8 w) { 97datetime_talk(Device *d, Uint8 b0, Uint8 w) {
85 struct tm *t = localtime(&time_seconds); 98 (void)d;
86 t->tm_year += 1900;
87 mempoke16(d->dat, 0x0, t->tm_year);
88 d->dat[0x2] = t->tm_mon;
89 d->dat[0x3] = t->tm_mday;
90 d->dat[0x4] = t->tm_hour;
91 d->dat[0x5] = t->tm_min;
92 d->dat[0x6] = t->tm_sec;
93 d->dat[0x7] = t->tm_wday;
94 mempoke16(d->dat, 0x08, t->tm_yday);
95 d->dat[0xa] = t->tm_isdst;
96 (void)b0; 99 (void)b0;
97 (void)w; 100 (void)w;
98} 101}
@@ -102,9 +105,6 @@ init_uxn(Uxn *u) {
102 // Initialize PPU. 105 // Initialize PPU.
103 initppu(&ppu, 30, 20, 0); 106 initppu(&ppu, 30, 20, 0);
104 107
105 // Init clock.
106 time_seconds = time(NULL);
107
108 // Copy rom to VM. 108 // Copy rom to VM.
109 memcpy(u->ram.dat + PAGE_PROGRAM, uxn_rom, sizeof(uxn_rom)); 109 memcpy(u->ram.dat + PAGE_PROGRAM, uxn_rom, sizeof(uxn_rom));
110 110
@@ -118,7 +118,7 @@ init_uxn(Uxn *u) {
118 portuxn(u, 0x6, "---", nil_talk); 118 portuxn(u, 0x6, "---", nil_talk);
119 portuxn(u, 0x7, "---", nil_talk); 119 portuxn(u, 0x7, "---", nil_talk);
120 devctrl = portuxn(u, 0x8, "controller", nil_talk); 120 devctrl = portuxn(u, 0x8, "controller", nil_talk);
121 portuxn(u, 0x9, "---", nil_talk); 121 devmouse = portuxn(u, 0x9, "mouse", nil_talk);
122 portuxn(u, 0xa, "---", nil_talk); 122 portuxn(u, 0xa, "---", nil_talk);
123 portuxn(u, 0xb, "datetime", datetime_talk); 123 portuxn(u, 0xb, "datetime", datetime_talk);
124 portuxn(u, 0xc, "---", nil_talk); 124 portuxn(u, 0xc, "---", nil_talk);
@@ -132,53 +132,102 @@ init_uxn(Uxn *u) {
132void 132void
133handle_input(Uxn *u) { 133handle_input(Uxn *u) {
134 poll_keys(); 134 poll_keys();
135 135 if (key_pressed(KEY_SELECT)) {
136 // TODO: We don't need ifs if we use KEY_INPUTS directly and mayvbe just 136 switch (control_method) {
137 // swap some things if needed. 137 case CONTROL_CONTROLLER: {
138 Uint8 *flag = &devctrl->dat[2]; 138 devctrl->dat[2] = 0;
139 if (key_pressed(KEY_A) | key_hold(KEY_A)) { 139 evaluxn(u, mempeek16(devctrl->dat, 0));
140 *flag |= 0x01; 140 devctrl->dat[3] = 0;
141 } else { 141 control_method = CONTROL_MOUSE;
142 *flag &= ~0x01; 142 } break;
143 } 143 default: {
144 if (key_pressed(KEY_B) | key_hold(KEY_B)) { 144 devmouse->dat[6] = 0;
145 *flag |= 0x02; 145 devmouse->dat[7] = 0;
146 } else { 146 mempoke16(devmouse->dat, 0x2, -10);
147 *flag &= ~0x02; 147 mempoke16(devmouse->dat, 0x4, -10);
148 } 148 evaluxn(u, mempeek16(devmouse->dat, 0));
149 if (key_pressed(KEY_L) | key_hold(KEY_L)) { 149 control_method = CONTROL_CONTROLLER;
150 *flag |= 0x04; 150 } break;
151 } else { 151 }
152 *flag &= ~0x04;
153 }
154 if (key_pressed(KEY_R) | key_hold(KEY_R)) {
155 *flag |= 0x08;
156 } else {
157 *flag &= ~0x08;
158 }
159 if (key_pressed(KEY_UP) | key_hold(KEY_UP)) {
160 *flag |= 0x10;
161 } else {
162 *flag &= ~0x10;
163 }
164 if (key_pressed(KEY_DOWN) | key_hold(KEY_DOWN)) {
165 *flag |= 0x20;
166 } else {
167 *flag &= ~0x20;
168 }
169 if (key_pressed(KEY_LEFT) | key_hold(KEY_LEFT)) {
170 *flag |= 0x40;
171 } else {
172 *flag &= ~0x40;
173 }
174 if (key_pressed(KEY_RIGHT) | key_hold(KEY_RIGHT)) {
175 *flag |= 0x80;
176 } else {
177 *flag &= ~0x80;
178 } 152 }
179 153
180 evaluxn(u, mempeek16(devctrl->dat, 0)); 154 if (control_method == CONTROL_CONTROLLER) {
181 devctrl->dat[3] = 0; 155 // TODO: We don't need ifs if we use KEY_INPUTS directly and mayvbe just
156 // swap some things if needed.
157 Uint8 *flag = &devctrl->dat[2];
158 if (key_pressed(KEY_A) | key_hold(KEY_A)) {
159 *flag |= 0x01;
160 } else {
161 *flag &= ~0x01;
162 }
163 if (key_pressed(KEY_B) | key_hold(KEY_B)) {
164 *flag |= 0x02;
165 } else {
166 *flag &= ~0x02;
167 }
168 if (key_pressed(KEY_L) | key_hold(KEY_L)) {
169 *flag |= 0x04;
170 } else {
171 *flag &= ~0x04;
172 }
173 if (key_pressed(KEY_R) | key_hold(KEY_R)) {
174 *flag |= 0x08;
175 } else {
176 *flag &= ~0x08;
177 }
178 if (key_pressed(KEY_UP) | key_hold(KEY_UP)) {
179 *flag |= 0x10;
180 } else {
181 *flag &= ~0x10;
182 }
183 if (key_pressed(KEY_DOWN) | key_hold(KEY_DOWN)) {
184 *flag |= 0x20;
185 } else {
186 *flag &= ~0x20;
187 }
188 if (key_pressed(KEY_LEFT) | key_hold(KEY_LEFT)) {
189 *flag |= 0x40;
190 } else {
191 *flag &= ~0x40;
192 }
193 if (key_pressed(KEY_RIGHT) | key_hold(KEY_RIGHT)) {
194 *flag |= 0x80;
195 } else {
196 *flag &= ~0x80;
197 }
198
199 evaluxn(u, mempeek16(devctrl->dat, 0));
200 devctrl->dat[3] = 0;
201 } else if (control_method == CONTROL_MOUSE) {
202 devmouse->dat[7] = 0x00;
203 if (key_pressed(KEY_B) | key_hold(KEY_B)) {
204 devmouse->dat[6] |= 0x01;
205 devmouse->dat[7] = 0x01;
206 } else {
207 devmouse->dat[6] &= ~0x01;
208 }
209 if (key_pressed(KEY_A) | key_hold(KEY_A)) {
210 devmouse->dat[6] |= 0x10;
211 devmouse->dat[7] = 0x10;
212 } else {
213 devmouse->dat[6] &= ~0x10;
214 }
215 if (key_pressed(KEY_UP) | key_hold(KEY_UP)) {
216 mouse.y = CLAMP(mouse.y - MOUSE_DELTA, 0, SCREEN_HEIGHT - 8);
217 }
218 if (key_pressed(KEY_DOWN) | key_hold(KEY_DOWN)) {
219 mouse.y = CLAMP(mouse.y + MOUSE_DELTA, 0, SCREEN_HEIGHT - 8);
220 }
221 if (key_pressed(KEY_LEFT) | key_hold(KEY_LEFT)) {
222 mouse.x = CLAMP(mouse.x - MOUSE_DELTA, 0, SCREEN_WIDTH - 8);
223 }
224 if (key_pressed(KEY_RIGHT) | key_hold(KEY_RIGHT)) {
225 mouse.x = CLAMP(mouse.x + MOUSE_DELTA, 0, SCREEN_WIDTH - 8);
226 }
227 mempoke16(devmouse->dat, 0x2, mouse.x);
228 mempoke16(devmouse->dat, 0x4, mouse.y);
229 evaluxn(u, mempeek16(devmouse->dat, 0));
230 }
182} 231}
183 232
184static Uxn u; 233static Uxn u;
@@ -208,13 +257,11 @@ int main(void) {
208 // Main loop. 257 // Main loop.
209 int frame_counter = 0; 258 int frame_counter = 0;
210 evaluxn(&u, 0x0100); 259 evaluxn(&u, 0x0100);
211 // flipbuf(&ppu);
212 while(true) { 260 while(true) {
213 bios_vblank_wait(); 261 bios_vblank_wait();
214 handle_input(&u); 262 handle_input(&u);
215 evaluxn(&u, mempeek16(devscreen->dat, 0)); 263 evaluxn(&u, mempeek16(devscreen->dat, 0));
216 flipbuf(&ppu); 264 flipbuf(&ppu);
217 time_seconds++;
218 frame_counter++; 265 frame_counter++;
219 } 266 }
220 267