Browse code

Revert "Removed second texture of PPU"

Andrew Alderwick authored on 30/05/2021 22:15:37
Showing 3 changed files
... ...
@@ -41,11 +41,11 @@ readpixel(Uint8 *sprite, Uint8 h, Uint8 v)
41 41
 void
42 42
 clear(Ppu *p)
43 43
 {
44
-	int i;
45
-	for(i = p->height * p->width - 1; i >= 0; --i)
46
-		p->rgba[i] = p->colors[0];
47
-	for(i = p->height * p->width / 8 - 1; i >= 0; --i)
48
-		p->index[i] = 0;
44
+	int i, sz = p->height * p->width;
45
+	for(i = 0; i < sz; ++i) {
46
+		p->fg.pixels[i] = p->fg.colors[0];
47
+		p->bg.pixels[i] = p->bg.colors[0];
48
+	}
49 49
 }
50 50
 
51 51
 void
... ...
@@ -57,28 +57,23 @@ putcolors(Ppu *p, Uint8 *addr)
57 57
 			r = (*(addr + i / 2) >> (!(i % 2) << 2)) & 0x0f,
58 58
 			g = (*(addr + 2 + i / 2) >> (!(i % 2) << 2)) & 0x0f,
59 59
 			b = (*(addr + 4 + i / 2) >> (!(i % 2) << 2)) & 0x0f;
60
-		p->colors[i] = 0xff000000 | (r << 20) | (r << 16) | (g << 12) | (g << 8) | (b << 4) | b;
60
+		p->bg.colors[i] = 0xff000000 | (r << 20) | (r << 16) | (g << 12) | (g << 8) | (b << 4) | b;
61
+		p->fg.colors[i] = 0xff000000 | (r << 20) | (r << 16) | (g << 12) | (g << 8) | (b << 4) | b;
61 62
 	}
62
-	for(i = 4; i < 16; ++i) p->colors[i] = p->colors[i / 4];
63
+	p->fg.colors[0] = 0;
63 64
 	clear(p);
64 65
 }
65 66
 
66 67
 void
67
-putpixel(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color)
68
+putpixel(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color)
68 69
 {
69
-	int rgba_i, index_i, shift_pixel, shift_layer;
70 70
 	if(x >= p->width || y >= p->height)
71 71
 		return;
72
-	rgba_i = y * p->width + x;
73
-	index_i = rgba_i >> 3;
74
-	shift_pixel = (rgba_i & 0x7) * 4;
75
-	shift_layer = shift_pixel + layer * 2;
76
-	p->index[index_i] = (p->index[index_i] & ~(0x3 << shift_layer)) | (color << shift_layer);
77
-	p->rgba[rgba_i] = p->colors[(p->index[index_i] >> shift_pixel) & 0xf];
72
+	layer->pixels[y * p->width + x] = layer->colors[color];
78 73
 }
79 74
 
80 75
 void
