diff --git a/gbscreen.cpp b/gbscreen.cpp index aa7e2ef..e66570b 100644 --- a/gbscreen.cpp +++ b/gbscreen.cpp @@ -137,6 +137,7 @@ namespace gbscreen const uint8_t LCDC = mem::readMem(0xff40); const uint8_t SCY = mem::readMem(0xff42); const uint8_t SCX = mem::readMem(0xff43); + const uint8_t BGP = mem::readMem(0xff47); const uint16_t ty = uint8_t(SCY+LY) >> 3; const uint8_t ly = uint8_t(SCY+LY) & 0x7; uint16_t tx = SCX >> 3; @@ -156,7 +157,8 @@ namespace gbscreen uint8_t b = mem::readMem(tile_address+1); for (int i=0; i<8; ++i) { if (ox==0) { - line_buffer[pi++] = (a&0x80 ? 1 : 0) + (b&0x80 ? 2 : 0 ); + uint8_t index = (a&0x80 ? 1 : 0) + (b&0x80 ? 2 : 0 ); + line_buffer[pi++] = (BGP >> (index*2)) & 0x3; } else { ox--; } @@ -176,6 +178,7 @@ namespace gbscreen void fill_line_buffer_obj(uint8_t LY) { const uint8_t LCDC = mem::readMem(0xff40); + const uint8_t OBP[2] = { mem::readMem(0xff48), mem::readMem(0xff49) }; if ((LCDC & 0x2) == 0) return; oam = (oam_entry_t*)mem::rawPtr(0xfe00); @@ -195,7 +198,7 @@ namespace gbscreen // Pintem els sprites en el buffer de sprites uint8_t pixels[160]; uint8_t x_pos[160]; - for (int i=0;i<160;++i) { pixels[i] = 0; x_pos[i] = 255; } + for (int i=0;i<160;++i) { pixels[i] = 255; x_pos[i] = 255; } obj=0; while (obj_list[obj] != 255) { oam_entry_t *o = &oam[obj_list[obj]]; @@ -213,10 +216,11 @@ namespace gbscreen if (o->x+i>=8) { // Si està dins de la pantalla... if (x_pos[o->x+i-8]>o->x) { // Si te una x menor que la que tenía //uint8_t xflip = o->attr&0x20 ? 8 : 0; // està invertit horitzontalment? - uint8_t ppos = 1 << ( o->attr&0x20 ? i : 7-i); - uint8_t val = (a&ppos ? 1 : 0) + (b&ppos ? 2 : 0 ); // agafem el pixel que toca + const uint8_t ppos = 1 << ( o->attr&0x20 ? i : 7-i); + const uint8_t val = (a&ppos ? 1 : 0) + (b&ppos ? 2 : 0 ); // agafem el pixel que toca if (val) { // Si el pixel no es transparent... - pixels[o->x+i-8] = val | o->attr&0x80; // el pintem al buffer, amb el flag de prioritat respecte al BKG + const uint8_t color = (OBP[(LCDC>>4)&1] >> (val*2)) & 0x3; + pixels[o->x+i-8] = color | o->attr&0x80;; // el pintem al buffer, amb el flag de prioritat respecte al BKG x_pos[o->x+i-8] = o->x; // I apuntem la seua x per a comparar després } } @@ -226,7 +230,7 @@ namespace gbscreen } // Per últim, volquem els pixels que toque al buffer de linea for (int i=0; i<160; ++i) { - if (pixels[i]>0) { // si el pixel no es transparent... + if (pixels[i]!=255) { // si el pixel no es transparent... if ( !(pixels[i]&0x80) || (line_buffer[i]==0) ) { // Si te prioritat o el color de fondo es 0... line_buffer[i] = pixels[i]&0x03; // pintem el pixel (llevant el flag de prioritat) }