SFML community forums

Help => Graphics => Topic started by: Kaymak on August 03, 2020, 08:37:30 pm

Title: Rectangle Shape Bugg
Post by: Kaymak on August 03, 2020, 08:37:30 pm
Good evening lovely community,
im quite new working with SFML.NET using C#, and cannot come around a bug or a coding problem.
I simply try to draw a grid with 32x32 cells on the screen. With my code it works on "Monogame" but not with SFML 2.5. Strangely when i set the Outline Thickness less then .5 the vertical lines dissapear, above .5 it draws the horizontal lines double for some reason.

Here My code:
Quote
RectangleShape rec = new RectangleShape();
            {
                for (int x = Globals.TileView.Left; x < GameData.Map[Form1.instance.listBox1.SelectedIndex].MaxX; x++)
                {
                    for (int y = Globals.TileView.Top; y < GameData.Map[Form1.instance.listBox1.SelectedIndex].MaxY; y++)
                    {
                        rec.OutlineColor = new SFML.Graphics.Color(SFML.Graphics.Color.Red);
                        rec.OutlineThickness =(float) 0.6f;
                        rec.FillColor = new SFML.Graphics.Color(SFML.Graphics.Color.Transparent);
                        rec.Size = new Vector2f(System.Convert.ToSingle(System.Convert.ToSingle(x * GameInfo.PIC_X)), System.Convert.ToSingle(y * GameInfo.PIC_X));
                        rec.Position = new Vector2f(Graphics.ConvertMapX((x - 1) * GameInfo.PIC_X), Graphics.ConvertMapY((y - 1) * GameInfo.PIC_Y));

                        Graphics.GameWindow.Draw(rec);
                    }
                }
            }

Outline Thickness .5
(https://i.gyazo.com/a6c4da108e3bb7de40af12767f487c7c.png)

Outline Thickness .6
(https://i.gyazo.com/7e0f2c1ad34f4b9d8a71c254d8a8ed23.png)
Title: Re: Rectangle Shape Bugg
Post by: Hapax on August 04, 2020, 03:17:31 pm
This is likely to be linked to fractional pixel co-ordinates and the way it can be rounded by OpenGL.

You could try the half-pixel trick: add 0.5 to both the x and y co-ordinate, placing the position in the middle of the pixel, allowing the outline to start from the same place on each side (stays within same pixel at less than 0.5).

That said, a more robust solution is create a your grid using a vertex array (https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php).
You can create the grid of lines with a single vertex array (and just the full lines themselves) but remember to use the half-pixel trick above as the lines should go through the centre of the pixels.
If you also have rectangles/boxes/quads to draw inbetween those gridlines, you can use a vertex array to display all of those too. Just add multiple quads. You could take inspiration from the tile map example (https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php#example-tile-map).


NOTE: it's important that the half-pixel trick above is actually half of a pixel - not just half a unit - so if your co-ordinate system is not 1:1 to the pixels, you will need to adjust for this.