PDA

View Full Version : 100% CPU usage



Daniel Harrison
01-15-2011, 02:21 PM
glutIdleFunc() uses up 100% of the CPU on a single core and 50% on a dual core.

How can I get around this high CPU usage without using *shrieks* Win32 "Sleep()" function?

I could use a glutTimerFunc() to glutPostRedisplay() every so often but I'm wondering if there is a cleaner way to do it?



#include <GL/freeglut.h>
#include <GL/glu.h>

using namespace std;

class box
{
public:
int Width;
int Height;
float X;
float Y;
float R;
float G;
float B;

void draw()
{
glBegin(GL_POLYGON);
glColor3f(R, G, B);
glVertex2i(X, Y);
glVertex2i(X + Width, Y);
glVertex2i(X + Width, Y + Height);
glVertex2i(X, Y + Height);
glEnd();
}
};

class player : public box
{
public:
bool Jumped;
};

box Terrain;
box Sky;
box Grass;
box Sun;
player Player;
box Platform;
unsigned char keys[256];

void load()
{
Terrain.X = 0;
Terrain.Y = 300;
Terrain.Height = 200;
Terrain.Width = 500;
Terrain.R = (float) 66 / 255;
Terrain.G = (float) 48 / 255;
Terrain.B = (float) 8 / 255;
Grass.X = 0;
Grass.Y = 298;
Grass.Height = 4;
Grass.Width = 500;
Grass.R = (float) 0 / 255;
Grass.G = (float) 255 / 255;
Grass.B = (float) 0 / 255;
Sky.X = 0;
Sky.Y = 0;
Sky.Height = 300;
Sky.Width = 500;
Sky.R = (float) 184 / 255;
Sky.G = (float) 254 / 255;
Sky.B = (float) 254 / 255;
Sun.X = 20;
Sun.Y = 20;
Sun.Width = 50;
Sun.Height = 50;
Sun.R = (float) 237 / 255;
Sun.G = (float) 182 / 255;
Sun.B = (float) 65 / 255;
Player.X = 248;
Player.Y = 290;
Player.Width = 4;
Player.Height = 8;
Player.R = 0.0f;
Player.G = 0.0f;
Player.B = 0.0f;
Player.Jumped = false;
Platform.X = 0;
Platform.Y = 280;
Platform.Height = 3;
Platform.Width = 100;
Platform.R = 1.0f;
Platform.G = 0.0f;
Platform.B = 0.0f;
}

void keydown(unsigned char key, int x, int y)
{
keys[key] = true;
}

void keyup(unsigned char key, int x, int y)
{
keys[key] = false;
}

void process()
{
if (keys['a'])
{
Player.X -= 0.04f;
}

if (keys['d'])
{
Player.X += 0.04f;
}

if (keys['w'])
{
if (Player.Jumped == false)
{
Player.Jumped = true;
Player.Y -= 25.0f;
}
}

if (keys['q'])
{
glutLeaveMainLoop();
}
if (Player.Jumped == true)
{
if((Player.Y > Platform.Y - Player.Height) & (Player.X > Platform.X &amp;&amp; Player.X < Platform.X + Platform.Width))
{

}
else
{
Player.Y += 0.025f;
}
if (Player.Y > 290.0f)
{
Player.Jumped = false;
}
}
glutPostRedisplay();
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
Terrain.draw();
Sky.draw();
Sun.draw();
Grass.draw();
Player.draw();
Platform.draw();
glutSwapBuffers();
}

int main(int argc, char **argv)
{
load();
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(500, 500);
glutCreateWindow("Rendering Test");
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gluOrtho2D(0, 500, 500, 0);
glutDisplayFunc(display);
glutKeyboardFunc(keydown);
glutKeyboardUpFunc(keyup);
glutIdleFunc(process);
glutMainLoop();
return 0;
}

_arts_
01-15-2011, 03:09 PM
glutIdleFunc is called whenever your CPU is not used. So it's absolutely normal that your program uses all the CPU.

