From c90ca119eb740d15313a7bcb3d0a5047129e342a Mon Sep 17 00:00:00 2001 From: Gymnasiast Date: Sun, 6 Sep 2026 19:50:48 +0200 Subject: [PATCH] Move line drawing stuff into Line.{cpp.h} --- src/openrct2-ui/interface/Graph.cpp | 2 +- src/openrct2-ui/scripting/CustomListView.cpp | 1 + .../scripting/ScGraphicsContext.hpp | 1 + src/openrct2-ui/windows/ScenarioSelect.cpp | 1 + src/openrct2/drawing/Drawing.cpp | 7 +- src/openrct2/drawing/Drawing.h | 7 - src/openrct2/drawing/Line.cpp | 321 ++++++++++-------- src/openrct2/drawing/Line.h | 25 ++ src/openrct2/drawing/NewDrawing.cpp | 43 --- src/openrct2/drawing/X8DrawingEngine.cpp | 1 + src/openrct2/libopenrct2.vcxproj | 1 + src/openrct2/paint/Paint.cpp | 1 + 12 files changed, 218 insertions(+), 193 deletions(-) create mode 100644 src/openrct2/drawing/Line.h diff --git a/src/openrct2-ui/interface/Graph.cpp b/src/openrct2-ui/interface/Graph.cpp index c41da7aa1d..9e5e908543 100644 --- a/src/openrct2-ui/interface/Graph.cpp +++ b/src/openrct2-ui/interface/Graph.cpp @@ -11,7 +11,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/openrct2-ui/scripting/CustomListView.cpp b/src/openrct2-ui/scripting/CustomListView.cpp index 413cb33694..555cc78341 100644 --- a/src/openrct2-ui/scripting/CustomListView.cpp +++ b/src/openrct2-ui/scripting/CustomListView.cpp @@ -19,6 +19,7 @@ #include #include #include + #include #include #include #include diff --git a/src/openrct2-ui/scripting/ScGraphicsContext.hpp b/src/openrct2-ui/scripting/ScGraphicsContext.hpp index 61ff36115f..6d0d147cbb 100644 --- a/src/openrct2-ui/scripting/ScGraphicsContext.hpp +++ b/src/openrct2-ui/scripting/ScGraphicsContext.hpp @@ -15,6 +15,7 @@ #include #include + #include #include #include #include diff --git a/src/openrct2-ui/windows/ScenarioSelect.cpp b/src/openrct2-ui/windows/ScenarioSelect.cpp index 4020d1284f..bf60727fbb 100644 --- a/src/openrct2-ui/windows/ScenarioSelect.cpp +++ b/src/openrct2-ui/windows/ScenarioSelect.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include diff --git a/src/openrct2/drawing/Drawing.cpp b/src/openrct2/drawing/Drawing.cpp index 73b96dbe83..3b6eb5c9f7 100644 --- a/src/openrct2/drawing/Drawing.cpp +++ b/src/openrct2/drawing/Drawing.cpp @@ -9,24 +9,19 @@ #include "Drawing.h" -#include "../Context.h" #include "../Game.h" #include "../GameState.h" #include "../SpriteIds.h" -#include "../config/Config.h" #include "../interface/ScreenCoords.hpp" #include "Drawing.Sprite.h" #include "FilterPaletteIds.h" #include "Font.h" -#include "NewDrawing.h" -#include "Palette.h" +#include "Line.h" #include "Rectangle.h" #include "RenderTarget.h" #include "Text.h" -#include #include -#include using namespace OpenRCT2; using namespace OpenRCT2::Drawing; diff --git a/src/openrct2/drawing/Drawing.h b/src/openrct2/drawing/Drawing.h index 384ff74896..694300401c 100644 --- a/src/openrct2/drawing/Drawing.h +++ b/src/openrct2/drawing/Drawing.h @@ -54,13 +54,6 @@ void GfxClear(OpenRCT2::Drawing::RenderTarget& rt, OpenRCT2::Drawing::PaletteInd void GfxFilterPixel( OpenRCT2::Drawing::RenderTarget& rt, const ScreenCoordsXY& coords, OpenRCT2::Drawing::FilterPaletteID palette); -// line -void GfxDrawLine(OpenRCT2::Drawing::RenderTarget& rt, const ScreenLine& line, OpenRCT2::Drawing::PaletteIndex colour); -void GfxDrawLineSoftware(OpenRCT2::Drawing::RenderTarget& rt, const ScreenLine& line, OpenRCT2::Drawing::PaletteIndex colour); -void GfxDrawDashedLine( - OpenRCT2::Drawing::RenderTarget& rt, const ScreenLine& screenLine, int32_t dashedLineSegmentLength, - OpenRCT2::Drawing::PaletteIndex colour); - // sprite (NewDrawing.cpp functions that use the drawing engine) void FASTCALL GfxDrawSprite(OpenRCT2::Drawing::RenderTarget& rt, ImageId image_id, const ScreenCoordsXY& coords); void FASTCALL GfxDrawGlyph( diff --git a/src/openrct2/drawing/Line.cpp b/src/openrct2/drawing/Line.cpp index 14bcb12e59..f2a84471eb 100644 --- a/src/openrct2/drawing/Line.cpp +++ b/src/openrct2/drawing/Line.cpp @@ -7,164 +7,213 @@ * OpenRCT2 is licensed under the GNU General Public License version 3. *****************************************************************************/ +#include "Line.h" + +#include "../interface/ScreenCoords.hpp" #include "Drawing.h" +#include "IDrawingContext.h" +#include "IDrawingEngine.h" #include "RenderTarget.h" #include -using namespace OpenRCT2::Drawing; - -/** - * Draws a horizontal line of specified colour to a buffer. - * rct2: 0x0068474C - */ -static void GfxDrawLineOnBuffer(RenderTarget& rt, PaletteIndex colour, const ScreenCoordsXY& coords, int32_t no_pixels) +namespace OpenRCT2::Drawing { - ScreenCoordsXY offset{ coords.x - rt.x, coords.y - rt.y }; - - const int32_t width = rt.width; - const int32_t height = rt.height; - - // Check to make sure point is in the y range - if (offset.y < 0) - return; - if (offset.y >= height) - return; - // Check to make sure we are drawing at least a pixel - if (!no_pixels) - no_pixels++; - - // If x coord outside range leave - if (offset.x < 0) + /** + * Draws a horizontal line of specified colour to a buffer. + * rct2: 0x0068474C + */ + static void GfxDrawLineOnBuffer(RenderTarget& rt, PaletteIndex colour, const ScreenCoordsXY& coords, int32_t no_pixels) { - // Unless the number of pixels is enough to be in range - no_pixels += offset.x; - if (no_pixels <= 0) + ScreenCoordsXY offset{ coords.x - rt.x, coords.y - rt.y }; + + const int32_t width = rt.width; + const int32_t height = rt.height; + + // Check to make sure point is in the y range + if (offset.y < 0) return; - // Resets starting point to 0 as we don't draw outside the range - offset.x = 0; - } - - // Ensure that the end point of the line is within range - if (offset.x + no_pixels - width > 0) - { - // If the end point has any pixels outside range - // cut them off. If there are now no pixels return. - no_pixels -= offset.x + no_pixels - width; - if (no_pixels <= 0) + if (offset.y >= height) return; + // Check to make sure we are drawing at least a pixel + if (!no_pixels) + no_pixels++; + + // If x coord outside range leave + if (offset.x < 0) + { + // Unless the number of pixels is enough to be in range + no_pixels += offset.x; + if (no_pixels <= 0) + return; + // Resets starting point to 0 as we don't draw outside the range + offset.x = 0; + } + + // Ensure that the end point of the line is within range + if (offset.x + no_pixels - width > 0) + { + // If the end point has any pixels outside range + // cut them off. If there are now no pixels return. + no_pixels -= offset.x + no_pixels - width; + if (no_pixels <= 0) + return; + } + + // Get the buffer we are drawing to and move to the first coordinate. + PaletteIndex* bits_pointer = reinterpret_cast(rt.bits + offset.y * rt.LineStride() + offset.x); + + // Draw the line to the specified colour + for (; no_pixels > 0; --no_pixels, ++bits_pointer) + { + *bits_pointer = colour; + } } - // Get the buffer we are drawing to and move to the first coordinate. - PaletteIndex* bits_pointer = reinterpret_cast(rt.bits + offset.y * rt.LineStride() + offset.x); + /** + * Draws a line on rt if within rt boundaries + * rct2: 0x00684466 + * rt (edi) + * x1 (ax) + * y1 (bx) + * x2 (cx) + * y2 (dx) + * colour (ebp) + */ - // Draw the line to the specified colour - for (; no_pixels > 0; --no_pixels, ++bits_pointer) + void GfxDrawLineSoftware(RenderTarget& rt, const ScreenLine& line, PaletteIndex colour) { - *bits_pointer = colour; - } -} + const ZoomLevel zoom = rt.zoom_level; + int32_t x1 = zoom.ApplyInversedTo(line.GetX1()); + int32_t x2 = zoom.ApplyInversedTo(line.GetX2()); + int32_t y1 = zoom.ApplyInversedTo(line.GetY1()); + int32_t y2 = zoom.ApplyInversedTo(line.GetY2()); + // Check to make sure the line is within the drawing area + if ((x1 < rt.x) && (x2 < rt.x)) + { + return; + } -/** - * Draws a line on rt if within rt boundaries - * rct2: 0x00684466 - * rt (edi) - * x1 (ax) - * y1 (bx) - * x2 (cx) - * y2 (dx) - * colour (ebp) - */ + if ((y1 < rt.y) && (y2 < rt.y)) + { + return; + } -void GfxDrawLineSoftware(RenderTarget& rt, const ScreenLine& line, PaletteIndex colour) -{ - const ZoomLevel zoom = rt.zoom_level; - int32_t x1 = zoom.ApplyInversedTo(line.GetX1()); - int32_t x2 = zoom.ApplyInversedTo(line.GetX2()); - int32_t y1 = zoom.ApplyInversedTo(line.GetY1()); - int32_t y2 = zoom.ApplyInversedTo(line.GetY2()); - // Check to make sure the line is within the drawing area - if ((x1 < rt.x) && (x2 < rt.x)) - { - return; - } + if ((x1 > (rt.x + rt.width)) && (x2 > (rt.x + rt.width))) + { + return; + } - if ((y1 < rt.y) && (y2 < rt.y)) - { - return; - } + if ((y1 > (rt.y + rt.height)) && (y2 > (rt.y + rt.height))) + { + return; + } - if ((x1 > (rt.x + rt.width)) && (x2 > (rt.x + rt.width))) - { - return; - } + // Bresenham's algorithm - if ((y1 > (rt.y + rt.height)) && (y2 > (rt.y + rt.height))) - { - return; - } - - // Bresenham's algorithm - - // If vertical plot points upwards - int32_t steep = std::abs(y2 - y1) > std::abs(x2 - x1); - if (steep) - { - int32_t temp_y2 = y2; - int32_t temp_x2 = x2; - y2 = x1; - x2 = y1; - y1 = temp_x2; - x1 = temp_y2; - } - - // If line is right to left swap direction - if (x1 > x2) - { - int32_t temp_y2 = y2; - int32_t temp_x2 = x2; - y2 = y1; - x2 = x1; - y1 = temp_y2; - x1 = temp_x2; - } - - int32_t delta_x = x2 - x1; - int32_t delta_y = std::abs(y2 - y1); - int32_t error = delta_x / 2; - int32_t y_step; - int32_t y = y1; - - // Direction of step - if (y1 < y2) - y_step = 1; - else - y_step = -1; - - for (int32_t x = x1, x_start = x1, length = 1; x < x2; ++x, ++length) - { - // Vertical lines are drawn 1 pixel at a time + // If vertical plot points upwards + int32_t steep = std::abs(y2 - y1) > std::abs(x2 - x1); if (steep) - GfxDrawLineOnBuffer(rt, colour, { y, x }, 1); - - error -= delta_y; - if (error < 0) { - // Non vertical lines are drawn with as many pixels in a horizontal line as possible - if (!steep) - GfxDrawLineOnBuffer(rt, colour, { x_start, y }, length); - - // Reset non vertical line vars - x_start = x + 1; - length = 0; // NB: will be incremented in next iteration - y += y_step; - error += delta_x; + int32_t temp_y2 = y2; + int32_t temp_x2 = x2; + y2 = x1; + x2 = y1; + y1 = temp_x2; + x1 = temp_y2; } - // Catch the case of the last line - if (x + 1 == x2 && !steep) + // If line is right to left swap direction + if (x1 > x2) { - GfxDrawLineOnBuffer(rt, colour, { x_start, y }, length); + int32_t temp_y2 = y2; + int32_t temp_x2 = x2; + y2 = y1; + x2 = x1; + y1 = temp_y2; + x1 = temp_x2; + } + + int32_t delta_x = x2 - x1; + int32_t delta_y = std::abs(y2 - y1); + int32_t error = delta_x / 2; + int32_t y_step; + int32_t y = y1; + + // Direction of step + if (y1 < y2) + y_step = 1; + else + y_step = -1; + + for (int32_t x = x1, x_start = x1, length = 1; x < x2; ++x, ++length) + { + // Vertical lines are drawn 1 pixel at a time + if (steep) + GfxDrawLineOnBuffer(rt, colour, { y, x }, 1); + + error -= delta_y; + if (error < 0) + { + // Non vertical lines are drawn with as many pixels in a horizontal line as possible + if (!steep) + GfxDrawLineOnBuffer(rt, colour, { x_start, y }, length); + + // Reset non vertical line vars + x_start = x + 1; + length = 0; // NB: will be incremented in next iteration + y += y_step; + error += delta_x; + } + + // Catch the case of the last line + if (x + 1 == x2 && !steep) + { + GfxDrawLineOnBuffer(rt, colour, { x_start, y }, length); + } } } -} + + void GfxDrawLine(RenderTarget& rt, const ScreenLine& line, PaletteIndex colour) + { + auto drawingEngine = rt.DrawingEngine; + if (drawingEngine != nullptr) + { + IDrawingContext* dc = drawingEngine->GetDrawingContext(); + dc->DrawLine(rt, colour, line); + } + } + + void GfxDrawDashedLine( + RenderTarget& rt, const ScreenLine& screenLine, const int32_t dashedLineSegmentLength, const PaletteIndex colour) + { + assert(dashedLineSegmentLength > 0); + + const auto drawingEngine = rt.DrawingEngine; + if (drawingEngine != nullptr) + { + constexpr int32_t kPrecisionFactor = 1000; + + const int32_t dashedLineLength = std::hypot( + screenLine.GetX2() - screenLine.GetX1(), screenLine.GetY2() - screenLine.GetY1()); + const int32_t lineSegmentCount = dashedLineLength / dashedLineSegmentLength / 2; + if (lineSegmentCount == 0) + { + return; + } + + const int32_t lineXDist = std::abs(screenLine.GetX2() - screenLine.GetX1()); + const int32_t lineYDist = std::abs(screenLine.GetY2() - screenLine.GetY1()); + const int32_t dxPrecise = kPrecisionFactor * lineXDist / lineSegmentCount / 2; + const int32_t dyPrecise = kPrecisionFactor * lineYDist / lineSegmentCount / 2; + IDrawingContext* dc = drawingEngine->GetDrawingContext(); + + for (int32_t i = 0, x, y; i < lineSegmentCount; ++i) + { + x = screenLine.GetX1() + dxPrecise * i * 2 / kPrecisionFactor; + y = screenLine.GetY1() + dyPrecise * i * 2 / kPrecisionFactor; + dc->DrawLine(rt, colour, { { x, y }, { x + dxPrecise / kPrecisionFactor, y + dyPrecise / kPrecisionFactor } }); + } + } + } +} // namespace OpenRCT2::Drawing diff --git a/src/openrct2/drawing/Line.h b/src/openrct2/drawing/Line.h new file mode 100644 index 0000000000..a532ea3a2f --- /dev/null +++ b/src/openrct2/drawing/Line.h @@ -0,0 +1,25 @@ +/***************************************************************************** + * Copyright (c) 2014-2026 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#pragma once + +#include + +struct ScreenLine; + +namespace OpenRCT2::Drawing +{ + enum class PaletteIndex : uint8_t; + struct RenderTarget; + + void GfxDrawLine(RenderTarget& rt, const ScreenLine& line, PaletteIndex colour); + void GfxDrawLineSoftware(RenderTarget& rt, const ScreenLine& line, PaletteIndex colour); + void GfxDrawDashedLine( + RenderTarget& rt, const ScreenLine& screenLine, int32_t dashedLineSegmentLength, PaletteIndex colour); +} // namespace OpenRCT2::Drawing diff --git a/src/openrct2/drawing/NewDrawing.cpp b/src/openrct2/drawing/NewDrawing.cpp index 11e026f6d8..983a76f7ac 100644 --- a/src/openrct2/drawing/NewDrawing.cpp +++ b/src/openrct2/drawing/NewDrawing.cpp @@ -163,49 +163,6 @@ void GfxClear(RenderTarget& rt, PaletteIndex paletteIndex) } } -void GfxDrawLine(RenderTarget& rt, const ScreenLine& line, PaletteIndex colour) -{ - auto drawingEngine = rt.DrawingEngine; - if (drawingEngine != nullptr) - { - IDrawingContext* dc = drawingEngine->GetDrawingContext(); - dc->DrawLine(rt, colour, line); - } -} - -void GfxDrawDashedLine( - RenderTarget& rt, const ScreenLine& screenLine, const int32_t dashedLineSegmentLength, const PaletteIndex colour) -{ - assert(dashedLineSegmentLength > 0); - - const auto drawingEngine = rt.DrawingEngine; - if (drawingEngine != nullptr) - { - constexpr int32_t kPrecisionFactor = 1000; - - const int32_t dashedLineLength = std::hypot( - screenLine.GetX2() - screenLine.GetX1(), screenLine.GetY2() - screenLine.GetY1()); - const int32_t lineSegmentCount = dashedLineLength / dashedLineSegmentLength / 2; - if (lineSegmentCount == 0) - { - return; - } - - const int32_t lineXDist = std::abs(screenLine.GetX2() - screenLine.GetX1()); - const int32_t lineYDist = std::abs(screenLine.GetY2() - screenLine.GetY1()); - const int32_t dxPrecise = kPrecisionFactor * lineXDist / lineSegmentCount / 2; - const int32_t dyPrecise = kPrecisionFactor * lineYDist / lineSegmentCount / 2; - IDrawingContext* dc = drawingEngine->GetDrawingContext(); - - for (int32_t i = 0, x, y; i < lineSegmentCount; ++i) - { - x = screenLine.GetX1() + dxPrecise * i * 2 / kPrecisionFactor; - y = screenLine.GetY1() + dyPrecise * i * 2 / kPrecisionFactor; - dc->DrawLine(rt, colour, { { x, y }, { x + dxPrecise / kPrecisionFactor, y + dyPrecise / kPrecisionFactor } }); - } - } -} - void FASTCALL GfxDrawSprite(RenderTarget& rt, const ImageId imageId, const ScreenCoordsXY& coords) { auto drawingEngine = rt.DrawingEngine; diff --git a/src/openrct2/drawing/X8DrawingEngine.cpp b/src/openrct2/drawing/X8DrawingEngine.cpp index 61b41671ee..4bd3fd35fd 100644 --- a/src/openrct2/drawing/X8DrawingEngine.cpp +++ b/src/openrct2/drawing/X8DrawingEngine.cpp @@ -24,6 +24,7 @@ #include "IDrawingEngine.h" #include "InvalidationGrid.h" #include "LightFX.h" +#include "Line.h" #include "TTF.h" #include "WeatherDrawer.h" diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 9eb3d42a09..07bd95fa48 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -265,6 +265,7 @@ + diff --git a/src/openrct2/paint/Paint.cpp b/src/openrct2/paint/Paint.cpp index d62475f30d..6c23548fd3 100644 --- a/src/openrct2/paint/Paint.cpp +++ b/src/openrct2/paint/Paint.cpp @@ -17,6 +17,7 @@ #include "../drawing/Drawing.String.h" #include "../drawing/Drawing.h" #include "../drawing/Font.h" +#include "../drawing/Line.h" #include "../interface/Viewport.h" #include "../localisation/Currency.h" #include "../localisation/Formatting.h"