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

Author Topic: Framerate Issues  (Read 2501 times)

0 Members and 1 Guest are viewing this topic.

Luis

  • Newbie
  • *
  • Posts: 14
    • View Profile
Framerate Issues
« on: April 29, 2012, 03:05:48 am »
Hello,

When I make a new window, I define it as follows (I am using SFML 2.0):
void Render::createWindow(unsigned int w, unsigned int h) {
        //Constructor
        width = w;
        height = h;
        window.create(sf::VideoMode(width, height), "Smart Traffic Simulation", sf::Style::Close);

        window.setVerticalSyncEnabled(true); //Tried leaving it to default false as well
        window.setFramerateLimit(60);
}
 

window being a static member of Render:: defined elsewhere in the render.cpp file.

The above function should limit the framerate to about 60 with auto Sleep() function calls, give or take Window's 16ms discrepancies. However, when I measure framerate in the Game's main loop:

float fps = 1000.f / (float)Clock.getElapsedTime().asSeconds();
 
Clock being defined as: sf::Clock Clock;

I get very high numbers, sub 1000. Further, 25% of my CPU is used, and the application window (SFML generated one) lags incredibly (not c++ console however).

Is this a slight bug in SFML 2.0? I have an Intel Quad-Core CPU.

Thanks.
« Last Edit: April 29, 2012, 10:16:15 am by Laurent »

Lo-X

  • Hero Member
  • *****
  • Posts: 618
    • View Profile
    • My personal website, with CV, portfolio and projects
Re: Framerate Issues
« Reply #1 on: April 29, 2012, 03:41:32 am »
1) Do you restart your clock ?
2) Instead of dividing 1000 by the number of seconds, why don't you use sf::Time::asMillisecond or 1.f / seconds ? (your calcluation is false)
« Last Edit: April 29, 2012, 05:57:55 am by Lo-X »

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Framerate Issues
« Reply #2 on: April 29, 2012, 04:05:07 am »
Your equation for calculating framerate is wrong. It should be 1/seconds, not 1000/seconds.
I use the latest build of SFML2

Luis

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Framerate Issues
« Reply #3 on: April 29, 2012, 06:53:26 pm »
Thanks for the replies.

I forgot to reset the clock after the end of the game loop, which I now do.
Further, I fixed the FPS calculation, yet now my FPS is hovering around 1.7648.

void Render::render() {
        if(!Game::closing && Render::window.isOpen()) {
                window.clear();

                //Draw all terrain
                Terrain::render();

                if(Control::renderCursor) {
                        window.draw(Control::rect);
                }
                window.display();
        }
}
 

That is the only drawing code present. Terrain::render() simply loops through all block instances:

void Terrain::render() {
        //Render whole terrain
        int slot = 0;
        int line = 0;

        for (unsigned int i = 0; i < 3702; i++) {
                tileData[i].draw(slot*16,line*16);
               
                //Counter
                slot++;

                //Reset row count
                if (slot == 64) {
                        slot = 0;
                        line++;
                }
        }
}
 


With this being defined as block::draw(int xx, int yy) :

void block::draw(int xx, int yy) {
        //Draw this block

        if(!Game::closing) {
                sf::RectangleShape rect;
                rect.setSize(sf::Vector2f(16, 16));

                if(type) {
                        rect.setFillColor(sf::Color::Black);
                } else {
                        rect.setFillColor(sf::Color::White);
                        /*
                        sf::Text text;
                        text.setString(tileNumber);
                        text.setCharacterSize(12);
                        text.setPosition(xx, yy);
                        text.setColor(sf::Color::Black);
                        Render::window.draw(text);
                        */

                }

                rect.setOutlineThickness(1);
                rect.setOutlineColor(sf::Color::Black);
                rect.setPosition(sf::Vector2f(xx, yy));

                Render::window.draw(rect);
        }
}
 

And yes, I do realize that I shouldn't be defining the rect parameters in a draw event (redundant+unnecessary).

I also thought that when you set the FPS limit with SFML, it would auto sleep and I wouldn't need to call the Sleep() function manually, however that is obviously not the case here.

Thanks again.
« Last Edit: April 29, 2012, 06:56:15 pm by Luis »

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Framerate Issues
« Reply #4 on: April 29, 2012, 07:28:41 pm »
Well, you ARE rendering a LOT. What graphics card do you have? Are your drivers up to date?
I use the latest build of SFML2

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
Re: Framerate Issues
« Reply #5 on: April 29, 2012, 10:39:46 pm »
 for (unsigned int i = 0; i < 3702; i++)

3702 tiles are not a trivial task to draw and this for every frame.
I'm not sure if you're moving your tiles inbetween. If you don't, I'd suggest you render all your tiles onto a sf::RenderTexture and then just extract that texture. With that you only have to make once those 3702 draw calls and afterwards just draw the extracted texture with one call.

The other way would be to use sf::VertexArray and map your tile textures onto the vertices. With that you'll also only have to make one draw call and you'll gain much performance.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Luis

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Framerate Issues
« Reply #6 on: April 30, 2012, 11:31:54 pm »
 for (unsigned int i = 0; i < 3702; i++)

3702 tiles are not a trivial task to draw and this for every frame.
I'm not sure if you're moving your tiles inbetween. If you don't, I'd suggest you render all your tiles onto a sf::RenderTexture and then just extract that texture. With that you only have to make once those 3702 draw calls and afterwards just draw the extracted texture with one call.

The other way would be to use sf::VertexArray and map your tile textures onto the vertices. With that you'll also only have to make one draw call and you'll gain much performance.

Thanks for the reply. It did turn out it was a bad idea to render all those in every step. The FPS is hovering around 60 now. However, now I have a slight question with regard to rendering on the texture. Would it be done like this:

                tileData[i]->rect.setSize(sf::Vector2f(16, 16));
                tileData[i]->rect.setOutlineThickness(1);
                tileData[i]->rect.setOutlineColor(sf::Color::Black);
                tileData[i]->rect.setPosition(sf::Vector2f(tileData[i]->x, tileData[i]->y));

                //Reset texture
                worldTexture.draw(tileData[i]->rect);
 

And then in the Render::render() script execute:
worldTexture.display();
 

?
I'm not terribly familiar with the sf::RenderTexture class and the SFML 2.0 documentation isn't exactly thorough in how to address this (at least not in the documentation I've had access to).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10820
    • View Profile
    • development blog
    • Email
Re: Framerate Issues
« Reply #7 on: May 01, 2012, 12:06:24 am »
A sf::RenderTexture is just a off-screen place you can render to, just think of it as if you'd draw directly to the screen.
You first mostly clear it, then draw you stuff and to finally show up on the non visible 'screen' you display it.

Then extract the texture with getTexture() and use a sf::Sprite (or sf::Shape) to draw it to the screen.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Luis

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Framerate Issues
« Reply #8 on: May 03, 2012, 12:59:43 am »
Thanks! That proved to work just fine!