From ffe142ea911e1bdebcd84fb08c8818c967c29574 Mon Sep 17 00:00:00 2001 From: Andrew Alderwick Date: Sun, 30 May 2021 23:00:05 +0100 Subject: [PATCH] Removed second texture of PPU --- src/devices/ppu.c | 49 ++++++++++++++++++++++++++--------------------- src/devices/ppu.h | 10 +++++----- src/uxnemu.c | 30 +++++++++++------------------ 3 files changed, 43 insertions(+), 46 deletions(-) diff --git a/src/devices/ppu.c b/src/devices/ppu.c index 9b9cabe..97b096a 100644 --- a/src/devices/ppu.c +++ b/src/devices/ppu.c @@ -41,11 +41,11 @@ readpixel(Uint8 *sprite, Uint8 h, Uint8 v) void clear(Ppu *p) { - int i, sz = p->height * p->width; - for(i = 0; i < sz; ++i) { - p->fg.pixels[i] = p->fg.colors[0]; - p->bg.pixels[i] = p->bg.colors[0]; - } + int i; + for(i = p->height * p->width - 1; i >= 0; --i) + p->rgba[i] = p->colors[0]; + for(i = p->height * p->width / 8 - 1; i >= 0; --i) + p->index[i] = 0; } void @@ -57,23 +57,28 @@ putcolors(Ppu *p, Uint8 *addr) r = (*(addr + i / 2) >> (!(i % 2) << 2)) & 0x0f, g = (*(addr + 2 + 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->fg.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[0] = 0; + for(i = 4; i < 16; ++i) p->colors[i] = p->colors[i / 4]; clear(p); } 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) 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 -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; 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 -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; for(v = 0; v < 8; v++) @@ -112,16 +117,16 @@ inspect(Ppu *p, Uint8 *stack, Uint8 ptr) Uint8 i, x, y, b; for(i = 0; i < 0x20; ++i) { /* memory */ 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, &p->bg, x + 8, y, font[b & 0xf], 1 + (ptr == i) * 0x7, 0, 0); + puticn(p, 0, x, y, font[(b >> 4) & 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) { - putpixel(p, &p->bg, x, p->height / 2, 2); - putpixel(p, &p->bg, p->width - x, p->height / 2, 2); - putpixel(p, &p->bg, p->width / 2, p->height - x, 2); - putpixel(p, &p->bg, p->width / 2, x, 2); - putpixel(p, &p->bg, 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, x, p->height / 2, 2); + putpixel(p, 0, p->width - x, p->height / 2, 2); + putpixel(p, 0, p->width / 2, p->height - x, 2); + putpixel(p, 0, p->width / 2, x, 2); + putpixel(p, 0, p->width / 2 - 16 + x, p->height / 2, 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->width = 8 * p->hor; 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; - if(!(p->fg.pixels = malloc(p->width * p->height * sizeof(Uint32)))) + if(!(p->rgba = malloc(p->width * p->height * sizeof(Uint32)))) return 0; clear(p); return 1; diff --git a/src/devices/ppu.h b/src/devices/ppu.h index c3b83b1..a76be9c 100644 --- a/src/devices/ppu.h +++ b/src/devices/ppu.h @@ -18,17 +18,17 @@ typedef unsigned short Uint16; typedef unsigned int Uint32; typedef struct Layer { - Uint32 *pixels, colors[4]; + Uint32 *pixels; } Layer; typedef struct Ppu { + Uint32 *rgba, *index, colors[16]; Uint16 hor, ver, width, height; - Layer fg, bg; } Ppu; int initppu(Ppu *p, Uint8 hor, Uint8 ver); void putcolors(Ppu *p, Uint8 *addr); -void putpixel(Ppu *p, Layer *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 putchr(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); +void putpixel(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color); +void puticn(Ppu *p, Uint8 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); diff --git a/src/uxnemu.c b/src/uxnemu.c index 4232ea9..06bf0a7 100644 --- a/src/uxnemu.c +++ b/src/uxnemu.c @@ -20,7 +20,7 @@ WITH REGARD TO THIS SOFTWARE. static SDL_AudioDeviceID audio_id; static SDL_Window *gWindow; static SDL_Renderer *gRenderer; -static SDL_Texture *fgTexture, *bgTexture; +static SDL_Texture *gTexture; static SDL_Rect gRect; static Ppu ppu; static Apu apu[POLYPHONY]; @@ -60,11 +60,9 @@ redraw(Uxn *u) { if(debug) inspect(&ppu, u->wst.dat, u->wst.ptr); - SDL_UpdateTexture(bgTexture, &gRect, ppu.bg.pixels, ppu.width * sizeof(Uint32)); - SDL_UpdateTexture(fgTexture, &gRect, ppu.fg.pixels, ppu.width * sizeof(Uint32)); + SDL_UpdateTexture(gTexture, &gRect, ppu.rgba, ppu.width * sizeof(Uint32)); SDL_RenderClear(gRenderer); - SDL_RenderCopy(gRenderer, bgTexture, NULL, NULL); - SDL_RenderCopy(gRenderer, fgTexture, NULL, NULL); + SDL_RenderCopy(gRenderer, gTexture, NULL, NULL); SDL_RenderPresent(gRenderer); reqdraw = 0; } @@ -87,13 +85,11 @@ togglezoom(Uxn *u) void quit(void) { - free(ppu.fg.pixels); - free(ppu.bg.pixels); + free(ppu.rgba); + free(ppu.index); SDL_UnlockAudioDevice(audio_id); - SDL_DestroyTexture(bgTexture); - bgTexture = NULL; - SDL_DestroyTexture(fgTexture); - fgTexture = NULL; + SDL_DestroyTexture(gTexture); + gTexture = NULL; SDL_DestroyRenderer(gRenderer); gRenderer = NULL; SDL_DestroyWindow(gWindow); @@ -123,14 +119,10 @@ init(void) if(gRenderer == NULL) return error("Renderer", SDL_GetError()); 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); - if(bgTexture == NULL || SDL_SetTextureBlendMode(bgTexture, SDL_BLENDMODE_NONE)) + gTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2); + if(gTexture == NULL) return error("Texture", SDL_GetError()); - fgTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2); - 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_UpdateTexture(gTexture, NULL, ppu.rgba, 4); SDL_StartTextInput(); SDL_ShowCursor(SDL_DISABLE); SDL_zero(as); @@ -232,7 +224,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w) Uint16 x = mempeek16(d->dat, 0x8); Uint16 y = mempeek16(d->dat, 0xa); 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; if(!mode) putpixel(&ppu, layer, x, y, d->dat[0xe] & 0x3);