Click to See Complete Forum and Search --> : c++ Opengl - window freezes/stops drawing


speedy6
June 5th, 2006, 08:57 AM
Hello,

I have started learning opengl but I still do not know very much, so I have a small problem :).
I am making a program which draws some lines and shapes. Additional lines and shapes to draw are determined at runtime, and these affect the ones that were already drawn. So I assume I need to redraw everything when a new object is created that changes the previous ones. To solve this, I created a while(1) loop inside the display() function which checks for new objects that have been added and clears the screen and redraws them all correctly once a new object is added.
It works and does exactly what I wanted, but the window freezes if I do anything to it. If I run it and never touch it again it works fine. If I press a keyboard key it doesn't freeze. But if I do something with the mouse (except move), for example, if I minimize the drawing window, or move the drawing window, or resize the drawing window, or just click on the drawing window, it stops drawing the new shapes that are added and freezes. The program is still running and I see the objects being added in the dos window, but it stops drawing if I do any of the things mentioned above.
Why could this be happening?

Thank you for any help :)

ahoodin
June 5th, 2006, 01:22 PM
Mmmm....a while in the display would cause an hourglass scenario. Additionally it might be necessary to display twice.

Take the hourglass out, find a way to iterate through the list of elements with a bounded loop instead of an endless group.

while (!(++pobj)->EndOfList())
{
//draw my object
}

Endless loops can also create exponentially hungry memory pits that suck up heap space your stack space and all sorts of stuff.

ahoodin

speedy6
June 5th, 2006, 02:02 PM
I have a bounded loop to get the shapes, but an infinite loop to keep redrawing the shapes forever. I have that endless loop inside display because I don't know how often the glutMainLoop() calls display, and I would like to redraw everything every few seconds ( actually I would like to be able to specify that interval myself, but it seems I am doing it the wrong way :( I currently specify that interval by Sleep()ing the chosen interval inside the while(1).. )
What do you mean by it being necessary to display twice? I am sorry I am not very experienced yet. It is displaying everything correctly though, as long as I don't click on anything. Thanks for your help :)

nolxev
June 6th, 2006, 09:18 AM
I think your problems occurs because you make a wrong use of GLUT function. For example, do you call "glFlush" at the end of DrawScene? Is the function "glutMainLoop" at the end of main? Do you call "glutSwapBuffers" at the end of DrawScene? Consider to show us the base code, it comes us a little difficult to throw to guess!

However, see if this tutorial can help you:

http://www.lighthouse3d.com/opengl/glut

speedy6
June 6th, 2006, 07:28 PM
I think your problems occurs because you make a wrong use of GLUT function. For example, do you call "glFlush" at the end of DrawScene? Is the function "glutMainLoop" at the end of main? Do you call "glutSwapBuffers" at the end of DrawScene? Consider to show us the base code, it comes us a little difficult to throw to guess!

However, see if this tutorial can help you:

http://www.lighthouse3d.com/opengl/glut


Ok, I removed all the "Sleep(..)"s from the display function and it doesn't freeze anymore. However, it takes a very long time to open (for example, if it is minimized and I want to open it it takes about 30 seconds... and it is not my computer's fault ).

I do not call glflush at all, I don't know if I should or not?
My main:


int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(900, 600);
glutInitWindowPosition(50, 50);
glutCreateWindow("test");
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}


I call glutSwapBuffers right at the end of the display function. My display function is small since it just draws a few lines and shapes, it takes about 1second for the display function to execute. So I don't know why it takes so long to open. It's like when you try to maximize a program that is not responding, it takes 20 seconds to open the window but it is all white, then after another 10 seconds it shows the lines and shapes and it works fine from there (until I minimize again).
Thanks for the help!

edit:
I also notice that when I move the window around it fills the whole screen white and takes around 3 seconds for it to be updated to normal. How can I prevent this?

nolxev
June 7th, 2006, 10:20 AM
glFlush force the execution of OpenGL commands in finite time, but I think glFlush is not the problem. Your main seems ok, I don't know what's wrong!
You should attach the full source or follow a tutorial to see if you have written all stuffs correctly. We aren't wizards, all I can do is to test you code if you give it to me.