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

Author Topic: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.  (Read 23621 times)

0 Members and 1 Guest are viewing this topic.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #15 on: July 16, 2013, 02:08:53 pm »
It must be the same reason why very first code didin't work: operator[] returns a copy of the vertex, even in a native array.
Laurent Gomila - SFML developer

Anteara

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #16 on: July 16, 2013, 03:20:00 pm »
It must be the same reason why very first code didin't work: operator[] returns a copy of the vertex, even in a native array.

Dang. I think I could get this to work by using unsafe code but I don't really want to do that. I tried using lists to get it to work but again couldn't figure that out (since I've never used lists before).

Do you have any ideas? Or do you think I should just use unsafe code if I want it to be as efficient as possible?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #17 on: July 16, 2013, 03:44:51 pm »
I don't think you can do better than your current code. Don't focus too much on this, this is not a real problem.
Laurent Gomila - SFML developer

Anteara

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #18 on: July 16, 2013, 03:51:36 pm »
I don't think you can do better than your current code. Don't focus too much on this, this is not a real problem.

Alright, I guess you're right, and maybe my optimizations are premature, maybe these are the things that are going to take the bulk of the CPU usage anyway, the rest of my engine would just "sit on top of it", for the lack of better words. And that kind of makes sense because the renderer probably is what would take the most CPU usage.
I'm just worried my program will have high CPU usage, but I guess i'll implement more stuff before worrying about it :)

Thanks for the help :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #19 on: July 16, 2013, 04:06:42 pm »
Do you activate vertical synchronization in your program?
Laurent Gomila - SFML developer

Anteara

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #20 on: July 16, 2013, 04:49:19 pm »
Do you activate vertical synchronization in your program?

I set vertical sync to enabled on my RenderWindow, however I'm using the handle of a panel in WinForms.

After your comment i checked fraps and in fact the window is at 500 FPS, I'm not sure, however, if that means that my main loop is being executed far too many times, since I did apply vertical sync to the RenderWindow.

I'll try and see if I can figure out how to limit the framerate of the WinForm.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #21 on: July 16, 2013, 07:00:18 pm »
I'll try and see if I can figure out how to limit the framerate of the WinForm.

Check your graphics driver settings to see if you can force VSync on. If that doesn't change your FPS then try disabled VSync and using SFML's set framerate limit function.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Anteara

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #22 on: July 17, 2013, 08:15:47 am »
Tried all of that and it doesn't change my fps unfortunately.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #23 on: July 17, 2013, 08:38:57 am »
How do you trigger refresh cycles for your widget? A fixed timer? A "repaint" event?
Laurent Gomila - SFML developer

Anteara

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #24 on: July 17, 2013, 09:30:45 am »
How do you trigger refresh cycles for your widget? A fixed timer? A "repaint" event?

Well, I don't really rely on events from the winforms to do the refreshing; only for other things (like responding to button click events etc)

So, in the form_load function I initiate the window and loop:
I *think* form_load function is called only once, on initial startup.

        private void Form1_Load(object sender, EventArgs e)
        {
            Init();
            //create the window to bind to the form
            SFMLWindow = new RenderWindow(this.Handle); // Create the SFMLWindow from the handle of any control. In this example we will use our current forms handle
            //SFMLWindow.SetVerticalSyncEnabled(true);
            //SFMLWindow.SetFramerateLimit(60);
            //start the game loop
            MainLoop();
        }

In my main loop, I do all the normal stuff, but use while formopen instead of the normal way, as It didn't seem to work, probably because it's within a form:

 while (FormOpen)
            {
                ProcessInput();
                window.Clear(SFML.Graphics.Color.White);
                ...
            }

Perhaps I could use a timer to only have it called 60 times per second, I'll look in to that.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #25 on: July 17, 2013, 10:06:34 am »
You're running your game loop in Form1_Load, so it never returns? ???
Are your window & other widgets responsive? Everything works fine??
Laurent Gomila - SFML developer

Anteara

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #26 on: July 17, 2013, 11:33:25 am »
Yeah everything is responsive and runs fine. I'm not running my game loop from Form1_Load, I'm calling it there though. I should probably have specified that my Form1 class is also serving as my 'Engine' class to which everything else is created.

Here's a video showing what it working:



p.s. what exactly do youm ean by "so it never returns?"?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #27 on: July 17, 2013, 02:25:40 pm »
Quote
what exactly do youm ean by "so it never returns?
Because of this:
while (FormOpen)

So whoever calls Form1_Load (the UI thread or whatever) will never see this call end, and do whatever has to be done after it.

When you integrate SFML into a UI, you should not run a blocking main loop. You should rather use a timer or the repaint event to refresh your SFML view, while not blocking the main flow of the application (which is most likely already driven by the main UI system and internal events).
Laurent Gomila - SFML developer

Anteara

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #28 on: July 18, 2013, 07:19:14 am »
Ah, Okay. So I think I understand what your saying:

Because I've implemented my loop in Form1_Load, which will only stop looping if the form is closed, nothing will ever be executed below the loop, so the function can never exit and return void; and some events may be waiting for Form1_Load to return void.

Instead, I should use a main loop in a less traditional sense; have no while loop at all, instead rely on constantly firing events to execute my game code, such as the ReDraw event. If i put my loop inside the ReDraw event function, it essentially acts as a loop as ReDraw is always being called.

OR, use a Timer and in the timer_tick (or whatever its called) implement my loop there (again, without a technical while loop)

Did I interpret that correctly?


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: sf::Vertex* quad = &m_vertices[(i + j * width) * 4]; from C++ to C#.
« Reply #29 on: July 18, 2013, 08:33:32 am »
Quote
Did I interpret that correctly?
Absolutely ;)
Laurent Gomila - SFML developer