SFML community forums

Help => Graphics => Topic started by: smurf on September 16, 2022, 04:01:53 pm

Title: VertexArray with Texture not working - nothing drawn
Post by: smurf on September 16, 2022, 04:01:53 pm
See below for a minimal example. I've tried lots of variations of this code as well.
Nothing is drawn on the screen (except the green background color). If I instead create sprites from this same texture, I can draw those no problem.
If I add a white color to the vertices, and don't pass the RenderStates to Draw(), then it draws a white square of the expected size. So it seems to be just the texture association with the vertex array thats broken.

using SFML.Graphics;
using SFML.System;
using SFML.Window;

namespace VAD
{
    class Program
    {      
        static void Main(string[] args)
        {
            VideoMode videoMode = new VideoMode(500, 500);
            RenderWindow window = new RenderWindow(videoMode, "vertex arrays");

            VertexArray va = new VertexArray(PrimitiveType.Quads);            
            Texture theTexture = new Texture(@"C:\pics\house.png");
            RenderStates renderStates = new RenderStates();
            renderStates.Texture = theTexture;

            va.Append(new Vertex(new Vector2f(0, 0), new Vector2f(0, 0)));
            va.Append(new Vertex(new Vector2f(500, 0), new Vector2f(100, 0)));
            va.Append(new Vertex(new Vector2f(500, 500), new Vector2f(100, 100)));
            va.Append(new Vertex(new Vector2f(0, 500), new Vector2f(0, 100)));

            while (true)
            {
                window.DispatchEvents();
                window.Clear(Color.Green);
                window.Draw(va, renderStates);
                window.Display();
            }
        }
    }
}

 


This is C# obviously, using the SFML 2.5 nuget package. .NET6, Windows 10, Visual Studio 2022.
Title: Re: VertexArray with Texture not working - nothing drawn
Post by: eXpl0it3r on September 20, 2022, 08:54:14 pm
The issue isn't very obvious and stems from some constraints towards the CSFML interop.

The solution is, turning this
            RenderStates renderStates = new RenderStates();
            renderStates.Texture = theTexture;

into this
            RenderStates renderStates = new RenderStates(theTexture);
Title: Re: VertexArray with Texture not working - nothing drawn
Post by: smurf on September 23, 2022, 05:15:17 am
Oh wow that is super tricky. Thank you!!

It seems that when using the constructor, other properties are set besides just the texture that I passed in.
But when using the parameterless ctor and just setting the texture property, those other properties (like transform) are NOT set.

maybe an improvement would be, in the setter for the properties, the transform property is also set to Identity matrix, if it has not already been set to something.