Experimenting with 2 layers

This commit is contained in:
neauoire 2021-09-29 17:58:58 -07:00
parent f3b3823b0c
commit ecc1d7c416
3 changed files with 75 additions and 78 deletions

View File

@ -19,48 +19,49 @@ static Uint8 blending[5][16] = {
{2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2, 2, 3, 1, 2},
{1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0}};
void
ppu_frame(Ppu *p)
{
p->reqdraw = 0;
p->i0 = p->width / PPW + p->height * p->stride;
p->i1 = 0;
}
static void
ppu_clear(Ppu *p)
{
unsigned int i;
for(i = 0; i < p->stride * p->height; ++i)
p->dat[i] = 0;
int x, y;
for(y = 0; y < p->height; ++y) {
for(x = 0; x < p->width; ++x) {
ppu_write(p, p->bg, x, y, 0);
ppu_write(p, p->fg, x, y, 0);
}
}
}
Uint8
ppu_read(Ppu *p, Uint16 x, Uint16 y)
{
unsigned int i = x / PPW + y * p->stride, shift = x % PPW * 4;
return (p->dat[i] >> shift) & 0xf;
int ch1, ch2, r = (y % 8) + ((x / 8 + y / 8 * p->width / 8) * 16);
ch1 = (p->fg[r] >> (7 - x % 8)) & 1;
ch2 = (p->fg[r + 8] >> (7 - x % 8)) & 1;
if(!ch1 && !ch2) {
ch1 = (p->bg[r] >> (7 - x % 8)) & 1;
ch2 = (p->bg[r + 8] >> (7 - x % 8)) & 1;
}
return ch1 + (ch2 << 1);
}
void
ppu_write(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 color)
ppu_write(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color)
{
unsigned int v, i = x / PPW + y * p->stride, shift = x % PPW * 4;
int row = (y % 8) + ((x / 8 + y / 8 * p->width / 8) * 16), col = x % 8;
if(x >= p->width || y >= p->height)
return;
v = p->dat[i];
if(fg) shift += 2;
p->dat[i] &= ~(3 << shift);
p->dat[i] |= color << shift;
if((v ^ p->dat[i]) != 0) {
p->reqdraw = 1;
p->i0 = p->i0 < i ? p->i0 : i;
p->i1 = p->i1 > i ? p->i1 : i;
}
if(color == 0 || color == 2)
layer[row] &= ~(1UL << (7 - col));
else
layer[row] |= 1UL << (7 - col);
if(color == 0 || color == 1)
layer[row + 8] &= ~(1UL << (7 - col));
else
layer[row + 8] |= 1UL << (7 - col);
}
void
ppu_1bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
ppu_1bpp(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++)
@ -68,7 +69,7 @@ ppu_1bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 f
Uint8 ch1 = (sprite[v] >> (7 - h)) & 0x1;
if(ch1 || blending[4][color])
ppu_write(p,
fg,
layer,
x + (flipx ? 7 - h : h),
y + (flipy ? 7 - v : v),
blending[ch1][color]);
@ -76,7 +77,7 @@ ppu_1bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 f
}
void
ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy)
ppu_2bpp(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++)
@ -86,7 +87,7 @@ ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 f
Uint8 ch = ch1 + ch2 * 2;
if(ch || blending[4][color])
ppu_write(p,
fg,
layer,
x + (flipx ? 7 - h : h),
y + (flipy ? 7 - v : v),
blending[ch][color]);
@ -98,12 +99,12 @@ ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 f
int
ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
{
p->width = width;
p->stride = (width + PPW - 1) / PPW;
p->height = height;
p->dat = realloc(p->dat, p->stride * p->height * sizeof(unsigned int));
if(p->dat == NULL) return 0;
ppu_clear(p);
ppu_frame(p);
return 1;
p->width = width;
p->height = height;
p->pixels = realloc(p->bg, p->width / 4 * p->height * sizeof(Uint8) * 2);
p->bg = p->pixels;
p->fg = p->pixels + (p->width / 4 * p->height * sizeof(Uint8));
ppu_clear(p);
return p->bg && p->fg;
}

View File

@ -22,14 +22,14 @@ typedef unsigned short Uint16;
typedef unsigned int Uint32;
typedef struct Ppu {
Uint8 reqdraw;
Uint8 *bg, *fg, *pixels, reqdraw;
Uint16 width, height;
unsigned int i0, i1, *dat, stride;
} Ppu;
Uint8 ppu_read(Ppu *p, Uint16 x, Uint16 y);
void ppu_write(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 color);
void ppu_write(Ppu *p, Uint8 *layer, Uint16 x, Uint16 y, Uint8 color);
void ppu_frame(Ppu *p);
void ppu_1bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
void ppu_2bpp(Ppu *p, int fg, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy);
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);
int ppu_set_size(Ppu *p, Uint16 width, Uint16 height);

View File