81
-puticn(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
76
+puticn(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
82 77
 {
83 78
 	Uint16 v, h;
84 79
 	for(v = 0; v < 8; v++)
... ...
@@ -94,7 +89,7 @@ puticn(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint
94 89
 }
95 90
 
96 91
 void
97
-putchr(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
92
+putchr(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
98 93
 {
99 94
 	Uint16 v, h;
100 95
 	for(v = 0; v < 8; v++)
... ...
@@ -117,16 +112,16 @@ inspect(Ppu *p, Uint8 *stack, Uint8 ptr)
117 112
 	Uint8 i, x, y, b;
118 113
 	for(i = 0; i < 0x20; ++i) { /* memory */
119 114
 		x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i];
120
-		puticn(p, 0, x, y, font[(b >> 4) & 0xf], 1 + (ptr == i) * 0x7, 0, 0);
121
-		puticn(p, 0, x + 8, y, font[b & 0xf], 1 + (ptr == i) * 0x7, 0, 0);
115
+		puticn(p, &p->bg, x, y, font[(b >> 4) & 0xf], 1 + (ptr == i) * 0x7, 0, 0);
116
+		puticn(p, &p->bg, x + 8, y, font[b & 0xf], 1 + (ptr == i) * 0x7, 0, 0);
122 117
 	}
123 118
 	for(x = 0; x < 0x20; ++x) {
124
-		putpixel(p, 0, x, p->height / 2, 2);
125
-		putpixel(p, 0, p->width - x, p->height / 2, 2);
126
-		putpixel(p, 0, p->width / 2, p->height - x, 2);
127
-		putpixel(p, 0, p->width / 2, x, 2);
128
-		putpixel(p, 0, p->width / 2 - 16 + x, p->height / 2, 2);
129
-		putpixel(p, 0, p->width / 2, p->height / 2 - 16 + x, 2);
119
+		putpixel(p, &p->bg, x, p->height / 2, 2);
120
+		putpixel(p, &p->bg, p->width - x, p->height / 2, 2);
121
+		putpixel(p, &p->bg, p->width / 2, p->height - x, 2);
122
+		putpixel(p, &p->bg, p->width / 2, x, 2);
123
+		putpixel(p, &p->bg, p->width / 2 - 16 + x, p->height / 2, 2);
124
+		putpixel(p, &p->bg, p->width / 2, p->height / 2 - 16 + x, 2);
130 125
 	}
131 126
 }
132 127
 
... ...
@@ -137,9 +132,9 @@ initppu(Ppu *p, Uint8 hor, Uint8 ver)
137 132
 	p->ver = ver;
138 133
 	p->width = 8 * p->hor;
139 134
 	p->height = 8 * p->ver;
140
-	if(!(p->index = malloc(p->width * p->height / 8 * sizeof(Uint32))))
135
+	if(!(p->bg.pixels = malloc(p->width * p->height * sizeof(Uint32))))
141 136
 		return 0;
142
-	if(!(p->rgba = malloc(p->width * p->height * sizeof(Uint32))))
137
+	if(!(p->fg.pixels = malloc(p->width * p->height * sizeof(Uint32))))
143 138
 		return 0;
144 139
 	clear(p);
145 140
 	return 1;
... ...
@@ -18,17 +18,17 @@ typedef unsigned short Uint16;
18 18
 typedef unsigned int Uint32;
19 19
 
20 20
 typedef struct Layer {
21
-	Uint32 *pixels;
21
+	Uint32 *pixels, colors[4];
22 22
 } Layer;
23 23
 
24 24
 typedef struct Ppu {
25
-	Uint32 *rgba, *index, colors[16];
26 25
 	Uint16 hor, ver, width, height;
26
+	Layer fg, bg;
27 27
 } Ppu;
28 28
 
29 29
 int initppu(Ppu *p, Uint8 hor, Uint8 ver);
30 30
 void putcolors(Ppu *p, Uint8 *addr);
31
-void putpixel(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color);
32
-void puticn(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
33
-void putchr(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
31
+void putpixel(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color);
32
+void puticn(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
33
+void putchr(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
34 34
 void inspect(Ppu *p, Uint8 *stack, Uint8 ptr);
... ...
@@ -20,7 +20,7 @@ WITH REGARD TO THIS SOFTWARE.
20 20
 static SDL_AudioDeviceID audio_id;
21 21
 static SDL_Window *gWindow;
22 22
 static SDL_Renderer *gRenderer;
23
-static SDL_Texture *gTexture;
23
+static SDL_Texture *fgTexture, *bgTexture;
24 24
 static SDL_Rect gRect;
25 25
 static Ppu ppu;
26 26
 static Apu apu[POLYPHONY];
... ...
@@ -60,9 +60,11 @@ redraw(Uxn *u)
60 60
 {
61 61
 	if(debug)
62 62
 		inspect(&ppu, u->wst.dat, u->wst.ptr);
63
-	SDL_UpdateTexture(gTexture, &gRect, ppu.rgba, ppu.width * sizeof(Uint32));
63
+	SDL_UpdateTexture(bgTexture, &gRect, ppu.bg.pixels, ppu.width * sizeof(Uint32));
64
+	SDL_UpdateTexture(fgTexture, &gRect, ppu.fg.pixels, ppu.width * sizeof(Uint32));
64 65
 	SDL_RenderClear(gRenderer);
65
-	SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
66
+	SDL_RenderCopy(gRenderer, bgTexture, NULL, NULL);
67
+	SDL_RenderCopy(gRenderer, fgTexture, NULL, NULL);
66 68
 	SDL_RenderPresent(gRenderer);
67 69
 	reqdraw = 0;
68 70
 }
... ...
@@ -85,11 +87,13 @@ togglezoom(Uxn *u)
85 87
 void
86 88
 quit(void)
87 89
 {
88
-	free(ppu.rgba);
89
-	free(ppu.index);
90
+	free(ppu.fg.pixels);
91
+	free(ppu.bg.pixels);
90 92
 	SDL_UnlockAudioDevice(audio_id);
91
-	SDL_DestroyTexture(gTexture);
92
-	gTexture = NULL;
93
+	SDL_DestroyTexture(bgTexture);
94
+	bgTexture = NULL;
95
+	SDL_DestroyTexture(fgTexture);
96
+	fgTexture = NULL;
93 97
 	SDL_DestroyRenderer(gRenderer);
94 98
 	gRenderer = NULL;
95 99
 	SDL_DestroyWindow(gWindow);
... ...
@@ -119,10 +123,14 @@ init(void)
119 123
 	if(gRenderer == NULL)
120 124
 		return error("Renderer", SDL_GetError());
121 125
 	SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2);
122
-	gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
123
-	if(gTexture == NULL)
126
+	bgTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
127
+	if(bgTexture == NULL || SDL_SetTextureBlendMode(bgTexture, SDL_BLENDMODE_NONE))
124 128
 		return error("Texture", SDL_GetError());
125
-	SDL_UpdateTexture(gTexture, NULL, ppu.rgba, 4);
129
+	fgTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
130
+	if(fgTexture == NULL || SDL_SetTextureBlendMode(fgTexture, SDL_BLENDMODE_BLEND))
131
+		return error("Texture", SDL_GetError());
132
+	SDL_UpdateTexture(bgTexture, NULL, ppu.bg.pixels, 4);
133
+	SDL_UpdateTexture(fgTexture, NULL, ppu.fg.pixels, 4);
126 134
 	SDL_StartTextInput();
127 135
 	SDL_ShowCursor(SDL_DISABLE);
128 136
 	SDL_zero(as);
... ...
@@ -224,7 +232,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
224 232
 		Uint16 x = mempeek16(d->dat, 0x8);
225 233
 		Uint16 y = mempeek16(d->dat, 0xa);
226 234
 		Uint8 *addr = &d->mem[mempeek16(d->dat, 0xc)];
227
-		Uint8 layer = (d->dat[0xe] >> 4) & 0x1;
235
+		Layer *layer = d->dat[0xe] >> 4 & 0x1 ? &ppu.fg : &ppu.bg;
228 236
 		Uint8 mode = d->dat[0xe] >> 5;
229 237
 		if(!mode)
230 238
 			putpixel(&ppu, layer, x, y, d->dat[0xe] & 0x3);