Jump to content
  • Advertisement
Sign in to follow this  
gcs584

OpenGL processor usage

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

Hey fellas, I've just started messing around with OpenGl, and I think it's great so far....not that I've played around with it that much. I wanted to create a simple pong-like game just to get me started. I have the two paddles so far, but not the ball which I could easily get going. The problem I am having is that at this current point my app. is using 100% of my 2Ghz processor (without the ball)!! But I don't know the reason. Even if I let the app. sit there without doing anything it's still using all the processor. I looked at my code and I saw that my glutIdleFunc() function call is the one that is using up all the processor. How would I go about fixing the problem? Does it have something to do with my video card? I've attached my code below, and I'm using the glut library #include <GL/glut.h> #include <stdlib.h> float angle=1.45,deltaAngle = 2.05,ratio; float compangle=1.45, compdeltaAngle = 2.05; bool right = true; bool down = false; float x=0.0f,y=1.75f,z=5.0f; float lx=0.0f,ly=0.0f,lz=-1.0f; double changex = 0; double changey = 1.75; int font=(int)GLUT_BITMAP_8_BY_13; void changeSize(int w, int h) { // Prevent a divide by zero, when window is too short // (you cant make a window of zero width). if(h == 0) h = 1; ratio = 1.0f * w / h; // Reset the coordinate system before modifying glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Set the viewport to be the entire window glViewport(0, 0, w, h); // Set the clipping volume gluPerspective(45,ratio,0.5,1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(x, y, z, x + lx,y + ly,z + lz, 0.0f,1.0f,0.0f); } void initScene() { glEnable(GL_DEPTH_TEST); } void renderBitmapCharacter(float x, float y, float z, void *font,char *string) { char *c; glRasterPos3f(x,y,z); for (c=string; *c != '\0'; c++) { glutBitmapCharacter(font, *c); } } void renderScene(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glColor3f(1.0, 1.0, 1.0); glRectf(2.72,angle,2.63,deltaAngle); glRectf(-2.72,1.45,-2.63,compdeltaAngle); glPopMatrix(); glutSwapBuffers(); } void pressKey(int key, int x, int y) { switch (key) { case GLUT_KEY_UP : if(angle < 3.2){ deltaAngle -= -0.2; angle += 0.2; } break; case GLUT_KEY_DOWN : if(angle > -0.25){ deltaAngle += -0.2; angle -= 0.2; } break; } } void processMenuEvents(int option) { font = option; } void processNormalKeys(unsigned char key, int x, int y) { if (key == 27) exit(0); } int main(int argc,char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); glutInitWindowPosition(100,100); glutInitWindowSize(800,600); glutCreateWindow("Pong Test"); initScene(); glutKeyboardFunc(processNormalKeys); glutIgnoreKeyRepeat(1); glutSpecialFunc(pressKey); glutDisplayFunc(renderScene); glutIdleFunc(renderScene); glutReshapeFunc(changeSize); glutMainLoop(); return 0; } Any help would be much appreciated....Later! GCS584

Share this post


Link to post
Share on other sites
Advertisement
It's perfectly normal that glutIdleFunc() uses all processor time.
The function gets called whenever no other event occurred (e.g. keypress, mouse movement or OS releated window messages.

If you want your program to only use a minimal amount of CPU time, you'll have to use glutTimerFunc() along with a fixed update interval. This will, however, not only reduce CPU time but also limits the frame-rate. As this should be no problem for such a simple game as Pong, you can go with that.

Cheers,
Pat

Share this post


Link to post
Share on other sites
mmm when you use glut try to put the drawing function in glutDisplayFunc and the dynamic var in glutIdleFunc

Share this post


Link to post
Share on other sites


Thanks to darookie I got my cpu down to 2% of total processes.

ff8 could I ask you to elaborate a bit more. I don't quite understand. I'm interested to hear what you have to say.

Thanks

GCS584

Share this post


Link to post
Share on other sites
what i meant the glutIdleFunc & glutDisplayFunc functions they used for something like this

float speed=0.0f;
....
void vars(void)
{
speed += 0.05;
glutPostRedisplay();
}
void draw(void){
glLoadIdentity();
glTranslatef(0.0f,speed,0.0f);
glBegin(GL_POINT);
glVertex2i(0,0);
glEnd();
....
}
....
glutDisplayFunc(draw);
glutIdleFunc(vars);
....


i hope you understand me :)
"sorry for my bad englis "

Share this post


Link to post
Share on other sites
Hey ff8,


Yeah, thanks for your input! I looked at a tutorial, and I understand the concept; although, I thank you for your help!

Your English is a lot better than my French ;)

thanks,

GCS584

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!