GB Video: Expand custom palettes to separate BG/OBJ palettes

This commit is contained in:
Vicki Pfau 2017-09-26 18:21:51 -07:00
parent 431ee76c1a
commit b11171c6f1
5 changed files with 129 additions and 43 deletions

View File

@ -17,7 +17,7 @@ struct GBCartridgeOverride {
enum GBModel model;
enum GBMemoryBankControllerType mbc;
uint32_t gbColors[4];
uint32_t gbColors[12];
};
struct Configuration;

View File

@ -145,7 +145,7 @@ struct GBVideo {
bool ocpIncrement;
uint8_t sgbCommandHeader;
uint16_t dmgPalette[4];
uint16_t dmgPalette[12];
uint16_t palette[64];
bool sgbBorders;

View File

@ -179,6 +179,30 @@ static void _GBCoreLoadConfig(struct mCore* core, const struct mCoreConfig* conf
if (mCoreConfigGetIntValue(config, "gb.pal[3]", &color)) {
GBVideoSetPalette(&gb->video, 3, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[4]", &color)) {
GBVideoSetPalette(&gb->video, 4, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[5]", &color)) {
GBVideoSetPalette(&gb->video, 5, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[6]", &color)) {
GBVideoSetPalette(&gb->video, 6, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[7]", &color)) {
GBVideoSetPalette(&gb->video, 7, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[8]", &color)) {
GBVideoSetPalette(&gb->video, 8, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[9]", &color)) {
GBVideoSetPalette(&gb->video, 9, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[10]", &color)) {
GBVideoSetPalette(&gb->video, 10, color);
}
if (mCoreConfigGetIntValue(config, "gb.pal[11]", &color)) {
GBVideoSetPalette(&gb->video, 11, color);
}
mCoreConfigCopyValue(&core->config, config, "gb.bios");
mCoreConfigCopyValue(&core->config, config, "sgb.bios");

View File

@ -19,6 +19,7 @@ static const struct GBCartridgeOverride _overrides[] = {
bool GBOverrideFind(const struct Configuration* config, struct GBCartridgeOverride* override) {
override->model = GB_MODEL_AUTODETECT;
override->mbc = GB_MBC_AUTODETECT;
memset(override->gbColors, 0, sizeof(override->gbColors));
bool found = false;
int i;
@ -35,11 +36,19 @@ bool GBOverrideFind(const struct Configuration* config, struct GBCartridgeOverri
snprintf(sectionName, sizeof(sectionName), "gb.override.%08X", override->headerCrc32);
const char* model = ConfigurationGetValue(config, sectionName, "model");
const char* mbc = ConfigurationGetValue(config, sectionName, "mbc");
const char* pal[4] = {
const char* pal[12] = {
ConfigurationGetValue(config, sectionName, "pal[0]"),
ConfigurationGetValue(config, sectionName, "pal[1]"),
ConfigurationGetValue(config, sectionName, "pal[2]"),
ConfigurationGetValue(config, sectionName, "pal[3]")
ConfigurationGetValue(config, sectionName, "pal[3]"),
ConfigurationGetValue(config, sectionName, "pal[4]"),
ConfigurationGetValue(config, sectionName, "pal[5]"),
ConfigurationGetValue(config, sectionName, "pal[6]"),
ConfigurationGetValue(config, sectionName, "pal[7]"),
ConfigurationGetValue(config, sectionName, "pal[8]"),
ConfigurationGetValue(config, sectionName, "pal[9]"),
ConfigurationGetValue(config, sectionName, "pal[10]"),
ConfigurationGetValue(config, sectionName, "pal[11]")
};
if (model) {
@ -56,18 +65,25 @@ bool GBOverrideFind(const struct Configuration* config, struct GBCartridgeOverri
}
}
if (pal[0] && pal[1] && pal[2] && pal[3]) {
int i;
for (i = 0; i < 4; ++i) {
char* end;
unsigned long value = strtoul(pal[i], &end, 10);
if (end == &pal[i][1] && *end == 'x') {
value = strtoul(pal[i], &end, 16);
}
if (*end) {
continue;
}
override->gbColors[i] = value;
for (i = 0; i < 12; ++i) {
if (!pal[i]) {
continue;
}
char* end;
unsigned long value = strtoul(pal[i], &end, 10);
if (end == &pal[i][1] && *end == 'x') {
value = strtoul(pal[i], &end, 16);
}
if (*end) {
continue;
}
value |= 0xFF000000;
override->gbColors[i] = value;
if (i < 8) {
override->gbColors[i + 4] = value;
}
if (i < 4) {
override->gbColors[i + 8] = value;
}
}
}
@ -80,12 +96,43 @@ void GBOverrideSave(struct Configuration* config, const struct GBCartridgeOverri
const char* model = GBModelToName(override->model);
ConfigurationSetValue(config, sectionName, "model", model);
if (override->gbColors[0] | override->gbColors[1] | override->gbColors[2] | override->gbColors[3]) {
ConfigurationSetIntValue(config, sectionName, "pal[0]", override->gbColors[0]);
ConfigurationSetIntValue(config, sectionName, "pal[1]", override->gbColors[1]);
ConfigurationSetIntValue(config, sectionName, "pal[2]", override->gbColors[2]);
ConfigurationSetIntValue(config, sectionName, "pal[3]", override->gbColors[3]);
if (override->gbColors[0] & 0xFF000000) {
ConfigurationSetIntValue(config, sectionName, "pal[0]", override->gbColors[0] & ~0xFF000000);
}
if (override->gbColors[1] & 0xFF000000) {
ConfigurationSetIntValue(config, sectionName, "pal[1]", override->gbColors[1] & ~0xFF000000);
}
if (override->gbColors[2] & 0xFF000000) {
ConfigurationSetIntValue(config, sectionName, "pal[2]", override->gbColors[2] & ~0xFF000000);
}
if (override->gbColors[3] & 0xFF000000) {
ConfigurationSetIntValue(config, sectionName, "pal[3]", override->gbColors[3] & ~0xFF000000);
}
if (override->gbColors[4] & 0xFF000000) {
ConfigurationSetIntValue(config, sectionName, "pal[4]", override->gbColors[4] & ~0xFF000000);
}
if (override->gbColors[5] & 0xFF000000) {
ConfigurationSetIntValue(config, sectionName, "pal[5]", override->gbColors[5] & ~0xFF000000);
}
if (override->gbColors[6] & 0xFF000000) {
ConfigurationSetIntValue(config, sectionName, "pal[6]", override->gbColors[6] & ~0xFF000000);
}
if (override->gbColors[7] & 0xFF000000) {
ConfigurationSetIntValue(config, sectionName, "pal[7]", override->gbColors[7] & ~0xFF000000);
}
if (override->gbColors[8] & 0xFF000000) {
ConfigurationSetIntValue(config, sectionName, "pal[8]", override->gbColors[8] & ~0xFF000000);
}
if (override->gbColors[9] & 0xFF000000) {
ConfigurationSetIntValue(config, sectionName, "pal[9]", override->gbColors[9] & ~0xFF000000);
}
if (override->gbColors[10] & 0xFF000000) {
ConfigurationSetIntValue(config, sectionName, "pal[10]", override->gbColors[10] & ~0xFF000000);
}
if (override->gbColors[11] & 0xFF000000) {
ConfigurationSetIntValue(config, sectionName, "pal[11]", override->gbColors[11] & ~0xFF000000);
}
if (override->mbc != GB_MBC_AUTODETECT) {
ConfigurationSetIntValue(config, sectionName, "mbc", override->mbc);
} else {
@ -103,11 +150,18 @@ void GBOverrideApply(struct GB* gb, const struct GBCartridgeOverride* override)
GBMBCInit(gb);
}
if (override->gbColors[0] | override->gbColors[1] | override->gbColors[2] | override->gbColors[3]) {
GBVideoSetPalette(&gb->video, 0, override->gbColors[0]);
GBVideoSetPalette(&gb->video, 1, override->gbColors[1]);
GBVideoSetPalette(&gb->video, 2, override->gbColors[2]);
GBVideoSetPalette(&gb->video, 3, override->gbColors[3]);
int i;
for (i = 0; i < 12; ++i) {
if (!(override->gbColors[i] & 0xFF000000)) {
continue;
}
GBVideoSetPalette(&gb->video, i, override->gbColors[i]);
if (i < 8) {
GBVideoSetPalette(&gb->video, i + 4, override->gbColors[i]);
}
if (i < 4) {
GBVideoSetPalette(&gb->video, i + 8, override->gbColors[i]);
}
}
}

View File

@ -72,6 +72,14 @@ void GBVideoInit(struct GBVideo* video) {
video->dmgPalette[1] = 0x56B5;
video->dmgPalette[2] = 0x294A;
video->dmgPalette[3] = 0x0000;
video->dmgPalette[4] = 0x7FFF;
video->dmgPalette[5] = 0x56B5;
video->dmgPalette[6] = 0x294A;
video->dmgPalette[7] = 0x0000;
video->dmgPalette[8] = 0x7FFF;
video->dmgPalette[9] = 0x56B5;
video->dmgPalette[10] = 0x294A;
video->dmgPalette[11] = 0x0000;
video->sgbBorders = true;
@ -115,14 +123,14 @@ void GBVideoReset(struct GBVideo* video) {
video->palette[1] = video->dmgPalette[1];
video->palette[2] = video->dmgPalette[2];
video->palette[3] = video->dmgPalette[3];
video->palette[8 * 4 + 0] = video->dmgPalette[0];
video->palette[8 * 4 + 1] = video->dmgPalette[1];
video->palette[8 * 4 + 2] = video->dmgPalette[2];
video->palette[8 * 4 + 3] = video->dmgPalette[3];
video->palette[9 * 4 + 0] = video->dmgPalette[0];
video->palette[9 * 4 + 1] = video->dmgPalette[1];
video->palette[9 * 4 + 2] = video->dmgPalette[2];
video->palette[9 * 4 + 3] = video->dmgPalette[3];
video->palette[8 * 4 + 0] = video->dmgPalette[4];
video->palette[8 * 4 + 1] = video->dmgPalette[5];
video->palette[8 * 4 + 2] = video->dmgPalette[6];
video->palette[8 * 4 + 3] = video->dmgPalette[7];
video->palette[9 * 4 + 0] = video->dmgPalette[8];
video->palette[9 * 4 + 1] = video->dmgPalette[9];
video->palette[9 * 4 + 2] = video->dmgPalette[10];
video->palette[9 * 4 + 3] = video->dmgPalette[11];
video->renderer->deinit(video->renderer);
video->renderer->init(video->renderer, video->p->model, video->sgbBorders);
@ -478,20 +486,20 @@ void GBVideoWritePalette(struct GBVideo* video, uint16_t address, uint8_t value)
video->renderer->writePalette(video->renderer, 3, video->palette[3]);
break;
case REG_OBP0:
video->palette[8 * 4 + 0] = video->dmgPalette[value & 3];
video->palette[8 * 4 + 1] = video->dmgPalette[(value >> 2) & 3];
video->palette[8 * 4 + 2] = video->dmgPalette[(value >> 4) & 3];
video->palette[8 * 4 + 3] = video->dmgPalette[(value >> 6) & 3];
video->palette[8 * 4 + 0] = video->dmgPalette[(value & 3) + 4];
video->palette[8 * 4 + 1] = video->dmgPalette[((value >> 2) & 3) + 4];
video->palette[8 * 4 + 2] = video->dmgPalette[((value >> 4) & 3) + 4];
video->palette[8 * 4 + 3] = video->dmgPalette[((value >> 6) & 3) + 4];
video->renderer->writePalette(video->renderer, 8 * 4 + 0, video->palette[8 * 4 + 0]);
video->renderer->writePalette(video->renderer, 8 * 4 + 1, video->palette[8 * 4 + 1]);
video->renderer->writePalette(video->renderer, 8 * 4 + 2, video->palette[8 * 4 + 2]);
video->renderer->writePalette(video->renderer, 8 * 4 + 3, video->palette[8 * 4 + 3]);
break;
case REG_OBP1:
video->palette[9 * 4 + 0] = video->dmgPalette[value & 3];
video->palette[9 * 4 + 1] = video->dmgPalette[(value >> 2) & 3];
video->palette[9 * 4 + 2] = video->dmgPalette[(value >> 4) & 3];
video->palette[9 * 4 + 3] = video->dmgPalette[(value >> 6) & 3];
video->palette[9 * 4 + 0] = video->dmgPalette[(value & 3) + 8];
video->palette[9 * 4 + 1] = video->dmgPalette[((value >> 2) & 3) + 8];
video->palette[9 * 4 + 2] = video->dmgPalette[((value >> 4) & 3) + 8];
video->palette[9 * 4 + 3] = video->dmgPalette[((value >> 6) & 3) + 8];
video->renderer->writePalette(video->renderer, 9 * 4 + 0, video->palette[9 * 4 + 0]);
video->renderer->writePalette(video->renderer, 9 * 4 + 1, video->palette[9 * 4 + 1]);
video->renderer->writePalette(video->renderer, 9 * 4 + 2, video->palette[9 * 4 + 2]);
@ -547,7 +555,7 @@ void GBVideoSwitchBank(struct GBVideo* video, uint8_t value) {
}
void GBVideoSetPalette(struct GBVideo* video, unsigned index, uint32_t color) {
if (index >= 4) {
if (index >= 12) {
return;
}
video->dmgPalette[index] = M_RGB8_TO_RGB5(color);