Browse code

ppu: keep track of modified rows and only redraw those in drawppu

Sigrid Solveig Haflínudóttir authored on 19/05/2021 12:25:18
Showing 3 changed files
... ...
@@ -41,13 +41,11 @@ readpixel(Uint8 *sprite, Uint8 h, Uint8 v)
41 41
 void
42 42
 clear(Ppu *p)
43 43
 {
44
-	int i, sz = p->height * p->width, rows = sz / 4;
45
-	for(i = 0; i < sz; ++i)
46
-		p->output[i] = p->colors[0];
47
-	for(i = 0; i < rows; i++) {
48
-		p->fg[i] = 0;
49
-		p->bg[i] = 0;
50
-	}
44
+	int sz = p->height * p->width, rows = sz / 4;
45
+	memset(p->output, p->colors[0], sz);
46
+	memset(p->fg, 0, rows);
47
+	memset(p->bg, 0, rows);
48
+	memset(p->up, 0xff, p->ver);
51 49
 }
52 50
 
53 51
 void
... ...
@@ -66,9 +64,12 @@ putcolors(Ppu *p, Uint8 *addr)
66 64
 void
67 65
 putpixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color)
68 66
 {
69
-	Uint16 row = (y % 8) + ((x / 8 + y / 8 * p->hor) * 16), col = 7 - (x % 8);
70
-	if(x >= p->hor * 8 || y >= p->ver * 8 || row > (p->hor * p->ver * 16) - 8)
67
+	if(x >= p->hor * 8 || y >= p->ver * 8)
68
+		return;
69
+	int row = (y % 8) + ((x / 8 + y / 8 * p->hor) * 16);
70
+	if(row > (p->hor * p->ver * 16) - 8)
71 71
 		return;
72
+	int col = 7 - (x % 8);
72 73
 	if(color == 0 || color == 2)
73 74
 		layer[row] &= ~(1UL << col);
74 75
 	else
... ...
@@ -77,6 +78,8 @@ putpixel(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color)
77 78
 		layer[row + 8] &= ~(1UL << col);
78 79
 	else
79 80
 		layer[row + 8] |= 1UL << col;
81
+
82
+	p->up[y / 8] |= 1<<(y % 8);
80 83
 }
81 84
 
82 85
 void
... ...
@@ -143,18 +146,25 @@ void
143 146
 drawppu(Ppu *p)
144 147
 {
145 148
 	Uint16 x, y;
146
-	for(y = 0; y < p->ver; ++y)
149
+	for(y = 0; y < p->ver; ++y) {
150
+		if(p->up[y] == 0)
151
+			continue;
147 152
 		for(x = 0; x < p->hor; ++x) {
148 153
 			Uint8 v, h;
149 154
 			Uint16 key = (y * p->hor + x) * 16;
150
-			for(v = 0; v < 8; v++)
155
+			for(v = 0; v < 8; v++) {
156
+				if((p->up[y] & (1<<v)) == 0)
157
+					continue;
151 158
 				for(h = 0; h < 8; h++) {
152 159
 					Uint8 color = readpixel(&p->fg[key], h, v);
153 160
 					if(color == 0)
154 161
 						color = readpixel(&p->bg[key], h, v);
155 162
 					drawpixel(p, x * 8 + p->pad + 7 - h, y * 8 + p->pad + v, color);
156 163
 				}
164
+			}
157 165
 		}
166
+		p->up[y] = 0;
167
+	}
158 168
 }
159 169
 
160 170
 int
... ...
@@ -171,6 +181,8 @@ initppu(Ppu *p, Uint8 hor, Uint8 ver, Uint8 pad)
171 181
 		return 0;
172 182
 	if(!(p->fg = malloc(p->width * p->height * sizeof(Uint8) / 4)))
173 183
 		return 0;
184
+	if(!(p->up = malloc(p->ver)))
185
+		return 0;
174 186
 	clear(p);
175 187
 	return 1;
176 188
 }
... ...
@@ -18,7 +18,7 @@ typedef unsigned short Uint16;
18 18
 typedef unsigned int Uint32;
19 19
 
20 20
 typedef struct Ppu {
21
-	Uint8 *bg, *fg;
21
+	Uint8 *bg, *fg, *up;
22 22
 	Uint16 hor, ver, pad, width, height;
23 23
 	Uint32 *output, colors[4];
24 24
 } Ppu;
... ...
@@ -86,6 +86,7 @@ quit(void)
86 86
 	free(ppu.output);
87 87
 	free(ppu.fg);
88 88
 	free(ppu.bg);
89
+	free(ppu.up);
89 90
 	SDL_UnlockAudioDevice(audio_id);
90 91
 	SDL_DestroyTexture(gTexture);
91 92
 	gTexture = NULL;