SFML community forums

Help => Graphics => Topic started by: smurf on February 01, 2021, 08:10:20 pm

Title: drawing a 1 pixel thick line
Post by: smurf on February 01, 2021, 08:10:20 pm
How can I draw a simple 1-pixel thick line?
I've tried the obvious but it draws a 2-pixel thick line thats the wrong color! (I'm trying to draw it white but it comes out grey instead).

BTW I'm using the C# bindings if that matters.


Vertex[] line = new Vertex[2];
line[0].Position.X = 10;
line[0].Position.Y = 10;
line[0].Color.R = col.R;
line[0].Color.G = col.G;
line[0].Color.B = col.B;
line[0].Color.A = col.A;

line[1].Position.X = 10;
line[1].Position.Y = 100;
line[1].Color.R = col.R;
line[1].Color.G = col.G;
line[1].Color.B = col.B;
line[1].Color.A = col.A;

currentRenderTarget.Draw(line,0, 2, PrimitiveType.Lines);
Title: Re: drawing a 1 pixel thick line
Post by: Ignatius_Z on February 02, 2021, 12:21:06 am
is anti-aliasing on it or something?
Title: Re: drawing a 1 pixel thick line
Post by: smurf on February 02, 2021, 03:04:18 am
I don't think so.
I don't see anywhere to set anti-aliasing in regards to these lines.
Title: Re: drawing a 1 pixel thick line
Post by: Laurent on February 02, 2021, 07:51:24 am
Try 10.5 and 100.5 for the points coordinates. I assume that you didn't change the view, nor resized the window.
Title: Re: drawing a 1 pixel thick line
Post by: smurf on February 02, 2021, 05:21:22 pm
Very interesting, thank you that worked!

Now the obvious next question, is there a global setting to add (or subtract) 0.5 from all line coordinates or something like that?

I'm trying to build a pixel-perfect drawing that is composed of lines, and all of my inputs and math is using integers. It would be really nice to have a global solution rather than adding 0.5 to everything all over the place.

Interestingly, when I draw a RectangleShape it doesn't have this problem.
Title: Re: drawing a 1 pixel thick line
Post by: Laurent on February 02, 2021, 06:07:36 pm
This only applies to points and lines primitives.

A simple solution would be to offset the whole view by (0.5, 0.5). But then you'll probably have troubles with other shapes (sprites, rectangles, ...). So adding the offset manually, only for lines and points, is probably the best solution.
Title: Re: drawing a 1 pixel thick line
Post by: smurf on February 02, 2021, 06:25:58 pm
Disappointing answer but I really appreciate your time (and your library) anyway!

Thanks,