From d3a204086d670377e2aa6b685218ac09024b15ba Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Fri, 22 Jan 2016 20:24:21 -0800 Subject: [PATCH] GB Video: Sprite enhancements --- src/gb/renderers/software.c | 73 +++++++++++++++++-------------------- src/gb/renderers/software.h | 5 +-- 2 files changed, 36 insertions(+), 42 deletions(-) diff --git a/src/gb/renderers/software.c b/src/gb/renderers/software.c index f63c4697b..66f0af181 100644 --- a/src/gb/renderers/software.c +++ b/src/gb/renderers/software.c @@ -97,22 +97,22 @@ static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* softwareRenderer->lcdc = value; break; case REG_BGP: - softwareRenderer->bgPalette[0] = GB_PALETTE[value & 3]; - softwareRenderer->bgPalette[1] = GB_PALETTE[(value >> 2) & 3]; - softwareRenderer->bgPalette[2] = GB_PALETTE[(value >> 4) & 3]; - softwareRenderer->bgPalette[3] = GB_PALETTE[(value >> 6) & 3]; + softwareRenderer->palette[0] = GB_PALETTE[value & 3]; + softwareRenderer->palette[1] = GB_PALETTE[(value >> 2) & 3]; + softwareRenderer->palette[2] = GB_PALETTE[(value >> 4) & 3]; + softwareRenderer->palette[3] = GB_PALETTE[(value >> 6) & 3]; break; case REG_OBP0: - softwareRenderer->objPalette[0][0] = GB_PALETTE[value & 3]; - softwareRenderer->objPalette[0][1] = GB_PALETTE[(value >> 2) & 3]; - softwareRenderer->objPalette[0][2] = GB_PALETTE[(value >> 4) & 3]; - softwareRenderer->objPalette[0][3] = GB_PALETTE[(value >> 6) & 3]; + softwareRenderer->palette[8 * 4 + 0] = GB_PALETTE[value & 3]; + softwareRenderer->palette[8 * 4 + 1] = GB_PALETTE[(value >> 2) & 3]; + softwareRenderer->palette[8 * 4 + 2] = GB_PALETTE[(value >> 4) & 3]; + softwareRenderer->palette[8 * 4 + 3] = GB_PALETTE[(value >> 6) & 3]; break; case REG_OBP1: - softwareRenderer->objPalette[1][0] = GB_PALETTE[value & 3]; - softwareRenderer->objPalette[1][1] = GB_PALETTE[(value >> 2) & 3]; - softwareRenderer->objPalette[1][2] = GB_PALETTE[(value >> 4) & 3]; - softwareRenderer->objPalette[1][3] = GB_PALETTE[(value >> 6) & 3]; + softwareRenderer->palette[9 * 4 + 0] = GB_PALETTE[value & 3]; + softwareRenderer->palette[9 * 4 + 1] = GB_PALETTE[(value >> 2) & 3]; + softwareRenderer->palette[9 * 4 + 2] = GB_PALETTE[(value >> 4) & 3]; + softwareRenderer->palette[9 * 4 + 3] = GB_PALETTE[(value >> 6) & 3]; break; case REG_SCY: softwareRenderer->scy = value; @@ -133,11 +133,6 @@ static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* static void GBVideoSoftwareRendererDrawScanline(struct GBVideoRenderer* renderer, int y) { struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer; - size_t x; - for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) { - softwareRenderer->row[x] = GB_PALETTE[0]; - } - if (softwareRenderer->oamDirty) { _cleanOAM(softwareRenderer); } @@ -146,14 +141,21 @@ static void GBVideoSoftwareRendererDrawScanline(struct GBVideoRenderer* renderer if (GBRegisterLCDCIsTileMap(softwareRenderer->lcdc)) { maps += GB_SIZE_MAP; } - GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, 0, y, softwareRenderer->scx, softwareRenderer->scy); + size_t x; + if (GBRegisterLCDCIsBgEnable(softwareRenderer->lcdc)) { + GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, 0, y, softwareRenderer->scx, softwareRenderer->scy); - if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y) { - maps = &softwareRenderer->d.vram[GB_BASE_MAP]; - if (GBRegisterLCDCIsWindowTileMap(softwareRenderer->lcdc)) { - maps += GB_SIZE_MAP; + if (GBRegisterLCDCIsWindow(softwareRenderer->lcdc) && softwareRenderer->wy <= y) { + maps = &softwareRenderer->d.vram[GB_BASE_MAP]; + if (GBRegisterLCDCIsWindowTileMap(softwareRenderer->lcdc)) { + maps += GB_SIZE_MAP; + } + GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, 0, y, 7 - softwareRenderer->wx, -softwareRenderer->wy); + } + } else { + for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) { + softwareRenderer->row[x] = 0; } - GBVideoSoftwareRendererDrawBackground(softwareRenderer, maps, 0, y, 7 - softwareRenderer->wx, -softwareRenderer->wy); } int spriteHeight = 8; @@ -169,17 +171,9 @@ static void GBVideoSoftwareRendererDrawScanline(struct GBVideoRenderer* renderer } color_t* row = &softwareRenderer->outputBuffer[softwareRenderer->outputBufferStride * y]; -#ifdef COLOR_16_BIT -#if defined(__ARM_NEON) && !defined(__APPLE__) - _to16Bit(row, softwareRenderer->row, GB_VIDEO_HORIZONTAL_PIXELS); -#else for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) { - row[x] = softwareRenderer->row[x]; + row[x] = softwareRenderer->palette[softwareRenderer->row[x]]; } -#endif -#else - memcpy(row, softwareRenderer->row, GB_VIDEO_HORIZONTAL_PIXELS * sizeof(*row)); -#endif } static void _cleanOAM(struct GBVideoSoftwareRenderer* renderer) { @@ -189,7 +183,7 @@ static void _cleanOAM(struct GBVideoSoftwareRenderer* renderer) { int i; for (i = 0; i < 40; ++i) { uint8_t y = renderer->d.oam->obj[i].y; - if (y < 16 || y >= GB_VIDEO_VERTICAL_PIXELS + 16) { + if (y == 0 || y >= GB_VIDEO_VERTICAL_PIXELS + 16) { continue; } // TODO: Sort @@ -229,7 +223,7 @@ static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer uint8_t tileDataUpper = data[(bgTile * 8 + bottomY) * 2 + 1]; tileDataUpper >>= bottomX; tileDataLower >>= bottomX; - renderer->row[x] = renderer->bgPalette[((tileDataUpper & 1) << 1) | (tileDataLower & 1)]; + renderer->row[x] = ((tileDataUpper & 1) << 1) | (tileDataLower & 1); } } @@ -239,12 +233,12 @@ static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* rende int bottomY; if (GBObjAttributesIsYFlip(obj->attr)) { bottomY = 7 - ((y - obj->y - 16) & 7); - if (y - obj->y < -8) { + if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y < -8) { ++tileOffset; } } else { bottomY = (y - obj->y - 16) & 7; - if (y - obj->y >= -8) { + if (GBRegisterLCDCIsObjSize(renderer->lcdc) && y - obj->y >= -8) { ++tileOffset; } } @@ -256,7 +250,8 @@ static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* rende if (x < 0) { x = 0; } - int p = GBObjAttributesGetPalette(obj->attr); + uint8_t mask = GBObjAttributesIsPriority(obj->attr) ? 0 : 0x20; + int p = (GBObjAttributesGetPalette(obj->attr) + 8) * 4; for (; x < end; ++x) { int bottomX; if (GBObjAttributesIsXFlip(obj->attr)) { @@ -270,8 +265,8 @@ static void GBVideoSoftwareRendererDrawObj(struct GBVideoSoftwareRenderer* rende tileDataUpper >>= bottomX; tileDataLower >>= bottomX; color_t current = renderer->row[x]; - if (((tileDataUpper | tileDataLower) & 1) && (!GBObjAttributesIsPriority(obj->attr) || current == GB_PALETTE[0])) { - renderer->row[x] = renderer->objPalette[p][((tileDataUpper & 1) << 1) | (tileDataLower & 1)]; + if (((tileDataUpper | tileDataLower) & 1) && current <= mask) { + renderer->row[x] = p | ((tileDataUpper & 1) << 1) | (tileDataLower & 1); } } } diff --git a/src/gb/renderers/software.h b/src/gb/renderers/software.h index 9332b4487..323a1ef37 100644 --- a/src/gb/renderers/software.h +++ b/src/gb/renderers/software.h @@ -22,10 +22,9 @@ struct GBVideoSoftwareRenderer { color_t* outputBuffer; int outputBufferStride; - uint32_t row[GB_VIDEO_HORIZONTAL_PIXELS]; + uint8_t row[GB_VIDEO_HORIZONTAL_PIXELS + 8]; - color_t bgPalette[4]; - color_t objPalette[2][4]; + color_t palette[128]; uint32_t* temporaryBuffer;