# Game runs REALLY slow when using game states

This topic is 2805 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have been reading http://www.dreamincode.net/forums/topic/120775-beginning-sdl-part-4-state-manager/, and managed to implement a state management system in my game.
However when I run the game after adding this it runs much slower than before.

Does anyone know what can cause this?
I am using openGL and SDL.

##### Share on other sites
The link describes a very basic and general approach to solving a problem.
It essentially replaces a set of [font="Lucida Console"]if[/font] statements with abstract base classes, which is a good and well tried way of doing things.
For first, please define what you mean by "much slower".
For second, note that everything could be done using this method. Without further information about what you've done, we can only have speculations and could be pretty much everything.

##### Share on other sites

The link describes a very basic and general approach to solving a problem.
It essentially replaces a set of [font="Lucida Console"]if[/font] statements with abstract base classes, which is a good and well tried way of doing things.
For first, please define what you mean by "much slower".
For second, note that everything could be done using this method. Without further information about what you've done, we can only have speculations and could be pretty much everything.

Alright, if I just use my old standard gameloop where I initialize and render all my objects in the int main function it works properly and things move at the correct speed.
However when I run the game using states instead the game slows down as if it lags.

This is the code for my "active" state, in which the player can move around. If you can spot anything glaringly wrong or point me to some other article I could look at, it would help a lot.
 #include "ActiveState.h" #include "game.h" #include "texturemanager.h" #include "MenuState.h" ActiveState ActiveState::m_ActiveState; //Initialize player object Block Player(64, 40, 34, 34, 0, 0, 1, false); //sprite is 34x34 //Initialize state void ActiveState::Init() { loadTextures("Graphics/playertest.bmp", 0); loadTextures("Graphics/texturemaptest2.bmp", 1); } //Clean state void ActiveState::Clean() { loadTextures(NULL, 0); loadTextures(NULL, 1); } void ActiveState::Pause() { } void ActiveState::Resume() { } void ActiveState::HandleEvents(Game* game) { while( SDL_PollEvent( &event ) ) { //Handle player input Player.handle_input(); if( event.type == SDL_QUIT ) { game->Quit(); } if(event.type == SDL_KEYDOWN) { switch(event.key.keysym.sym) { case SDLK_ESCAPE: game->Quit(); break; case SDLK_RETURN: game->ChangeState(MenuState::Instance()); break; } } } } void ActiveState::Logic(Game* game) { Player.createcollisionbox(); Player.collisiondetect(); Player.move(); } void ActiveState::Render(Game* game) { glClearColor( 0.4, 0.6, 0.8, 1.0 ); animateSprite(Player, 0, Player.getX(), Player.getY(), Player.getWidth(), Player.getHeight(), 1, 0.15, 4); draw_tilesTexMap(32, 10, 1); } 

##### Share on other sites
Have you used a profiling tool on this code.

A profiling tool is a tool that will perform analysis on the code as it is running and then give you a report at the end. The tool will then show you a list of functions and the time taken.

There are a few profilers out there, Sleepy being free and one http://www.codersnotes.com/sleepy/sleepy

Netbeans also has a inbuilt profiler.

Also the enterprise versions of Visual Studio also have profilers.

Sure others will have suggestions of profilers out there.

##### Share on other sites
The code you've posted is pretty trivial. The only reason this would cause your overall program to slow down is if your program was doing almost nothing before, or you are calling it incorrectly (e.g. loading textures every frame).

Can you clarify how you are measuring how "slow" the game runs? In particular, if you are using FPS remember that it is a non-linear measurement WRT time and a "large" drop in FPS might be the result of doing relatively small amounts more work per frame.

As deepdene mentions, if you are really having trouble the best bet is to profile. I wouldn't bother with such an approach unless the frame rate was wanting on the minimum target hardware.

##### Share on other sites
I'm going to go out on a limb and say that it's fairly unlikely that simply switching to an object-oriented state system would cause any sort of noticeable slowdown. (I suspect the source of the problem is elsewhere; as for finding the source of the problem, profiling the application as suggested previously would definitely be a good place to start.)

##### Share on other sites
Ok I tried profiling it, but I can't say I understand too much of this.

Is it normal for the KiFastSystemCallRet function to use 40%?

##### Share on other sites
Are you calling Sleep somewhere in your code? The description from KiFastSystemCallRet says in the description box that it could be from calling sleep, and "Sleep" is the #3 time-waster in the list.

Do you have any idea what atiPS is?

##### Share on other sites

Are you calling Sleep somewhere in your code? The description from KiFastSystemCallRet says in the description box that it could be from calling sleep, and "Sleep" is the #3 time-waster in the list.

Do you have any idea what atiPS is?

Not calling Sleep anywhere, could it be that it has something to do with loading texture files?

Also I believe atiPS is related to the ATi display drivers.

##### Share on other sites
Your game is VSynced, in other words it spends its time waiting for the vertical retrace.

I would expect you to be easily hitting 60FPS if you spend 40% of your time waiting for vsync. Is the FPS acceptable? If so: problem solved - there was never a problem. If not, get back to us.

You're calling SDL_Flip/SDL_UpdateRect(s) only once per frame, right? If not, that might explain a game that seems "slow".

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
632566
• Total Posts
3007107

×