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

Author Topic: Main while loop is not executing  (Read 9650 times)

0 Members and 1 Guest are viewing this topic.

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Main while loop is not executing
« on: January 22, 2013, 07:13:43 pm »
Hy,
I've been working on graphics application for a while, but suddenly I'm experiencing some trouble with it. Somehow the main loop is not being executed. I've managed to make a simple example which still shows my problem:

sf::Window window(sf::VideoMode(1024, 576), "First Person Shooter");
        window.setVerticalSyncEnabled(true); // 60 FPS

        sf::Clock c;
        unsigned int fps_counter = 0;
        unsigned int fps = 0;

        while (true)
        {
                sf::Time time = c.getElapsedTime();
                c.restart();
                unsigned int ms = time.asMilliseconds();

                fps_counter += ms;
                ++fps;
                if(fps_counter >= FPS_REFRESH) {
                        char buffer[32];
                        int av = (int) (fps * 1000) / fps_counter;
                        sprintf(buffer, "%d - frames per second", av);
                        window.setTitle(buffer);

                        // reset
                        fps_counter = 0;
                        fps = 0;
                }

                //draw();

                window.display();
        }

        window.close();

What I'm trying to do is to print out the frames per second. But the title of the window is never updated...
I suppose I'm overlooking something very easy... :/
Anyone a suggestion?

// edit
Forgot to mention this:
When I add one line in the loop, for example
cout << "test" << endl;
It all suddenly works.
« Last Edit: January 22, 2013, 07:46:49 pm by genzm »

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Main while loop is not executing
« Reply #1 on: January 22, 2013, 07:54:37 pm »
Ugly code but works for me, empty window opens and it's caption keeps changing.
Maybe you're just getting consistent fps and it's udating with same non stop?
Back to C++ gamedev with SFML in May 2023

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Main while loop is not executing
« Reply #2 on: January 22, 2013, 07:59:15 pm »
No, I get the intial title, being: "First Person Shooter".
And maybe just as a tip, what do find so ugly about the code?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Main while loop is not executing
« Reply #3 on: January 22, 2013, 08:09:27 pm »
while (true)
Never ever do this. It doesn't matter if it's just for testing or whatever.

sf::Time time = c.getElapsedTime();
c.restart();
unsigned int ms = time.asMilliseconds();

fps_counter += ms;
Why all those indirections?
This looks much better.
fps_counter += c.restart().asMilliseconds();

FPS_REFRESH is not defined in the code you gave us.


char buffer[32];
int av = (int) (fps * 1000) / fps_counter;
sprintf(buffer, "%d - frames per second", av);
window.setTitle(buffer);
This is positional dangerous C (and not C++) code.
You should better use a std::stringstream instead and don't use C-casts.

//draw();
window.display();
You never call window.clear(). It's a must!

As for the problem, I don't really know...

What OS do you use?
« Last Edit: January 22, 2013, 08:14:17 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Main while loop is not executing
« Reply #4 on: January 22, 2013, 08:11:39 pm »
I use windows.
Most of the things you mention are different in my original code, but I didn't know about the stringstream, so thanks for that. :P

As for the window.clear();
Why is this a must?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Main while loop is not executing
« Reply #5 on: January 22, 2013, 08:12:14 pm »
sf::Time time = c.getElapsedTime();
        c.restart();
        unsigned int ms = time.asMilliseconds();

        fps_counter += ms;
=>
fps_counter+=c.restart().asMilliseconds();

FPS_REFRESH
Macro in c++ = no-no (I'm guessing it's macro, I put 16 there, maybe you have your constant there wrong).

    window.close();
Redundant.
sprintf(buffer, "%d - frames per second", av);
C and potentially unsafe for anything non-trivial, I use boost.format instead for printf-like formating into console, file, string streams.
std::stringstream ss;
ss<<boost::format("%1% frames per second")%some_number;
window.setTitle(ss.str());
 

