Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Rectangle Shape Bugg  (Read 1661 times)

0 Members and 1 Guest are viewing this topic.

Kaymak

  • Newbie
  • *
  • Posts: 1
    • View Profile
Rectangle Shape Bugg
« 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


Outline Thickness .6

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Rectangle Shape Bugg
« Reply #1 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.
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.


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.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything