Browse code

Moved some extra things into PPU

neauoire authored on 08/04/2021 00:42:30
Showing 3 changed files
... ...
@@ -20,24 +20,6 @@ int initapu(Uxn *u, Uint8 id);
20 20
 
21 21
 static Ppu screen;
22 22
 
23
-Uint8 font[][8] = {
24
-	{0x00, 0x3c, 0x46, 0x4a, 0x52, 0x62, 0x3c, 0x00},
25
-	{0x00, 0x18, 0x28, 0x08, 0x08, 0x08, 0x3e, 0x00},
26
-	{0x00, 0x3c, 0x42, 0x02, 0x3c, 0x40, 0x7e, 0x00},
27
-	{0x00, 0x3c, 0x42, 0x1c, 0x02, 0x42, 0x3c, 0x00},
28
-	{0x00, 0x08, 0x18, 0x28, 0x48, 0x7e, 0x08, 0x00},
29
-	{0x00, 0x7e, 0x40, 0x7c, 0x02, 0x42, 0x3c, 0x00},
30
-	{0x00, 0x3c, 0x40, 0x7c, 0x42, 0x42, 0x3c, 0x00},
31
-	{0x00, 0x7e, 0x02, 0x04, 0x08, 0x10, 0x10, 0x00},
32
-	{0x00, 0x3c, 0x42, 0x3c, 0x42, 0x42, 0x3c, 0x00},
33
-	{0x00, 0x3c, 0x42, 0x42, 0x3e, 0x02, 0x3c, 0x00},
34
-	{0x00, 0x3c, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x00},
35
-	{0x00, 0x7c, 0x42, 0x7c, 0x42, 0x42, 0x7c, 0x00},
36
-	{0x00, 0x3c, 0x42, 0x40, 0x40, 0x42, 0x3c, 0x00},
37
-	{0x00, 0x78, 0x44, 0x42, 0x42, 0x44, 0x78, 0x00},
38
-	{0x00, 0x7e, 0x40, 0x7c, 0x40, 0x40, 0x7e, 0x00},
39
-	{0x00, 0x7e, 0x40, 0x40, 0x7c, 0x40, 0x40, 0x00}};
40
-
41 23
 static SDL_Window *gWindow;
42 24
 static SDL_Renderer *gRenderer;
43 25
 static SDL_Texture *gTexture;
... ...
@@ -67,8 +49,6 @@ getflag(Uint8 *a, char flag)
67 49
 	return *a & flag;
68 50
 }
69 51
 
70
-#pragma mark - Helpers
71
-
72 52
 #pragma mark - Core
73 53
 
74 54
 int
... ...
@@ -78,25 +58,6 @@ error(char *msg, const char *err)
78 58
 	return 0;
79 59
 }
80 60
 
81
-void
82
-drawdebugger(Uint32 *dst, Uxn *u)
83
-{
84
-	Uint8 i, x, y, b;
85
-	for(i = 0; i < 0x10; ++i) { /* memory */
86
-		x = ((i % 8) * 3 + 3) * 8, y = screen.x1 + 8 + i / 8 * 8, b = u->wst.dat[i];
87
-		drawicn(&screen, x, y, font[(b >> 4) & 0xf], 1 + (u->wst.ptr == i), 0);
88
-		drawicn(&screen, x + 8, y, font[b & 0xf], 1 + (u->wst.ptr == i), 0);
89
-	}
90
-	for(x = 0; x < 32; ++x) {
91
-		drawpixel(&screen, x, HEIGHT / 2, 2);
92
-		drawpixel(&screen, WIDTH - x, HEIGHT / 2, 2);
93
-		drawpixel(&screen, WIDTH / 2, HEIGHT - x, 2);
94
-		drawpixel(&screen, WIDTH / 2, x, 2);
95
-		drawpixel(&screen, WIDTH / 2 - 16 + x, HEIGHT / 2, 2);
96
-		drawpixel(&screen, WIDTH / 2, HEIGHT / 2 - 16 + x, 2);
97
-	}
98
-}
99
-
100 61
 void
