commit f4e3dc9

Devine Lu Linvega  ·  2024-01-15 18:17:37 +0000 UTC
parent 83762de
Merge display functions
2 files changed,  +39, -33
+1, -1
1@@ -35,7 +35,7 @@ system_load(Uxn *u, char *filename)
2 {
3 	FILE *f = fopen(filename, "rb");
4 	if(f) {
5-		int i, l = fread(&u->ram[PAGE_PROGRAM], 0x10000 - PAGE_PROGRAM, 1, f);
6+		int i = 0, l = fread(&u->ram[PAGE_PROGRAM], 0x10000 - PAGE_PROGRAM, 1, f);
7 		while(l && ++i < RAM_PAGES)
8 			l = fread(u->ram + 0x10000 * i, 0x10000, 1, f);
9 		fclose(f);
+38, -32
  1@@ -43,18 +43,6 @@ clamp(int val, int min, int max)
  2 	return (val >= min) ? (val <= max) ? val : max : min;
  3 }
  4 
  5-static void
  6-hide_cursor(void)
  7-{
  8-	XColor black = {0};
  9-	static char empty[] = {0};
 10-	Pixmap bitmap = XCreateBitmapFromData(display, window, empty, 1, 1);
 11-	Cursor blank = XCreatePixmapCursor(display, bitmap, bitmap, &black, &black, 0, 0);
 12-	XDefineCursor(display, window, blank);
 13-	XFreeCursor(display, blank);
 14-	XFreePixmap(display, bitmap);
 15-}
 16-
 17 Uint8
 18 emu_dei(Uxn *u, Uint8 addr)
 19 {
 20@@ -86,9 +74,9 @@ emu_deo(Uxn *u, Uint8 addr, Uint8 value)
 21 }
 22 
 23 int
 24-emu_resize(int width, int height)
 25+emu_resize(int w, int h)
 26 {
 27-	int w = uxn_screen.width, h = uxn_screen.height, s = uxn_screen.scale;
 28+	int s = uxn_screen.scale;
 29 	static Visual *visual;
 30 	if(window) {
 31 		visual = DefaultVisual(display, 0);
 32@@ -137,7 +125,7 @@ get_button(KeySym sym)
 33 }
 34 
 35 static void
 36-toggle_scale(Uxn *u)
 37+toggle_scale(void)
 38 {
 39 	int s = uxn_screen.scale + 1;
 40 	if(s > 3) s = 1;
 41@@ -162,7 +150,7 @@ emu_event(Uxn *u)
 42 		char buf[7];
 43 		XLookupString((XKeyPressedEvent *)&ev, buf, 7, &sym, 0);
 44 		if(sym == XK_F1)
 45-			toggle_scale(u);
 46+			toggle_scale();
 47 		else if(sym == XK_F2)
 48 			u->dev[0x0e] = !u->dev[0x0e];
 49 		else if(sym == XK_F4)
 50@@ -203,18 +191,19 @@ emu_event(Uxn *u)
 51 }
 52 
 53 static int
 54-emu_run(Uxn *u, char *rom)
 55+display_init(void)
 56 {
 57-	int i = 1, n, s = uxn_screen.scale;
 58-	char expirations[8];
 59-	char coninp[CONINBUFSIZE];
 60-	struct pollfd fds[3];
 61-	static const struct itimerspec screen_tspec = {{0, 16666666}, {0, 16666666}};
 62-	loaded_rom = rom;
 63-
 64-	/* display */
 65+	int s = uxn_screen.scale;
 66 	Atom wmDelete;
 67 	static Visual *visual;
 68+	XColor black = {0};
 69+	static char empty[] = {0};
 70+	Pixmap bitmap;
 71+	Cursor blank;
 72+	display = XOpenDisplay(NULL);
 73+	if(!display)
 74+		return system_error("init", "Display failed");
 75+	/* display */
 76 	visual = DefaultVisual(display, 0);
 77 	window = XCreateSimpleWindow(display, RootWindow(display, 0), 0, 0, uxn_screen.width * s + PAD * 2, uxn_screen.height * s + PAD * 2, 1, 0, 0);
 78 	if(visual->class != TrueColor)
 79@@ -222,10 +211,27 @@ emu_run(Uxn *u, char *rom)
 80 	XSelectInput(display, window, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | ExposureMask | KeyPressMask | KeyReleaseMask);
 81 	wmDelete = XInternAtom(display, "WM_DELETE_WINDOW", True);
 82 	XSetWMProtocols(display, window, &wmDelete, 1);
 83-	XStoreName(display, window, rom);
 84+	XStoreName(display, window, boot_rom);
 85 	XMapWindow(display, window);
 86 	ximage = XCreateImage(display, visual, DefaultDepth(display, DefaultScreen(display)), ZPixmap, 0, (char *)uxn_screen.pixels, uxn_screen.width * s, uxn_screen.height * s, 32, 0);
 87-	hide_cursor();
 88+	/* hide cursor */
 89+	bitmap = XCreateBitmapFromData(display, window, empty, 1, 1);
 90+	blank = XCreatePixmapCursor(display, bitmap, bitmap, &black, &black, 0, 0);
 91+	XDefineCursor(display, window, blank);
 92+	XFreeCursor(display, blank);
 93+	XFreePixmap(display, bitmap);
 94+	return 1;
 95+}
 96+
 97+static int
 98+emu_run(Uxn *u, char *rom)
 99+{
100+	int i = 1, n;
101+	char expirations[8];
102+	char coninp[CONINBUFSIZE];
103+	struct pollfd fds[3];
104+	static const struct itimerspec screen_tspec = {{0, 16666666}, {0, 16666666}};
105+	loaded_rom = rom;
106 
107 	/* timer */
108 	fds[0].fd = XConnectionNumber(display);
109@@ -243,7 +249,7 @@ emu_run(Uxn *u, char *rom)
110 			read(fds[1].fd, expirations, 8);   /* Indicate we handled the timer */
111 			uxn_eval(u, PEEK2(u->dev + 0x20)); /* Call the vector once, even if the timer fired multiple times */
112 			if(uxn_screen.x2) {
113-				s = uxn_screen.scale;
114+				int s = uxn_screen.scale;
115 				int x1 = uxn_screen.x1 * s, y1 = uxn_screen.y1 * s, x2 = uxn_screen.x2 * s, y2 = uxn_screen.y2 * s;
116 				screen_redraw(u);
117 				XPutImage(display, window, DefaultGC(display, 0), ximage, x1, y1, x1 + PAD, y1 + PAD, x2 - x1, y2 - y1);
118@@ -272,14 +278,14 @@ main(int argc, char **argv)
119 		fprintf(stdout, "Uxn11 - Varvara Emulator, 15 Jan 2023.\n");
120 		i++;
121 	}
122-	if(!(display = XOpenDisplay(NULL))) {
123-		fprintf(stdout, "Could not open display.\n");
124-		return 0;
125-	}
126 	if(!system_boot(&u, (Uint8 *)calloc(0x10000 * RAM_PAGES, sizeof(Uint8)), argv[i++])) {
127 		fprintf(stdout, "Could not boot.\n");
128 		return 0;
129 	}
130+	if(!display_init()) {
131+		fprintf(stdout, "Could not open display.\n");
132+		return 0;
133+	}
134 	/* Game Loop */
135 	screen_resize(WIDTH, HEIGHT, 1);
136 	u.dev[0x17] = argc - i;