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

734 lines
20 KiB
C++
Raw Normal View History

2017-06-08 21:56:39 +01:00
/*****************************************************************************
2023-01-01 04:58:01 -06:00
* Copyright (c) 2014-2023 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 "../Context.h"
2017-11-30 18:17:06 +01:00
#include "../Game.h"
2018-06-22 22:59:03 +02:00
#include "../Intro.h"
#include "../config/Config.h"
#include "../core/Numerics.hpp"
2018-06-22 22:59:03 +02:00
#include "../interface/Screenshot.h"
2018-01-05 23:45:53 +00:00
#include "../interface/Viewport.h"
2018-01-06 00:05:16 +00:00
#include "../interface/Window.h"
2018-06-22 22:59:03 +02:00
#include "../ui/UiContext.h"
#include "../util/Util.h"
2020-02-20 22:57:04 +00:00
#include "../world/Climate.h"
2018-01-05 21:17:33 +00:00
#include "Drawing.h"
2018-06-22 22:59:03 +02:00
#include "IDrawingContext.h"
#include "IDrawingEngine.h"
2018-01-05 21:11:19 +00:00
#include "LightFX.h"
#include "Weather.h"
2018-06-22 22:59:03 +02:00
#include <algorithm>
#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(
DrawPixelInfo* dpi, 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 = (dpi->pitch + dpi->width) * y + x;
uint8_t patternYPos = patternStartYOffset % patternYSpace;
2017-06-08 21:56:39 +01:00
uint8_t* screenBits = dpi->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)
{
uint8_t current_pixel = screenBits[xPixelOffset];
2017-06-08 21:56:39 +01:00
screenBits[xPixelOffset] = 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 += dpi->pitch + dpi->width;
2017-06-08 21:56:39 +01:00
patternYPos++;
patternYPos %= patternYSpace;
}
}
void X8WeatherDrawer::Restore(DrawPixelInfo* dpi)
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 = (dpi->width + dpi->pitch) * dpi->height;
uint8_t* bits = dpi->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__
2018-07-21 16:17:06 +02:00
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wsuggest-final-methods"
#endif
2017-06-09 21:02:24 +01:00
X8DrawingEngine::X8DrawingEngine([[maybe_unused]] const std::shared_ptr<Ui::IUiContext>& uiContext)
2017-06-08 21:56:39 +01:00
{
_drawingContext = new X8DrawingContext(this);
_bitsDPI.DrawingEngine = this;
LightFXSetAvailable(true);
2022-10-16 17:27:01 +02:00
_lastLightFXenabled = (gConfigGeneral.EnableLightFx != 0);
2017-06-08 21:56:39 +01:00
}
X8DrawingEngine::~X8DrawingEngine()
{
delete _drawingContext;
2018-06-22 22:59:03 +02:00
delete[] _dirtyGrid.Blocks;
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);
}
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
{
2018-06-20 17:11:35 +02:00
left = std::max(left, 0);
top = std::max(top, 0);
2020-04-22 17:09:29 +02:00
right = std::min(right, static_cast<int32_t>(_width));
bottom = std::min(bottom, static_cast<int32_t>(_height));
2017-06-08 21:56:39 +01:00
2018-06-22 22:59:03 +02:00
if (left >= right)
return;
if (top >= bottom)
return;
2017-06-08 21:56:39 +01:00
right--;
bottom--;
left >>= _dirtyGrid.BlockShiftX;
right >>= _dirtyGrid.BlockShiftX;
top >>= _dirtyGrid.BlockShiftY;
bottom >>= _dirtyGrid.BlockShiftY;
uint32_t dirtyBlockColumns = _dirtyGrid.BlockColumns;
2018-06-22 22:59:03 +02:00
uint8_t* screenDirtyBlocks = _dirtyGrid.Blocks;
for (int16_t y = top; y <= bottom; y++)
2017-06-08 21:56:39 +01:00
{
uint32_t yOffset = y * dirtyBlockColumns;
for (int16_t x = left; x <= right; x++)
2017-06-08 21:56:39 +01:00
{
screenDirtyBlocks[yOffset + x] = 0xFF;
}
}
}
2017-07-15 19:47:38 +01:00
void X8DrawingEngine::BeginDraw()
2017-06-08 21:56:39 +01:00
{
if (gIntroState == IntroState::None)
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
2022-10-16 17:27:01 +02:00
if (_lastLightFXenabled != (gConfigGeneral.EnableLightFx != 0))
2017-07-15 19:47:38 +01:00
{
Resize(_width, _height);
}
_weatherDrawer.Restore(&_bitsDPI);
2017-06-08 21:56:39 +01:00
}
}
2017-07-15 19:47:38 +01:00
void X8DrawingEngine::EndDraw()
{
}
void X8DrawingEngine::PaintWindows()
{
WindowResetVisibilities();
2017-07-15 19:47:38 +01:00
// Redraw dirty regions before updating the viewports, otherwise
// when viewports get panned, they copy dirty pixels
DrawAllDirtyBlocks();
WindowUpdateAllViewports();
2017-07-15 19:47:38 +01:00
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
{
2020-09-14 21:36:30 +02:00
DrawWeather(&_bitsDPI, &_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;
2018-06-22 22:59:03 +02:00
int32_t stride = _bitsDPI.width + _bitsDPI.pitch;
uint8_t* to = _bitsDPI.bits + y * stride + x;
uint8_t* from = _bitsDPI.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
{
2023-01-16 21:14:50 +01:00
return ScreenshotDumpPNG(&_bitsDPI);
2017-06-08 21:56:39 +01:00
}
IDrawingContext* X8DrawingEngine::GetDrawingContext()
2017-06-08 21:56:39 +01:00
{
return _drawingContext;
}
DrawPixelInfo* X8DrawingEngine::GetDrawingPixelInfo()
2017-06-08 21:56:39 +01:00
{
return &_bitsDPI;
}
DRAWING_ENGINE_FLAGS X8DrawingEngine::GetFlags()
{
return static_cast<DRAWING_ENGINE_FLAGS>(DEF_DIRTY_OPTIMISATIONS | DEF_PARALLEL_DRAWING);
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
}
DrawPixelInfo* X8DrawingEngine::GetDPI()
2017-06-08 21:56:39 +01:00
{
return &_bitsDPI;
}
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;
uint8_t* newBits = new uint8_t[newBitsSize];
2017-06-08 21:56:39 +01:00
if (_bits == nullptr)
{
std::fill_n(newBits, newBitsSize, 0);
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
{
2018-06-22 22:59:03 +02:00
uint8_t* src = _bits;
uint8_t* 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)
{
std::fill_n(dst + minWidth, pitch - minWidth, 0);
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;
DrawPixelInfo* dpi = &_bitsDPI;
2017-06-08 21:56:39 +01:00
dpi->bits = _bits;
dpi->x = 0;
dpi->y = 0;
dpi->width = width;
dpi->height = height;
dpi->pitch = _pitch - width;
ConfigureDirtyGrid();
2017-06-08 22:14:19 +01:00
if (LightFXIsAvailable())
2018-06-22 22:59:03 +02:00
{
LightFXUpdateBuffers(dpi);
2018-06-22 22:59:03 +02:00
}
2017-06-08 21:56:39 +01:00
}
void X8DrawingEngine::OnDrawDirtyBlock(
[[maybe_unused]] uint32_t x, [[maybe_unused]] uint32_t y, [[maybe_unused]] uint32_t columns, [[maybe_unused]] uint32_t rows)
2017-08-31 22:31:57 +01:00
{
}
2017-06-08 21:56:39 +01:00
void X8DrawingEngine::ConfigureDirtyGrid()
{
_dirtyGrid.BlockShiftX = 7;
_dirtyGrid.BlockShiftY = 6;
_dirtyGrid.BlockWidth = 1 << _dirtyGrid.BlockShiftX;
_dirtyGrid.BlockHeight = 1 << _dirtyGrid.BlockShiftY;
_dirtyGrid.BlockColumns = (_width >> _dirtyGrid.BlockShiftX) + 1;
_dirtyGrid.BlockRows = (_height >> _dirtyGrid.BlockShiftY) + 1;
2018-06-22 22:59:03 +02:00
delete[] _dirtyGrid.Blocks;
_dirtyGrid.Blocks = new uint8_t[_dirtyGrid.BlockColumns * _dirtyGrid.BlockRows];
2017-06-08 21:56:39 +01:00
}
void X8DrawingEngine::DrawAllDirtyBlocks()
{
for (uint32_t x = 0; x < _dirtyGrid.BlockColumns; x++)
2017-06-08 21:56:39 +01:00
{
for (uint32_t y = 0; y < _dirtyGrid.BlockRows; y++)
2017-06-08 21:56:39 +01:00
{
uint32_t yOffset = y * _dirtyGrid.BlockColumns;
if (_dirtyGrid.Blocks[yOffset + x] == 0)
2017-06-08 21:56:39 +01:00
{
continue;
}
// Determine columns
uint32_t xx;
for (xx = x; xx < _dirtyGrid.BlockColumns; xx++)
2017-06-08 21:56:39 +01:00
{
if (_dirtyGrid.Blocks[yOffset + xx] == 0)
2017-06-08 21:56:39 +01:00
{
break;
}
}
// Check rows
uint32_t columns = xx - x;
auto rows = GetNumDirtyRows(x, y, columns);
2017-06-08 21:56:39 +01:00
DrawDirtyBlocks(x, y, columns, rows);
}
}
}
uint32_t X8DrawingEngine::GetNumDirtyRows(const uint32_t x, const uint32_t y, const uint32_t columns)
{
uint32_t yy = y;
for (yy = y; yy < _dirtyGrid.BlockRows; yy++)
{
uint32_t yyOffset = yy * _dirtyGrid.BlockColumns;
for (uint32_t xx = x; xx < x + columns; xx++)
{
if (_dirtyGrid.Blocks[yyOffset + xx] == 0)
{
return yy - y;
}
}
}
return yy - y;
}
void X8DrawingEngine::DrawDirtyBlocks(uint32_t x, uint32_t y, uint32_t columns, uint32_t rows)
2017-06-08 21:56:39 +01:00
{
2018-06-22 22:59:03 +02:00
uint32_t dirtyBlockColumns = _dirtyGrid.BlockColumns;
uint8_t* screenDirtyBlocks = _dirtyGrid.Blocks;
2017-06-08 21:56:39 +01:00
// Unset dirty blocks
for (uint32_t top = y; top < y + rows; top++)
2017-06-08 21:56:39 +01:00
{
uint32_t topOffset = top * dirtyBlockColumns;
for (uint32_t left = x; left < x + columns; left++)
2017-06-08 21:56:39 +01:00
{
screenDirtyBlocks[topOffset + left] = 0;
}
}
// Determine region in pixels
uint32_t left = std::max<uint32_t>(0, x * _dirtyGrid.BlockWidth);
uint32_t top = std::max<uint32_t>(0, y * _dirtyGrid.BlockHeight);
uint32_t right = std::min(_width, left + (columns * _dirtyGrid.BlockWidth));
uint32_t bottom = std::min(_height, top + (rows * _dirtyGrid.BlockHeight));
2017-06-08 21:56:39 +01:00
if (right <= left || bottom <= top)
{
return;
}
// Draw region
2017-08-31 22:31:57 +01:00
OnDrawDirtyBlock(x, y, columns, rows);
WindowDrawAll(_bitsDPI, left, top, right, bottom);
2017-06-08 21:56:39 +01:00
}
#ifdef __WARN_SUGGEST_FINAL_METHODS__
2018-07-21 16:17:06 +02:00
# 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(DrawPixelInfo* dpi, uint8_t paletteIndex)
2017-06-08 21:56:39 +01:00
{
int32_t w = dpi->zoom_level.ApplyInversedTo(dpi->width);
int32_t h = dpi->zoom_level.ApplyInversedTo(dpi->height);
2018-06-22 22:59:03 +02:00
uint8_t* ptr = dpi->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
{
std::fill_n(ptr, w, paletteIndex);
2017-06-08 21:56:39 +01:00
ptr += w + dpi->pitch;
}
}
/** rct2: 0x0097FF04 */
2018-01-10 16:14:36 +01:00
// clang-format off
static constexpr const uint16_t Pattern[] = {
2017-06-08 21:56:39 +01:00
0b0111111110000000,
0b0011111111000000,
0b0001111111100000,
0b0000111111110000,
0b0000011111111000,
0b0000001111111100,
0b0000000111111110,
0b0000000011111111,
0b1000000001111111,
0b1100000000111111,
0b1110000000011111,
0b1111000000001111,
0b1111100000000111,
0b1111110000000011,
0b1111111000000001,
0b1111111100000000,
};
/** rct2: 0x0097FF14 */
static constexpr const uint16_t PatternInverse[] = {
2017-06-08 21:56:39 +01:00
0b1000000001111111,
0b1100000000111111,
0b1110000000011111,
0b1111000000001111,
0b1111100000000111,
0b1111110000000011,
0b1111111000000001,
0b1111111100000000,
0b0111111110000000,
0b0011111111000000,
0b0001111111100000,
0b0000111111110000,
0b0000011111111000,
0b0000001111111100,
0b0000000111111110,
2021-10-17 15:31:56 +02:00
0b0000000011111111,
2017-06-08 21:56:39 +01:00
};
/** rct2: 0x0097FEFC */
2021-10-17 15:31:56 +02:00
static constexpr const uint16_t* Patterns[] = {
2017-06-08 21:56:39 +01:00
Pattern,
2021-10-17 15:31:56 +02:00
PatternInverse,
2017-06-08 21:56:39 +01:00
};
2018-01-10 16:14:36 +01:00
// clang-format on
2017-06-08 21:56:39 +01:00
void X8DrawingContext::FillRect(DrawPixelInfo* dpi, uint32_t colour, int32_t left, int32_t top, int32_t right, int32_t bottom)
2017-06-08 21:56:39 +01:00
{
2018-06-22 22:59:03 +02:00
if (left > right)
return;
if (top > bottom)
return;
if (dpi->x > right)
return;
if (left >= dpi->x + dpi->width)
return;
if (bottom < dpi->y)
return;
if (top >= dpi->y + dpi->height)
return;
2017-06-08 21:56:39 +01:00
uint16_t crossPattern = 0;
2017-06-08 21:56:39 +01:00
int32_t startX = left - dpi->x;
2017-06-08 21:56:39 +01:00
if (startX < 0)
{
crossPattern ^= startX;
startX = 0;
}
int32_t endX = right - dpi->x + 1;
2017-06-08 21:56:39 +01:00
if (endX > dpi->width)
{
endX = dpi->width;
}
int32_t startY = top - dpi->y;
2017-06-08 21:56:39 +01:00
if (startY < 0)
{
crossPattern ^= startY;
startY = 0;
}
int32_t endY = bottom - dpi->y + 1;
2017-06-08 21:56:39 +01:00
if (endY > dpi->height)
{
endY = dpi->height;
}
int32_t width = endX - startX;
int32_t height = endY - startY;
2017-06-08 21:56:39 +01:00
if (colour & 0x1000000)
{
// Cross hatching
2018-06-22 22:59:03 +02:00
uint8_t* dst = (startY * (dpi->width + dpi->pitch)) + startX + dpi->bits;
for (int32_t i = 0; i < height; i++)
2017-06-08 21:56:39 +01:00
{
2018-06-22 22:59:03 +02:00
uint8_t* nextdst = dst + dpi->width + dpi->pitch;
uint32_t p = Numerics::ror32(crossPattern, 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)
{
*dst = colour & 0xFF;
}
dst++;
}
crossPattern ^= 1;
dst = nextdst;
}
}
else if (colour & 0x2000000)
{
assert(false);
}
else if (colour & 0x4000000)
{
2018-06-22 22:59:03 +02:00
uint8_t* dst = startY * (dpi->width + dpi->pitch) + startX + dpi->bits;
2017-06-08 21:56:39 +01:00
// The pattern loops every 15 lines this is which
// part the pattern is on.
int32_t patternY = (startY + dpi->y) % 16;
2017-06-08 21:56:39 +01:00
// The pattern loops every 15 pixels this is which
// part the pattern is on.
int32_t startPatternX = (startX + dpi->x) % 16;
int32_t patternX = startPatternX;
2017-06-08 21:56:39 +01:00
2018-06-22 22:59:03 +02:00
const uint16_t* patternsrc = Patterns[colour >> 28]; // or possibly uint8_t)[esi*4] ?
2017-06-08 21:56:39 +01:00
for (int32_t numLines = height; numLines > 0; numLines--)
2017-06-08 21:56:39 +01:00
{
2018-06-22 22:59:03 +02:00
uint8_t* nextdst = dst + dpi->width + dpi->pitch;
uint16_t pattern = patternsrc[patternY];
2017-06-08 21:56:39 +01:00
for (int32_t numPixels = width; numPixels > 0; numPixels--)
2017-06-08 21:56:39 +01:00
{
if (pattern & (1 << patternX))
{
*dst = colour & 0xFF;
}
patternX = (patternX + 1) % 16;
dst++;
}
patternX = startPatternX;
patternY = (patternY + 1) % 16;
dst = nextdst;
}
}
else
{
2018-06-22 22:59:03 +02:00
uint8_t* dst = startY * (dpi->width + dpi->pitch) + startX + dpi->bits;
for (int32_t i = 0; i < height; i++)
2017-06-08 21:56:39 +01:00
{
std::fill_n(dst, width, colour & 0xFF);
2017-06-08 21:56:39 +01:00
dst += dpi->width + dpi->pitch;
}
}
}
void X8DrawingContext::FilterRect(
DrawPixelInfo* dpi, FilterPaletteID palette, int32_t left, int32_t top, int32_t right, int32_t bottom)
2017-06-08 21:56:39 +01:00
{
2018-06-22 22:59:03 +02:00
if (left > right)
return;
if (top > bottom)
return;
if (dpi->x > right)
return;
if (left >= dpi->x + dpi->width)
return;
if (bottom < dpi->y)
return;
if (top >= dpi->y + dpi->height)
return;
2017-06-08 21:56:39 +01:00
int32_t startX = left - dpi->x;
2017-06-08 21:56:39 +01:00
if (startX < 0)
{
startX = 0;
}
int32_t endX = right - dpi->x + 1;
2017-06-08 21:56:39 +01:00
if (endX > dpi->width)
{
endX = dpi->width;
}
int32_t startY = top - dpi->y;
2017-06-08 21:56:39 +01:00
if (startY < 0)
{
startY = 0;
}
int32_t endY = bottom - dpi->y + 1;
2017-06-08 21:56:39 +01:00
if (endY > dpi->height)
{
endY = dpi->height;
}
int32_t width = endX - startX;
int32_t height = endY - startY;
2017-06-08 21:56:39 +01:00
2018-06-22 22:59:03 +02:00
// 0x2000000
2017-06-08 21:56:39 +01:00
// 00678B7E 00678C83
// Location in screen buffer?
2018-06-22 22:59:03 +02:00
uint8_t* dst = dpi->bits
2020-04-22 17:09:29 +02:00
+ static_cast<uint32_t>(
dpi->zoom_level.ApplyInversedTo(startY) * (dpi->zoom_level.ApplyInversedTo(dpi->width) + dpi->pitch)
+ dpi->zoom_level.ApplyInversedTo(startX));
2017-06-08 21:56:39 +01:00
// Find colour in colour table?
auto paletteMap = GetPaletteMapForColour(EnumValue(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 = dpi->zoom_level.ApplyInversedTo(width);
const int32_t step = dpi->zoom_level.ApplyInversedTo(dpi->width) + dpi->pitch;
2017-10-26 13:14:37 +01:00
// Fill the rectangle with the colours from the colour table
auto c = dpi->zoom_level.ApplyInversedTo(height);
for (int32_t i = 0; i < c; i++)
2017-06-08 21:56:39 +01:00
{
2018-06-22 22:59:03 +02:00
uint8_t* 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);
2021-09-13 19:47:13 +03:00
*(nextdst + j) = paletteEntries[index];
2017-10-26 13:14:37 +01:00
}
2017-06-08 21:56:39 +01:00
}
}
}
void X8DrawingContext::DrawLine(DrawPixelInfo* dpi, uint32_t colour, const ScreenLine& line)
2017-06-08 21:56:39 +01:00
{
GfxDrawLineSoftware(dpi, line, colour);
2017-06-08 21:56:39 +01:00
}
void X8DrawingContext::DrawSprite(DrawPixelInfo* dpi, const ImageId imageId, int32_t x, int32_t y)
2017-06-08 21:56:39 +01:00
{
GfxDrawSpriteSoftware(dpi, imageId, { x, y });
2017-06-08 21:56:39 +01:00
}
2022-09-28 23:00:58 +02:00
void X8DrawingContext::DrawSpriteRawMasked(
DrawPixelInfo* dpi, int32_t x, int32_t y, const ImageId maskImage, const ImageId colourImage)
2017-06-08 21:56:39 +01:00
{
GfxDrawSpriteRawMaskedSoftware(dpi, { x, y }, maskImage, colourImage);
2017-06-08 21:56:39 +01:00
}
void X8DrawingContext::DrawSpriteSolid(DrawPixelInfo* dpi, const ImageId image, int32_t x, int32_t y, uint8_t colour)
2017-06-08 21:56:39 +01:00
{
uint8_t palette[256];
std::fill_n(palette, sizeof(palette), colour);
palette[0] = 0;
const auto spriteCoords = ScreenCoordsXY{ x, y };
GfxDrawSpritePaletteSetSoftware(dpi, ImageId(image.GetIndex(), 0), spriteCoords, PaletteMap(palette));
2017-06-08 21:56:39 +01:00
}
void X8DrawingContext::DrawGlyph(DrawPixelInfo* dpi, const ImageId image, int32_t x, int32_t y, const PaletteMap& paletteMap)
2017-06-08 21:56:39 +01:00
{
GfxDrawSpritePaletteSetSoftware(dpi, image, { x, y }, paletteMap);
2017-06-08 21:56:39 +01:00
}