From 564b3207e7059f215fc2cd62959489c760c63718 Mon Sep 17 00:00:00 2001 From: neauoire Date: Sat, 31 Jul 2021 10:47:51 -0700 Subject: [PATCH] Fixed issue with drawing functions --- src/devices/ppu.c | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/src/devices/ppu.c b/src/devices/ppu.c index ac65b16..452ea43 100644 --- a/src/devices/ppu.c +++ b/src/devices/ppu.c @@ -41,9 +41,8 @@ putcolors(Ppu *p, Uint8 *addr) void putpixel(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color) { - if(x >= p->width || y >= p->height) - return; - layer->pixels[y * p->width + x] = layer->colors[color]; + if(x < p->width && y < p->height) + layer->pixels[y * p->width + x] = layer->colors[color]; } void @@ -52,15 +51,13 @@ puticn(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uin Uint16 v, h; for(v = 0; v < 8; v++) for(h = 0; h < 8; h++) { - Uint8 ch1 = ((sprite[v] >> (7 - h)) & 0x1); - Uint16 px = x + (flipx ? 7 - h : h); - Uint16 py = y + (flipy ? 7 - v : v); - if(!(ch1 || color % 0x5)) - continue; - if(px < p->width && py < p->height) { - Uint8 pc = ch1 ? (color & 0x3) : (color >> 0x2); - layer->pixels[py * p->width + px] = layer->colors[pc]; - } + Uint8 ch1 = (sprite[v] >> (7 - h)) & 0x1; + if(ch1 == 1 || (color != 0x05 && color != 0x0a && color != 0x0f)) + putpixel(p, + layer, + x + (flipx ? 7 - h : h), + y + (flipy ? 7 - v : v), + ch1 ? color % 4 : color / 4); } } @@ -72,12 +69,11 @@ putchr(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uin for(h = 0; h < 8; h++) { Uint8 ch1 = ((sprite[v] >> (7 - h)) & 0x1) * color; Uint8 ch2 = ((sprite[v + 8] >> (7 - h)) & 0x1) * color; - Uint16 px = x + (flipx ? 7 - h : h); - Uint16 py = y + (flipy ? 7 - v : v); - if(px < p->width && py < p->height) { - Uint8 pc = ((ch1 + ch2 * 2) + color / 4) & 0x3; - layer->pixels[py * p->width + px] = layer->colors[pc]; - } + putpixel(p, + layer, + x + (flipx ? 7 - h : h), + y + (flipy ? 7 - v : v), + ((ch1 + ch2 * 2) + color / 4) & 0x3); } } @@ -97,3 +93,12 @@ initppu(Ppu *p, Uint8 hor, Uint8 ver) clear(p); return 1; } +; + p->height = 8 * p->ver; + if(!(p->bg.pixels = malloc(p->width * p->height * sizeof(Uint32)))) + return 0; + if(!(p->fg.pixels = malloc(p->width * p->height * sizeof(Uint32)))) + return 0; + clear(p); + return 1; +}