sleep necessary?

I had to use sleep() function for my application to work(draw) properly. But the duration is not a fixed number depending on the system and on some systems it does not seeem to be needed.
Is there a way of avoiding sleep() function?


Two things :

  • first, you don’t want to draw more images per seconds than the display refresh rate of the screen. Activating vsync is simple and efficient (it depends on the OS and libs you use, search for wglSwapInterval on windows)
  • secondly you should adapt your animation according to the actual elapsed time. Something like :
    0 set initial scene
    1 get oldtime
    ---- render loop
    2 GL draw
    3 swapbuffers
    4 get newtime
    5 move stuff, according to delta newtime-oldtime, ex : newx = oldx + deltaT * speed
    6 oldtime=newtime
    7 loop to 2

Regarding swap(), is there any wgl functions that do swap implicitly?
I am not using swap function and drawing it to front buffer all the time. If there are such functions or Windows does it, that explains the symptom I am having now. Sometimes I have a window nothing drawn in it.


What ? never draw directly to the front buffer, it is “the way to great pains ™” ! Seriously. You should have a very very good reason to not use double buffering, and apparently you don’t.

I am not using swap function and drawing it to front buffer all the time.[…]Sometimes I have a window nothing drawn in it.

Do not use swap if you draw to the front buffer, it will hide your newly drawn stuff.
You can use glFlush() to trigger the actual execution of buffered GL commands. glFinish() ensure that after calling it, all draw commands have been executed and finished.

Try learning from tutorials, books, and reference materials, you need it, and it will you a lot to avoid loosing time on basics traps :

Actually, I am using mixed depending on the grapics of the window. If it’s massive, I am using double, if not single buf.
One more dumb question: if I use glDrawBuffer(FRONT_AND_BACK), is it slower than using glDrawBuffer(FRONT) or glDrawBuffer(BACK)?


Measure it, you will see (or not, depending on the complexity).
Don’t mix front and back buffer drawing, just stay with doublebuffering+drawbuffer back +swapbuffers, believe me. It is more future-proof : modern window manager compositors such as with Vista or linux Compiz/fusion have problems with front rendering.