Game Programming And Processing Efficiency

Started by
7 comments, last by kuramayoko10 13 years, 3 months ago
Hello game developers,

I have reached a good point on the development of the game I have been working for the university, the main features have been set and now I start to worry about memory usage and processing efficiency!

I rewrote some parts of the code, fixed the memory leaks I could find and simplified the functions, helping a lot to keep memory usage as low as possible, but I still have a high processing rate. When executing the game, it reaches right at the menu a rate of 20% of processor use. I don't think that this is a good percentage since my game is very simple, and other softwares more complex don't achieve such a rate on my computer!

More technically:
- I am programming using Visual Studio 2008, in pure C language and SDL to handle events, graphics and audio
- I am running on Windows 7 Home Premium 64bits on a Intel Core i5-750
- I am also testing it on Linux Ubuntu 10.04 32bits
- The SDL flags I am using to render on the screen surface are: SDL_HWSURFACE | SDL_DOUBLEBUF

More specifically:
- I am using the following routine as my game loop:

while(CurrentState != Exit)
{
switch(CurrentState)
{
case Menu:
MenuInput();
MenuLogic();
MenuDraw();
break;
case CharacterSelection:
SelectionInput();
SelectionLogic();
SelectionRender();
break;
case GamePlay:
GameInput();
GameLogic();
GameRender();
break;
case ScoreScreen:
NextState = Menu;
break;
}
}

So basically what every loop does is: get an event from the SDL_PollEvent(), calculate what is necessary to proceed (logic), and renders the result on screen.
One last comment, I am still using several Global Variables. I know it is "wrong" to do so, is that a strong factor that affect game performance?

Thanks in advance for the comments!
Programming is an art. Game programming is a masterpiece!
Advertisement
<h2> I know it is &quot;wrong&quot; to do so, is that a strong factor that affect game performance?<br /> <br /> The Computer only knows Global Variables. These local Scopes are just &quot;an illusion&quot; C and all other languages give you to organize your code better. So don&#39;t worry about it.

The Computer only knows Global Variables. These local Scopes are just "an illusion" C and all other languages give you to organize your code better. So don't worry about it.


Not quite. On any modern OS the "global" variables are localized by TLB. Then there's virtualization. And memory mapped files.

Another counter-argument lies in static being typically poorly defined or at least used without scrutiny. In most languages, global applies to per-process, but at least in C/C++ linker and dynamic libraries can cause quite a mess. Java solves this problem by defining globals as per-ClassLoader, allowing hot code redeployment by introducing a rudimentary versioning mechanism.

I rewrote some parts of the code, fixed the memory leaks I could find and simplified the functions, helping a lot to keep memory usage as low as possible, but I still have a high processing rate. When executing the game, it reaches right at the menu a rate of 20% of processor use. I don't think that this is a good percentage since my game is very simple, and other softwares more complex don't achieve such a rate on my computer!

So basically what every loop does is: get an event from the SDL_PollEvent(), calculate what is necessary to proceed (logic), and renders the result on screen.

Its a non-issue really. What's happening is that your game is just a loop running as fast as it can. The BEST solution is to just enable v-sync to cap the display speed, and thus your processor usage.


One last comment, I am still using several Global Variables. I know it is "wrong" to do so, is that a strong factor that affect game performance?

I can. It depends on what you are doing with them. A global variable isn't going to have the same cache locality (speed!) as a local variable, so using them in place of a local or member variable probably isn't good, but probably isn't going to kill your game either. Get a profiler and see where the actual performance problems are. (you may not have any, your game is likely just running to fast as i mention above.)
but I still have a high processing rate. When executing the game, it reaches right at the menu a rate of 20% of processor use. I don't think that this is a good percentage since my game is very simple, and other softwares more complex don't achieve such a rate on my computer!


It doesn't matter how simple the menu processing is. If you tell it to keep doing the processing repeatedly, without pausing in between, then it will take all of the available time - because that's what you told it to do: use all the time available in order to repeat the processing as often as possible.

The Computer only knows Global Variables. These local Scopes are just "an illusion" C and all other languages give you to organize your code better. So don't worry about it.

If you are planning on doing anything multithreaded, then global variables are something to (potentially) worry about.

How do you suggest to enable V-Sync using SDL? I have found a topic on the SDL forum but the guy was using an SDL/OpenGL command on a OpenGL application...

'Zahlman' said:

It doesn't matter how simple the menu processing is. If you tell it to keep doing the processing repeatedly, without pausing in between, then it will take all of the available time - because that's what you told it to do: use all the time available in order to repeat the processing as often as possible.

Do you have any suggestion on how to change the game loop so this problem is minimized?
Programming is an art. Game programming is a masterpiece!

How do you suggest to enable V-Sync using SDL? I have found a topic on the SDL forum but the guy was using an SDL/OpenGL command on a OpenGL application...

Do you have any suggestion on how to change the game loop so this problem is minimized?


Put a call to SDL_Delay() in your loop. This will put your program to sleep for a specified duration. Basically what you want to do is to determine an ideal frame time and then call SDL_Delay() for whatever time is leftover after doing the processing for that frame. So if you are targeting 60 FPS, each frame (a frame is one time through the loop) should take 1/60 seconds to complete. You will generally finish the frame in far less time than that, so putting the thread to sleep for the remainder of the frame will let the CPU go to sleep.

The basic loop looks like this:

// SDL timers use milliseconds, which is why we are using 1000 here
// our target frame length is 1000 milliseconds / 60 FPS
static const unsigned int desiredFrameLength = 1000 / 60;

// this is the time (in milliseconds since SDL was initialized) that we want to end the frame at.
unsigned int targetFrameTime = SDL_GetTicks() + desiredFrameLength;

while(CurrentState != Exit)
{
switch(CurrentState)
{
case Menu:
MenuInput();
MenuLogic();
MenuDraw();
break;
case CharacterSelection:
SelectionInput();
SelectionLogic();
SelectionRender();
break;
case GamePlay:
GameInput();
GameLogic();
GameRender();
break;
case ScoreScreen:
NextState = Menu;
break;
}

unsigned int endFrameTime = SDL_GetTicks();
if(targetFrameTime < endFrameTime)
{
// we finished early, so put the CPU to sleep
SDL_Delay(endFrameTime - targetFrameTime);
}

// advance the target time so the next loop knows when it should end
targetFrameTime += desiredFrameTime;
}


Once you do this, your CPU usage should drop to almost nothing.

Put a call to SDL_Delay() in your loop. This will put your program to sleep for a specified duration. Basically what you want to do is to determine an ideal frame time and then call SDL_Delay() for whatever time is leftover after doing the processing for that frame. So if you are targeting 60 FPS, each frame (a frame is one time through the loop) should take 1/60 seconds to complete. You will generally finish the frame in far less time than that, so putting the thread to sleep for the remainder of the frame will let the CPU go to sleep.

Once you do this, your CPU usage should drop to almost nothing.


Oh Yeah.. thta really helped a lot!!
I have done this before to cap the frame rate of the game. I forgot to add this for the whole game loop though.

Thanks!!
Programming is an art. Game programming is a masterpiece!

This topic is closed to new replies.

Advertisement