GB Video: Add basic scrolling support

This commit is contained in:
Jeffrey Pfau 2016-01-21 01:09:58 -08:00
parent a8b473870d
commit 06f47c8d2c
2 changed files with 21 additions and 9 deletions

View File

@ -61,9 +61,8 @@ static void GBVideoSoftwareRendererInit(struct GBVideoRenderer* renderer) {
static void GBVideoSoftwareRendererReset(struct GBVideoRenderer* renderer) { static void GBVideoSoftwareRendererReset(struct GBVideoRenderer* renderer) {
struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer; struct GBVideoSoftwareRenderer* softwareRenderer = (struct GBVideoSoftwareRenderer*) renderer;
int i; softwareRenderer->scy = 0;
softwareRenderer->scx = 0;
// TODO
} }
static void GBVideoSoftwareRendererDeinit(struct GBVideoRenderer* renderer) { 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][2] = GB_PALETTE[(value >> 4) & 3];
softwareRenderer->objPalette[1][3] = GB_PALETTE[(value >> 6) & 3]; softwareRenderer->objPalette[1][3] = GB_PALETTE[(value >> 6) & 3];
break; break;
case REG_SCY:
softwareRenderer->scy = value;
break;
case REG_SCX:
softwareRenderer->scx = value;
break;
} }
return value; return value;
} }
@ -143,17 +148,21 @@ static void GBVideoSoftwareRendererDrawBackground(struct GBVideoSoftwareRenderer
data += 0x1000; data += 0x1000;
} }
int x; 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) { 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; int bgTile;
if (GBRegisterLCDCIsTileData(renderer->lcdc)) { if (GBRegisterLCDCIsTileData(renderer->lcdc)) {
bgTile = maps[(x >> 3) + (0x20 * (y >> 3))]; bgTile = maps[topX + topY];
} else { } 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 tileDataLower = data[(bgTile * 8 + bottomY) * 2];
uint8_t tileDataUpper = data[(bgTile * 8 + (y & 7)) * 2 + 1]; uint8_t tileDataUpper = data[(bgTile * 8 + bottomY) * 2 + 1];
tileDataUpper >>= 7 - (x & 7); tileDataUpper >>= bottomX;
tileDataLower >>= 7 - (x & 7); tileDataLower >>= bottomX;
renderer->row[x] = renderer->bgPalette[((tileDataUpper & 1) << 1) | (tileDataLower & 1)]; renderer->row[x] = renderer->bgPalette[((tileDataUpper & 1) << 1) | (tileDataLower & 1)];
} }
} }

View File

@ -29,6 +29,9 @@ struct GBVideoSoftwareRenderer {
uint32_t* temporaryBuffer; uint32_t* temporaryBuffer;
uint8_t scy;
uint8_t scx;
GBRegisterLCDC lcdc; GBRegisterLCDC lcdc;
}; };