From 06f47c8d2ca39ac0a02a5878ae09ccf6958b05e6 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Thu, 21 Jan 2016 01:09:58 -0800 Subject: [PATCH] GB Video: Add basic scrolling support --- src/gb/renderers/software.c | 27 ++++++++++++++++++--------- src/gb/renderers/software.h | 3 +++ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/gb/renderers/software.c b/src/gb/renderers/software.c index 3b72f1835..fc821e12b 100644 --- a/src/gb/renderers/software.c +++ b/src/gb/renderers/software.c @@ -61,9 +61,8 @@ static void GBVideoSoftwareRendererInit(struct GBVideoRenderer* renderer) { static void GBVideoSoftwareRendererReset(struct GBVideoRenderer* renderer) { struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer; - int i; - - // TODO + softwareRenderer->scy = 0; + softwareRenderer->scx = 0; } static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer) { @@ -99,6 +98,12 @@ static uint8_t GBVideoSoftwareRendererWriteVideoRegister(struct GBVideoRenderer* softwareRenderer->objPalette[1][2] = GB_PALETTE[(value >> 4) & 3]; softwareRenderer->objPalette[1][3] = GB_PALETTE[(value >> 6) & 3]; break; + case REG_SCY: + softwareRenderer->scy = value; + break; + case REG_SCX: + softwareRenderer->scx = value; + break; } return value; } @@ -143,17 +148,21 @@ static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer data += 0x1000; } int x; + int topY = (((y + renderer->scy) >> 3) & 0x1F) * 0x20; + int bottomY = (y + renderer->scy) & 7; for (x = 0; x < GB_VIDEO_HORIZONTAL_PIXELS; ++x) { + int topX = ((x + renderer->scx) >> 3) & 0x1F; + int bottomX = 7 - ((x + renderer->scx) & 7); int bgTile; if (GBRegisterLCDCIsTileData(renderer->lcdc)) { - bgTile = maps[(x >> 3) + (0x20 * (y >> 3))]; + bgTile = maps[topX + topY]; } else { - bgTile = ((int8_t*) maps)[(x >> 3) + (0x20 * (y >> 3))]; + bgTile = ((int8_t*) maps)[topX + topY]; } - uint8_t tileDataLower = data[(bgTile * 8 + (y & 7)) * 2]; - uint8_t tileDataUpper = data[(bgTile * 8 + (y & 7)) * 2 + 1]; - tileDataUpper >>= 7 - (x & 7); - tileDataLower >>= 7 - (x & 7); + uint8_t tileDataLower = data[(bgTile * 8 + bottomY) * 2]; + uint8_t tileDataUpper = data[(bgTile * 8 + bottomY) * 2 + 1]; + tileDataUpper >>= bottomX; + tileDataLower >>= bottomX; renderer->row[x] = renderer->bgPalette[((tileDataUpper & 1) << 1) | (tileDataLower & 1)]; } } diff --git a/src/gb/renderers/software.h b/src/gb/renderers/software.h index 2fbc5ad0e..4b3b69418 100644 --- a/src/gb/renderers/software.h +++ b/src/gb/renderers/software.h @@ -29,6 +29,9 @@ struct GBVideoSoftwareRenderer { uint32_t* temporaryBuffer; + uint8_t scy; + uint8_t scx; + GBRegisterLCDC lcdc; };