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