Fixed issue with drawing functions

This commit is contained in:
neauoire 2021-07-31 10:47:51 -07:00
parent 8bf99e6d76
commit 564b3207e7
1 changed files with 23 additions and 18 deletions

View File

@ -41,9 +41,8 @@ putcolors(Ppu *p, Uint8 *addr)
void void
putpixel(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color) putpixel(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color)
{ {
if(x >= p->width || y >= p->height) if(x < p->width && y < p->height)
return; layer->pixels[y * p->width + x] = layer->colors[color];
layer->pixels[y * p->width + x] = layer->colors[color];
} }
void void
@ -52,15 +51,13 @@ puticn(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uin
Uint16 v, h; Uint16 v, h;
for(v = 0; v < 8; v++) for(v = 0; v < 8; v++)
for(h = 0; h < 8; h++) { for(h = 0; h < 8; h++) {
Uint8 ch1 = ((sprite[v] >> (7 - h)) & 0x1); Uint8 ch1 = (sprite[v] >> (7 - h)) & 0x1;
Uint16 px = x + (flipx ? 7 - h : h); if(ch1 == 1 || (color != 0x05 && color != 0x0a && color != 0x0f))
Uint16 py = y + (flipy ? 7 - v : v); putpixel(p,
if(!(ch1 || color % 0x5)) layer,
continue; x + (flipx ? 7 - h : h),
if(px < p->width && py < p->height) { y + (flipy ? 7 - v : v),
Uint8 pc = ch1 ? (color & 0x3) : (color >> 0x2); ch1 ? color % 4 : color / 4);
layer->pixels[py * p->width + px] = layer->colors[pc];
}
} }
} }
@ -72,12 +69,11 @@ putchr(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uin
for(h = 0; h < 8; h++) { for(h = 0; h < 8; h++) {
Uint8 ch1 = ((sprite[v] >> (7 - h)) & 0x1) * color; Uint8 ch1 = ((sprite[v] >> (7 - h)) & 0x1) * color;
Uint8 ch2 = ((sprite[v + 8] >> (7 - h)) & 0x1) * color; Uint8 ch2 = ((sprite[v + 8] >> (7 - h)) & 0x1) * color;
Uint16 px = x + (flipx ? 7 - h : h); putpixel(p,
Uint16 py = y + (flipy ? 7 - v : v); layer,
if(px < p->width && py < p->height) { x + (flipx ? 7 - h : h),
Uint8 pc = ((ch1 + ch2 * 2) + color / 4) & 0x3; y + (flipy ? 7 - v : v),
layer->pixels[py * p->width + px] = layer->colors[pc]; ((ch1 + ch2 * 2) + color / 4) & 0x3);
}
} }
} }
@ -97,3 +93,12 @@ initppu(Ppu *p, Uint8 hor, Uint8 ver)
clear(p); clear(p);
return 1; 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;
}