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, 02: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, 02: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, 02: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, 02:59 PM

Great, thanks!

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

Alfonse Reinheart

01-15-2011, 03: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, 04: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, 04: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, 04: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, 05: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, 05: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, 06: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, 07: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, 03: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, 06: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, 07: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.