forked from rosebudai/OpenRCT2-Web
Refactor reading BGR values from g1 palette entries (#25778)
This commit is contained in:
1 parent
ba3fc759fb
commit
50c77a074c
9 files changed
+137
-99
No files matched your search
@@ -5,6 +5,12 @@
|
||||
|
||||
namespace OpenRCT2::Drawing
|
||||
{
|
||||
struct BGRColour
|
||||
{
|
||||
uint8_t blue{};
|
||||
uint8_t green{};
|
||||
uint8_t red{};
|
||||
};
|
||||
|
||||
struct PaletteBGRA
|
||||
{
|
||||
|
||||
@@ -1105,6 +1105,15 @@ const G1Element* GfxGetG1Element(ImageIndex image_id)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const G1Palette* GfxGetG1Palette(ImageIndex imageId)
|
||||
{
|
||||
const auto* element = GfxGetG1Element(imageId);
|
||||
if (element == nullptr)
|
||||
return nullptr;
|
||||
|
||||
return element->asPalette();
|
||||
}
|
||||
|
||||
void GfxSetG1Element(ImageIndex imageId, const G1Element* g1)
|
||||
{
|
||||
bool isTemp = imageId == SPR_TEMP;
|
||||
@@ -1161,9 +1170,9 @@ bool IsCsgLoaded()
|
||||
|
||||
size_t G1CalculateDataSize(const G1Element* g1)
|
||||
{
|
||||
if (g1->flags.has(G1Flag::isPalette))
|
||||
if (const auto* asPalette = g1->asPalette())
|
||||
{
|
||||
return g1->numColours * 3;
|
||||
return asPalette->numColours * 3;
|
||||
}
|
||||
|
||||
if (g1->flags.has(G1Flag::hasRLECompression))
|
||||
|
||||
@@ -698,29 +698,28 @@ void GfxFilterPixel(RenderTarget& rt, const ScreenCoordsXY& coords, FilterPalett
|
||||
* a1 (ebx)
|
||||
* product (cl)
|
||||
*/
|
||||
void GfxTransposePalette(int32_t pal, uint8_t product)
|
||||
void GfxTransposePalette(ImageIndex pal, uint8_t product)
|
||||
{
|
||||
const G1Element* g1 = GfxGetG1Element(pal);
|
||||
if (g1 != nullptr)
|
||||
const auto* g1 = GfxGetG1Palette(pal);
|
||||
if (g1 == nullptr)
|
||||
return;
|
||||
|
||||
auto index = g1->startIndex;
|
||||
auto* src = g1->palette;
|
||||
|
||||
for (auto numColours = g1->numColours; numColours > 0; numColours--)
|
||||
{
|
||||
int32_t width = g1->width;
|
||||
int32_t x = g1->xOffset;
|
||||
uint8_t* source_pointer = g1->offset;
|
||||
auto& dst = gGamePalette[index];
|
||||
// Make sure the image never gets darker than the void colour (not-quite-black), to avoid the background colour
|
||||
// jumping between void and 100% black.
|
||||
dst.Blue = std::max<uint8_t>(35, ((src->blue * product) >> 8));
|
||||
dst.Green = std::max<uint8_t>(35, ((src->green * product) >> 8));
|
||||
dst.Red = std::max<uint8_t>(23, ((src->red * product) >> 8));
|
||||
src++;
|
||||
|
||||
for (; width > 0; width--)
|
||||
{
|
||||
auto& dest_pointer = gGamePalette[x];
|
||||
// Make sure the image never gets darker than the void colour (not-quite-block), to the background colour jumping
|
||||
// between void and 100% black.
|
||||
dest_pointer.Blue = std::max<uint8_t>(35, ((source_pointer[0] * product) >> 8));
|
||||
dest_pointer.Green = std::max<uint8_t>(35, ((source_pointer[1] * product) >> 8));
|
||||
dest_pointer.Red = std::max<uint8_t>(23, ((source_pointer[2] * product) >> 8));
|
||||
source_pointer += 3;
|
||||
|
||||
x++;
|
||||
}
|
||||
UpdatePalette(gGamePalette, PaletteIndex::pi10, 236);
|
||||
index++;
|
||||
}
|
||||
UpdatePalette(gGamePalette, PaletteIndex::pi10, 236);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -743,20 +742,19 @@ void LoadPalette()
|
||||
palette = water_type->image_id;
|
||||
}
|
||||
|
||||
const G1Element* g1 = GfxGetG1Element(palette);
|
||||
const auto* g1 = GfxGetG1Palette(palette);
|
||||
if (g1 != nullptr)
|
||||
{
|
||||
int32_t width = g1->width;
|
||||
int32_t x = g1->xOffset;
|
||||
uint8_t* src = g1->offset;
|
||||
for (; width > 0; width--)
|
||||
auto index = g1->startIndex;
|
||||
auto* src = g1->palette;
|
||||
for (auto numColours = g1->numColours; numColours > 0; numColours--)
|
||||
{
|
||||
auto& dst = gGamePalette[x];
|
||||
dst.Blue = src[0];
|
||||
dst.Green = src[1];
|
||||
dst.Red = src[2];
|
||||
src += 3;
|
||||
x++;
|
||||
auto& dst = gGamePalette[index];
|
||||
dst.Blue = src->blue;
|
||||
dst.Green = src->green;
|
||||
dst.Red = src->red;
|
||||
src++;
|
||||
index++;
|
||||
}
|
||||
}
|
||||
UpdatePalette(gGamePalette, PaletteIndex::pi10, 236);
|
||||
@@ -957,17 +955,18 @@ void UpdatePaletteEffects()
|
||||
{
|
||||
palette = water_type->image_id;
|
||||
}
|
||||
const G1Element* g1 = GfxGetG1Element(palette);
|
||||
const auto* g1 = GfxGetG1Palette(palette);
|
||||
if (g1 != nullptr)
|
||||
{
|
||||
int32_t xoffset = g1->xOffset;
|
||||
auto startIndex = g1->startIndex;
|
||||
|
||||
for (int32_t i = 0; i < g1->width; i++)
|
||||
for (int32_t i = 0; i < g1->numColours; i++)
|
||||
{
|
||||
auto& paletteOffset = gGamePalette[xoffset + i];
|
||||
paletteOffset.Blue = -((0xFF - g1->offset[(i * 3) + 0]) / 2) - 1;
|
||||
paletteOffset.Green = -((0xFF - g1->offset[(i * 3) + 1]) / 2) - 1;
|
||||
paletteOffset.Red = -((0xFF - g1->offset[(i * 3) + 2]) / 2) - 1;
|
||||
auto& paletteOffset = gGamePalette[startIndex + i];
|
||||
const auto& g1PaletteEntry = g1->palette[i];
|
||||
paletteOffset.Blue = -((0xFF - g1PaletteEntry.blue) / 2) - 1;
|
||||
paletteOffset.Green = -((0xFF - g1PaletteEntry.green) / 2) - 1;
|
||||
paletteOffset.Red = -((0xFF - g1PaletteEntry.red) / 2) - 1;
|
||||
}
|
||||
|
||||
UpdatePalette(gGamePalette, kPaletteOffsetDynamic, kPaletteLengthDynamic);
|
||||
@@ -986,17 +985,18 @@ void UpdatePaletteEffects()
|
||||
palette = water_type->image_id;
|
||||
}
|
||||
|
||||
const G1Element* g1 = GfxGetG1Element(palette);
|
||||
const auto* g1 = GfxGetG1Palette(palette);
|
||||
if (g1 != nullptr)
|
||||
{
|
||||
int32_t xoffset = g1->xOffset;
|
||||
auto startIndex = g1->startIndex;
|
||||
|
||||
for (int32_t i = 0; i < g1->width; i++)
|
||||
for (int32_t i = 0; i < g1->numColours; i++)
|
||||
{
|
||||
auto& paletteOffset = gGamePalette[xoffset + i];
|
||||
paletteOffset.Blue = g1->offset[(i * 3) + 0];
|
||||
paletteOffset.Green = g1->offset[(i * 3) + 1];
|
||||
paletteOffset.Red = g1->offset[(i * 3) + 2];
|
||||
auto& paletteOffset = gGamePalette[startIndex + i];
|
||||
const auto& g1PaletteEntry = g1->palette[i];
|
||||
paletteOffset.Blue = g1PaletteEntry.blue;
|
||||
paletteOffset.Green = g1PaletteEntry.green;
|
||||
paletteOffset.Red = g1PaletteEntry.red;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1022,21 +1022,21 @@ void UpdatePaletteEffects()
|
||||
{
|
||||
waterId = water_type->palette_index_1;
|
||||
}
|
||||
const G1Element* g1 = GfxGetG1Element(shade + waterId);
|
||||
const auto* g1 = GfxGetG1Palette(shade + waterId);
|
||||
if (g1 != nullptr)
|
||||
{
|
||||
uint8_t* vs = &g1->offset[j * 3];
|
||||
const auto* g1PaletteEntry = &g1->palette[j];
|
||||
int32_t n = kPaletteLengthWaterWaves;
|
||||
for (int32_t i = 0; i < n; i++)
|
||||
{
|
||||
auto& vd = gGamePalette[EnumValue(kPaletteOffsetWaterWaves) + i];
|
||||
vd.Blue = vs[0];
|
||||
vd.Green = vs[1];
|
||||
vd.Red = vs[2];
|
||||
vs += 9;
|
||||
if (vs >= &g1->offset[9 * n])
|
||||
vd.Blue = g1PaletteEntry->blue;
|
||||
vd.Green = g1PaletteEntry->green;
|
||||
vd.Red = g1PaletteEntry->red;
|
||||
g1PaletteEntry += 3;
|
||||
if (g1PaletteEntry >= &g1->palette[3 * n])
|
||||
{
|
||||
vs -= 9 * n;
|
||||
g1PaletteEntry -= 3 * n;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1047,42 +1047,42 @@ void UpdatePaletteEffects()
|
||||
waterId = water_type->palette_index_2;
|
||||
}
|
||||
|
||||
g1 = GfxGetG1Element(shade + waterId);
|
||||
g1 = GfxGetG1Palette(shade + waterId);
|
||||
if (g1 != nullptr)
|
||||
{
|
||||
uint8_t* vs = &g1->offset[j * 3];
|
||||
auto* src = &g1->palette[j];
|
||||
int32_t n = kPaletteLengthWaterSparkles;
|
||||
for (int32_t i = 0; i < n; i++)
|
||||
{
|
||||
auto& vd = gGamePalette[EnumValue(kPaletteOffsetWaterSparkles) + i];
|
||||
vd.Blue = vs[0];
|
||||
vd.Green = vs[1];
|
||||
vd.Red = vs[2];
|
||||
vs += 9;
|
||||
if (vs >= &g1->offset[9 * n])
|
||||
vd.Blue = src->blue;
|
||||
vd.Green = src->green;
|
||||
vd.Red = src->red;
|
||||
src += 3;
|
||||
if (src >= &g1->palette[3 * n])
|
||||
{
|
||||
vs -= 9 * n;
|
||||
src -= 3 * n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
j = (static_cast<uint16_t>(gPaletteEffectFrame * -960) * 3) >> 16;
|
||||
waterId = SPR_GAME_PALETTE_4;
|
||||
g1 = GfxGetG1Element(shade + waterId);
|
||||
g1 = GfxGetG1Palette(shade + waterId);
|
||||
if (g1 != nullptr)
|
||||
{
|
||||
uint8_t* vs = &g1->offset[j * 3];
|
||||
int32_t n = 3;
|
||||
auto* src = &g1->palette[j];
|
||||
const int32_t n = 3;
|
||||
for (int32_t i = 0; i < n; i++)
|
||||
{
|
||||
auto& vd = gGamePalette[EnumValue(PaletteIndex::pi243) + i];
|
||||
vd.Blue = vs[0];
|
||||
vd.Green = vs[1];
|
||||
vd.Red = vs[2];
|
||||
vs += 3;
|
||||
if (vs >= &g1->offset[3 * n])
|
||||
vd.Blue = src->blue;
|
||||
vd.Green = src->green;
|
||||
vd.Red = src->red;
|
||||
src++;
|
||||
if (src >= &g1->palette[3])
|
||||
{
|
||||
vs -= 3 * n;
|
||||
src -= n;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -245,7 +245,7 @@ void GfxSetDirtyBlocks(const ScreenRect& rect);
|
||||
void GfxInvalidateScreen();
|
||||
|
||||
// palette
|
||||
void GfxTransposePalette(int32_t pal, uint8_t product);
|
||||
void GfxTransposePalette(ImageIndex pal, uint8_t product);
|
||||
void LoadPalette();
|
||||
|
||||
// other
|
||||
@@ -271,6 +271,7 @@ void GfxUnloadG2AndFonts();
|
||||
void GfxUnloadCsg();
|
||||
const OpenRCT2::G1Element* GfxGetG1Element(const ImageId imageId);
|
||||
const OpenRCT2::G1Element* GfxGetG1Element(ImageIndex image_id);
|
||||
const OpenRCT2::G1Palette* GfxGetG1Palette(ImageIndex imageId);
|
||||
void GfxSetG1Element(ImageIndex imageId, const OpenRCT2::G1Element* g1);
|
||||
std::optional<OpenRCT2::Gx> GfxLoadGx(const std::vector<uint8_t>& buffer);
|
||||
bool IsCsgLoaded();
|
||||
|
||||
@@ -15,6 +15,11 @@
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace OpenRCT2::Drawing
|
||||
{
|
||||
struct BGRColour;
|
||||
}
|
||||
|
||||
namespace OpenRCT2
|
||||
{
|
||||
enum class G1Flag : uint8_t
|
||||
@@ -28,24 +33,37 @@ namespace OpenRCT2
|
||||
};
|
||||
using G1Flags = FlagHolder<uint16_t, G1Flag>;
|
||||
|
||||
struct G1Palette
|
||||
{
|
||||
Drawing::BGRColour* palette = nullptr;
|
||||
int16_t numColours = 0;
|
||||
int16_t pad1 = 0; // unused for palettes
|
||||
int16_t startIndex = 0;
|
||||
int16_t pad2 = 0; // unused for palettes
|
||||
G1Flags flags = { G1Flag::isPalette };
|
||||
int32_t pad3 = 0; // unused for palettes
|
||||
};
|
||||
|
||||
struct G1Element
|
||||
{
|
||||
uint8_t* offset = nullptr; // 0x00
|
||||
union
|
||||
uint8_t* offset = nullptr;
|
||||
int16_t width = 0;
|
||||
int16_t height = 0;
|
||||
int16_t xOffset = 0;
|
||||
int16_t yOffset = 0;
|
||||
|
||||
G1Flags flags = {};
|
||||
int32_t zoomedOffset = 0;
|
||||
|
||||
const G1Palette* asPalette() const
|
||||
{
|
||||
int16_t width = 0; // 0x04
|
||||
int16_t numColours; // If G1Flag::isPalette is set
|
||||
};
|
||||
int16_t height = 0; // 0x06
|
||||
union
|
||||
{
|
||||
int16_t xOffset = 0; // 0x08
|
||||
int16_t startIndex; // If G1Flag::isPalette is set
|
||||
};
|
||||
int16_t yOffset = 0; // 0x0A
|
||||
G1Flags flags = {}; // 0x0C
|
||||
int32_t zoomedOffset = 0; // 0x0E
|
||||
if (!flags.has(G1Flag::isPalette))
|
||||
return nullptr;
|
||||
|
||||
return reinterpret_cast<const G1Palette*>(this);
|
||||
}
|
||||
};
|
||||
static_assert(sizeof(G1Palette) == sizeof(G1Element));
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct G1Header
|
||||
|
||||
@@ -643,4 +643,11 @@ namespace OpenRCT2
|
||||
}
|
||||
_entries.push_back(std::move(newg1));
|
||||
}
|
||||
|
||||
void ImageTable::addPalette(const G1Palette& g1)
|
||||
{
|
||||
Guard::Assert(g1.flags.has(G1Flag::isPalette));
|
||||
const auto base = reinterpret_cast<const G1Element*>(&g1);
|
||||
AddImage(base);
|
||||
}
|
||||
} // namespace OpenRCT2
|
||||
@@ -68,5 +68,6 @@ namespace OpenRCT2
|
||||
return static_cast<uint32_t>(_entries.size());
|
||||
}
|
||||
void AddImage(const G1Element* g1);
|
||||
void addPalette(const G1Palette& g1);
|
||||
};
|
||||
} // namespace OpenRCT2
|
||||
@@ -107,32 +107,28 @@ namespace OpenRCT2
|
||||
auto numColours = jColours.size();
|
||||
|
||||
// This pointer gets memcopied in ImageTable::AddImage so it's fine for the unique_ptr to go out of scope
|
||||
auto data = std::make_unique<uint8_t[]>(numColours * 3);
|
||||
auto data = std::make_unique<Drawing::BGRColour[]>(numColours);
|
||||
size_t dataIndex = 0;
|
||||
|
||||
for (auto& jColour : jColours)
|
||||
{
|
||||
if (jColour.is_string())
|
||||
{
|
||||
auto colour = ParseColour(Json::GetString(jColour));
|
||||
data[dataIndex + 0] = (colour >> 16) & 0xFF;
|
||||
data[dataIndex + 1] = (colour >> 8) & 0xFF;
|
||||
data[dataIndex + 2] = colour & 0xFF;
|
||||
data[dataIndex] = ParseColour(Json::GetString(jColour));
|
||||
}
|
||||
dataIndex += 3;
|
||||
dataIndex++;
|
||||
}
|
||||
|
||||
G1Element g1 = {};
|
||||
g1.offset = data.get();
|
||||
G1Palette g1 = {};
|
||||
g1.palette = data.get();
|
||||
g1.numColours = static_cast<int16_t>(numColours);
|
||||
g1.startIndex = Json::GetNumber<int16_t>(jPalette["index"]);
|
||||
g1.flags = { G1Flag::isPalette };
|
||||
|
||||
auto& imageTable = GetImageTable();
|
||||
imageTable.AddImage(&g1);
|
||||
imageTable.addPalette(g1);
|
||||
}
|
||||
|
||||
uint32_t WaterObject::ParseColour(const std::string& s) const
|
||||
Drawing::BGRColour WaterObject::ParseColour(const std::string& s) const
|
||||
{
|
||||
uint8_t r = 0;
|
||||
uint8_t g = 0;
|
||||
@@ -144,6 +140,6 @@ namespace OpenRCT2
|
||||
g = std::stoul(s.substr(3, 2), nullptr, 16) & 0xFF;
|
||||
b = std::stoul(s.substr(5, 2), nullptr, 16) & 0xFF;
|
||||
}
|
||||
return (b << 16) | (g << 8) | r;
|
||||
return { b, g, r };
|
||||
}
|
||||
} // namespace OpenRCT2
|
||||
@@ -36,6 +36,6 @@ namespace OpenRCT2
|
||||
|
||||
private:
|
||||
void ReadJsonPalette(json_t& jPalette);
|
||||
uint32_t ParseColour(const std::string& s) const;
|
||||
Drawing::BGRColour ParseColour(const std::string& s) const;
|
||||
};
|
||||
} // namespace OpenRCT2
|
||||
Reference in new issue
Block a user