Browse code

Removed second texture of PPU

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