@ -37,7 +37,7 @@ static SDL_Rect gRect;
static Ppu ppu;
static Apu apu[POLYPHONY];
static Device *devsystem, *devscreen, *devmouse, *devctrl, *devaudio0, *devconsole;
static Uint8 zoom = 1, reqdraw = 0;
static Uint8 zoom = 1;
static Uint32 *ppu_screen, stdin_event, audio0_event, palette[16];
static Uint8 font[][8] = {
@ -118,14 +118,14 @@ set_palette(Uint8 *addr)
}
for(i = 4; i < 16; ++i)
palette[i] = palette[i / 4];
reqdraw = 1;
ppu.reqdraw = 1;
}
static void
set_inspect(Uint8 flag)
{
devsystem->dat[0xe] = flag;
reqdraw = 1;
ppu.reqdraw = 1;
}
static void
@ -144,7 +144,7 @@ set_zoom(Uint8 scale)
if(scale == zoom || !gWindow)
return;
set_window_size(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
reqdraw = 1;
ppu.reqdraw = 1;
}
static int
@ -165,7 +165,7 @@ set_size(Uint16 width, Uint16 height, int is_resize)
return error("sdl_texture", SDL_GetError());
if(is_resize)
set_window_size(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom);
reqdraw = 1;
ppu.reqdraw = 1;
return 1;
}
@ -190,51 +190,46 @@ static void
draw_inspect(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory)
{
Uint8 i, x, y, b;
for(i = 0; i < 0x20; ++i) { /* stack */
for(i = 0; i < 0x20; ++i) {
x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i];
ppu_1bpp(p, 1, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
ppu_1bpp(p, 1, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
/* working stack */
ppu_1bpp(p, ppu.fg, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0);
ppu_1bpp(p, ppu.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, ppu.fg, x, y, font[(b >> 4) & 0xf], 3, 0, 0);
ppu_1bpp(p, ppu.fg, x + 8, y, font[b & 0xf], 3, 0, 0);
}
/* return pointer */
ppu_1bpp(p, 1, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0);
ppu_1bpp(p, 1, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0);
for(i = 0; i < 0x20; ++i) { /* memory */
x = ((i % 8) * 3 + 1) * 8, y = 0x38 + (i / 8 + 1) * 8, b = memory[i];
ppu_1bpp(p, 1, x, y, font[(b >> 4) & 0xf], 3, 0, 0);
ppu_1bpp(p, 1, x + 8, y, font[b & 0xf], 3, 0, 0);
}
for(x = 0; x < 0x10; ++x) { /* guides */
ppu_write(p, 1, x, p->height / 2, 2);
ppu_write(p, 1, p->width - x, p->height / 2, 2);
ppu_write(p, 1, p->width / 2, p->height - x, 2);
ppu_write(p, 1, p->width / 2, x, 2);
ppu_write(p, 1, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2);
ppu_write(p, 1, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2);
ppu_1bpp(p, ppu.fg, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0);
ppu_1bpp(p, ppu.fg, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0);
/* guides */
for(x = 0; x < 0x10; ++x) {
ppu_write(p, ppu.fg, x, p->height / 2, 2);
ppu_write(p, ppu.fg, p->width - x, p->height / 2, 2);
ppu_write(p, ppu.fg, p->width / 2, p->height - x, 2);
ppu_write(p, ppu.fg, p->width / 2, x, 2);
ppu_write(p, ppu.fg, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2);
ppu_write(p, ppu.fg, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2);
}
}
static void
redraw(Uxn *u)
{
Uint16 x, y, y0 = 0, y1 = ppu.height;
SDL_Rect up = gRect;
Uint16 x, y;
if(devsystem->dat[0xe])
draw_inspect(&ppu, u->wst.dat, u->wst.ptr, u->rst.ptr, u->ram.dat);
if(!reqdraw && ppu.reqdraw) {
y0 = ppu.i0 / ppu.stride;
y1 = ppu.i1 / ppu.stride + 1;
up.y += y0;
up.h = y1 - y0;
}
for(y = y0; y < y1; ++y)
for(y = 0; y < ppu.height; ++y)
for(x = 0; x < ppu.width; ++x)
ppu_screen[x + y * ppu.width] = palette[ppu_read(&ppu, x, y)];
SDL_UpdateTexture(gTexture, &up, ppu_screen + y0 * ppu.width, ppu.width * sizeof(Uint32));
SDL_UpdateTexture(gTexture, &gRect, ppu_screen, ppu.width * sizeof(Uint32));
SDL_RenderClear(gRenderer);
SDL_RenderCopy(gRenderer, gTexture, NULL, NULL);
SDL_RenderPresent(gRenderer);
reqdraw = 0;
ppu_frame(&ppu);
ppu.reqdraw = 0;
}
static void
@ -395,7 +390,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
Uint16 x = peek16(d->dat, 0x8);
Uint16 y = peek16(d->dat, 0xa);
Uint8 layer = d->dat[0xe] & 0x40;
ppu_write(&ppu, layer, 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;
@ -406,14 +401,15 @@ screen_talk(Device *d, Uint8 b0, Uint8 w)
Uint8 layer = d->dat[0xf] & 0x40;
Uint8 *addr = &d->mem[peek16(d->dat, 0xc)];
if(d->dat[0xf] & 0x80) {
ppu_2bpp(&ppu, layer, 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, 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 */
if(d->dat[0x6] & 0x02) poke16(d->dat, 0xa, y + 8); /* auto y+8 */
ppu.reqdraw = 1;
break;
}
}
@ -546,7 +542,7 @@ run(Uxn *u)
}
}
uxn_eval(u, devscreen->vector);
if(reqdraw || ppu.reqdraw || devsystem->dat[0xe])
if(ppu.reqdraw || devsystem->dat[0xe])
redraw(u);
if(!BENCH) {
elapsed = (SDL_GetPerformanceCounter() - start) / (double)SDL_GetPerformanceFrequency() * 1000.0f;