1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-02-02 11:45:13 +01:00

Merge pull request #25851 from Gymnasiast/refactor/name-water-object-fields

Move BlendColourMap to own file, name Water Object palette indices
This commit is contained in:
Michael Steenbeek
2026-01-19 11:37:23 +01:00
committed by GitHub
11 changed files with 143 additions and 102 deletions

View File

@@ -14,6 +14,7 @@
#include <algorithm>
#include <openrct2/Diagnostic.h>
#include <openrct2/core/EnumUtils.hpp>
#include <openrct2/drawing/BlendColourMap.h>
#include <openrct2/drawing/Drawing.h>
#include <openrct2/interface/Colour.h>
#include <openrct2/world/Location.hpp>

View File

@@ -0,0 +1,91 @@
/*****************************************************************************
* Copyright (c) 2014-2026 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#include "BlendColourMap.h"
#ifndef DISABLE_TTF
#include "../core/EnumUtils.hpp"
#include "ColourPalette.h"
#include "Drawing.h"
#include <cmath>
namespace OpenRCT2::Drawing
{
static BlendColourMapType _blendColourMap = {};
static bool _blendColourMapInitialised = false;
static PaletteIndex FindClosestPaletteIndex(uint8_t red, uint8_t green, uint8_t blue)
{
PaletteIndex closest = PaletteIndex::pi255;
int32_t closestDistance = INT32_MAX;
for (auto i = PaletteIndex::transparent; i < PaletteIndex::waterWaves0; i = static_cast<PaletteIndex>(EnumValue(i) + 1))
{
const auto& paletteEntry = gPalette[EnumValue(i)];
const int32_t distance = std::pow(paletteEntry.red - red, 2) + std::pow(paletteEntry.green - green, 2)
+ std::pow(paletteEntry.blue - blue, 2);
if (distance < closestDistance)
{
closest = i;
closestDistance = distance;
}
}
return closest;
}
static void InitBlendColourMap()
{
for (size_t i = 0; i < kGamePaletteSize; i++)
{
for (size_t j = i; j < kGamePaletteSize; j++)
{
uint8_t red = (gPalette[i].red + gPalette[j].red) / 2;
uint8_t green = (gPalette[i].green + gPalette[j].green) / 2;
uint8_t blue = (gPalette[i].blue + gPalette[j].blue) / 2;
auto colour = FindClosestPaletteIndex(red, green, blue);
_blendColourMap[i][j] = colour;
_blendColourMap[j][i] = colour;
}
}
_blendColourMapInitialised = true;
}
BlendColourMapType* GetBlendColourMap()
{
if (!_blendColourMapInitialised)
{
InitBlendColourMap();
}
return &_blendColourMap;
}
PaletteIndex BlendColours(const PaletteIndex paletteIndex1, const PaletteIndex paletteIndex2)
{
if (!_blendColourMapInitialised)
{
InitBlendColourMap();
}
return _blendColourMap[EnumValue(paletteIndex1)][EnumValue(paletteIndex2)];
}
} // namespace OpenRCT2::Drawing
#else
namespace OpenRCT2::Drawing
{
BlendColourMapType* GetBlendColourMap()
{
return nullptr;
}
} // namespace OpenRCT2::Drawing
#endif

View File

@@ -0,0 +1,26 @@
/*****************************************************************************
* Copyright (c) 2014-2026 OpenRCT2 developers
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
*****************************************************************************/
#pragma once
#include "ColourPalette.h"
#include <cstdint>
namespace OpenRCT2::Drawing
{
enum class PaletteIndex : uint8_t;
using BlendColourMapType = std::array<std::array<PaletteIndex, kGamePaletteSize>, kGamePaletteSize>;
BlendColourMapType* GetBlendColourMap();
#ifndef DISABLE_TTF
PaletteIndex BlendColours(PaletteIndex paletteIndex1, PaletteIndex paletteIndex2);
#endif
} // namespace OpenRCT2::Drawing

View File

