ppu: separate reqdraw for fg and bg

This commit is contained in:
Sigrid Solveig Haflínudóttir 2021-12-24 20:45:31 +01:00
parent 0a040824b7
commit f79b092e71
3 changed files with 47 additions and 42 deletions

View File

@ -50,31 +50,31 @@ ppu_palette(Ppu *p, Uint8 *addr)
}
for(i = 4; i < 16; ++i)
p->palette[i] = p->palette[i / 4];
p->reqdraw = 1;
p->fg.reqdraw = p->bg.reqdraw = 1;
}
void
ppu_resize(Ppu *p, Uint16 width, Uint16 height)
{
Uint8
*bg = realloc(p->bg, width * height),
*fg = realloc(p->fg, width * height);
*bg = realloc(p->bg.p, width * height),
*fg = realloc(p->fg.p, width * height);
if(!bg || !fg)
return;
p->bg = bg;
p->fg = fg;
p->bg.p = bg;
p->fg.p = fg;
p->width = width;
p->height = height;
ppu_clear(p, p->bg);
ppu_clear(p, p->fg);
ppu_clear(p, &p->bg);
ppu_clear(p, &p->fg);
}
void
ppu_clear(Ppu *p, Uint8 *layer)
ppu_clear(Ppu *p, Layer *layer)
{
Uint32 i, size = p->width * p->height;
for(i = 0; i < size; ++i)
layer[i] = 0x00;
layer->p[i] = 0x00;
}
void
@ -82,25 +82,25 @@ ppu_redraw(Ppu *p, Uint32 *screen)
{
Uint32 i, size = p->width * p->height;
for(i = 0; i < size; ++i)
screen[i] = p->palette[p->fg[i] ? p->fg[i] : p->bg[i]];
p->reqdraw = 0;
screen[i] = p->palette[p->fg.p[i] ? p->fg.p[i] : p->bg.p[i]];
p->fg.reqdraw = p->bg.reqdraw = 0;
}
void
ppu_write(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color)
ppu_write(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color)
{
if(x < p->width && y < p->height) {
Uint32 row = (x + y * p->width);
Uint8 prev = layer[row];
Uint32 i = (x + y * p->width);
Uint8 prev = layer->p[i];
if(color != prev) {
layer[row] = color;
p->reqdraw = 1;
layer->p[i] = color;
layer->reqdraw = 1;
}
}
}
void
ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
ppu_1bpp(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
{
Uint16 v, h;
for(v = 0; v < 8; ++v)
@ -116,7 +116,7 @@ ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, U
}
void
ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
ppu_2bpp(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
{
Uint16 v, h;
for(v = 0; v < 8; ++v)
@ -140,24 +140,24 @@ ppu_debug(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
for(i = 0; i < 0x20; ++i) {
x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i];
/* working stack */
ppu_1bpp(p, p->fg, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
ppu_1bpp(p, p->fg, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
ppu_1bpp(p, &p->fg, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
ppu_1bpp(p, &p->fg, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
y = 0x28 + (i / 8 + 1) * 8;
b = memory[i];
/* return stack */
ppu_1bpp(p, p->fg, x, y, font[(b >> 4) & 0xf], 3, 0, 0);
ppu_1bpp(p, p->fg, x + 8, y, font[b & 0xf], 3, 0, 0);
ppu_1bpp(p, &p->fg, x, y, font[(b >> 4) & 0xf], 3, 0, 0);
ppu_1bpp(p, &p->fg, x + 8, y, font[b & 0xf], 3, 0, 0);
}
/* return pointer */
ppu_1bpp(p, p->fg, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0);
ppu_1bpp(p, p->fg, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0);
ppu_1bpp(p, &p->fg, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0);
ppu_1bpp(p, &p->fg, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0);
/* guides */
for(x = 0; x < 0x10; ++x) {
ppu_write(p, p->fg, x, p->height / 2, 2);
ppu_write(p, p->fg, p->width - x, p->height / 2, 2);
ppu_write(p, p->fg, p->width / 2, p->height - x, 2);
ppu_write(p, p->fg, p->width / 2, x, 2);
ppu_write(p, p->fg, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2);
ppu_write(p, p->fg, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2);
ppu_write(p, &p->fg, x, p->height / 2, 2);
ppu_write(p, &p->fg, p->width - x, p->height / 2, 2);
ppu_write(p, &p->fg, p->width / 2, p->height - x, 2);
ppu_write(p, &p->fg, p->width / 2, x, 2);
ppu_write(p, &p->fg, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2);
ppu_write(p, &p->fg, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2);
}
}

View File

@ -20,18 +20,23 @@ typedef unsigned char Uint8;
typedef unsigned short Uint16;
typedef unsigned int Uint32;
typedef struct Layer {
Uint8 *p;
Uint8 reqdraw;
} Layer;
typedef struct Ppu {
Uint8 *fg, *bg, reqdraw;
Layer fg, bg;
Uint16 width, height;
Uint32 palette[16];
} Ppu;
void ppu_palette(Ppu *p, Uint8 *addr);
void ppu_resize(Ppu *p, Uint16 width, Uint16 height);
void ppu_clear(Ppu *p, Uint8 *layer);
void ppu_clear(Ppu *p, Layer *layer);
void ppu_redraw(Ppu *p, Uint32 *screen);
void ppu_write(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color);
void ppu_1bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
void ppu_2bpp(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
void ppu_write(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color);
void ppu_1bpp(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
void ppu_2bpp(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
void ppu_debug(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory);

View File

@ -105,7 +105,7 @@ set_zoom(Uint8 scale)
if(!gWindow)
return;
set_window_size(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
ppu.reqdraw = 1;
ppu.fg.reqdraw = ppu.bg.reqdraw = 1;
}
static int
@ -128,7 +128,7 @@ set_size(Uint16 width, Uint16 height, int is_resize)
return error("SDL_UpdateTexture", SDL_GetError());
if(is_resize)
set_window_size(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
ppu.reqdraw = 1;
ppu.fg.reqdraw = ppu.bg.reqdraw = 1;
return 1;
}
@ -293,7 +293,7 @@ screen_deo(Device *d, Uint8 port)
Uint16 x = peek16(d->dat, 0x8);
Uint16 y = peek16(d->dat, 0xa);
Uint8 layer = d->dat[0xe] & 0x40;
ppu_write(&ppu, layer ? ppu.fg : ppu.bg, x, y, d->dat[0xe] & 0x3);
ppu_write(&ppu, layer ? &ppu.fg : &ppu.bg, x, y, d->dat[0xe] & 0x3);
if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 1); /* auto x+1 */
if(d->dat[0x6] & 0x02) poke16(d->dat, 0xa, y + 1); /* auto y+1 */
break;
@ -304,10 +304,10 @@ screen_deo(Device *d, Uint8 port)
Uint8 layer = d->dat[0xf] & 0x40;
Uint8 *addr = &d->mem[peek16(d->dat, 0xc)];
if(d->dat[0xf] & 0x80) {
ppu_2bpp(&ppu, layer ? ppu.fg : ppu.bg, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
ppu_2bpp(&ppu, layer ? &ppu.fg : &ppu.bg, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 16); /* auto addr+16 */
} else {
ppu_1bpp(&ppu, layer ? ppu.fg : ppu.bg, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
ppu_1bpp(&ppu, layer ? &ppu.fg : &ppu.bg, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] & 0x10, d->dat[0xf] & 0x20);
if(d->dat[0x6] & 0x04) poke16(d->dat, 0xc, peek16(d->dat, 0xc) + 8); /* auto addr+8 */
}
if(d->dat[0x6] & 0x01) poke16(d->dat, 0x8, x + 8); /* auto x+8 */
@ -467,7 +467,7 @@ doctrl(Uxn *u, SDL_Event *event, int z)
case SDLK_LEFT: flag = 0x40; break;
case SDLK_RIGHT: flag = 0x80; break;
case SDLK_F1: if(z) set_zoom(zoom > 2 ? 1 : zoom + 1); break;
case SDLK_F2: if(z) devsystem->dat[0xe] = !devsystem->dat[0xe]; ppu_clear(&ppu, ppu.fg); break;
case SDLK_F2: if(z) devsystem->dat[0xe] = !devsystem->dat[0xe]; ppu_clear(&ppu, &ppu.fg); break;
case SDLK_F3: if(z) capture_screen(); break;
case SDLK_AC_BACK:
case SDLK_F4: if(z) restart(u); break;
@ -550,7 +550,7 @@ run(Uxn *u)
}
breakout:
uxn_eval(u, devscreen->vector);
if(ppu.reqdraw || devsystem->dat[0xe])
if(ppu.fg.reqdraw || ppu.bg.reqdraw || devsystem->dat[0xe])
redraw(u);
if(!BENCH) {
elapsed = (SDL_GetPerformanceCounter() - begin) / (double)SDL_GetPerformanceFrequency() * 1000.0f;