Browse code

Minor cleanup

neauoire authored on 22/09/2021 17:57:43
Showing 1 changed files
... ...
@@ -105,20 +105,75 @@ stdin_handler(void *p)
105 105
 	(void)p;
106 106
 }
107 107
 
108
+static void
109
+set_inspect(Uint8 flag)
110
+{
111
+	devsystem->dat[0xe] = flag;
112
+	reqdraw = 1;
113
+}
114
+
108 115
 static void
109 116
 set_window_size(SDL_Window *window, int w, int h)
110 117
 {
111
-	SDL_Point win, win_old, win_ratio;
118
+	SDL_Point win, win_old;
112 119
 	SDL_GetWindowPosition(window, &win.x, &win.y);
113 120
 	SDL_GetWindowSize(window, &win_old.x, &win_old.y);
114
-	win_ratio.x = win.x + win_old.x / 2;
115
-	win_ratio.y = win.y + win_old.y / 2;
116
-	SDL_SetWindowPosition(window, win_ratio.x - w / 2, win_ratio.y - h / 2);
121
+	SDL_SetWindowPosition(window, (win.x + win_old.x / 2) - w / 2, (win.y + win_old.y / 2) - h / 2);
117 122
 	SDL_SetWindowSize(window, w, h);
118 123
 }
119 124
 
120 125
 static void
