diff --git a/src/openrct2-ui/CursorRepository.cpp b/src/openrct2-ui/CursorRepository.cpp index 6fab01a596..4c703a1089 100644 --- a/src/openrct2-ui/CursorRepository.cpp +++ b/src/openrct2-ui/CursorRepository.cpp @@ -14,6 +14,7 @@ #include #include #include +#include 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 scaleDataArray(const uint8_t data[], size_t width, size_t height, size_t scale) { - size_t length = width * height; - auto* ret = static_cast(calloc(sizeof(uint8_t), length * scale * scale)); + const size_t length = width * height; + + std::vector 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(round(scale)); + const auto integer_scale = static_cast(round(scale)); auto data = scaleDataArray(cursorInfo->Data, CURSOR_BIT_WIDTH, CURSOR_HEIGHT, static_cast(integer_scale)); auto mask = scaleDataArray(cursorInfo->Mask, CURSOR_BIT_WIDTH, CURSOR_HEIGHT, static_cast(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; }