Removed second texture of PPU

This commit is contained in:
Andrew Alderwick 2021-05-30 23:00:05 +01:00
parent 70edfce206
commit ffe142ea91
3 changed files with 43 additions and 46 deletions

View File

@ -41,11 +41,11 @@ readpixel(Uint8 *sprite, Uint8 h, Uint8 v)
void void
clear(Ppu *p) clear(Ppu *p)
{ {
int i, sz = p->height * p->width; int i;
for(i = 0; i < sz; ++i) { for(i = p->height * p->width - 1; i >= 0; --i)
p->fg.pixels[i] = p->fg.colors[0]; p->rgba[i] = p->colors[0];
p->bg.pixels[i] = p->bg.colors[0]; for(i = p->height * p->width / 8 - 1; i >= 0; --i)
} p->index[i] = 0;
} }
void void
@ -57,23 +57,28 @@ putcolors(Ppu *p, Uint8 *addr)
r = (*(addr + i / 2) >> (!(i % 2) << 2)) & 0x0f, r = (*(addr + i / 2) >> (!(i % 2) << 2)) & 0x0f,
g = (*(addr + 2 + i / 2) >> (!(i % 2) << 2)) & 0x0f, g = (*(addr + 2 + i / 2) >> (!(i % 2) << 2)) & 0x0f,
b = (*(addr + 4 + i / 2) >> (!(i % 2) << 2)) & 0x0f; b = (*(addr + 4 + i / 2) >> (!(i % 2) << 2)) & 0x0f;
p->bg.colors[i] = 0xff000000 | (r << 20) | (r << 16) | (g << 12) | (g << 8) | (b << 4) | b; p->colors[i] = 0xff000000 | (r << 20) | (r << 16) | (g << 12) | (g << 8) | (b << 4) | b;
p->fg.colors[i] = 0xff000000 | (r << 20) | (r << 16) | (g << 12) | (g << 8) | (b << 4) | b;
} }
p->fg.colors[0] = 0; for(i = 4; i < 16; ++i) p->colors[i] = p->colors[i / 4];
clear(p); clear(p);
} }
void void
putpixel(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color) putpixel(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color)
{ {
int rgba_i, index_i, shift_pixel, shift_layer;
if(x >= p->width || y >= p->height) if(x >= p->width || y >= p->height)
return; return;
layer->pixels[y * p->width + x] = layer->colors[color]; rgba_i = y * p->width + x;
index_i = rgba_i >> 3;
shift_pixel = (rgba_i & 0x7) * 4;
shift_layer = shift_pixel + layer * 2;
p->index[index_i] = (p->index[index_i] & ~(0x3 << shift_layer)) | (color << shift_layer);
p->rgba[rgba_i] = p->colors[(p->index[index_i] >> shift_pixel) & 0xf];
} }
void void
puticn(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) puticn(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
{ {
Uint16 v, h; Uint16 v, h;
for(v = 0; v < 8; v++) for(v = 0; v < 8; v++)
@ -89,7 +94,7 @@ puticn(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uin
} }
void void
putchr(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) putchr(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
{ {
Uint16 v, h; Uint16 v, h;
for(v = 0; v < 8; v++) for(v = 0; v < 8; v++)
@ -112,16 +117,16 @@ inspect(Ppu *p, Uint8 *stack, Uint8 ptr)
Uint8 i, x, y, b; Uint8 i, x, y, b;
for(i = 0; i < 0x20; ++i) { /* memory */ for(i = 0; i < 0x20; ++i) { /* memory */
x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i]; x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i];
puticn(p, &p->bg, x, y, font[(b >> 4) & 0xf], 1 + (ptr == i) * 0x7, 0, 0); puticn(p, 0, x, y, font[(b >> 4) & 0xf], 1 + (ptr == i) * 0x7, 0, 0);
puticn(p, &p->bg, x + 8, y, font[b & 0xf], 1 + (ptr == i) * 0x7, 0, 0); puticn(p, 0, x + 8, y, font[b & 0xf], 1 + (ptr == i) * 0x7, 0, 0);
} }
for(x = 0; x < 0x20; ++x) { for(x = 0; x < 0x20; ++x) {
putpixel(p, &p->bg, x, p->height / 2, 2); putpixel(p, 0, x, p->height / 2, 2);
putpixel(p, &p->bg, p->width - x, p->height / 2, 2); putpixel(p, 0, p->width - x, p->height / 2, 2);
putpixel(p, &p->bg, p->width / 2, p->height - x, 2); putpixel(p, 0, p->width / 2, p->height - x, 2);
putpixel(p, &p->bg, p->width / 2, x, 2); putpixel(p, 0, p->width / 2, x, 2);
putpixel(p, &p->bg, p->width / 2 - 16 + x, p->height / 2, 2); putpixel(p, 0, p->width / 2 - 16 + x, p->height / 2, 2);
putpixel(p, &p->bg, p->width / 2, p->height / 2 - 16 + x, 2); putpixel(p, 0, p->width / 2, p->height / 2 - 16 + x, 2);
} }
} }
@ -132,9 +137,9 @@ initppu(Ppu *p, Uint8 hor, Uint8 ver)
p->ver = ver; p->ver = ver;
p->width = 8 * p->hor; p->width = 8 * p->hor;
p->height = 8 * p->ver; p->height = 8 * p->ver;
if(!(p->bg.pixels = malloc(p->width * p->height * sizeof(Uint32)))) if(!(p->index = malloc(p->width * p->height / 8 * sizeof(Uint32))))
return 0; return 0;
if(!(p->fg.pixels = malloc(p->width * p->height * sizeof(Uint32)))) if(!(p->rgba = malloc(p->width * p->height * sizeof(Uint32))))
return 0; return 0;
clear(p); clear(p);
return 1; return 1;

View File

@ -18,17 +18,17 @@ typedef unsigned short Uint16;
typedef unsigned int Uint32; typedef unsigned int Uint32;
typedef struct Layer { typedef struct Layer {
Uint32 *pixels, colors[4]; Uint32 *pixels;
} Layer; } Layer;
typedef struct Ppu { typedef struct Ppu {
Uint32 *rgba, *index, colors[16];
Uint16 hor, ver, width, height; Uint16 hor, ver, width, height;
Layer fg, bg;
} Ppu; } Ppu;
int initppu(Ppu *p, Uint8 hor, Uint8 ver); int initppu(Ppu *p, Uint8 hor, Uint8 ver);
void putcolors(Ppu *p, Uint8 *addr); void putcolors(Ppu *p, Uint8 *addr);
void putpixel(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color); void putpixel(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color);
void puticn(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); void puticn(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
void putchr(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); void putchr(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
void inspect(Ppu *p, Uint8 *stack, Uint8 ptr); void inspect(Ppu *p, Uint8 *stack, Uint8 ptr);

View File

@ -20,7 +20,7 @@ WITH REGARD TO THIS SOFTWARE.
static SDL_AudioDeviceID audio_id; static SDL_AudioDeviceID audio_id;
static SDL_Window *gWindow; static SDL_Window *gWindow;
static SDL_Renderer *gRenderer; static SDL_Renderer *gRenderer;
static SDL_Texture *fgTexture, *bgTexture; static SDL_Texture *gTexture;
static SDL_Rect gRect; static SDL_Rect gRect;
static Ppu ppu; static Ppu ppu;
static Apu apu[POLYPHONY]; static Apu apu[POLYPHONY];
@ -60,11 +60,9 @@ redraw(Uxn *u)
{ {
if(debug) if(debug)
inspect(&ppu, u->wst.dat, u->wst.ptr); inspect(&ppu, u->wst.dat, u->wst.ptr);
SDL_UpdateTexture(bgTexture, &gRect, ppu.bg.pixels, ppu.width * sizeof(Uint32)); SDL_UpdateTexture(gTexture, &gRect, ppu.rgba, ppu.width * sizeof(Uint32));
SDL_UpdateTexture(fgTexture, &gRect, ppu.fg.pixels, ppu.width * sizeof(Uint32));
SDL_RenderClear(gRenderer); SDL_RenderClear(gRenderer);
SDL_RenderCopy(gRenderer, bgTexture, NULL, NULL); SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
SDL_RenderCopy(gRenderer, fgTexture, NULL, NULL);
SDL_RenderPresent(gRenderer); SDL_RenderPresent(gRenderer);
reqdraw = 0; reqdraw = 0;
} }
@ -87,13 +85,11 @@ togglezoom(Uxn *u)
void void
quit(void) quit(void)
{ {
free(ppu.fg.pixels); free(ppu.rgba);
free(ppu.bg.pixels); free(ppu.index);
SDL_UnlockAudioDevice(audio_id); SDL_UnlockAudioDevice(audio_id);
SDL_DestroyTexture(bgTexture); SDL_DestroyTexture(gTexture);
bgTexture = NULL; gTexture = NULL;
SDL_DestroyTexture(fgTexture);
fgTexture = NULL;
SDL_DestroyRenderer(gRenderer); SDL_DestroyRenderer(gRenderer);
gRenderer = NULL; gRenderer = NULL;
SDL_DestroyWindow(gWindow); SDL_DestroyWindow(gWindow);
@ -123,14 +119,10 @@ init(void)
if(gRenderer == NULL) if(gRenderer == NULL)
return error("Renderer", SDL_GetError()); return error("Renderer", SDL_GetError());
SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2); SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2);
bgTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2); gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2);
if(bgTexture == NULL || SDL_SetTextureBlendMode(bgTexture, SDL_BLENDMODE_NONE)) if(gTexture == NULL)
return error("Texture", SDL_GetError()); return error("Texture", SDL_GetError());
fgTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2); SDL_UpdateTexture(gTexture, NULL, ppu.rgba, 4);
if(fgTexture == NULL || SDL_SetTextureBlendMode(fgTexture, SDL_BLENDMODE_BLEND))
return error("Texture", SDL_GetError());
SDL_UpdateTexture(bgTexture, NULL, ppu.bg.pixels, 4);
SDL_UpdateTexture(fgTexture, NULL, ppu.fg.pixels, 4);
SDL_StartTextInput(); SDL_StartTextInput();
SDL_ShowCursor(SDL_DISABLE); SDL_ShowCursor(SDL_DISABLE);
SDL_zero(as); SDL_zero(as);
@ -232,7 +224,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
Uint16 x = mempeek16(d->dat, 0x8); Uint16 x = mempeek16(d->dat, 0x8);
Uint16 y = mempeek16(d->dat, 0xa); Uint16 y = mempeek16(d->dat, 0xa);
Uint8 *addr = &d->mem[mempeek16(d->dat, 0xc)]; Uint8 *addr = &d->mem[mempeek16(d->dat, 0xc)];
Layer *layer = d->dat[0xe] >> 4 & 0x1 ? &ppu.fg : &ppu.bg; Uint8 layer = (d->dat[0xe] >> 4) & 0x1;
Uint8 mode = d->dat[0xe] >> 5; Uint8 mode = d->dat[0xe] >> 5;
if(!mode) if(!mode)
putpixel(&ppu, layer, x, y, d->dat[0xe] & 0x3); putpixel(&ppu, layer, x, y, d->dat[0xe] & 0x3);