@@ -738,8 +738,8 @@ void LoadPalette()
auto water_type = OpenRCT2::ObjectManager::GetObjectEntry<WaterObjectEntry>(0);
if (water_type != nullptr)
{
Guard::Assert(water_type->image_id != kImageIndexUndefined, "Failed to load water palette");
palette = water_type->image_id;
Guard::Assert(water_type->mainPalette != kImageIndexUndefined, "Failed to load water palette");
palette = water_type->mainPalette;
}
const auto* g1 = GfxGetG1Palette(palette);
@@ -953,7 +953,7 @@ void UpdatePaletteEffects()
if (water_type != nullptr)
{
palette = water_type->image_id;
palette = water_type->mainPalette;
}
const auto* g1 = GfxGetG1Palette(palette);
if (g1 != nullptr)
@@ -982,7 +982,7 @@ void UpdatePaletteEffects()
if (water_type != nullptr)
{
palette = water_type->image_id;
palette = water_type->mainPalette;
}
const auto* g1 = GfxGetG1Palette(palette);
@@ -1020,7 +1020,7 @@ void UpdatePaletteEffects()
uint32_t waterId = SPR_GAME_PALETTE_WATER;
if (water_type != nullptr)
{
waterId = water_type->palette_index_1;
waterId = water_type->waterWavesPalette;
}
const auto* g1 = GfxGetG1Palette(shade + waterId);
if (g1 != nullptr)
@@ -1044,7 +1044,7 @@ void UpdatePaletteEffects()
waterId = SPR_GAME_PALETTE_3;
if (water_type != nullptr)
{
waterId = water_type->palette_index_2;
waterId = water_type->waterSparklesPalette;
}
g1 = GfxGetG1Palette(shade + waterId);

View File

@@ -19,6 +19,7 @@
#include "../localisation/Formatting.h"
#include "../localisation/LocalisationService.h"
#include "../paint/Paint.h"
#include "BlendColourMap.h"
#include "Drawing.h"
#include "TTF.h"

View File

@@ -18,6 +18,7 @@
#include "../interface/Window.h"
#include "../scenes/intro/IntroScene.h"
#include "../ui/UiContext.h"
#include "BlendColourMap.h"
#include "Drawing.String.h"
#include "Drawing.h"
#include "IDrawingContext.h"

View File

@@ -9,13 +9,9 @@
#include "Colour.h"
#include "../SpriteIds.h"
#include "../core/EnumMap.hpp"
#include "../core/EnumUtils.hpp"
#include "../drawing/Drawing.h"
#include <cmath>
using namespace OpenRCT2::Drawing;
namespace OpenRCT2::Colour
@@ -95,71 +91,3 @@ namespace OpenRCT2::Colour
}
} // namespace OpenRCT2::Colour
#ifndef DISABLE_TTF
static BlendColourMapType BlendColourMap = {};
static bool BlendColourMapInitialised = false;
static PaletteIndex FindClosestPaletteIndex(uint8_t red, uint8_t green, uint8_t blue)
{
PaletteIndex closest = PaletteIndex::pi255;
int32_t closestDistance = INT32_MAX;
for (auto i = PaletteIndex::transparent; i < PaletteIndex::waterWaves0; i = static_cast<PaletteIndex>(EnumValue(i) + 1))
{
const auto& paletteEntry = gPalette[EnumValue(i)];
const int32_t distance = std::pow(paletteEntry.red - red, 2) + std::pow(paletteEntry.green - green, 2)
+ std::pow(paletteEntry.blue - blue, 2);
if (distance < closestDistance)
{
closest = i;
closestDistance = distance;
}
}
return closest;
}
static void InitBlendColourMap()
{
for (size_t i = 0; i < kGamePaletteSize; i++)
{
for (size_t j = i; j < kGamePaletteSize; j++)
{
uint8_t red = (gPalette[i].red + gPalette[j].red) / 2;
uint8_t green = (gPalette[i].green + gPalette[j].green) / 2;
uint8_t blue = (gPalette[i].blue + gPalette[j].blue) / 2;
auto colour = FindClosestPaletteIndex(red, green, blue);
BlendColourMap[i][j] = colour;
BlendColourMap[j][i] = colour;
}
}
BlendColourMapInitialised = true;
}
BlendColourMapType* GetBlendColourMap()
{
if (!BlendColourMapInitialised)
{
InitBlendColourMap();
}
return &BlendColourMap;
}
PaletteIndex BlendColours(const PaletteIndex paletteIndex1, const PaletteIndex paletteIndex2)
{
if (!BlendColourMapInitialised)
{
InitBlendColourMap();
}
return BlendColourMap[EnumValue(paletteIndex1)][EnumValue(paletteIndex2)];
}
#else
BlendColourMapType* GetBlendColourMap()
{
return nullptr;
}
#endif

