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

Author Topic: Beginner - SFML wait for message  (Read 4721 times)

0 Members and 1 Guest are viewing this topic.

Lowest0ne

  • Newbie
  • *
  • Posts: 11
    • View Profile
Beginner - SFML wait for message
« on: June 28, 2012, 10:03:19 pm »
Hi all, I just installed SFML and went through the "make a window" tutorial.  Almost everything is okay :)

I'm sure this question is asked a lot...  The program using all of the computer's resources (100% of a CPU and 1000 FPS).  I would like to be able to make the program do nothing unless it gets a message to do something (you know, like notepad).  Is this possible?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Beginner - SFML wait for message
« Reply #1 on: June 28, 2012, 11:25:23 pm »
I'm sure this question is asked a lot...

Well have you used the search function then? ::)

You can use window.setFramerateLimit(60) which will make use of sf::Sleep which will put your application to sleep and not waste valuable CPU time.
SFML was designed to run with a certain framerate, if you're only interested in GUI applications, SFML isn't what you're looking for, but maybe Qt would be?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Celtic Minstrel

  • Jr. Member
  • **
  • Posts: 80
    • View Profile
Re: Beginner - SFML wait for message
« Reply #2 on: June 28, 2012, 11:40:59 pm »
In SFML 2.0, you can use app.waitEvent() instead of app.pollEvent().

Lowest0ne

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Beginner - SFML wait for message
« Reply #3 on: June 29, 2012, 05:58:56 pm »
I did look for an answer, here and elsewhere.

Like the topic says, I'm a beginner.  Right now I am making the data structures for a game, so although I'm mostly doing GUI right now I don't want to have been locked into the Win32 API.  On the other hand, a program like VLC media player (which uses hardware rendering I would guess), doesn't use %50 of the CPU when paused.

I'll look into sf::Sleep.  Windows' Sleep is not good, because it makes the whole program unresponsive for that amount of time. 

Is 2.0 stable?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Beginner - SFML wait for message
« Reply #4 on: June 29, 2012, 09:48:01 pm »
One should always pay attention to the details or better reread what someelse posts, because you kind of ingored many parts...

...VLC media player (which uses hardware rendering I would guess), doesn't use %50 of the CPU when paused.
Yes I'm sure VLC is making use of hardware acceleration for the video rendering part. For the whole GUI stuff they're using wxWidgets and/or Qt 4.
As I've mentioned before (= details), Qt is a great crossplatform GUI library, i.e. you won't be stuck with Win API...

I'll look into sf::Sleep.  Windows' Sleep is not good, because it makes the whole program unresponsive for that amount of time. 

You'd better look into sf::RenderWindow::setFramerateLimit(float). Also sf::Sleep is basically a wrapper for the OS specific sleep functions, thus it will have the same effect as sleep() on Windows. What matters is just how you use it.

Is 2.0 stable?
SFML 2.0RC is a Release Candidate (RC), which means the API won't change that much if at all. The stability of the library itself was always kind of guaranteed. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lowest0ne

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Beginner - SFML wait for message
« Reply #5 on: June 30, 2012, 06:45:03 pm »
I appreciate the help, but not the attitude.

Coming from GLUT, there are two very different functions: glutIdleFunc() and glutPostRedisplay().  Framerate/Sleeping has nothing to do with it.  Using Idle will loop as fast as told to (Framerate/Sleeping), and PostRedisplay is called when drawing is needed.

I'm looking to see if SFML has something similar.  If not, then I might as well just stick with the "more complex" APIs.
« Last Edit: June 30, 2012, 06:46:37 pm by Lowest0ne »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Beginner - SFML wait for message
« Reply #6 on: June 30, 2012, 11:47:48 pm »
I appreciate the help, but not the attitude.
Well thank you! :)
My attitude only reflects how questions are ask, specially how much of the suggested rules have been 'implemented' into the question and how the asker responses to given suggestions & solutions.

