Jump to content
  • Advertisement
Sign in to follow this  
horizon981

GLUT program problem

This topic is 4486 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

I was making a very simple program in GLUT using Dev C++. It just moves a square around in the screen. Although other programs have worked well, this one is giving lot of trouble. The New Window created doesn't show anything and tends to capture the screen behind it when minimized and maximized again, i.e. takes snapshots of the background (obviously not what it was written for). Please help. Here's the complete code: //An animated square #include <windows.h> #include <gl\glut.h> GLfloat x1=100.0f; GLfloat y1=150.0f; GLsizei rsize=50; GLfloat xstep=1.0f; GLfloat ystep=1.0f; GLfloat windowWidth; GLfloat windowHeight; void RenderScene(void) { glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f,0.0f,0.0f); glRectf(x1,y1,x1+rsize,y1+rsize); glutSwapBuffers(); } //Called by GLUT library when "idle" void TimerFunction(int value) { if( (x1 > windowWidth-rsize) || x1<0) xstep = -xstep; if( (y1 > windowHeight-rsize) || y1<0) ystep = -ystep; //Suspicious Code if(x1>windowWidth-rsize) x1=windowWidth-rsize-1; if(y1>windowHeight-rsize) y1=windowHeight-rsize-1; //End of Suspicious Code x1+=xstep; y1+=ystep; //Redraw the scene with new coordinates glutPostRedisplay(); glutTimerFunc(33,TimerFunction,1); } void SetupRC(void) { glClearColor(0.0f,1.0f,0.0f,1.0f); } void ChangeSize(GLsizei w, GLsizei h) { if(h==0) h=1; glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w<=h) { windowHeight = 250.0f*h/w; windowWidth = 250.0f; } else { windowWidth = 250.0*w/h; windowHeight = 250.0f; } glOrtho(0.0f,windowWidth,0.0f,windowHeight,1.0f,-1.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } int main(void) { glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutCreateWindow("Bouncing Stuff"); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); glutTimerFunc(33,TimerFunction,1); SetupRC(); glutMainLoop(); return 0; } Thanks in advance... any help is appreciated.

Share this post


Link to post
Share on other sites
Advertisement
It has been a while since I've used glut, but it looks like you have an infinite loop in your TimerFunction. At the end of the timer function, you are calling the timer function.

Googled:
glutTimerFunc registers the timer callback func to be triggered in at least msecs milliseconds.

Share this post


Link to post
Share on other sites
I've removed the last TimerFunction call but it still doesn't work...
God Help me now!!!
you said there's been a lot of time since you last used glut. then, what
do u use now for game development?

Share this post


Link to post
Share on other sites
You're requesting a single frame buffer, but the code is written as if it was using double buffering. Either change glutSwapBuffers to glFlush, or GLUT_SINGLE to GLUT_DOUBLE. At least one thing I saw, could be more.

Share this post


Link to post
Share on other sites
A couple problems:

Your reshape function should take arguments as int instead of GLsizei.

You request a single-buffered window but your display function calls glutSwapBuffers() (and doesn't flush).

You need to call glutInit with the arguments to your program.

Also, please use the source tags in the future. It makes your code a lot easier to read.

Share this post


Link to post
Share on other sites
What a relief!!!!!!!!!
Thanks to all of you who took pain to answer.
You saved me from a lot of frustration.
Just had to change GLUT_SINGLE to GLUT_DOUBLE!!
By the way, what are source tags?

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!