101 62
 redraw(Uint32 *dst, Uxn *u)
102 63
 {
... ...
@@ -108,7 +69,7 @@ redraw(Uint32 *dst, Uxn *u)
108 69
 			drawchr(&screen, (x + PAD) * 8, (y + PAD) * 8, &screen.fg[key], 1);
109 70
 		}
110 71
 	if(screen.debugger)
111
-		drawdebugger(dst, u);
72
+		drawdebugger(&screen, u->wst.dat, u->wst.ptr);
112 73
 	SDL_UpdateTexture(gTexture, NULL, dst, WIDTH * sizeof(Uint32));
113 74
 	SDL_RenderClear(gRenderer);
114 75
 	SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
... ...
@@ -159,15 +120,10 @@ init(void)
159 120
 	gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, WIDTH, HEIGHT);
160 121
 	if(gTexture == NULL)
161 122
 		return error("Texture", SDL_GetError());
162
-	if(!(screen.output = (Uint32 *)malloc(WIDTH * HEIGHT * sizeof(Uint32))))
163
-		return error("Pixels", "Failed to allocate memory");
164
-	clear(&screen);
165 123
 	SDL_StartTextInput();
166 124
 	SDL_ShowCursor(SDL_DISABLE);
167
-	screen.x1 = PAD * 8;
168
-	screen.x2 = WIDTH - PAD * 8 - 1;
169
-	screen.y1 = PAD * 8;
170
-	screen.y2 = HEIGHT - PAD * 8 - 1;
125
+	if(!initppu(&screen))
126
+		return error("PPU", "Init failure");
171 127
 	return 1;
172 128
 }
173 129
 
... ...
@@ -12,6 +12,24 @@ WITH REGARD TO THIS SOFTWARE.
12 12
 
13 13
 #include "ppu.h"
14 14
 
15
+Uint8 font[][8] = {
16
+	{0x00, 0x3c, 0x46, 0x4a, 0x52, 0x62, 0x3c, 0x00},
17
+	{0x00, 0x18, 0x28, 0x08, 0x08, 0x08, 0x3e, 0x00},
18
+	{0x00, 0x3c, 0x42, 0x02, 0x3c, 0x40, 0x7e, 0x00},
19
+	{0x00, 0x3c, 0x42, 0x1c, 0x02, 0x42, 0x3c, 0x00},
20
+	{0x00, 0x08, 0x18, 0x28, 0x48, 0x7e, 0x08, 0x00},
21
+	{0x00, 0x7e, 0x40, 0x7c, 0x02, 0x42, 0x3c, 0x00},
22
+	{0x00, 0x3c, 0x40, 0x7c, 0x42, 0x42, 0x3c, 0x00},
23
+	{0x00, 0x7e, 0x02, 0x04, 0x08, 0x10, 0x10, 0x00},
24
+	{0x00, 0x3c, 0x42, 0x3c, 0x42, 0x42, 0x3c, 0x00},
25
+	{0x00, 0x3c, 0x42, 0x42, 0x3e, 0x02, 0x3c, 0x00},
26
+	{0x00, 0x3c, 0x42, 0x42, 0x7e, 0x42, 0x42, 0x00},
27
+	{0x00, 0x7c, 0x42, 0x7c, 0x42, 0x42, 0x7c, 0x00},
28
+	{0x00, 0x3c, 0x42, 0x40, 0x40, 0x42, 0x3c, 0x00},
29
+	{0x00, 0x78, 0x44, 0x42, 0x42, 0x44, 0x78, 0x00},
30
+	{0x00, 0x7e, 0x40, 0x7c, 0x40, 0x40, 0x7e, 0x00},
31
+	{0x00, 0x7e, 0x40, 0x40, 0x7c, 0x40, 0x40, 0x00}};
32
+
15 33
 void