121
-inspect(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
126
+set_zoom(Uint8 scale)
127
+{
128
+	if(scale == zoom)
129
+		return;
130
+	zoom = clamp(scale, 1, 3);
131
+	set_window_size(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
132
+	reqdraw = 1;
133
+}
134
+
135
+static int
136
+set_size(Uint16 width, Uint16 height, int is_resize)
137
+{
138
+	ppu_set_size(&ppu, width, height);
139
+	gRect.x = PAD;
140
+	gRect.y = PAD;
141
+	gRect.w = ppu.width;
142
+	gRect.h = ppu.height;
143
+	if(!(ppu_screen = realloc(ppu_screen, ppu.width * ppu.height * sizeof(Uint32))))
144
+		return error("ppu_screen", "Memory failure");
145
+	memset(ppu_screen, 0, ppu.width * ppu.height * sizeof(Uint32));
146
+	if(gTexture != NULL) SDL_DestroyTexture(gTexture);
147
+	SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2);
148
+	gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
149
+	if(gTexture == NULL || SDL_SetTextureBlendMode(gTexture, SDL_BLENDMODE_NONE))
150
+		return error("sdl_texture", SDL_GetError());
151
+	SDL_UpdateTexture(gTexture, NULL, ppu_screen, sizeof(Uint32));
152
+	if(is_resize)
153
+		set_window_size(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
154
+	reqdraw = 1;
155
+	return 1;
156
+}
157
+
158
+static void
159
+capture_screen(void)
160
+{
161
+	const Uint32 format = SDL_PIXELFORMAT_RGB24;
162
+	time_t t = time(NULL);
163
+	char fname[64];
164
+	int w, h;
165
+	SDL_Surface *surface;
166
+	SDL_GetRendererOutputSize(gRenderer, &w, &h);
167
+	surface = SDL_CreateRGBSurfaceWithFormat(0, w, h, 24, format);
168
+	SDL_RenderReadPixels(gRenderer, NULL, format, surface->pixels, surface->pitch);
169
+	strftime(fname, sizeof(fname), "screenshot-%Y%m%d-%H%M%S.bmp", localtime(&t));
170
+	SDL_SaveBMP(surface, fname);
171
+	SDL_FreeSurface(surface);
172
+	fprintf(stderr, "Saved %s\n", fname);
173
+}
174
+
175
+static void
176
+draw_inspect(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
122 177
 {
123 178
 	Uint8 i, x, y, b;
124 179
 	for(i = 0; i < 0x20; ++i) { /* stack */
... ...
@@ -157,7 +212,7 @@ redraw(Uxn *u)
157 212
 	Uint16 x, y, y0 = 0, y1 = ppu.height;
158 213
 	SDL_Rect up = gRect;
159 214
 	if(devsystem->dat[0xe])
160
-		inspect(&ppu, u->wst.dat, u->wst.ptr, u->rst.ptr, u->ram.dat);
215
+		draw_inspect(&ppu, u->wst.dat, u->wst.ptr, u->rst.ptr, u->ram.dat);
161 216
 	if(!reqdraw && ppu.redraw) {
162 217
 		y0 = ppu.i0 / ppu.stride;
163 218
 		y1 = ppu.i1 / ppu.stride + 1;
... ...
@@ -175,62 +230,6 @@ redraw(Uxn *u)
175 230
 	ppu_frame(&ppu);
176 231
 }
177 232
 
178
-static void
179
-toggle_debug(Uxn *u)
180
-{
181
-	devsystem->dat[0xe] = !devsystem->dat[0xe];
182
-	redraw(u);
183
-}
184
-
185
-static void
186
-set_zoom(Uxn *u, Uint8 scale)
187
-{
188
-	if(scale == zoom)
189
-		return;
190
-	zoom = clamp(scale, 1, 3);
191
-	set_window_size(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
192
-	redraw(u);
193
-}
194
-
195
-static void
196
-capture_screen(void)
197
-{
198
-	const Uint32 format = SDL_PIXELFORMAT_RGB24;
199
-	time_t t = time(NULL);
200
-	char fname[64];
201
-	int w, h;
202
-	SDL_Surface *surface;
203
-	SDL_GetRendererOutputSize(gRenderer, &w, &h);
204
-	surface = SDL_CreateRGBSurfaceWithFormat(0, w, h, 24, format);
205
-	SDL_RenderReadPixels(gRenderer, NULL, format, surface->pixels, surface->pitch);
206
-	strftime(fname, sizeof(fname), "screenshot-%Y%m%d-%H%M%S.bmp", localtime(&t));
207
-	SDL_SaveBMP(surface, fname);
208
-	SDL_FreeSurface(surface);
209
-	fprintf(stderr, "Saved %s\n", fname);
210
-}
211
-
212
-static int
213
-set_size(Uint16 width, Uint16 height, int is_resize)
214
-{
215
-	ppu_set_size(&ppu, width, height);
216
-	gRect.x = PAD;
217
-	gRect.y = PAD;
218
-	gRect.w = ppu.width;
219
-	gRect.h = ppu.height;
220
-	if(!(ppu_screen = realloc(ppu_screen, ppu.width * ppu.height * sizeof(Uint32))))
221
-		return error("ppu_screen", "Memory failure");
222
-	memset(ppu_screen, 0, ppu.width * ppu.height * sizeof(Uint32));
223
-	if(gTexture != NULL) SDL_DestroyTexture(gTexture);
224
-	SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2);
225
-	gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
226
-	if(gTexture == NULL || SDL_SetTextureBlendMode(gTexture, SDL_BLENDMODE_NONE))
227
-		return error("sdl_texture", SDL_GetError());
228
-	SDL_UpdateTexture(gTexture, NULL, ppu_screen, sizeof(Uint32));
229
-	if(is_resize) set_window_size(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
230
-	reqdraw = 1;
231
-	return 1;
232
-}
233
-
234 233
 static void
235 234
 quit(void)
236 235
 {
... ...
@@ -301,7 +300,7 @@ domouse(SDL_Event *event)
301 300
 }
302 301
 
303 302
 static void
304
-doctrl(Uxn *u, SDL_Event *event, int z)
303
+doctrl(SDL_Event *event, int z)
305 304
 {
306 305
 	Uint8 flag = 0x00;
307 306
 	SDL_Keymod mods = SDL_GetModState();
... ...
@@ -316,8 +315,8 @@ doctrl(Uxn *u, SDL_Event *event, int z)
316 315
 	case SDLK_DOWN: flag = 0x20; break;
317 316
 	case SDLK_LEFT: flag = 0x40; break;
318 317
 	case SDLK_RIGHT: flag = 0x80; break;
319
-	case SDLK_F1: if(z) set_zoom(u, zoom == 3 ? 1 : zoom + 1); break;
320
-	case SDLK_F2: if(z) toggle_debug(u); break;
318
+	case SDLK_F1: if(z) set_zoom(zoom == 3 ? 1 : zoom + 1); break;
319
+	case SDLK_F2: if(z) set_inspect(!devsystem->dat[0xe]); break;
321 320
 	case SDLK_F3: if(z) capture_screen(); break;
322 321
 	}
323 322
 	/* clang-format on */
... ...
@@ -520,7 +519,7 @@ run(Uxn *u)
520 519
 				devctrl->dat[3] = event.text.text[0]; /* fall-thru */
521 520
 			case SDL_KEYDOWN:
522 521
 			case SDL_KEYUP:
523
-				doctrl(u, &event, event.type == SDL_KEYDOWN);
522
+				doctrl(&event, event.type == SDL_KEYDOWN);
524 523
 				uxn_eval(u, peek16(devctrl->dat, 0));
525 524
 				devctrl->dat[3] = 0;
526 525
 				break;
... ...
@@ -605,12 +604,12 @@ main(int argc, char **argv)
605 604
 		return error("Window", "Failed to set window size.");
606 605
 	/* default zoom */
607 606
 	SDL_GetCurrentDisplayMode(0, &DM);
608
-	set_zoom(&u, DM.w / 1000);
607
+	set_zoom(DM.w / 1000);
609 608
 	/* zoom from flags */
610 609
 	for(i = 1; i < argc - 1; i++) {
611 610
 		if(strcmp(argv[i], "-s") == 0) {
612 611
 			if((i + 1) < argc - 1)
613
-				set_zoom(&u, atoi(argv[++i]));
612
+				set_zoom(atoi(argv[++i]));
614 613
 			else
615 614
 				return error("Opt", "-s No scale provided.");
616 615
 		}