1
0
mirror of https://github.com/OpenRCT2/OpenRCT2 synced 2026-01-18 04:23:20 +01:00

Use std::vector<> instead of malloc/free in CursorRepository.cpp

This commit is contained in:
ZehMatt
2021-07-30 01:44:01 +03:00
parent dac7d76e7a
commit 0f74591f40

View File

@@ -14,6 +14,7 @@
#include <openrct2/config/Config.h>
#include <openrct2/core/Guard.hpp>
#include <openrct2/interface/Cursors.h>
#include <vector>
using namespace OpenRCT2::Ui;
@@ -45,19 +46,19 @@ void CursorRepository::SetCurrentCursor(CursorID cursorId)
}
}
static bool getBit(const uint8_t* data, size_t x, size_t y, size_t width)
static bool getBit(const uint8_t data[], size_t x, size_t y, size_t width) noexcept
{
size_t position = y * width + x;
const size_t position = y * width + x;
return (data[position / 8] & (1 << (7 - (x % 8)))) != 0;
}
static void setBit(uint8_t* data, size_t x, size_t y, size_t width)
static void setBit(uint8_t data[], size_t x, size_t y, size_t width) noexcept
{
size_t position = y * width + x;
data[position / 8] |= (1 << (7 - (position % 8)));
}
static void drawRect(uint8_t* data, size_t x, size_t y, size_t width, size_t scale)
static void drawRect(uint8_t data[], size_t x, size_t y, size_t width, size_t scale) noexcept
{
for (size_t outY = (y * scale); outY < ((1 + y) * scale); outY++)
{
@@ -68,42 +69,39 @@ static void drawRect(uint8_t* data, size_t x, size_t y, size_t width, size_t sca
}
}
static uint8_t* scaleDataArray(const uint8_t data[], size_t width, size_t height, size_t scale)
static std::vector<uint8_t> scaleDataArray(const uint8_t data[], size_t width, size_t height, size_t scale)
{
size_t length = width * height;
auto* ret = static_cast<uint8_t*>(calloc(sizeof(uint8_t), length * scale * scale));
const size_t length = width * height;
std::vector<uint8_t> res;
res.resize(length * scale * scale);
for (size_t y = 0; y < height * 8; y++)
{
for (size_t x = 0; x < width; x++)
{
bool value = getBit(data, x, y, width);
const bool value = getBit(data, x, y, width);
if (!value)
continue;
drawRect(ret, x, y, width, scale);
drawRect(res.data(), x, y, width, scale);
}
}
return ret;
return res;
}
SDL_Cursor* CursorRepository::Create(const CursorData* cursorInfo, uint8_t scale)
{
SDL_Cursor* cursor;
auto integer_scale = static_cast<int>(round(scale));
const auto integer_scale = static_cast<int>(round(scale));
auto data = scaleDataArray(cursorInfo->Data, CURSOR_BIT_WIDTH, CURSOR_HEIGHT, static_cast<size_t>(integer_scale));
auto mask = scaleDataArray(cursorInfo->Mask, CURSOR_BIT_WIDTH, CURSOR_HEIGHT, static_cast<size_t>(integer_scale));
cursor = SDL_CreateCursor(
data, mask, BASE_CURSOR_WIDTH * integer_scale, BASE_CURSOR_HEIGHT * integer_scale,
auto* cursor = SDL_CreateCursor(
data.data(), mask.data(), BASE_CURSOR_WIDTH * integer_scale, BASE_CURSOR_HEIGHT * integer_scale,
cursorInfo->HotSpot.X * integer_scale, cursorInfo->HotSpot.Y * integer_scale);
free(data);
free(mask);
return cursor;
}