Advertisement

Help with wait function in fps game please

Started by November 30, 2013 08:30 AM
18 comments, last by Kryzon 11 years, 2 months ago

Hi guys,

Working a dragon warrior type clone where the character is essentially in two states. The first one is a 2d world exploration state, and the other is a battle state.

When you enter the battle state, you turn the game into a turn based system, where the player presses a button, and you attack or use an item or whatever. Here is where my problem comes in. I am having a world of trouble dealing with the timer running at 30 fps and waiting for the player to make an input.

Here's the cleaned up main loop.


int main(int,char**){
	
	

	while(running){
		time_start=SDL_GetTicks();


		state->handleInput();
		state->handleLogic();
		state->handleRender();

		SDL_Flip(screen);

		if(1000/FPS>(SDL_GetTicks()-time_start))
			SDL_Delay(1000/FPS-(SDL_GetTicks()-time_start));
	}
        cleanResources();
	return 0;
}

and here are the internals of my "handleLogic();" function in my "Battle" state.


int player_initiative=getRandom(1,10);
int monster_initiative=getRandom(1,10);

if(player_inititive>monster_initiative)
	playerAttacks();
else
	monsterAttacks();

It seems no matter how many booleans I set up, it either get's too ugly and I lose track of what's going on, or I can't stop the main loop.

Could someone please let me know how to solve this problem? That is turn the Battle state into a turn based system where the player has a chance to make an input.

Thanks,

Mike

Can't you make it so that you enter another loop inside the main loop where you just wait for an action from the user? Something like:


while(some_action_from_user == 0)
{
    delay(few_millisecs);
}

Basically pause the game while the user doesn't take any action.

Advertisement

I am not completly sure what the best implementation is in a situation like this (as I have never encountered it before). Is it to use flags? Work with the timer? or both?

I believe a better solution would be to run the main loop seperate of the render code (a.k.a. the fps limited loop) this way you can update your logic as soon as it has to be updated (while now you are delaying everything from player input to future physics engine and whatever you will put in it just so you can reach your desired 30 fps limit) and for the rendering part you should design a timer which has the delay set to your desired number (which results in 30 fps) and everytime your main loop run you should just send a query whether the timer has reached 0 or not (a boolean function) and if it returns true run the render code.

This should give you the responsivness of a real game and your desired limit of 30 fps. And of course to tell the program it entered a battle state just use a boolean (or a bit of data should do the trick but this can be harder to ghasp).

Hope this helps smile.png

Have a nice day/night,

JKKDev.

EDIT: You should change the title as the current one can be a bit misleading as fps also stands for First Person Shooter.

As a former Q3 player, I should have caught that. I will try out your solution JKKDev, thanks for the reply. Now if I can only figure out how to change the title.

Please report back on how it went.

Advertisement

Hi JKKDev I will, but right now I am finishing up something else and then going to sleep. I will message you when I figure it out.

Thanks again!

Mike

Unless you have a reason not to, the most flexible game loop would generally be to have the rendering and updates running at the same rate and pass a delta time to your update function, the delta time is then passed to objects and they use that to update their internal clocks.

Unless you have a reason not to, the most flexible game loop would generally be to have the rendering and updates running at the same rate and pass a delta time to your update function, the delta time is then passed to objects and they use that to update their internal clocks.

I can agree with your statement and I can see where you are comming from as my method would cause difference when comparing user input and display. What I cannot see and would like to know is how do you lock your fps using this method. I understand that delta is the time the game uses to run the render code/update code which then offsets the timers in the respective code. Which part of the code would be the frame limiter and not disturb the input as it does in the OPs code?

Another question to OP: Why do you want to limit your fps?


Why do you want to limit your fps?

Try playing an older game that doesn't have some kind of fps limiter and watch that surreal speed of animations, movement etc(it gets basically unplayable).

This topic is closed to new replies.

Advertisement