Jump to content
  • Advertisement
Sign in to follow this  
nickme

Opengl: How To Make Sure That Program Don't Only Run Once And Wait For Input?

This topic is 829 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

HI

 

I wonder how one can make sure that the rending only run once and then stay in the background until the user give some input (keyboard or mouse)?

 

that way, the program do not waste resource and battery.

 

thanks

Edited by nickme

Share this post


Link to post
Share on other sites
Advertisement

HI

 

I wonder how one can make sure that the rending only run once and then stay in the background until the user give some input (keyboard or mouse)?

 

that way, the program do not waste resource and battery.

 

thanks

I am sorry about the typo in the title.  the title should read:

Opengl: how to make sure that opengl only runs once and waits for input and go idle?

 

does it has something to do with

 

glutPostRedisplay();?
Edited by nickme

Share this post


Link to post
Share on other sites

does it has something to do with   glutPostRedisplay();?
Yes - don't call it and glut won't call your drawing function.

 

Note that this is a question specific to the GLUT library, not OpenGL itself. 

Share this post


Link to post
Share on other sites

What I have done in the past that has worked is to flag-out events I don't want to run. Intrinsically loop is got to be continuous but if your methods are well structured it could be flagged out of the loop until it is needed

 

So if it loops on zero events, wastage (as you put it) is minimised 

Edited by grumpyOldDude

Share this post


Link to post
Share on other sites

 

does it has something to do with   glutPostRedisplay();?
Yes - don't call it and glut won't call your drawing function.

 

Note that this is a question specific to the GLUT library, not OpenGL itself. 

 

 

 

does it has something to do with   glutPostRedisplay();?
Yes - don't call it and glut won't call your drawing function.

 

Note that this is a question specific to the GLUT library, not OpenGL itself. 

 

that is not true.  I have no glutpostredisplay() in my program but it still loop, it even execute my init(). what have i done wrong?

 

does anyone know how to change the password in gamedev.net?  thanks in advance.

 

here is my main():

 

int main(int argc, char** argv)
{
//create GL context
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(sizeX, sizeY);
glutCreateWindow("Testing Program");
 
init();
glutIgnoreKeyRepeat(1);
glutReshapeFunc(winReshapeFcn);
glutDisplayFunc(render);
glutMainLoop();
 
cleanup();
 
return 0;
}
 
when my prog ran in vs 2015 in "idle" mode, the diagnostic tool window showed that the process memory it used was under 80MB, is that a lot or normal for a 200+ lines program?
Edited by nickme

Share this post


Link to post
Share on other sites

that is not true.  I have no glutpostredisplay() in my program but it still loop, it even execute my init().

It always has to loop. A program receives messages from Windows, these must be processed. Most of that is "wake me when a message arrives" but there is always a loop. The work that needs to be done there is completely negligible in regard to processor time or battery used but it needs to be done.

What you might want to avoid is rendering with a high frequency. Hodgman's answer as given is still correct.

It might be a good idea not to say something like "that is not true" when you understand so little of the problem domain.
 

when my prog ran in vs 2015 in "idle" mode, the diagnostic tool window showed that the process memory it used was under 80MB, is that a lot or normal for a 200+ lines program?

When I run a program doing nothing but print "Hello world", I expect significantly less memory to be consumed by the process. When I run a program which loads geospational raster data at 20cm resolution of a few square kilometers into memory I expect a significantly higher memory load. I can do both in 80 lines. Without information what you are doing this cannot be answered. Edited by BitMaster

Share this post


Link to post
Share on other sites
Your problem is hard to deal with, because glut hides a loop named "message loop".

Message loop do things about rendering only when message loop doesn't receive any message.
 
So if you don't use glut, try to learn how to init a window, how to init opengl, maybe you could get what you want. 
 
Just Like this( ... a silly way ): 
// ....
bool doDraw = false;
// ....
while(msg.message != WM_QUIT)  // Message loop never stops until the window dies.
{
	// If there are Window messages then process them.
	if(PeekMessage( &msg, 0, 0, 0, PM_REMOVE ))
	{
              TranslateMessage( &msg );
              DispatchMessage( &msg );
	}

	// Otherwise, do animation/game stuff.
	else
        {	
	      if( GetAsyncKeyState('1') & 0x8000 )  // Press the key '1'
	      {
                     doDraw = true;
	      }
              if( doDraw )
              {
                     UpdateScene();
                     DrawScene();
                     doDraw = false;
               } 
         }
 }

When application starts, it only shows the background. Then if you press the key '1', it will draw scene; else, not draw. 

 

But it still has a problem : press the key '1' can't guarantee that you truly press only one time.( The loop still will draw 1-2 seconds even if you release '1' very quickly )

 

Edited by Drimoon

Share this post


Link to post
Share on other sites

If you are asking about the so called update loop then you need to know that this attemps to happen linear in your program so you will first get anything from last frame including input then query update on each of your objects and after that go into rendering again before closing the frame.

Depending on your input processing function you have two options to act here. In polling approach everything is handled by the components interested in the input where the input itself is only collected during the update loop. On the other hand an event based approach where you will fire events and handle each component registered to.

 

There are also systems out there mostly in AAA titles where everything is handled independent frome each other. Input will be filled in a queue and handled by an event scheduler where rendering happens in parallel to the object update, sometimes in multiple threads (background rendering current frame and post processing last frame at the same time)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!