OpenGL 100% CPU problem

This is a discussion on OpenGL 100% CPU problem within the Game Programming forums, part of the General Programming Boards category; I dont know, maybe this thread belongs in the games section. Well anyways, my code is a compalation of a ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    5

    Smile OpenGL 100% CPU problem

    I dont know, maybe this thread belongs in the games section. Well anyways, my code is a compalation of a few vague examples and a real tutorial, i just dont think they go together.

    Well I was reading the NeHe (Neon Helium) OpenGL tutorials, and i get the drift, and i was going through an example of a Menu for OpenGL, like u press Up and Down and it draws a box with a certain color evey time. Well its ALOT of code, and im not even close to understanding why this CPP file when compiled uses 100% CPU.
    The menu works perfectly, but i dont know what it is.

    If you would like to help me out, i will post the code at this location for you to download and to look at.
    http://realityxtreme.tripod.com/MyOpenGLMenu.cpp.txt

    I will also attach the CPP file since i just saw that option :S.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    urgh, well im affraid i can't help you, even tough i program in
    opengl i use an api for opengl that cuts all the bull, do you
    understand what's coded in that ccp?

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    5
    i dont have any header files and stuff.

    If you want to know how good i am with OpenGL.
    This is the one and only tutorial i have used to teach myself anything at all.

    http://nehe.gamedev.net/
    I've dont tutorial 1 threw 6.

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    5
    thanks for looking threw my code vVv, this code is to bug my buddy thats why the 31337 title is there.

    Do you think it could be that since the main loop is called every frame refresh (or however many times its called a second) and it is linking to my KeyPressed event that checks that keys[...] variable???

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    5
    When i put the Sleep(75); into the WinMain procedure it lowers CPU usage from 100% to anywhere from 25% - 35%
    Is this really the best solution?
    Isn't this just covering up the problem?
    When both Sleep(75); and the call to the KeyPressed(); code is commented out, CPU usage is back to 100%. Does this mean that the problem doesnt lye in the repetitious calling of the KeyPressed(); function, but somewhere else?
    The Sleep(75); command is the easy way out, i would like to fix the problem at its source. If anyone is good with OpenGL and C++, could they look over the code and see if you can help me.

    Thanks so much in advance.

  6. #6
    napKINfolk.com napkin111's Avatar
    Join Date
    Apr 2002
    Posts
    310
    The problem is that your' CPU is trying to go through the loop as many times as it can in as little time as it can. Imagine it like this: you are told to do loops around a track, without any breaks. You want to eat pudding, but can't cause all your time is taken by running. If you get a little break in between each lap, you can eat pudding for the time you aren't running. I think the "source" of the problem is just as vVv said.

    //napKIN
    (NOTE: I know that was a dumb example...)
    "The best way to get answers is to just keep working the problem, recognizing when you are stalled, and directing the search pattern.....Donít just wait for The Right Thing to strike you Ė try everything you think might even be in the right direction, so you can collect clues about the nature of the problem."
    -John Carmack

  7. #7
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    most professional games contain a portion of code that acts as a frame rate limiter. Otherwise the program will try to execute as fast as possible. This may seem like a good idea - but what happens when you run it on a REALLY fast machine is that everything moves ten times to fast and looks ridiculous.

    I have to say that when I ran your code it was impossible to select the menu items because the program was reading my input and processing it too fast, (probably because my machine is quite fast).

    Solution: use a peice of code that acts as a frame rate limiter. Sleep() isn't necessarily the best option for this. Below is an example of locking the framerate within the message loop to a fixed number of frames per second.
    Code:
    while (TRUE)
    {
        // Get a time reference
        DWORD start_time = GetTickCount();
        
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
             if (msg.message == WM_QUIT)
                 break;
    
             TranslateMessage(&msg);
             DispatchMessage(&msg);
        }
        else
        {
            // Wait until it is time to render a frame 
            // This locks the frame rate to around 30 FPS
            while((GetTickCount() - start_time) < 33);
    
            // Do game stuff, rendering here
        }
    }
    Last edited by foniks munkee; 12-23-2002 at 05:54 AM.
    "Queen and huntress, chaste and fair,
    Now the sun is laid to sleep,
    Seated in thy silver chair,
    State in wonted manner keep."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithread pthread with OpenGL
    By parad0x13 in forum C++ Programming
    Replies: 8
    Last Post: 07-24-2008, 04:04 PM
  2. OpenGL Color update problem
    By arifin in forum C++ Programming
    Replies: 0
    Last Post: 07-22-2008, 01:17 AM
  3. Opengl masking problem
    By kaptenkraek in forum Game Programming
    Replies: 1
    Last Post: 03-14-2008, 01:26 PM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  5. OpenGL rendering problem
    By JJFMJR in forum Game Programming
    Replies: 8
    Last Post: 08-31-2007, 08:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21