diff --git a/src/openrct2-ui/drawing/engines/opengl/TextureCache.cpp b/src/openrct2-ui/drawing/engines/opengl/TextureCache.cpp index 57c220001b..2fb3f82887 100644 --- a/src/openrct2-ui/drawing/engines/opengl/TextureCache.cpp +++ b/src/openrct2-ui/drawing/engines/opengl/TextureCache.cpp @@ -21,6 +21,7 @@ #include using namespace OpenRCT2::Ui; +using namespace OpenRCT2::Drawing; constexpr uint32_t kUnusedIndex = 0xFFFFFFFF; @@ -206,7 +207,7 @@ void TextureCache::CreateTextures() glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D( - GL_TEXTURE_2D, 0, GL_R8UI, PALETTE_SIZE, PALETTE_SIZE, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, blendArray); + GL_TEXTURE_2D, 0, GL_R8UI, kGamePaletteSize, kGamePaletteSize, 0, GL_RED_INTEGER, GL_UNSIGNED_BYTE, blendArray); } _initialized = true; diff --git a/src/openrct2/CommandLineSprite.cpp b/src/openrct2/CommandLineSprite.cpp index 233e21b883..d65583e0ef 100644 --- a/src/openrct2/CommandLineSprite.cpp +++ b/src/openrct2/CommandLineSprite.cpp @@ -214,7 +214,7 @@ static bool SpriteImageExport(const G1Element& spriteElement, u8string_view outP image.Height = dpi.height; image.Depth = 8; image.Stride = dpi.LineStride(); - image.Palette = std::make_unique(StandardPalette); + image.Palette = StandardPalette; image.Pixels = std::vector(pixels8, pixels8 + pixelsLen); Imaging::WriteToFile(outPath, image, IMAGE_FORMAT::PNG); return true; diff --git a/src/openrct2/core/Imaging.cpp b/src/openrct2/core/Imaging.cpp index 4b2b5f0df6..363a6baac3 100644 --- a/src/openrct2/core/Imaging.cpp +++ b/src/openrct2/core/Imaging.cpp @@ -189,7 +189,7 @@ namespace OpenRCT2::Imaging if (image.Depth == 8) { - if (image.Palette == nullptr) + if (!image.Palette.has_value()) { throw std::runtime_error("Expected a palette for 8-bit image."); } diff --git a/src/openrct2/core/Imaging.h b/src/openrct2/core/Imaging.h index 854c7698c9..6515bcda01 100644 --- a/src/openrct2/core/Imaging.h +++ b/src/openrct2/core/Imaging.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -37,7 +38,7 @@ struct Image // Data std::vector Pixels; - std::unique_ptr Palette; + std::optional Palette; uint32_t Stride{}; }; diff --git a/src/openrct2/drawing/ColourPalette.h b/src/openrct2/drawing/ColourPalette.h new file mode 100644 index 0000000000..ee15a1b63c --- /dev/null +++ b/src/openrct2/drawing/ColourPalette.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +namespace OpenRCT2::Drawing +{ + + struct PaletteBGRA + { + uint8_t Blue{}; + uint8_t Green{}; + uint8_t Red{}; + uint8_t Alpha{}; + }; + + constexpr auto kGamePaletteSize = 256u; + + using GamePalette = std::array; + +} // namespace OpenRCT2::Drawing diff --git a/src/openrct2/drawing/Drawing.cpp b/src/openrct2/drawing/Drawing.cpp index f59f4946dc..6aea394594 100644 --- a/src/openrct2/drawing/Drawing.cpp +++ b/src/openrct2/drawing/Drawing.cpp @@ -95,7 +95,7 @@ void PaletteMap::Copy(size_t dstIndex, const PaletteMap& src, size_t srcIndex, s std::memcpy(&_data[dstIndex], &src._data[srcIndex], copyLength); } -GamePalette gPalette; +OpenRCT2::Drawing::GamePalette gPalette; uint8_t gGamePalette[256 * 4]; uint32_t gPaletteEffectFrame; @@ -1161,4 +1161,4 @@ void DebugDPI(DrawPixelInfo& dpi) const auto str2 = std::to_string(dpi.y); DrawText(dpi, ScreenCoordsXY{ dpi.x, dpi.y + 6 }, { COLOUR_WHITE, FontStyle::Tiny }, str2.c_str()); -} \ No newline at end of file +} diff --git a/src/openrct2/drawing/Drawing.h b/src/openrct2/drawing/Drawing.h index 5a69037d41..530c876520 100644 --- a/src/openrct2/drawing/Drawing.h +++ b/src/openrct2/drawing/Drawing.h @@ -14,10 +14,12 @@ #include "../interface/Colour.h" #include "../interface/ZoomLevel.h" #include "../world/Location.hpp" +#include "ColourPalette.h" #include "Font.h" #include "ImageId.hpp" #include "Text.h" +#include #include #include #include @@ -26,6 +28,7 @@ struct ScreenCoordsXY; struct ScreenLine; struct ScreenRect; + namespace OpenRCT2 { struct IPlatformEnvironment; @@ -37,47 +40,6 @@ namespace OpenRCT2::Drawing struct IDrawingEngine; } -struct PaletteBGRA -{ - uint8_t Blue{}; - uint8_t Green{}; - uint8_t Red{}; - uint8_t Alpha{}; -}; - -constexpr auto PALETTE_SIZE = 256u; - -struct GamePalette -{ - PaletteBGRA Colour[PALETTE_SIZE]{}; - - PaletteBGRA& operator[](size_t idx) - { - assert(idx < PALETTE_SIZE); - if (idx >= PALETTE_SIZE) - { - static PaletteBGRA dummy; - return dummy; - } - - return Colour[idx]; - } - - const PaletteBGRA operator[](size_t idx) const - { - assert(idx < PALETTE_SIZE); - if (idx >= PALETTE_SIZE) - return {}; - - return Colour[idx]; - } - - explicit operator uint8_t*() - { - return reinterpret_cast(Colour); - } -}; - struct G1Element { uint8_t* offset = nullptr; // 0x00 @@ -528,7 +490,7 @@ constexpr uint8_t kPaletteTotalOffsets = 192; constexpr int8_t kMaxScrollingTextModes = 38; -extern GamePalette gPalette; +extern OpenRCT2::Drawing::GamePalette gPalette; extern uint8_t gGamePalette[256 * 4]; extern uint32_t gPaletteEffectFrame; diff --git a/src/openrct2/drawing/IDrawingEngine.h b/src/openrct2/drawing/IDrawingEngine.h index 3f8751af56..20d8d0d288 100644 --- a/src/openrct2/drawing/IDrawingEngine.h +++ b/src/openrct2/drawing/IDrawingEngine.h @@ -10,6 +10,7 @@ #pragma once #include "./Weather.h" +#include "ColourPalette.h" #include #include @@ -39,7 +40,6 @@ enum DRAWING_ENGINE_FLAGS }; struct DrawPixelInfo; -struct GamePalette; namespace OpenRCT2::Ui { diff --git a/src/openrct2/drawing/ImageImporter.cpp b/src/openrct2/drawing/ImageImporter.cpp index e4573630e3..a71fbeefa3 100644 --- a/src/openrct2/drawing/ImageImporter.cpp +++ b/src/openrct2/drawing/ImageImporter.cpp @@ -306,7 +306,7 @@ namespace OpenRCT2::Drawing { if (!IsTransparentPixel(colour)) { - for (uint32_t i = 0; i < PALETTE_SIZE; i++) + for (uint32_t i = 0; i < kGamePaletteSize; i++) { if (static_cast(palette[i].Red) == colour[0] && static_cast(palette[i].Green) == colour[1] && static_cast(palette[i].Blue) == colour[2]) @@ -364,7 +364,7 @@ namespace OpenRCT2::Drawing { auto smallestError = static_cast(-1); auto bestMatch = PALETTE_TRANSPARENT; - for (uint32_t x = 0; x < PALETTE_SIZE; x++) + for (uint32_t x = 0; x < kGamePaletteSize; x++) { if (IsChangablePixel(x)) { diff --git a/src/openrct2/drawing/ImageImporter.h b/src/openrct2/drawing/ImageImporter.h index 20fefbb07c..b57ca39120 100644 --- a/src/openrct2/drawing/ImageImporter.h +++ b/src/openrct2/drawing/ImageImporter.h @@ -93,7 +93,7 @@ namespace OpenRCT2::Drawing ImageImportMeta createImageImportMetaFromJson(json_t& input); } // namespace OpenRCT2::Drawing -constexpr GamePalette StandardPalette = { { +constexpr OpenRCT2::Drawing::GamePalette StandardPalette = { { // 0 (Unused/Transparent) { 0, 0, 0, 255 }, diff --git a/src/openrct2/drawing/LightFX.cpp b/src/openrct2/drawing/LightFX.cpp index b1ee8e3cac..ae5fe1d6de 100644 --- a/src/openrct2/drawing/LightFX.cpp +++ b/src/openrct2/drawing/LightFX.cpp @@ -29,6 +29,7 @@ #include using namespace OpenRCT2; +using namespace OpenRCT2::Drawing; static uint8_t _bakedLightTexture_lantern_0[32 * 32]; static uint8_t _bakedLightTexture_lantern_1[64 * 64]; diff --git a/src/openrct2/drawing/LightFX.h b/src/openrct2/drawing/LightFX.h index 1c869f59ff..1d42ff4729 100644 --- a/src/openrct2/drawing/LightFX.h +++ b/src/openrct2/drawing/LightFX.h @@ -9,12 +9,13 @@ #pragma once +#include "ColourPalette.h" + #include struct CoordsXY; struct Vehicle; struct DrawPixelInfo; -struct GamePalette; struct CoordsXYZ; struct EntityBase; @@ -56,7 +57,7 @@ void LightFXRenderLightsToFrontBuffer(); void LightFXUpdateViewportSettings(); void* LightFXGetFrontBuffer(); -const GamePalette& LightFXGetPalette(); +const OpenRCT2::Drawing::GamePalette& LightFXGetPalette(); void LightFXAdd3DLight(const EntityBase& entity, const uint8_t id, const CoordsXYZ& loc, const LightType lightType); diff --git a/src/openrct2/drawing/NewDrawing.h b/src/openrct2/drawing/NewDrawing.h index cd71c7b3bf..4b693f3ef0 100644 --- a/src/openrct2/drawing/NewDrawing.h +++ b/src/openrct2/drawing/NewDrawing.h @@ -10,9 +10,10 @@ #pragma once #include "../localisation/StringIdType.h" +#include "ColourPalette.h" struct DrawPixelInfo; -struct GamePalette; +struct PaletteBGRA; enum class DrawingEngine : int32_t; extern StringId DrawingEngineStringIds[3]; @@ -21,7 +22,7 @@ DrawingEngine drawing_engine_get_type(); bool DrawingEngineRequiresNewWindow(DrawingEngine srcEngine, DrawingEngine dstEngine); void DrawingEngineInit(); void DrawingEngineResize(); -void DrawingEngineSetPalette(const GamePalette& colours); +void DrawingEngineSetPalette(const OpenRCT2::Drawing::GamePalette& colours); void DrawingEngineCopyRect(int32_t x, int32_t y, int32_t width, int32_t height, int32_t dx, int32_t dy); void DrawingEngineDispose(); diff --git a/src/openrct2/interface/Colour.cpp b/src/openrct2/interface/Colour.cpp index 2060010cba..0c6e9ec027 100644 --- a/src/openrct2/interface/Colour.cpp +++ b/src/openrct2/interface/Colour.cpp @@ -16,6 +16,8 @@ #include +using namespace OpenRCT2::Drawing; + ColourShadeMap ColourMapA[COLOUR_COUNT] = {}; static constexpr uint8_t kLegacyColourMaskBase = 0x1F; @@ -169,9 +171,9 @@ static uint8_t FindClosestPaletteIndex(uint8_t red, uint8_t green, uint8_t blue) static void InitBlendColourMap() { - for (size_t i = 0; i < PALETTE_SIZE; i++) + for (size_t i = 0; i < kGamePaletteSize; i++) { - for (size_t j = i; j < PALETTE_SIZE; j++) + 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; diff --git a/src/openrct2/interface/Screenshot.cpp b/src/openrct2/interface/Screenshot.cpp index da464d4079..583f6d007c 100644 --- a/src/openrct2/interface/Screenshot.cpp +++ b/src/openrct2/interface/Screenshot.cpp @@ -63,7 +63,7 @@ static bool WriteDpiToFile(std::string_view path, const DrawPixelInfo& dpi, cons image.Height = dpi.height; image.Depth = 8; image.Stride = dpi.LineStride(); - image.Palette = std::make_unique(palette); + image.Palette = palette; image.Pixels = std::vector(pixels8, pixels8 + pixelsLen); Imaging::WriteToFile(path, image, IMAGE_FORMAT::PNG); return true; diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 86d0c493c7..d45b938b9d 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -237,6 +237,7 @@ +