View File

@@ -97,8 +97,6 @@ enum
COLOUR_DEEP_WATER = 50
};
constexpr size_t kPaletteCount = 256;
constexpr auto kPaletteOffsetDynamic = OpenRCT2::Drawing::PaletteIndex::pi10;
constexpr uint8_t kPaletteLengthDynamic = 236;
@@ -121,11 +119,3 @@ namespace OpenRCT2::Colour
colour_t FromString(std::string_view s, colour_t defaultValue = COLOUR_BLACK);
u8string ToString(colour_t colour);
} // namespace OpenRCT2::Colour
#ifndef DISABLE_TTF
OpenRCT2::Drawing::PaletteIndex BlendColours(
OpenRCT2::Drawing::PaletteIndex paletteIndex1, OpenRCT2::Drawing::PaletteIndex paletteIndex2);
#endif
typedef OpenRCT2::Drawing::PaletteIndex BlendColourMapType[kPaletteCount][kPaletteCount];
BlendColourMapType* GetBlendColourMap();

View File

@@ -242,6 +242,7 @@
<ClInclude Include="core\ZipStream.hpp" />
<ClInclude Include="Date.h" />
<ClInclude Include="Diagnostic.h" />
<ClInclude Include="drawing\BlendColourMap.h" />
<ClInclude Include="drawing\ColourMap.h" />
<ClInclude Include="drawing\ColourPalette.h" />
<ClInclude Include="drawing\Drawing.h" />
@@ -829,6 +830,7 @@
<ClCompile Include="Date.cpp" />
<ClCompile Include="Diagnostic.cpp" />
<ClCompile Include="drawing\AVX2Drawing.cpp" />
<ClCompile Include="drawing\BlendColourMap.cpp" />
<ClCompile Include="drawing\ColourMap.cpp" />
<ClCompile Include="drawing\Drawing.cpp" />
<ClCompile Include="drawing\Drawing.Sprite.BMP.cpp" />

View File

@@ -9,6 +9,7 @@
#pragma once
#include "../drawing/ImageIndexType.h"
#include "../localisation/StringIdType.h"
#include "ObjectTypes.h"
@@ -23,10 +24,10 @@ namespace OpenRCT2
{
static constexpr auto kObjectType = ObjectType::water;
StringId string_idx; // 0x00
uint32_t image_id; // 0x02
uint32_t palette_index_1; // 0x06
uint32_t palette_index_2; // 0x0A
uint16_t flags; // 0x0E
StringId stringId;
ImageIndex mainPalette;
ImageIndex waterWavesPalette;
ImageIndex waterSparklesPalette;
uint16_t flags;
};
} // namespace OpenRCT2

View File

@@ -39,10 +39,10 @@ namespace OpenRCT2
void WaterObject::Load()
{
GetStringTable().Sort();
_legacyType.string_idx = LanguageAllocateObjectString(GetName());
_legacyType.image_id = LoadImages();
_legacyType.palette_index_1 = _legacyType.image_id + 1;
_legacyType.palette_index_2 = _legacyType.image_id + 4;
_legacyType.stringId = LanguageAllocateObjectString(GetName());
_legacyType.mainPalette = LoadImages();
_legacyType.waterWavesPalette = _legacyType.mainPalette + 1;
_legacyType.waterSparklesPalette = _legacyType.mainPalette + 4;
LoadPalette();
}
@@ -50,12 +50,12 @@ namespace OpenRCT2
void WaterObject::Unload()
{
UnloadImages();
LanguageFreeObjectString(_legacyType.string_idx);
LanguageFreeObjectString(_legacyType.stringId);
_legacyType.string_idx = 0;
_legacyType.image_id = 0;
_legacyType.palette_index_1 = 0;
_legacyType.palette_index_2 = 0;
_legacyType.stringId = 0;
_legacyType.mainPalette = 0;
_legacyType.waterWavesPalette = 0;
_legacyType.waterSparklesPalette = 0;
}
void WaterObject::DrawPreview(Drawing::RenderTarget& rt, int32_t width, int32_t height) const