Coming from GLUT, there are two very different functions: glutIdleFunc() and glutPostRedisplay().  Framerate/Sleeping has nothing to do with it.  Using Idle will loop as fast as told to (Framerate/Sleeping), and PostRedisplay is called when drawing is needed.

Why couldn't you have said that in your opening question?
If you have a question then don't hold back on information.

Since I don't know GLUT, I can't really tell if you can implement something similar to this.
Reading a bit about the functions I can say that SFML doesn't provide such a function.
If you really need that effect you'd best of with using GLUT directly, otherwise you'd probably have to change some of your application design.

Since I'm curious do you mind ellaborate what the two mentioned functions exactly do, or better how/where to use them. The documentation isn't clear at all imho.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lowest0ne

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Beginner - SFML wait for message
« Reply #7 on: July 01, 2012, 12:29:06 am »
Quote
Why couldn't you have said that in your opening question?
Just because I figured the question had been asked a lot.  Anyway...

Basically, a display function is given to GLUT to use: glutDisplayFunc(myRenderSceneFunction).

glutIdlefunc() is given a function to do whenever the program is idle.  Most beginner tutorials have this function calling "MyRenderSceneFunction", so basically the graphics card catches on fire.

The solution is to set up a timer and use glutPostRedisplay, which would call the function defined with glutDisplayFunc().  Here's the code for the timer function that I have right now:
Code: [Select]
void update(int value) {


if (camera.moving())
{
camera.moveX();
camera.moveZ();
camera.moveY();
glutPostRedisplay(); //Tell GLUT that the scene has changed

}
//Tell GLUT to call update again in 25 milliseconds
glutTimerFunc(25, update, 0);
}

It is a very simple program.  The main thing is that rendering is never done unless there is something to render.  The timer simply ensures that framerate stays low.

I imagine something like this is possible with SFML, however it isn't with the way the tutorial code is presented, basically:
Code: [Select]
bool Running = true;
while (Running)
{
    App.Display();
}

We can limit framerate, but this still doesn't solve the fact that if nothing is happening, the frame shouldn't be updated.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Beginner - SFML wait for message
« Reply #8 on: July 01, 2012, 08:53:16 am »
SFML doesn't forward the "paint" event (which tells whenever the window needs to be refreshed). This is not how things must be done nowadays. A main loop and vertical-sync enabled is all that you need, don't try to keep the same structure as you had in programs that use 20-year old APIs.
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Beginner - SFML wait for message
« Reply #9 on: July 02, 2012, 03:16:41 pm »
SFML doesn't forward the "paint" event (which tells whenever the window needs to be refreshed). This is not how things must be done nowadays. A main loop and vertical-sync enabled is all that you need, don't try to keep the same structure as you had in programs that use 20-year old APIs.
What are the drawbacks for such an API calling? Or why does the new way superseed the old one?

Basically, a display function is given to GLUT ...
Thanks for explaining! :)

I imagine something like this is possible with SFML, however it isn't with the way the tutorial code is presented, basically:
bool Running = true;
while (Running)
{
    App.Display();
}

We can limit framerate, but this still doesn't solve the fact that if nothing is happening, the frame shouldn't be updated.
Yes, there's no API call for SFML to idle the application and since the paint event doesn't get propagated you can't use sf::Window::waitEvent() which would wait until the next event happens.
But you can still kind of reproduce your wanted effect with sf::Sleep(). So you check (from events/inputs) if the window needs a redraw, if so then draw, otherwise call the sleep function to free CPU time.

PS: There's a code=cpp tag to highlight posted code.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Beginner - SFML wait for message
« Reply #10 on: July 02, 2012, 03:33:16 pm »
Quote
What are the drawbacks for such an API calling? Or why does the new way superseed the old one?
Reacting to the paint event is still useful for desktop applications that don't need constant refreshing. But SFML doesn't target this kind of application, its target is real-time applications (games).
Laurent Gomila - SFML developer