mirror of
https://github.com/OpenRCT2/OpenRCT2
synced 2025-12-10 09:32:29 +01:00
Refactor PaletteMap, this is just a view of data
This commit is contained in:
@@ -25,56 +25,31 @@
|
||||
#include "../world/Location.hpp"
|
||||
#include "LightFX.h"
|
||||
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstring>
|
||||
#include <numeric>
|
||||
|
||||
using namespace OpenRCT2;
|
||||
|
||||
const PaletteMap& PaletteMap::GetDefault()
|
||||
{
|
||||
static bool initialised = false;
|
||||
static uint8_t data[256];
|
||||
static PaletteMap defaultMap(data);
|
||||
if (!initialised)
|
||||
{
|
||||
for (size_t i = 0; i < sizeof(data); i++)
|
||||
{
|
||||
data[i] = static_cast<uint8_t>(i);
|
||||
}
|
||||
initialised = true;
|
||||
}
|
||||
return defaultMap;
|
||||
}
|
||||
static auto _defaultPaletteMapping = []() {
|
||||
std::array<uint8_t, 256> res;
|
||||
std::iota(res.begin(), res.end(), 0);
|
||||
return res;
|
||||
}();
|
||||
|
||||
bool PaletteMap::operator==(const PaletteMap& lhs) const
|
||||
PaletteMap PaletteMap::GetDefault()
|
||||
{
|
||||
return _data == lhs._data && _dataLength == lhs._dataLength && _numMaps == lhs._numMaps && _mapLength == lhs._mapLength;
|
||||
return PaletteMap(_defaultPaletteMapping);
|
||||
}
|
||||
|
||||
uint8_t& PaletteMap::operator[](size_t index)
|
||||
{
|
||||
assert(index < _dataLength);
|
||||
|
||||
// Provide safety in release builds
|
||||
if (index >= _dataLength)
|
||||
{
|
||||
static uint8_t dummy;
|
||||
return dummy;
|
||||
}
|
||||
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
uint8_t PaletteMap::operator[](size_t index) const
|
||||
{
|
||||
assert(index < _dataLength);
|
||||
|
||||
// Provide safety in release builds
|
||||
if (index >= _dataLength)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return _data[index];
|
||||
}
|
||||
|
||||
@@ -84,15 +59,15 @@ uint8_t PaletteMap::Blend(uint8_t src, uint8_t dst) const
|
||||
assert(src != 0 && (src - 1) < _numMaps);
|
||||
assert(dst < _mapLength);
|
||||
auto idx = ((src - 1) * 256) + dst;
|
||||
return (*this)[idx];
|
||||
return _data[idx];
|
||||
}
|
||||
|
||||
void PaletteMap::Copy(size_t dstIndex, const PaletteMap& src, size_t srcIndex, size_t length)
|
||||
{
|
||||
auto maxLength = std::min(_mapLength - srcIndex, _mapLength - dstIndex);
|
||||
auto maxLength = std::min(_data.size() - srcIndex, _data.size() - dstIndex);
|
||||
assert(length <= maxLength);
|
||||
auto copyLength = std::min(length, maxLength);
|
||||
std::memcpy(&_data[dstIndex], &src._data[srcIndex], copyLength);
|
||||
std::copy(src._data.begin() + srcIndex, src._data.begin() + srcIndex + copyLength, _data.begin() + dstIndex);
|
||||
}
|
||||
|
||||
OpenRCT2::Drawing::GamePalette gPalette;
|
||||
|
||||
@@ -351,39 +351,39 @@ struct TranslucentWindowPalette
|
||||
struct PaletteMap
|
||||
{
|
||||
private:
|
||||
uint8_t* _data{};
|
||||
uint32_t _dataLength{};
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wunused-private-field"
|
||||
uint16_t _numMaps;
|
||||
#pragma clang diagnostic pop
|
||||
uint16_t _mapLength;
|
||||
std::span<uint8_t> _data{};
|
||||
#ifdef _DEBUG
|
||||
// We only require those fields for the asserts in debug builds.
|
||||
size_t _numMaps{};
|
||||
size_t _mapLength{};
|
||||
#endif
|
||||
|
||||
public:
|
||||
static const PaletteMap& GetDefault();
|
||||
static PaletteMap GetDefault();
|
||||
|
||||
PaletteMap() = default;
|
||||
constexpr PaletteMap() = default;
|
||||
|
||||
PaletteMap(uint8_t* data, uint16_t numMaps, uint16_t mapLength)
|
||||
: _data(data)
|
||||
, _dataLength(numMaps * mapLength)
|
||||
constexpr PaletteMap(uint8_t* data, size_t numMaps, size_t mapLength)
|
||||
: _data{ data, numMaps * mapLength }
|
||||
#ifdef _DEBUG
|
||||
, _numMaps(numMaps)
|
||||
, _mapLength(mapLength)
|
||||
#endif
|
||||
{
|
||||
}
|
||||
|
||||
template<std::size_t TSize>
|
||||
PaletteMap(uint8_t (&map)[TSize])
|
||||
constexpr PaletteMap(std::span<uint8_t> map)
|
||||
: _data(map)
|
||||
, _dataLength(static_cast<uint32_t>(std::size(map)))
|
||||
#ifdef _DEBUG
|
||||
, _numMaps(1)
|
||||
, _mapLength(static_cast<uint16_t>(std::size(map)))
|
||||
, _mapLength(map.size())
|
||||
#endif
|
||||
{
|
||||
}
|
||||
|
||||
bool operator==(const PaletteMap& lhs) const;
|
||||
uint8_t& operator[](size_t index);
|
||||
uint8_t operator[](size_t index) const;
|
||||
|
||||
uint8_t Blend(uint8_t src, uint8_t dst) const;
|
||||
void Copy(size_t dstIndex, const PaletteMap& src, size_t srcIndex, size_t length);
|
||||
};
|
||||
@@ -391,7 +391,7 @@ public:
|
||||
struct DrawSpriteArgs
|
||||
{
|
||||
ImageId Image;
|
||||
const PaletteMap& PalMap;
|
||||
PaletteMap PalMap;
|
||||
const G1Element& SourceImage;
|
||||
int32_t SrcX;
|
||||
int32_t SrcY;
|
||||
|
||||
Reference in New Issue
Block a user