HD Packs: Allow transparency between background images and background sprites

This commit is contained in:
Sour 2020-02-01 09:52:49 -05:00
parent 8886396a39
commit 135ffc56a5
3 changed files with 38 additions and 26 deletions

View File

@ -21,11 +21,10 @@ HdNesPack::~HdNesPack()
void HdNesPack::BlendColors(uint8_t output[4], uint8_t input[4])
{
uint8_t alpha = input[3] + 1;
uint8_t invertedAlpha = 256 - input[3];
output[0] = (uint8_t)((alpha * input[0] + invertedAlpha * output[0]) >> 8);
output[1] = (uint8_t)((alpha * input[1] + invertedAlpha * output[1]) >> 8);
output[2] = (uint8_t)((alpha * input[2] + invertedAlpha * output[2]) >> 8);
output[0] = input[0] + (uint8_t)((invertedAlpha * output[0]) >> 8);
output[1] = input[1] + (uint8_t)((invertedAlpha * output[1]) >> 8);
output[2] = input[2] + (uint8_t)((invertedAlpha * output[2]) >> 8);
output[3] = 0xFF;
}
@ -58,30 +57,27 @@ void HdNesPack::DrawCustomBackground(uint32_t *outputBuffer, uint32_t x, uint32_
uint32_t top = _hdData->Backgrounds[_backgroundIndex].Top;
uint32_t width = _hdData->Backgrounds[_backgroundIndex].Data->Width;
uint32_t *pngData = _hdData->Backgrounds[_backgroundIndex].data() + ((top + y) * _hdData->Scale * width) + ((left + x) * _hdData->Scale);
uint32_t pixelColor;
if(scale == 1) {
if(brightness == 255) {
*outputBuffer = *pngData;
} else {
*outputBuffer = AdjustBrightness((uint8_t*)pngData, brightness);
}
} else {
uint32_t *buffer = outputBuffer;
for(uint32_t i = 0; i < scale; i++) {
memcpy(outputBuffer, pngData, sizeof(uint32_t) * scale);
outputBuffer += screenWidth;
pngData += width;
}
if(brightness != 255) {
for(uint32_t i = 0; i < scale; i++) {
for(uint32_t j = 0; j < scale; j++) {
*buffer = AdjustBrightness((uint8_t*)buffer, brightness);
buffer++;
}
buffer += screenWidth - scale;
for(uint32_t i = 0; i < scale; i++) {
for(uint32_t j = 0; j < scale; j++) {
if(brightness == 255) {
pixelColor = *pngData;
} else {
pixelColor = AdjustBrightness((uint8_t*)pngData, brightness);
}
if(((uint8_t*)pngData)[3] == 0xFF) {
*outputBuffer = pixelColor;
} else if(((uint8_t*)pngData)[3]) {
BlendColors((uint8_t*)outputBuffer, (uint8_t*)(&pixelColor));
}
outputBuffer++;
pngData++;
}
outputBuffer += screenWidth - scale;
pngData += width - scale;
}
}

View File

@ -212,7 +212,7 @@ bool HdPackLoader::ProcessImgTag(string src)
if(PNGHelper::ReadPNG(fileData, pixelData, bitmapInfo.Width, bitmapInfo.Height)) {
bitmapInfo.PixelData.resize(pixelData.size() / 4);
memcpy(bitmapInfo.PixelData.data(), pixelData.data(), bitmapInfo.PixelData.size() * sizeof(bitmapInfo.PixelData[0]));
PremultiplyAlpha(bitmapInfo.PixelData);
_hdNesBitmaps.push_back(bitmapInfo);
return true;
} else {
@ -221,6 +221,19 @@ bool HdPackLoader::ProcessImgTag(string src)
}
}
void HdPackLoader::PremultiplyAlpha(vector<uint32_t> &pixelData)
{
for(int i = 0; i < pixelData.size(); i++) {
if(pixelData[i] < 0xFF000000) {
uint8_t* output = (uint8_t*)(pixelData.data() + i);
uint8_t alpha = output[3] + 1;
output[0] = (uint8_t)((alpha * output[0]) >> 8);
output[1] = (uint8_t)((alpha * output[1]) >> 8);
output[2] = (uint8_t)((alpha * output[2]) >> 8);
}
}
}
void HdPackLoader::InitializeGlobalConditions()
{
HdPackCondition* hmirror = new HdPackHorizontalMirroringCondition();
@ -545,6 +558,8 @@ void HdPackLoader::ProcessBackgroundTag(vector<string> &tokens, vector<HdPackCon
bgFileData = _data->BackgroundFileData.back().get();
bgFileData->PixelData.resize(pixelData.size() / 4);
memcpy(bgFileData->PixelData.data(), pixelData.data(), bgFileData->PixelData.size() * sizeof(bgFileData->PixelData[0]));
PremultiplyAlpha(bgFileData->PixelData);
bgFileData->Width = width;
bgFileData->Height = height;
bgFileData->PngName = tokens[0];

View File

@ -32,6 +32,7 @@ private:
//Video
bool ProcessImgTag(string src);
void PremultiplyAlpha(vector<uint32_t>& pixelData);
void ProcessPatchTag(vector<string> &tokens);
void ProcessOverscanTag(vector<string> &tokens);
void ProcessConditionTag(vector<string> &tokens, bool createInvertedCondition);