Quote
Never ever do this. It doesn't matter if it's just for testing or whatever.
Really? Warum nicht?

Oh and static_cast<int>() instead of (int) or int() like he says.

Exploiter, you cannot clear a window, renderwindow yes but not plain window.
« Last Edit: January 22, 2013, 08:14:48 pm by FRex »
Back to C++ gamedev with SFML in May 2023

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Main while loop is not executing
« Reply #6 on: January 22, 2013, 08:18:09 pm »
while (true) isn't good, because this will use your cpu fully.
Or at least that's what they told me :P

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Main while loop is not executing
« Reply #7 on: January 22, 2013, 08:20:12 pm »
Really? Warum nicht?
Because you intentionally create an infinite loop, that your only be able to break; out of, which isn't a good design. One can discuss forever about this and I don't feel like having a big discussion now. It's the same reason, why one shouldn't use goto and use break only limited, etc.
I'm aware of the pros and cons and if you're not really experienced, it's just a nono.

And even when doing some tests, it's essential to stick to the principles, so one doesn't even have to worry about rewriting everything, once the testing is over.

while (true) isn't good, because this will use your cpu fully.
Or at least that's what they told me :P
No and yes. It will use you CPU fully, but it's not because of while(true) and to not use it at the maximum you setFramerateLimit(60).

As for the window.clear();
Why is this a must?
Exploiter, you cannot clear a window, renderwindow yes but not plain window.
Right, I didn't see, that he's using sf::Window. I usually assume people that don't know stringstreams don't even try to go the OpenGL way... :D
« Last Edit: January 22, 2013, 08:23:11 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Main while loop is not executing
« Reply #8 on: January 22, 2013, 08:22:30 pm »
Oh, don't under estimate me :P I do have quite some experience.
I'm just new to C and C++. But yes, I am using OpenGL.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Main while loop is not executing
« Reply #9 on: January 22, 2013, 08:24:01 pm »
genzm, go check what FPS_REFRESH is, it's not declared in your code, it might be wrong.

Quote
I usually assume people that don't know stringstreams don't even try to go the OpenGL way... :D
Obviously C graphics API reqires knowledge of c++ ::) Poor Carmack and his first engines in pure C...
"First Person Shooter"
Last time I checked Evil Laurent™ didn't add any 3D features(nor did binary write that 3D magic for Tank to start that GLEW argument again)..
Back to C++ gamedev with SFML in May 2023

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Main while loop is not executing
« Reply #10 on: January 22, 2013, 08:30:44 pm »
Is used:
#define FPS_REFRESH 500


FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Main while loop is not executing
« Reply #11 on: January 22, 2013, 08:32:23 pm »
Still works.
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Main while loop is not executing
« Reply #12 on: January 22, 2013, 08:33:34 pm »
Obviously C graphics API reqires knowledge of c++ ::) Poor Carmack and his first engines in pure C...
Where's the pure C when he's using SFML, a C++ library? (I probably didn't get that joke...)

Last time I checked Evil Laurent™ didn't add any 3D features
So? You can do all the 3D magic with OpenGL and use SFML just as context creator and optional audio output. ;)

#define FPS_REFRESH 500
Don't use macros!
const int FPS_REFRESH = 500;
...works too! ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Main while loop is not executing
« Reply #13 on: January 22, 2013, 08:34:06 pm »
Quote
Exploiter, you cannot clear a window, renderwindow yes but not plain window.

There's glClear() for that, the parameter depends on whether you use a depth buffer or not.

Quote
I'm just new to C and C++. But yes, I am using OpenGL.

JOGL before I suppose.
« Last Edit: January 22, 2013, 08:35:42 pm by masskiller »
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

genzm

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: Main while loop is not executing
« Reply #14 on: January 22, 2013, 08:36:16 pm »
I know I'm not supposed to use macro's, but that isn't going to solve the problem now, is it?

And yes, I used java before. But this is still my first openGL/sfml experience, so I never used JOGL.