// SPDX-License-Identifier: GPL-3.0-only #pragma once #include #include #include #include #include #include namespace bmh::app { // SDL owns pointer position; the UI supplies only the original bitmap/colors. class PitchPointer { public: ~PitchPointer() { clear(); } void clear() { if (cursor_) { SDL_SetCursor(SDL_GetDefaultCursor()); SDL_FreeCursor(cursor_); cursor_ = nullptr; } pixels_.reset(); } void show(const std::optional>& pixels, SDL_Renderer* renderer) { if (!pixels) { SDL_SetCursor(SDL_GetDefaultCursor()); return; } int width = 320, height = 240; SDL_GetRendererOutputSize(renderer, &width, &height); const int scale = std::max(1, std::min(width / 320, height / 240)); if (!cursor_ || pixels != pixels_ || scale != scale_) { std::vector enlarged(size_t(11 * scale) * size_t(11 * scale)); for (int y = 0; y < 11 * scale; ++y) for (int x = 0; x < 11 * scale; ++x) enlarged[size_t(y) * size_t(11 * scale) + size_t(x)] = pixels->at(size_t(y / scale) * 11 + size_t(x / scale)); SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(enlarged.data(), 11 * scale, 11 * scale, 32, 11 * scale * 4, SDL_PIXELFORMAT_ARGB8888); SDL_Cursor* next = surface ? SDL_CreateColorCursor(surface, 0, 0) : nullptr; if (surface) SDL_FreeSurface(surface); if (!next) return; // A headless SDL driver may not support host cursors. clear(); cursor_ = next; pixels_ = pixels; scale_ = scale; } SDL_SetCursor(cursor_); } private: SDL_Cursor* cursor_ = nullptr; std::optional> pixels_; int scale_ = 1; }; } // namespace bmh::app