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

Author Topic: [SOLVED] - Bug with RenderState.Texture when using VertexArray?  (Read 4260 times)

0 Members and 1 Guest are viewing this topic.

NZSpy

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Hello,

I have just started using the SFML.Net Bindings and I appear to be having an issue with the VertexArray and binding the texture.  In the code below I am loading a ship image.  I set up the VertexArray with Texture and Vertex Coords.  When I call display with the Vertexarray and the RenderStates I get a black screen.  Am I doing something wrong or is it a bug?  Thanks in advance.

EDIT:  Below code now works by calling RenderState(<texture>).

 public void Run()
                {
                        _gamewindow = new RenderWindow( new VideoMode(800,600),"Simple2D Game                  Window",Styles.Default);
                        _gamewindow.Closed += new EventHandler(OnClosed);
                        _gamewindow.KeyPressed += new EventHandler<KeyEventArgs>(OnKeyPressed);

                        int prevtime = 0;
                        int currtime = 0;
                        int elapsedtime = 0;
                        int totalelapsedtime = 0;

                        Texture tex = new Texture (@"Ship.png");

                        VertexArray array = new VertexArray ();
                        array.PrimitiveType = PrimitiveType.Quads;

                        Vertex vert1 = new Vertex ();
                        vert1.Position = new Vector2f (10, 10);
                        vert1.Color = Color.White;
                        vert1.TexCoords = new Vector2f (0, 0);
                        array.Append (vert1);


                        Vertex vert2 = new Vertex ();
                        vert2.Position = new Vector2f (90, 10);
                        vert2.Color = Color.White;
                        vert2.TexCoords = new Vector2f (80, 0);
                        array.Append (vert2);

                        Vertex vert3 = new Vertex ();
                        vert3.Position = new Vector2f (90, 93);
                        vert3.Color = Color.White;
                        vert3.TexCoords = new Vector2f (80, 83);
                        array.Append (vert3);

                        Vertex vert4 = new Vertex ();
                        vert4.Position = new Vector2f (10, 93);
                        vert4.TexCoords = new Vector2f (0, 83);
                        vert4.Color = Color.White;
                        array.Append (vert4);

                        RenderStates renStates = new RenderStates (tex);

                        while (_gamewindow.IsOpen())
                        {
                                currtime = Environment.TickCount;
                                elapsedtime = currtime - prevtime;
                                prevtime = currtime;
                                totalelapsedtime += elapsedtime;

                                _gamewindow.DispatchEvents();

                                if(totalelapsedtime>=15)
                                {
                                        Update (totalelapsedtime);
                                        totalelapsedtime=0;
                                }

                                _gamewindow.Clear(Color.Black);
                                _gamewindow.Draw (array,renStates);
                                _gamewindow.Display();
                        }
                }


 

So, after downloading the source for the .Net Bindings.  I saw there was a constructor that takes a Texture parameter.  I created the renderstate by passing my texture to the constructor and this works.  For some resaon the Constructors aren't showing up in my IDE(Xamarin, but that's another issue.)

It seems that when you assign values to the RenderStates structure they don't get assigned or kept internally for some reason.
« Last Edit: July 03, 2013, 07:37:40 pm by NZSpy »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SOLVED] - Bug with RenderState.Texture when using VertexArray?
« Reply #1 on: July 03, 2013, 08:09:44 pm »
Quote
It seems that when you assign values to the RenderStates structure they don't get assigned or kept internally for some reason.
Strange. What did your previous code look like?
Laurent Gomila - SFML developer

NZSpy

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: [SOLVED] - Bug with RenderState.Texture when using VertexArray?
« Reply #2 on: July 03, 2013, 08:25:53 pm »
Hi Laurent,

All you have to do is substitute:

                RenderStates renStates = new RenderStates(tex);

for:
      RenderStates renStates = new RenderStates ();
             renStates.Texture = tex;

If you do that with the posted code you won't see any texture on the screen.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SOLVED] - Bug with RenderState.Texture when using VertexArray?
« Reply #3 on: July 03, 2013, 08:50:24 pm »
Structures in C# cannot have a user-defined default (parameter-less) constructor. When you instanciate a structure with its default constructor, the .Net runtime fills the structure with zeroes. From my point of view of C++ developer that's weird and totally unintuitive (especially since you can't disable the default constructor either), but that's how it works.

So if you don't use one of the provided constructors, you have to explicitly initialize all the members with a correct value.
Laurent Gomila - SFML developer

NZSpy

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: [SOLVED] - Bug with RenderState.Texture when using VertexArray?
« Reply #4 on: July 03, 2013, 08:56:12 pm »
Very odd.  I have coded in C# for years but I rarely use structures for anything.  Thanks for the response  :)

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: [SOLVED] - Bug with RenderState.Texture when using VertexArray?
« Reply #5 on: July 04, 2013, 11:25:04 pm »
On a side note, the way your doing your fixed time step is wrong.

                if(totalelapsedtime>=15)
                {
                    Update (totalelapsedtime);
                    totalelapsedtime=0;
                }

With your current code you really don't have a fixed time step. You should change your code to...

               while (totalelapsedtime>=15)
                {
                    Update (15);
                    totalelapsedtime -= 15;
                }


Quote
From my point of view of C++ developer that's weird and totally unintuitive (especially since you can't disable the default constructor either)

I totally agree with you laurent, personally structures in c# have caused me more trouble than they are worth.
« Last Edit: July 04, 2013, 11:35:46 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

 

anything