Cleaned up ppu_read

This commit is contained in:
neauoire 2021-09-30 09:34:50 -07:00
parent c02dc5b0e2
commit 9de513ad47
2 changed files with 15 additions and 32 deletions

View File

@ -13,8 +13,9 @@ WITH REGARD TO THIS SOFTWARE.
*/ */
/* /*
fgbg fgbg pixel 0001 0002
byte [0000 0000] layer fgbg fgbg
byte 1010 1010
*/ */
static Uint8 blending[5][16] = { static Uint8 blending[5][16] = {
@ -27,42 +28,25 @@ static Uint8 blending[5][16] = {
static void static void
ppu_clear(Ppu *p) ppu_clear(Ppu *p)
{ {
int x, y; int row;
for(y = 0; y < p->height; ++y) { for(row = 0; row < p->height * p->width / 2; ++row)
for(x = 0; x < p->width; ++x) { p->pixels[row] = 0;
ppu_write(p, 0, x, y, 0);
ppu_write(p, 1, x, y, 0);
}
}
} }
Uint8 Uint8
ppu_read(Ppu *p, Uint16 x, Uint16 y) ppu_read(Ppu *p, Uint16 x, Uint16 y)
{ {
int row = (x + y * p->width) / 0x2; int row = (x + y * p->width) / 0x2;
Uint8 seg = !(x & 0x1) << 2;
if(x % 2) { Uint8 byte = p->pixels[row] >> seg;
if(p->pixels[row] & 0x0c) { return (byte & 0x0c ? (byte >> 2) : byte) & 0x3;
return (p->pixels[row] >> 0x2) & 0x3;
} else {
return (p->pixels[row] >> 0x0) & 0x3;
}
} else {
if(p->pixels[row] & 0xc0) {
return (p->pixels[row] >> 0x6) & 0x3;
} else {
return (p->pixels[row] >> 0x4) & 0x3;
}
}
return 0;
} }
void void
ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color) ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color)
{ {
int row = (x + y * p->width) / 0x2; int row = (x + y * p->width) / 0x2;
int original = p->pixels[row]; Uint8 original = p->pixels[row];
Uint8 next = 0x0; Uint8 next = 0x0;
if(x % 2) { if(x % 2) {
if(layer) { if(layer) {
@ -82,6 +66,7 @@ ppu_write(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color)
} }
} }
p->pixels[row] = next; p->pixels[row] = next;
if(original != next)
p->reqdraw = 1; p->reqdraw = 1;
} }
@ -127,9 +112,7 @@ ppu_set_size(Ppu *p, Uint16 width, Uint16 height)
ppu_clear(p); ppu_clear(p);
p->width = width; p->width = width;
p->height = height; p->height = height;
p->pixels = realloc(p->bg, p->width * p->height * sizeof(Uint8) * 2); p->pixels = realloc(p->pixels, p->width * p->height * sizeof(Uint8) / 2);
p->bg = p->pixels;
p->fg = p->pixels + (p->width * p->height * sizeof(Uint8));
ppu_clear(p); ppu_clear(p);
return p->bg && p->fg; return !!p->pixels;
} }

View File

@ -22,7 +22,7 @@ typedef unsigned short Uint16;
typedef unsigned int Uint32; typedef unsigned int Uint32;
typedef struct Ppu { typedef struct Ppu {
Uint8 *bg, *fg, *pixels, reqdraw; Uint8 *pixels, reqdraw;
Uint16 width, height; Uint16 width, height;
} Ppu; } Ppu;