From 914ff59b3b58314e696ef2014c896cd8485ab901 Mon Sep 17 00:00:00 2001 From: Michael Bernardi Date: Wed, 25 Sep 2024 00:55:38 +1000 Subject: [PATCH] Line drawing algorithms now use screen coords --- .../engines/opengl/OpenGLDrawingEngine.cpp | 18 ++++++------ src/openrct2/drawing/Line.cpp | 29 +++++++++---------- src/openrct2/interface/Viewport.cpp | 9 ------ 3 files changed, 22 insertions(+), 34 deletions(-) diff --git a/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp b/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp index a5d9500356..88b19016d2 100644 --- a/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp +++ b/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp @@ -607,9 +607,8 @@ uint8_t OpenGLDrawingContext::ComputeOutCode( // based on: https://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm bool OpenGLDrawingContext::CohenSutherlandLineClip(ScreenLine& line, const DrawPixelInfo& dpi) { - // TODO (mber) make this work entirely with screen coords. - ScreenCoordsXY topLeft = { dpi.WorldX(), dpi.WorldY() }; - ScreenCoordsXY bottomRight = { dpi.WorldX() + dpi.WorldWidth() - 1, dpi.WorldY() + dpi.WorldHeight() - 1 }; + ScreenCoordsXY topLeft = { dpi.ScreenX(), dpi.ScreenY() }; + ScreenCoordsXY bottomRight = { dpi.ScreenX() + dpi.ScreenWidth() - 1, dpi.ScreenY() + dpi.ScreenHeight() - 1 }; uint8_t outcode1 = ComputeOutCode(line.Point1, topLeft, bottomRight); uint8_t outcode2 = ComputeOutCode(line.Point2, topLeft, bottomRight); @@ -672,8 +671,9 @@ bool OpenGLDrawingContext::CohenSutherlandLineClip(ScreenLine& line, const DrawP void OpenGLDrawingContext::DrawLine(DrawPixelInfo& dpi, uint32_t colour, const ScreenLine& line) { - // TODO (mber) make this work entirely with screen coords. - ScreenLine trimmedLine = line; + const ZoomLevel zoom = dpi.zoom_level; + ScreenLine trimmedLine = { { zoom.ApplyInversedTo(line.GetX1()), zoom.ApplyInversedTo(line.GetY1()) }, + { zoom.ApplyInversedTo(line.GetX2()), zoom.ApplyInversedTo(line.GetY2()) } }; if (!CohenSutherlandLineClip(trimmedLine, dpi)) return; @@ -681,10 +681,10 @@ void OpenGLDrawingContext::DrawLine(DrawPixelInfo& dpi, uint32_t colour, const S DrawLineCommand& command = _commandBuffers.lines.allocate(); - const int32_t x1 = dpi.zoom_level.ApplyInversedTo(trimmedLine.GetX1() - dpi.WorldX()) + _clipLeft; - const int32_t y1 = dpi.zoom_level.ApplyInversedTo(trimmedLine.GetY1() - dpi.WorldY()) + _clipTop; - const int32_t x2 = dpi.zoom_level.ApplyInversedTo(trimmedLine.GetX2() - dpi.WorldX()) + _clipLeft; - const int32_t y2 = dpi.zoom_level.ApplyInversedTo(trimmedLine.GetY2() - dpi.WorldY()) + _clipTop; + const int32_t x1 = trimmedLine.GetX1() - dpi.ScreenX() + _clipLeft; + const int32_t y1 = trimmedLine.GetY1() - dpi.ScreenY() + _clipTop; + const int32_t x2 = trimmedLine.GetX2() - dpi.ScreenX() + _clipLeft; + const int32_t y2 = trimmedLine.GetY2() - dpi.ScreenY() + _clipTop; command.bounds = { x1, y1, x2, y2 }; command.colour = colour & 0xFF; diff --git a/src/openrct2/drawing/Line.cpp b/src/openrct2/drawing/Line.cpp index a7a183880e..ca7291605a 100644 --- a/src/openrct2/drawing/Line.cpp +++ b/src/openrct2/drawing/Line.cpp @@ -18,13 +18,10 @@ */ static void GfxDrawLineOnBuffer(DrawPixelInfo& dpi, char colour, const ScreenCoordsXY& coords, int32_t no_pixels) { - ScreenCoordsXY offset{ coords.x - dpi.WorldX(), coords.y - dpi.WorldY() }; + ScreenCoordsXY offset{ coords.x - dpi.ScreenX(), coords.y - dpi.ScreenY() }; - offset.x = dpi.zoom_level.ApplyInversedTo(offset.x); - offset.y = dpi.zoom_level.ApplyInversedTo(offset.y); - no_pixels = dpi.zoom_level.ApplyInversedTo(no_pixels); - const int32_t width = dpi.zoom_level.ApplyInversedTo(dpi.WorldWidth()); - const int32_t height = dpi.zoom_level.ApplyInversedTo(dpi.WorldHeight()); + const int32_t width = dpi.ScreenWidth(); + const int32_t height = dpi.ScreenHeight(); // Check to make sure point is in the y range if (offset.y < 0) @@ -57,7 +54,7 @@ static void GfxDrawLineOnBuffer(DrawPixelInfo& dpi, char colour, const ScreenCoo } // Get the buffer we are drawing to and move to the first coordinate. - uint8_t* bits_pointer = dpi.bits + offset.y * (static_cast(dpi.pitch) + static_cast(width)) + offset.x; + uint8_t* bits_pointer = dpi.bits + offset.y * dpi.LineStride() + offset.x; // Draw the line to the specified colour for (; no_pixels > 0; --no_pixels, ++bits_pointer) @@ -79,28 +76,28 @@ static void GfxDrawLineOnBuffer(DrawPixelInfo& dpi, char colour, const ScreenCoo void GfxDrawLineSoftware(DrawPixelInfo& dpi, const ScreenLine& line, int32_t colour) { - // TODO: (mber) Rewrite to work with screen DPI coordinates rather than world. - int32_t x1 = line.GetX1(); - int32_t x2 = line.GetX2(); - int32_t y1 = line.GetY1(); - int32_t y2 = line.GetY2(); + const ZoomLevel zoom = dpi.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 < dpi.WorldX()) && (x2 < dpi.WorldX())) + if ((x1 < dpi.ScreenX()) && (x2 < dpi.ScreenX())) { return; } - if ((y1 < dpi.WorldY()) && (y2 < dpi.WorldY())) + if ((y1 < dpi.ScreenY()) && (y2 < dpi.ScreenY())) { return; } - if ((x1 > (dpi.WorldX() + dpi.WorldWidth())) && (x2 > (dpi.WorldX() + dpi.WorldWidth()))) + if ((x1 > (dpi.ScreenX() + dpi.ScreenWidth())) && (x2 > (dpi.ScreenX() + dpi.ScreenWidth()))) { return; } - if ((y1 > (dpi.WorldY() + dpi.WorldHeight())) && (y2 > (dpi.WorldY() + dpi.WorldHeight()))) + if ((y1 > (dpi.ScreenY() + dpi.ScreenHeight())) && (y2 > (dpi.ScreenY() + dpi.ScreenHeight()))) { return; } diff --git a/src/openrct2/interface/Viewport.cpp b/src/openrct2/interface/Viewport.cpp index d7ebc047ce..11322b301f 100644 --- a/src/openrct2/interface/Viewport.cpp +++ b/src/openrct2/interface/Viewport.cpp @@ -2026,14 +2026,5 @@ void ViewportSetSavedView() ZoomLevel ZoomLevel::min() { - // TODO (mber) complete work for sofware zooming or revert change. return ZoomLevel{ -2 }; - // #ifndef DISABLE_OPENGL - // if (drawing_engine_get_type() == DrawingEngine::OpenGL) - // { - // return ZoomLevel{ -2 }; - // } - // #endif - // - // return ZoomLevel{ 0 }; }