16 34
 clear(Ppu *p)
17 35
 {
... ...
@@ -52,6 +70,25 @@ drawicn(Ppu *p, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 fg, Uint8 bg)
52 70
 		}
53 71
 }
54 72
 
73
+void
74
+drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr)
75
+{
76
+	Uint8 i, x, y, b;
77
+	for(i = 0; i < 0x10; ++i) { /* memory */
78
+		x = ((i % 8) * 3 + 3) * 8, y = p->x1 + 8 + i / 8 * 8, b = stack[i];
79
+		drawicn(p, x, y, font[(b >> 4) & 0xf], 1 + (ptr == i), 0);
80
+		drawicn(p, x + 8, y, font[b & 0xf], 1 + (ptr == i), 0);
81
+	}
82
+	for(x = 0; x < 32; ++x) {
83
+		drawpixel(p, x, HEIGHT / 2, 2);
84
+		drawpixel(p, WIDTH - x, HEIGHT / 2, 2);
85
+		drawpixel(p, WIDTH / 2, HEIGHT - x, 2);
86
+		drawpixel(p, WIDTH / 2, x, 2);
87
+		drawpixel(p, WIDTH / 2 - 16 + x, HEIGHT / 2, 2);
88
+		drawpixel(p, WIDTH / 2, HEIGHT / 2 - 16 + x, 2);
89
+	}
90
+}
91
+
55 92
 void
56 93
 paintpixel(Uint8 *dst, Uint16 x, Uint16 y, Uint8 color)
57 94
 {
... ...
@@ -80,4 +117,17 @@ loadtheme(Ppu *p, Uint8 *addr)
80 117
 		p->colors[i] = (r << 20) + (r << 16) + (g << 12) + (g << 8) + (b << 4) + b;
81 118
 	}
82 119
 	p->reqdraw = 1;
120
+}
121
+
122
+int
123
+initppu(Ppu *p)
124
+{
125
+	if(!(p->output = (Uint32 *)malloc(WIDTH * HEIGHT * sizeof(Uint32))))
126
+		return 0;
127
+	clear(p);
128
+	p->x1 = PAD * 8;
129
+	p->x2 = WIDTH - PAD * 8 - 1;
130
+	p->y1 = PAD * 8;
131
+	p->y2 = HEIGHT - PAD * 8 - 1;
132
+	return 1;
83 133
 }
84 134
\ No newline at end of file
... ...
@@ -1,4 +1,5 @@
1 1
 #include <stdio.h>
2
+#include <stdlib.h>
2 3
 
3 4
 /*
4 5
 Copyright (c) 2021 Devine Lu Linvega
... ...
@@ -31,14 +32,11 @@ typedef struct Ppu {
31 32
 	Uint32 *output, colors[4];
32 33
 } Ppu;
33 34
 
34
-void
35
-clear(Ppu *p);
36
-void
37
-drawpixel(Ppu *p, Uint16 x, Uint16 y, Uint8 color);
38
-void
39
-drawchr(Ppu *p, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 alpha);
40
-void
41
-drawicn(Ppu *p, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 fg, Uint8 bg);
42
-
35
+void clear(Ppu *p);
36
+void drawpixel(Ppu *p, Uint16 x, Uint16 y, Uint8 color);
37
+void drawchr(Ppu *p, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 alpha);
38
+void drawicn(Ppu *p, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 fg, Uint8 bg);
39
+void drawdebugger(Ppu *p, Uint8 *stack, Uint8 ptr);
43 40
 void paintpixel(Uint8 *dst, Uint16 x, Uint16 y, Uint8 color);
44
-void loadtheme(Ppu *p, Uint8 *addr);
45 41
\ No newline at end of file
42
+void loadtheme(Ppu *p, Uint8 *addr);
43
+int initppu(Ppu *p);
46 44
\ No newline at end of file