Skip to content

Commit

Permalink
metal: Make line drawing match software renderer.
Browse files Browse the repository at this point in the history
Partially fixes Bugzilla #2711.
  • Loading branch information
icculus committed Oct 21, 2020
1 parent c33f808 commit ba36eb0
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion src/render/metal/SDL_render_metal.m
Expand Up @@ -1044,6 +1044,57 @@ - (void)dealloc
return 0;
}

static int
METAL_QueueDrawLines(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FPoint * points, int count)
{
SDL_assert(count >= 2); /* should have been checked at the higher level. */

const size_t vertlen = (sizeof (float) * 2) * count;
float *verts = (float *) SDL_AllocateRenderVertices(renderer, vertlen, DEVICE_ALIGN(8), &cmd->data.draw.first);
if (!verts) {
return -1;
}
cmd->data.draw.count = count;
SDL_memcpy(verts, points, vertlen);

/* If the line segment is completely horizontal or vertical,
make it one pixel longer, to satisfy the diamond-exit rule.
We should probably do this for diagonal lines too, but we'd have to
do some trigonometry to figure out the correct pixel and generally
when we have problems with pixel perfection, it's for straight lines
that are missing a pixel that frames something and not arbitrary
angles. Maybe !!! FIXME for later, though. */

points += count - 2; /* update the last line. */
verts += count - 2;

float xstart = /*0.5f +*/ points[0].x; /* 0.5f to get to the center of the pixel. */
float ystart = /*0.5f +*/ points[0].y;
float xend = /*0.5f +*/ points[1].x;
float yend = /*0.5f +*/ points[1].y;

if (xstart == xend) { /* vertical line */
if (yend > ystart) {
yend += 1.0f;
} else {
ystart += 1.0f;
}
} else if (ystart == yend) { /* horizontal line */
if (xend > xstart) {
xend += 1.0f;
} else {
xstart += 1.0f;
}
}

*(verts++) = xstart;
*(verts++) = ystart;
*(verts++) = xend;
*(verts++) = yend;

return 0;
}

static int
METAL_QueueFillRects(SDL_Renderer * renderer, SDL_RenderCommand *cmd, const SDL_FRect * rects, int count)
{
Expand Down Expand Up @@ -1813,7 +1864,7 @@ - (void)dealloc
renderer->QueueSetViewport = METAL_QueueSetViewport;
renderer->QueueSetDrawColor = METAL_QueueSetDrawColor;
renderer->QueueDrawPoints = METAL_QueueDrawPoints;
renderer->QueueDrawLines = METAL_QueueDrawPoints; // lines and points queue the same way.
renderer->QueueDrawLines = METAL_QueueDrawLines;
renderer->QueueFillRects = METAL_QueueFillRects;
renderer->QueueCopy = METAL_QueueCopy;
renderer->QueueCopyEx = METAL_QueueCopyEx;
Expand Down

0 comments on commit ba36eb0

Please sign in to comment.