You should call glutPostRedisplay in the display function, and update the interactions in the matching keydown and keyup functions, and avoid glutIdleFunc to do this.

mhagain
01-15-2011, 03:10 PM
It's not actually high CPU usage; it's better to think of it as more efficient CPU usage, or not being CPU-bound, or not wasting CPU resources by doing nothing with them.

Code like the following will also max out a CPU core and you can't say it's doing much:
while (1) {i++;}
Anyway, 100% CPU usage is actually normal and desirable in a game loop. Think about it - you're running physics, updating stuff, playing sound, shoving geometry at the GPU, reading input, and so on. You want these to be running as fast and responsive as possible, no pauses, no waiting.

On the other hand if you're not writing a game you don't want it, and maybe then GLUT isn't a suitable choice for your use case needs. Something like SDL will give you much better and more fine-grained control over what happens and when it happens, with the trade-off of a little more complexity.

Daniel Harrison
01-15-2011, 03:43 PM
I see! But wouldn't the sustained load eventually overheat a CPU that wasn't well cooled, e.g stock cpu fans.

mhagain
01-15-2011, 03:57 PM
Not really; people have been playing games like Doom and Quake for well over a decade, they use 100% CPU, and we never had tales of exploded CPUs from them. Windows 9x didn't use the HLT instruction so was effectively constantly pummelling the CPU too. Busy database servers, SETI@home, virtualised environments, the list goes on. Not really something to worry too much about, in other words.

Daniel Harrison
01-15-2011, 03:59 PM
Great, thanks!

I suppose I can continue building my first little OpenGL RPG!

Alfonse Reinheart
01-15-2011, 04:46 PM
Anyway, 100% CPU usage is actually normal and desirable in a game loop. Think about it - you're running physics, updating stuff, playing sound, shoving geometry at the GPU, reading input, and so on. You want these to be running as fast and responsive as possible, no pauses, no waiting.

Um, no. That's completely untrue.

Games tend to have a specific speed at which they run the game logic. Attempting to run it slower than that speed would cause the game to slow down. Similarly, running it faster will cause the game to run faster. That's why so many old DOS games need special emulation software to emulate a much slower clockspeed.

Modern games simply prevent themselves from running faster than their fastest speed. The gameplay loop runs at around 20-30 fps for most games (high fidelity simulations like racing games tend to run their physics loop much faster). The rendering loop tends to be locked to the monitor's refresh rate allows, unless you're doing performance tests. Because otherwise, it's wasting time.

And you're wasting power. Running a GPU at full speed, the way that Furmark does for example, can cause exceedingly high power draw. And if you're only showing the user one frame out of 100, what are the other 99 frames for?


I suppose I can continue building my first little OpenGL RPG!

Step 1: Stop using GLUT. GLUT is a nice tool for devising effects, testing, and tutorials, but it is not a good tool for making a game.

Daniel Harrison
01-15-2011, 05:04 PM
No use telling me not to use something while failing to provide a solution.

I wanted to use GLUT for its simplicity and the fact my game isn't really going to be advanced in any way. I believe GLUT would take a way some of the more tedious work (e.g setting up a window) and allow me to concentrate on the things that interest me more.

But by all means I'm open to suggestions if you think there would be a better way forward for somebody wanting to make a simple, first game.

Alfonse Reinheart
01-15-2011, 05:23 PM
I wanted to use GLUT for its simplicity and the fact my game isn't really going to be advanced in any way.

Then why are you using OpenGL at all? There are any number of tools you can find online to make non-"advanced" games. And using them will be far, far easier than using OpenGL directly. Ogre3D, Irrilicht, etc, are all free alternatives that would give you more than everything you need.

The question you need to ask yourself is what you want. Do you want to make a game, or do you want to use OpenGL? If you want to make a game, then you need to spend your time and effort on the game making part of that task. Learning OpenGL is not helping you make that game.


But by all means I'm open to suggestions if you think there would be a better way forward for somebody wanting to make a simple, first game.

There is no kind of RPG that could possibly be described as "simple" when coded from scratch. Even text-based games have to have complicated grammar parsers.

