Files
OpenRCT2-Web/src/openrct2/drawing/X8DrawingEngine.cpp
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

693 lines
18 KiB
C++
Raw Normal View History

2017-06-08 21:56:39 +01:00
/*****************************************************************************
2026-01-05 14:42:59 +01:00
* Copyright (c) 2014-2026 OpenRCT2 developers
2017-06-08 21:56:39 +01:00
*
2018-06-15 14:07:34 +02:00
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2017-06-08 21:56:39 +01:00
*
2018-06-15 14:07:34 +02:00
* OpenRCT2 is licensed under the GNU General Public License version 3.
2017-06-08 21:56:39 +01:00
*****************************************************************************/
#include "X8DrawingEngine.h"
2018-06-22 22:59:03 +02:00
#include "../config/Config.h"
#include "../core/Guard.hpp"
#include "../core/Numerics.hpp"
2026-07-21 16:00:55 +01:00
#include "../interface/ColourWithFlags.h"
2018-06-22 22:59:03 +02:00
#include "../interface/Screenshot.h"
2018-01-06 00:05:16 +00:00
#include "../interface/Window.h"
2024-04-26 20:35:16 +02:00
#include "../scenes/intro/IntroScene.h"
2026-01-18 22:16:00 +01:00
#include "BlendColourMap.h"
#include "Drawing.Screen.h"
2026-07-21 16:00:55 +01:00
#include "Drawing.Sprite.h"
2025-10-16 20:44:13 +02:00
#include "Drawing.String.h"
2018-01-05 21:17:33 +00:00
#include "Drawing.h"
2018-06-22 22:59:03 +02:00
#include "IDrawingEngine.h"
2025-04-05 02:32:41 +03:00
#include "InvalidationGrid.h"
2018-01-05 21:11:19 +00:00
#include "LightFX.h"
2026-09-06 19:50:48 +02:00
#include "Line.h"
2026-07-21 16:00:55 +01:00
#include "TTF.h"
#include "WeatherDrawer.h"
2018-06-22 22:59:03 +02:00
#include <algorithm>
2024-07-09 16:43:03 +02:00
#include <cassert>
2018-06-22 22:59:03 +02:00
#include <cstring>
2017-06-08 21:56:39 +01:00
using namespace OpenRCT2;
using namespace OpenRCT2::Drawing;
using namespace OpenRCT2::Ui;
2020-09-14 21:36:30 +02:00
X8WeatherDrawer::X8WeatherDrawer()
2017-06-08 21:56:39 +01:00
{
2020-09-14 21:36:30 +02:00
_weatherPixels = new WeatherPixel[_weatherPixelsCapacity];
2017-06-08 21:56:39 +01:00
}
2020-09-14 21:36:30 +02:00
X8WeatherDrawer::~X8WeatherDrawer()
2017-06-08 21:56:39 +01:00
{
2020-09-14 21:36:30 +02:00
delete[] _weatherPixels;
2017-06-08 21:56:39 +01:00
}
2020-02-20 22:57:04 +00:00
void X8WeatherDrawer::Draw(
RenderTarget& rt, int32_t x, int32_t y, int32_t width, int32_t height, int32_t xStart, int32_t yStart,
const uint8_t* weatherpattern)
2017-06-08 21:56:39 +01:00
{
2020-02-20 22:57:04 +00:00
const uint8_t* pattern = weatherpattern;
auto patternXSpace = *pattern++;
auto patternYSpace = *pattern++;
2017-06-08 21:56:39 +01:00
uint8_t patternStartXOffset = xStart % patternXSpace;
uint8_t patternStartYOffset = yStart % patternYSpace;
2017-06-08 21:56:39 +01:00
uint32_t pixelOffset = rt.LineStride() * y + x;
uint8_t patternYPos = patternStartYOffset % patternYSpace;
2017-06-08 21:56:39 +01:00
2026-02-07 23:41:40 +01:00
PaletteIndex* screenBits = rt.bits;
2017-06-08 21:56:39 +01:00
2018-06-22 22:59:03 +02:00
// Stores the colours of changed pixels
2020-09-14 21:36:30 +02:00
WeatherPixel* newPixels = &_weatherPixels[_weatherPixelsCount];
2017-06-08 21:56:39 +01:00
for (; height != 0; height--)
{
auto patternX = pattern[patternYPos * 2];
2017-06-08 21:56:39 +01:00
if (patternX != 0xFF)
{
2020-09-14 21:36:30 +02:00
if (_weatherPixelsCount < (_weatherPixelsCapacity - static_cast<uint32_t>(width)))
2017-06-08 21:56:39 +01:00
{
uint32_t finalPixelOffset = width + pixelOffset;
2017-06-08 21:56:39 +01:00
uint32_t xPixelOffset = pixelOffset;
2020-04-22 17:09:29 +02:00
xPixelOffset += (static_cast<uint8_t>(patternX - patternStartXOffset)) % patternXSpace;
2017-06-08 21:56:39 +01:00
auto patternPixel = pattern[patternYPos * 2 + 1];
2017-06-08 21:56:39 +01:00
for (; xPixelOffset < finalPixelOffset; xPixelOffset += patternXSpace)
{
2026-02-07 23:41:40 +01:00
PaletteIndex current_pixel = screenBits[xPixelOffset];
screenBits[xPixelOffset] = static_cast<PaletteIndex>(patternPixel);
2020-09-14 21:36:30 +02:00
_weatherPixelsCount++;
2017-06-08 21:56:39 +01:00
// Store colour and position
*newPixels++ = { xPixelOffset, current_pixel };
}
}
}
pixelOffset += rt.LineStride();
2017-06-08 21:56:39 +01:00
patternYPos++;
patternYPos %= patternYSpace;
}
}
void X8WeatherDrawer::Restore(RenderTarget& rt)
2017-06-08 21:56:39 +01:00
{
2020-09-14 21:36:30 +02:00
if (_weatherPixelsCount > 0)
2017-06-08 21:56:39 +01:00
{
uint32_t numPixels = rt.LineStride() * rt.height;
2026-02-07 23:41:40 +01:00
PaletteIndex* bits = rt.bits;
2020-09-14 21:36:30 +02:00
for (uint32_t i = 0; i < _weatherPixelsCount; i++)
2017-06-08 21:56:39 +01:00
{
2020-02-20 22:57:04 +00:00
WeatherPixel weatherPixel = _weatherPixels[i];
if (weatherPixel.Position >= numPixels)
2017-06-08 21:56:39 +01:00
{
// Pixel out of bounds, bail
break;
}
2020-02-20 22:57:04 +00:00
bits[weatherPixel.Position] = weatherPixel.Colour;
2017-06-08 21:56:39 +01:00
}
2020-09-14 21:36:30 +02:00
_weatherPixelsCount = 0;
2017-06-08 21:56:39 +01:00
}
}
#ifdef __WARN_SUGGEST_FINAL_METHODS__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wsuggest-final-methods"
#endif
2017-06-09 21:02:24 +01:00
X8DrawingEngine::X8DrawingEngine([[maybe_unused]] IUiContext& uiContext)
2017-06-08 21:56:39 +01:00
{
_drawingContext = new X8DrawingContext(this);
_mainRT.DrawingEngine = this;
LightFx::SetAvailable(true);
2025-10-07 20:14:37 +02:00
_lastLightFXenabled = Config::Get().general.enableLightFx;
2017-06-08 21:56:39 +01:00
}
X8DrawingEngine::~X8DrawingEngine()
{
delete _drawingContext;
2018-06-22 22:59:03 +02:00
delete[] _bits;
2017-06-08 21:56:39 +01:00
}
void X8DrawingEngine::Initialise()
2017-06-08 21:56:39 +01:00
{
}
void X8DrawingEngine::Resize(uint32_t width, uint32_t height)
2017-06-08 21:56:39 +01:00
{
uint32_t pitch = width;
2017-06-08 21:56:39 +01:00
ConfigureBits(width, height, pitch);
_drawingContext->BeginDraw();
_drawingContext->Clear(_mainRT, PaletteIndex::pi10);
_drawingContext->EndDraw();
2017-06-08 21:56:39 +01:00
}
2020-05-27 20:12:01 +01:00
void X8DrawingEngine::SetPalette([[maybe_unused]] const GamePalette& palette)
2017-06-08 21:56:39 +01:00
{
}
void X8DrawingEngine::SetVSync([[maybe_unused]] bool vsync)
2017-06-08 21:56:39 +01:00
{
// Not applicable for this engine
}
void X8DrawingEngine::Invalidate(int32_t left, int32_t top, int32_t right, int32_t bottom)
2017-06-08 21:56:39 +01:00
{
2025-04-05 02:32:41 +03:00
_invalidationGrid.invalidate(left, top, right, bottom);
2017-06-08 21:56:39 +01:00
}
2017-07-15 19:47:38 +01:00
void X8DrawingEngine::BeginDraw()
2017-06-08 21:56:39 +01:00
{
2024-04-26 20:35:16 +02:00
if (!IntroIsPlaying())
2017-06-08 21:56:39 +01:00
{
2017-07-15 19:47:38 +01:00
// HACK we need to re-configure the bits if light fx has been enabled / disabled
2025-10-07 20:14:37 +02:00
if (_lastLightFXenabled != Config::Get().general.enableLightFx)
2017-07-15 19:47:38 +01:00
{
Resize(_width, _height);
GfxInvalidateScreen();
2025-10-07 20:14:37 +02:00
_lastLightFXenabled = Config::Get().general.enableLightFx;
2017-07-15 19:47:38 +01:00
}
_weatherDrawer.Restore(_mainRT);
2017-06-08 21:56:39 +01:00
}
_drawingContext->BeginDraw();
2017-06-08 21:56:39 +01:00
}
2017-07-15 19:47:38 +01:00
void X8DrawingEngine::EndDraw()
{
_drawingContext->EndDraw();
2017-07-15 19:47:38 +01:00
}
void X8DrawingEngine::PaintWindows()
{
2025-09-29 13:47:48 +03:00
if (gPaintForceRedraw)
{
WindowUpdateAllViewports();
WindowDrawAll(_mainRT, 0, 0, _width, _height);
}
else
{
// Redraw dirty regions before updating the viewports, otherwise
// when viewports get panned, they copy dirty pixels
DrawAllDirtyBlocks();
WindowUpdateAllViewports();
DrawAllDirtyBlocks();
}
}
2017-07-15 19:47:38 +01:00
2020-09-14 21:36:30 +02:00
void X8DrawingEngine::PaintWeather()
2017-07-15 19:47:38 +01:00
{
DrawWeather(_mainRT, &_weatherDrawer);
2017-07-15 19:47:38 +01:00
}
void X8DrawingEngine::CopyRect(int32_t x, int32_t y, int32_t width, int32_t height, int32_t dx, int32_t dy)
2017-06-08 21:56:39 +01:00
{
2018-06-22 22:59:03 +02:00
if (dx == 0 && dy == 0)
return;
2017-06-08 21:56:39 +01:00
// Originally 0x00683359
// Adjust for move off screen
// NOTE: when zooming, there can be x, y, dx, dy combinations that go off the
// screen; hence the checks. This code should ultimately not be called when
// zooming because this function is specific to updating the screen on move
int32_t lmargin = std::min(x - dx, 0);
2020-04-22 17:09:29 +02:00
int32_t rmargin = std::min(static_cast<int32_t>(_width) - (x - dx + width), 0);
int32_t tmargin = std::min(y - dy, 0);
2020-04-22 17:09:29 +02:00
int32_t bmargin = std::min(static_cast<int32_t>(_height) - (y - dy + height), 0);
2017-06-08 21:56:39 +01:00
x -= lmargin;
y -= tmargin;
width += lmargin + rmargin;
height += tmargin + bmargin;
int32_t stride = _mainRT.LineStride();
2026-02-07 23:41:40 +01:00
PaletteIndex* to = _mainRT.bits + y * stride + x;
PaletteIndex* from = _mainRT.bits + (y - dy) * stride + x - dx;
2017-06-08 21:56:39 +01:00
if (dy > 0)
{
// If positive dy, reverse directions
to += (height - 1) * stride;
from += (height - 1) * stride;
stride = -stride;
}
// Move bytes
for (int32_t i = 0; i < height; i++)
2017-06-08 21:56:39 +01:00
{
memmove(to, from, width);
to += stride;
from += stride;
}
}
2019-01-31 23:15:05 +01:00
std::string X8DrawingEngine::Screenshot()
2017-06-08 21:56:39 +01:00
{
return ScreenshotDumpPNG(_mainRT);
2017-06-08 21:56:39 +01:00
}
IDrawingContext* X8DrawingEngine::GetDrawingContext()
2017-06-08 21:56:39 +01:00
{
if (!_drawingContext->IsActive())
{
Guard::Fail("Drawing context is not active.");
return nullptr;
}
2017-06-08 21:56:39 +01:00
return _drawingContext;
}
DrawingEngineFlags X8DrawingEngine::GetFlags()
2017-06-08 21:56:39 +01:00
{
return { DrawingEngineFlag::dirtyOptimisations, DrawingEngineFlag::parallelDrawing };
2017-06-08 21:56:39 +01:00
}
void X8DrawingEngine::InvalidateImage([[maybe_unused]] uint32_t image)
2017-06-08 21:56:39 +01:00
{
// Not applicable for this engine
}
RenderTarget* X8DrawingEngine::getRT()
2017-06-08 21:56:39 +01:00
{
return &_mainRT;
2017-06-08 21:56:39 +01:00
}
void X8DrawingEngine::ConfigureBits(uint32_t width, uint32_t height, uint32_t pitch)
2017-06-08 21:56:39 +01:00
{
2018-06-22 22:59:03 +02:00
size_t newBitsSize = pitch * height;
2026-02-07 23:41:40 +01:00
PaletteIndex* newBits = new PaletteIndex[newBitsSize];
2017-06-08 21:56:39 +01:00
if (_bits == nullptr)
{
2026-02-07 23:41:40 +01:00
std::fill_n(newBits, newBitsSize, PaletteIndex::transparent);
2017-06-08 21:56:39 +01:00
}
else
{
if (_pitch == pitch)
{
2018-02-01 12:52:39 +00:00
std::copy_n(_bits, std::min(_bitsSize, newBitsSize), newBits);
2017-06-08 21:56:39 +01:00
}
else
{
2026-02-07 23:41:40 +01:00
PaletteIndex* src = _bits;
PaletteIndex* dst = newBits;
2017-06-08 21:56:39 +01:00
uint32_t minWidth = std::min(_width, width);
uint32_t minHeight = std::min(_height, height);
for (uint32_t y = 0; y < minHeight; y++)
2017-06-08 21:56:39 +01:00
{
2018-02-01 12:52:39 +00:00
std::copy_n(src, minWidth, dst);
2017-06-08 21:56:39 +01:00
if (pitch - minWidth > 0)
{
2026-02-07 23:41:40 +01:00
std::fill_n(dst + minWidth, pitch - minWidth, PaletteIndex::transparent);
2017-06-08 21:56:39 +01:00
}
src += _pitch;
dst += pitch;
}
}
2018-06-22 22:59:03 +02:00
delete[] _bits;
2017-06-08 21:56:39 +01:00
}
_bits = newBits;
_bitsSize = newBitsSize;
_width = width;
_height = height;
_pitch = pitch;
RenderTarget* rt = &_mainRT;
rt->bits = _bits;
rt->x = 0;
rt->y = 0;
rt->width = width;
rt->height = height;
rt->pitch = _pitch - width;
2017-06-08 21:56:39 +01:00
ConfigureDirtyGrid();
2017-06-08 22:14:19 +01:00
if (LightFx::IsAvailable())
2018-06-22 22:59:03 +02:00
{
LightFx::UpdateBuffers(*rt);
2018-06-22 22:59:03 +02:00
}
2017-06-08 21:56:39 +01:00
}
2025-04-05 02:58:38 +03:00
void X8DrawingEngine::OnDrawDirtyBlock(int32_t, int32_t, int32_t, int32_t)
2017-08-31 22:31:57 +01:00
{
}
2017-06-08 21:56:39 +01:00
void X8DrawingEngine::ConfigureDirtyGrid()
{
2025-04-05 02:32:41 +03:00
const auto blockWidth = 1u << 7;
const auto blockHeight = 1u << 7;
2017-06-08 21:56:39 +01:00
2025-04-05 02:32:41 +03:00
_invalidationGrid.reset(_width, _height, blockWidth, blockHeight);
2017-06-08 21:56:39 +01:00
}
void X8DrawingEngine::DrawAllDirtyBlocks()
{
2025-04-05 02:58:38 +03:00
_invalidationGrid.traverseDirtyCells([this](int32_t left, int32_t top, int32_t right, int32_t bottom) {
// Draw region
DrawDirtyBlocks(left, top, right, bottom);
});
2017-06-08 21:56:39 +01:00
}
2025-04-05 02:32:41 +03:00
void X8DrawingEngine::DrawDirtyBlocks(int32_t left, int32_t top, int32_t right, int32_t bottom)
{
2017-06-08 21:56:39 +01:00
// Draw region
2025-04-05 02:58:38 +03:00
OnDrawDirtyBlock(left, top, right, bottom);
WindowDrawAll(_mainRT, left, top, right, bottom);
2017-06-08 21:56:39 +01:00
}
#ifdef __WARN_SUGGEST_FINAL_METHODS__
#pragma GCC diagnostic pop
#endif
2017-06-09 21:02:24 +01:00
2018-06-22 22:59:03 +02:00
X8DrawingContext::X8DrawingContext(X8DrawingEngine* engine)
2017-06-08 21:56:39 +01:00
{
_engine = engine;
}
void X8DrawingContext::Clear(RenderTarget& rt, PaletteIndex paletteIndex)
2017-06-08 21:56:39 +01:00
{
Guard::Assert(_isDrawing == true);
int32_t w = rt.width;
int32_t h = rt.height;
2026-02-07 23:41:40 +01:00
PaletteIndex* ptr = rt.bits;
2017-06-08 21:56:39 +01:00
for (int32_t y = 0; y < h; y++)
2017-06-08 21:56:39 +01:00
{
2026-02-07 23:41:40 +01:00
std::fill_n(ptr, w, paletteIndex);
ptr += w + rt.pitch;
2017-06-08 21:56:39 +01:00
}
}
void X8DrawingContext::FillRect(
RenderTarget& rt, PaletteIndex paletteIndex, int32_t left, int32_t top, int32_t right, int32_t bottom, bool crossHatch)
2017-06-08 21:56:39 +01:00
{
Guard::Assert(_isDrawing == true);
assert(rt.zoom_level == ZoomLevel{ 0 });
2018-06-22 22:59:03 +02:00
if (left > right)
return;
if (top > bottom)
return;
if (rt.x > right)
2018-06-22 22:59:03 +02:00
return;
if (left >= rt.x + rt.width)
2018-06-22 22:59:03 +02:00
return;
if (bottom < rt.y)
2018-06-22 22:59:03 +02:00
return;
if (top >= rt.y + rt.height)
2018-06-22 22:59:03 +02:00
return;
2017-06-08 21:56:39 +01:00
2025-01-19 16:59:45 +01:00
uint16_t crosskPattern = 0;
2017-06-08 21:56:39 +01:00
int32_t startX = left - rt.x;
2017-06-08 21:56:39 +01:00
if (startX < 0)
{
2025-01-19 16:59:45 +01:00
crosskPattern ^= startX;
2017-06-08 21:56:39 +01:00
startX = 0;
}
int32_t endX = right - rt.x + 1;
if (endX > rt.width)
2017-06-08 21:56:39 +01:00
{
endX = rt.width;
2017-06-08 21:56:39 +01:00
}
int32_t startY = top - rt.y;
2017-06-08 21:56:39 +01:00
if (startY < 0)
{
2025-01-19 16:59:45 +01:00
crosskPattern ^= startY;
2017-06-08 21:56:39 +01:00
startY = 0;
}
int32_t endY = bottom - rt.y + 1;
if (endY > rt.height)
2017-06-08 21:56:39 +01:00
{
endY = rt.height;
2017-06-08 21:56:39 +01:00
}
int32_t width = endX - startX;
int32_t height = endY - startY;
2017-06-08 21:56:39 +01:00
if (crossHatch)
2017-06-08 21:56:39 +01:00
{
// Cross hatching
2026-02-07 23:41:40 +01:00
PaletteIndex* dst = startY * rt.LineStride() + startX + rt.bits;
for (int32_t i = 0; i < height; i++)
2017-06-08 21:56:39 +01:00
{
2026-02-07 23:41:40 +01:00
PaletteIndex* nextdst = dst + rt.LineStride();
2025-01-19 16:59:45 +01:00
uint32_t p = Numerics::ror32(crosskPattern, 1);
2017-06-08 21:56:39 +01:00
p = (p & 0xFFFF0000) | width;
// Fill every other pixel with the colour
for (; (p & 0xFFFF) != 0; p--)
{
p = p ^ 0x80000000;
if (p & 0x80000000)
{
2026-02-07 23:41:40 +01:00
*dst = paletteIndex;
2017-06-08 21:56:39 +01:00
}
dst++;
}
2025-01-19 16:59:45 +01:00
crosskPattern ^= 1;
2017-06-08 21:56:39 +01:00
dst = nextdst;
}
}
else
{
2026-02-07 23:41:40 +01:00
PaletteIndex* dst = startY * rt.LineStride() + startX + rt.bits;
for (int32_t i = 0; i < height; i++)
2017-06-08 21:56:39 +01:00
{
2026-02-07 23:41:40 +01:00
std::fill_n(dst, width, paletteIndex);
dst += rt.LineStride();
2017-06-08 21:56:39 +01:00
}
}
}
void X8DrawingContext::FilterRect(
RenderTarget& rt, FilterPaletteID palette, int32_t left, int32_t top, int32_t right, int32_t bottom)
2017-06-08 21:56:39 +01:00
{
Guard::Assert(_isDrawing == true);
2018-06-22 22:59:03 +02:00
if (left > right)
return;
if (top > bottom)
return;
if (rt.x > right)
2018-06-22 22:59:03 +02:00
return;
if (left >= rt.x + rt.width)
2018-06-22 22:59:03 +02:00
return;
if (bottom < rt.y)
2018-06-22 22:59:03 +02:00
return;
if (top >= rt.y + rt.height)
2018-06-22 22:59:03 +02:00
return;
2017-06-08 21:56:39 +01:00
int32_t startX = left - rt.x;
2017-06-08 21:56:39 +01:00
if (startX < 0)
{
startX = 0;
}
int32_t endX = right - rt.x + 1;
if (endX > rt.width)
2017-06-08 21:56:39 +01:00
{
endX = rt.width;
2017-06-08 21:56:39 +01:00
}
int32_t startY = top - rt.y;
2017-06-08 21:56:39 +01:00
if (startY < 0)
{
startY = 0;
}
int32_t endY = bottom - rt.y + 1;
if (endY > rt.height)
2017-06-08 21:56:39 +01:00
{
endY = rt.height;
2017-06-08 21:56:39 +01:00
}
int32_t width = endX - startX;
int32_t height = endY - startY;
2017-06-08 21:56:39 +01:00
PaletteIndex* dst = reinterpret_cast<PaletteIndex*>(rt.bits + (startY * rt.LineStride() + startX));
2017-06-08 21:56:39 +01:00
// Find colour in colour table?
auto paletteMap = GetPaletteMapForColour(palette);
2021-09-13 19:47:13 +03:00
if (paletteMap.has_value())
2017-06-08 21:56:39 +01:00
{
2021-09-13 19:47:13 +03:00
const auto& paletteEntries = paletteMap.value();
const int32_t scaled_width = width;
const int32_t step = rt.LineStride();
2017-10-26 13:14:37 +01:00
// Fill the rectangle with the colours from the colour table
auto c = height;
for (int32_t i = 0; i < c; i++)
2017-06-08 21:56:39 +01:00
{
PaletteIndex* nextdst = dst + step * i;
for (int32_t j = 0; j < scaled_width; j++)
2017-10-26 13:14:37 +01:00
{
auto index = *(nextdst + j);
*(nextdst + j) = paletteEntries[EnumValue(index)];
2017-10-26 13:14:37 +01:00
}
2017-06-08 21:56:39 +01:00
}
}
}
void X8DrawingContext::DrawLine(RenderTarget& rt, PaletteIndex colour, const ScreenLine& line)
2017-06-08 21:56:39 +01:00
{
Guard::Assert(_isDrawing == true);
GfxDrawLineSoftware(rt, line, colour);
2017-06-08 21:56:39 +01:00
}
void X8DrawingContext::DrawSprite(RenderTarget& rt, const ImageId imageId, int32_t x, int32_t y)
2017-06-08 21:56:39 +01:00
{
Guard::Assert(_isDrawing == true);
GfxDrawSpriteSoftware(rt, imageId, { x, y });
2017-06-08 21:56:39 +01:00
}
2022-09-28 23:00:58 +02:00
void X8DrawingContext::DrawSpriteRawMasked(
RenderTarget& rt, int32_t x, int32_t y, const ImageId maskImage, const ImageId colourImage)
2017-06-08 21:56:39 +01:00
{
Guard::Assert(_isDrawing == true);
GfxDrawSpriteRawMaskedSoftware(rt, { x, y }, maskImage, colourImage);
2017-06-08 21:56:39 +01:00
}
void X8DrawingContext::DrawSpriteSolid(RenderTarget& rt, const ImageId image, int32_t x, int32_t y, PaletteIndex colour)
2017-06-08 21:56:39 +01:00
{
Guard::Assert(_isDrawing == true);
PaletteIndex palette[256];
std::fill_n(palette, sizeof(palette), colour);
2026-01-18 22:24:45 +01:00
palette[0] = PaletteIndex::transparent;
const auto spriteCoords = ScreenCoordsXY{ x, y };
2026-01-28 10:06:45 +01:00
GfxDrawSpritePaletteSetSoftware(rt, ImageId(image.GetIndex(), Colour::black), spriteCoords, PaletteMap(palette));
2017-06-08 21:56:39 +01:00
}
void X8DrawingContext::DrawGlyph(RenderTarget& rt, const ImageId image, int32_t x, int32_t y, const PaletteMap& paletteMap)
2017-06-08 21:56:39 +01:00
{
Guard::Assert(_isDrawing == true);
GfxDrawSpritePaletteSetSoftware(rt, image, { x, y }, paletteMap);
2017-06-08 21:56:39 +01:00
}
2024-03-28 23:28:54 +01:00
2025-04-11 16:09:52 +01:00
#ifndef DISABLE_TTF
2024-03-28 23:28:54 +01:00
template<bool TUseHinting>
static void DrawTTFBitmapInternal(
RenderTarget& rt, PaletteIndex colour, TTFSurface* surface, int32_t x, int32_t y, uint8_t hintingThreshold)
2024-03-28 23:28:54 +01:00
{
assert(rt.zoom_level == ZoomLevel{ 0 });
2024-03-28 23:28:54 +01:00
const int32_t surfaceWidth = surface->w;
int32_t width = surfaceWidth;
int32_t height = surface->h;
const int32_t overflowX = (rt.x + rt.width) - (x + width);
const int32_t overflowY = (rt.y + rt.height) - (y + height);
2024-03-28 23:28:54 +01:00
if (overflowX < 0)
width += overflowX;
if (overflowY < 0)
height += overflowY;
int32_t skipX = x - rt.x;
int32_t skipY = y - rt.y;
2024-03-28 23:28:54 +01:00
auto src = static_cast<const uint8_t*>(surface->pixels);
PaletteIndex* dst = reinterpret_cast<PaletteIndex*>(rt.bits);
2024-03-28 23:28:54 +01:00
if (skipX < 0)
{
width += skipX;
src += -skipX;
skipX = 0;
}
if (skipY < 0)
{
height += skipY;
src += (-skipY * surfaceWidth);
skipY = 0;
}
dst += skipX;
dst += skipY * rt.LineStride();
2024-03-28 23:28:54 +01:00
const int32_t srcScanSkip = surfaceWidth - width;
const int32_t dstScanSkip = rt.LineStride() - width;
2024-03-28 23:28:54 +01:00
for (int32_t yy = 0; yy < height; yy++)
{
for (int32_t xx = 0; xx < width; xx++)
{
if (*src != 0)
{
if constexpr (TUseHinting)
{
if (*src > 180)
{
// Centre of the glyph: use full colour.
*dst = colour;
}
else if (*src > hintingThreshold)
{
*dst = BlendColours(colour, *dst);
}
}
else
{
*dst = colour;
}
}
src++;
dst++;
}
src += srcScanSkip;
dst += dstScanSkip;
}
}
2025-04-11 16:09:52 +01:00
#endif // DISABLE_TTF
2024-03-28 23:28:54 +01:00
void X8DrawingContext::DrawTTFBitmap(
2026-03-22 15:28:20 +01:00
RenderTarget& rt, const TextDrawInfo& info, TTFSurface* surface, int32_t x, int32_t y, uint8_t hintingThreshold)
2024-03-28 23:28:54 +01:00
{
2025-04-11 16:09:52 +01:00
#ifndef DISABLE_TTF
2026-03-22 15:28:20 +01:00
const auto fgColor = info.palette.fill;
const auto bgColor = info.palette.shadowOutline;
2024-03-28 23:28:54 +01:00
2026-03-22 15:28:20 +01:00
if (info.colourFlags.has(ColourFlag::withOutline))
2024-03-28 23:28:54 +01:00
{
DrawTTFBitmapInternal<false>(rt, bgColor, surface, x + 1, y, 0);
DrawTTFBitmapInternal<false>(rt, bgColor, surface, x - 1, y, 0);
DrawTTFBitmapInternal<false>(rt, bgColor, surface, x, y + 1, 0);
DrawTTFBitmapInternal<false>(rt, bgColor, surface, x, y - 1, 0);
2024-03-28 23:28:54 +01:00
}
2026-03-22 15:28:20 +01:00
if (info.colourFlags.has(ColourFlag::inset))
2024-03-28 23:28:54 +01:00
{
DrawTTFBitmapInternal<false>(rt, bgColor, surface, x + 1, y + 1, 0);
2024-03-28 23:28:54 +01:00
}
if (hintingThreshold > 0)
DrawTTFBitmapInternal<true>(rt, fgColor, surface, x, y, hintingThreshold);
2024-03-28 23:28:54 +01:00
else
DrawTTFBitmapInternal<false>(rt, fgColor, surface, x, y, 0);
2025-04-11 16:09:52 +01:00
#endif // DISABLE_TTF
2024-04-26 20:35:16 +02:00
}
void X8DrawingContext::BeginDraw()
{
Guard::Assert(_isDrawing == false);
_isDrawing = true;
}
void X8DrawingContext::EndDraw()
{
Guard::Assert(_isDrawing == true);
_isDrawing = false;
}