Daniel Harrison
01-15-2011, 05:34 PM
I can't help but find you ostentatious.

I am not an idiot, you know?

I wanted to make a simple 2D RPG and I know two things: 1) GLUT is capable of that and 2) I am capable of that.

The fact I'm using GLUT to create a window, handle keyboard events and refresh the screen is not a big issue.

I plan to move on, after I've created this little project, and start using OpenGL without extensions but for now, I'm sticking with GLUT.

I don't want to use Ogre3D, I want to make something myself. Is that so wrong?

ugluk
01-15-2011, 06:12 PM
Alfonse is right, for 2D, better use SDL. Yet even SDL has an OpenGL wrapper, so you can do 3D with it as well.

Alfonse Reinheart
01-15-2011, 06:13 PM
I can't help but find you ostentatious.

I am not an idiot, you know?

I suggested that your time would be better spent making your game than reinventing the wheel. The only way that you could get "I think you're an idiot" from that is if you believe that people who use freely available tools to save time in making the things that they want to make are somehow less intelligent/cool programmers than those who reinvent the wheel.

Wanting to use all available and appropriate tools to make things easier on yourself doesn't make someone stupid. And not wanting to make things easier on yourself does not by itself make someone smart.


I don't want to use Ogre3D, I want to make something myself. Is that so wrong?

It depends on why you don't want to use it.

If you don't want to use something because it wouldn't be able to do what you need, that's perfectly rational. If you don't want to use something because you've looked at the API and hated it, I could understand that. And if you don't want to use something because you want to learn how to do it yourself, that also makes sense.

But if you want to learn how to be a graphics programmer, making a game (particularly a mechanics heavy game like an RPG) isn't really helping that process. Similarly, if you want to make an RPG, spending lots of time writing graphics engine code that could just as effectively be served by using an available library isn't helping that process.

And even if you want to learn both, you would be more effective if you spent some time with pure graphics code, and some time working on the RPG, without the two projects interacting. That way, time you might want to spend on the RPG would not be taken up by doing grunt graphics engine work.

It's really all about effective use of your time. But if you don't like that advice, I'm not going to track you down and make you follow it. Just consider the question of why you have an aversion against using other people tools, and consider whether this is helping you or hurting you.

Rosario Leonardi
01-15-2011, 07:06 PM
Let's say that GLUT have a lot of limitation.

A - Don't support multi thread
B - Have very basic input support, no joystick, no fancy mouse
C - No sound support
D - No support to load anything (mesh, texture, shaders... nothing)

GLUT was designed by SGI engineers to make test program when dinosaurs walked in the graphics labs.

ugluk
01-15-2011, 08:18 PM
He didn't yet shoot down the SDL idea, maybe he'll opt for SDL. Also, the beta SDL allows for modern GL 3+ context creation, handles input, can load .bmp :), handles some basic sound... Can't do much else though :)

Daniel Harrison
01-16-2011, 04:36 AM
I am going to try SDL. I just thought it was silly that he told me not to use GLUT without providing an alternative solution - comparable to citizens of wherever you reside complaining about their government without providing solutions.

@Alfonse I just wanted experience of "low level" OpenGL programming as opposed to using something that already exists. It's barely different from my decision to use C++ over C#.

I appreciate the advice I'm just fairly stubborn. I like knowing the details, how things really work, etc.

Thanks everyone for the information.

ZbuffeR
01-16-2011, 07:53 AM
On a side note, I prefer GLFW for simple game, low level OpenGL dev. Very simple, nice docs, game-oriented.
http://www.glfw.org/

ugluk
01-16-2011, 08:14 AM
With OGRE, irrlicht, ... you can find out how they work by analyzing their source code. It is not obfuscated AFAIK.

gongzhitaao
07-26-2012, 12:37 AM
I believe GLUT would take a way some of the more tedious work (e.g setting up a window) and allow me to concentrate on the things that interest me more.

Qt would provide what you want and more:)

Good luck to your game:P