Sign in to follow this  
TaylorMckinven

SDL Timer help

Recommended Posts

[font="arial, verdana, tahoma, sans-serif"][size="2"]I am working on a little RPG game, just as a hobby and to learn more in terms of programming.
Anyway I'm kinda stumped now, Im having problems with my timer (its a turn based combat system, based on the variable newTime reaching a certain value).

My timer seems to keep everything connected. Looking at the code here,could anyone give me some tips on how to implement a seperate timer for drawCombatBar() and drawCombatSprites(). So my animation, and player turn are not tied together ?


Id like to have say, the player hit SPACE_BAR, this starts a timer, for animation of the player attacking the enemy.[/size][/font]
When this timer reaches the end(ie of animation), the combatBar counter would kick back in, until the user is ready to make a move. When it reaches its end, you are allowed to press Space_Bar(make a move).
However while the space bar isnt pressed, I'd also like a seperate timer for animating idle player/enemy.


Sorry for the rubbish code, I am still learning..
And I probably didn't explain myself well =[

If needed I can post the entire code, I just dont want to give you all headaches [img]http://public.gamedev.net/public/style_emoticons/default/smile.gif[/img]

[code]
//window.h
void startTimer() // ev
{
start = SDL_GetTicks();
}


//window.h
int getTicks()
{
newTime = SDL_GetTicks() - start;
return newTime;
}

//window.h
void drawCombatBar() // uses newTime
{
if(newTime >= 0 && newTime <= 500) // ie while, just not infinite, ft that
{
apply_surface(b_counter_x,b_counter_y,tile[10], screen, NULL);
}
if(newTime >= 500 && newTime <= 1000)
{
apply_surface(b_counter_x,b_counter_y,tile[10], screen, NULL);
}
if(newTime >= 1000 && newTime <= 1500)
{
apply_surface(b_counter_x,b_counter_y,tile[10], screen, NULL);
apply_surface(b_counter_x+32, b_counter_y, tile[10], screen, NULL);
}
if(newTime >= 1500 && newTime <= 2000)
{
apply_surface(b_counter_x, b_counter_y, tile[10], screen, NULL);
apply_surface(b_counter_x+32, b_counter_y, tile[10], screen, NULL);
}
if(newTime >= 2000 && newTime <= 2500 )
{
apply_surface(b_counter_x, b_counter_y, tile[10], screen, NULL);
apply_surface(b_counter_x+32, b_counter_y, tile[10], screen, NULL);
apply_surface(b_counter_x+64, b_counter_y, tile[10], screen, NULL);
}
if(newTime >= 2500 && newTime <= 3000 )
{
apply_surface(b_counter_x, b_counter_y, tile[10], screen, NULL);
apply_surface(b_counter_x+32, b_counter_y, tile[10], screen, NULL);
apply_surface(b_counter_x+64, b_counter_y, tile[10], screen, NULL);
}
if(newTime >= 3000 && newTime <= 3500 )
{
apply_surface(b_counter_x, b_counter_y, tile[10], screen, NULL);
apply_surface(b_counter_x+32, b_counter_y, tile[10], screen, NULL);
apply_surface(b_counter_x+64, b_counter_y, tile[10], screen, NULL);
apply_surface(b_counter_x+96, b_counter_y, tile[10], screen, NULL);
}
if(newTime >= 3500)
{
apply_surface(b_counter_x, b_counter_y, tile[10], screen, NULL);
apply_surface(b_counter_x+32, b_counter_y, tile[10], screen, NULL);
apply_surface(b_counter_x+64, b_counter_y, tile[10], screen, NULL);
apply_surface(b_counter_x+96, b_counter_y, tile[10], screen, NULL);
pause = true;
}
}
//window.h
void drawCombatSprites() // uses newTime
{
switch(enemy){
case 1: // 1st enemy
if(newTime >= 0 && newTime <= 500) // ie while, just not infinite, ft that
{
apply_surface(100,200, enemy1bSpriteSheet, screen, &e2b_clip[0]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[0]);

}
if(newTime >= 500 && newTime <= 1000)
{
apply_surface(100,200, enemy1bSpriteSheet, screen, &e2b_clip[1]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[1]);
}
if(newTime >= 1000 && newTime <= 1500)
{
apply_surface(100,200, enemy1bSpriteSheet, screen, &e2b_clip[2]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[2]);
}
if(newTime >= 1500 && newTime <= 2000)
{
apply_surface(100,200, enemy1bSpriteSheet, screen, &e2b_clip[3]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[3]);
}
if(newTime >= 2000 && newTime <= 2500 )
{
apply_surface(100,200, enemy1bSpriteSheet, screen, &e2b_clip[0]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[0]);
}
if(newTime >= 2500 && newTime <= 3000 )
{
apply_surface(100,200, enemy1bSpriteSheet, screen, &e2b_clip[1]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[1]);
}
if(newTime >= 3000 && newTime <= 3500 )
{
apply_surface(100,200, enemy1bSpriteSheet, screen, &e2b_clip[2]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[2]);
}
if(newTime >= 3500)
{
apply_surface(100,200, enemy1bSpriteSheet, screen, &e2b_clip[3]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[3]);
}
break;
case 2:// second enemy
if(newTime >= 0 && newTime <= 500) // ie while, just not infinite, ft that
{
apply_surface(100,200, enemy2bSpriteSheet, screen, &e2b_clip[0]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[0]);

}
if(newTime >= 500 && newTime <= 1000)
{
apply_surface(100,200, enemy2bSpriteSheet, screen, &e2b_clip[1]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[1]);
}
if(newTime >= 1000 && newTime <= 1500)
{
apply_surface(100,200, enemy2bSpriteSheet, screen, &e2b_clip[2]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[2]);
}
if(newTime >= 1500 && newTime <= 2000)
{
apply_surface(100,200, enemy2bSpriteSheet, screen, &e2b_clip[3]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[3]);
}
if(newTime >= 2000 && newTime <= 2500 )
{
apply_surface(100,200, enemy2bSpriteSheet, screen, &e2b_clip[0]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[0]);
}
if(newTime >= 2500 && newTime <= 3000 )
{
apply_surface(100,200, enemy2bSpriteSheet, screen, &e2b_clip[1]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[1]);
}
if(newTime >= 3000 && newTime <= 3500 )
{
apply_surface(100,200, enemy2bSpriteSheet, screen, &e2b_clip[2]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[2]);
}
if(newTime >= 3500)
{
apply_surface(100,200, enemy2bSpriteSheet, screen, &e2b_clip[3]);
apply_surface(450,200, playerbSpriteSheet, screen, &pb_clip[3]);
}
break;
case 3: // third enemy

break;
}//end case enemy
}

//window.h
void updateTimer() // getTicks() + drawCombatSprites(), also determines allow move
{
getTicks();
drawCombatBar();
drawCombatSprites();

if(newTime <= 3999){allow_move = false;}
if(newTime >= 4000){allow_move = true;}
if(newTime >= 4000 && unpause == true){
newTime = 0;
startTimer();
unpause = false;
}
}[/code] Edited by tman2998

Share this post


Link to post
Share on other sites
well you could probably make a timer class, eg

[code]
class Timer

{

public:

int start, newtime;

void startTimer();

int getTicks();

};
[/code]


I wouldnt copy n paste that though, not really professional work. Anyways, with classes each instance has its own start and newtime, so one function (say drawCombatBar()) could feed off of Timer combatBarTimer, while say drawCombatSprites() relies on Timer combatSpritesTimer.

If you dont know about classes, you should really really really REALLY look them up ;) For one thing, youd learn about how to define all of those functions and where to put class definitions.

Also might i recommend this site: lazyfoo.net. Its the shizzle for SDL.

Sorry if I missed the point of your post and went on a tangent, but at least I think im helping lol.

Share this post


Link to post
Share on other sites
Bit of a late reply I know, just saying thanks :D


[quote name='walkingbush' timestamp='1318663624' post='4872764']
well you could probably make a timer class, eg

[code]
class Timer

{

public:

int start, newtime;

void startTimer();

int getTicks();

};
[/code]
[/quote]

This is what I did,


[code]

class Timer
{
public:
int start, newTime;
void startTimer()
{
start = SDL_GetTicks();
}
int getTicks()
{
newTime = SDL_GetTicks() - start;
return newTime;
}


} worldAnimation;
[/code]

I've re-written my entire program, so I've yet to get round to implementing this into the combat system, probably if i make a combatAnimation instance from the timer class I could do it. At least my idle sprites on the world map are animated now [img]http://public.gamedev.net/public/style_emoticons/default/cool.gif[/img]

My main, the timers in logic, not sure if it should be? seems to work fine for now anyway
[code] while(running == true)
{
draw(); // choose what to draw in relation to gameMode
handleEvents(); // handle all user input
logic(); // collision/battle initiation/transport
update(); // update screen
}[/code]


[code]

void logic()
{
checkTransport();
checkCollision();
worldAnimation.getTicks();
if(worldAnimation.getTicks() >= 1500){
genEnemyDirection();
wmoveEnemy();
worldAnimation.startTimer();
}
//check collision with enemy on world map[still to add this]

}

[/code]

[quote name='walkingbush' timestamp='1318663624' post='4872764']
If you dont know about classes, you should really really really REALLY look them up ;) For one thing, youd learn about how to define all of those functions and where to put class definitions.

Also might i recommend this site: lazyfoo.net. Its the shizzle for SDL.

Sorry if I missed the point of your post and went on a tangent, but at least I think im helping lol.
[/quote]

Yeah I've spent most of the past two weeks reading up what I can about classes, hard concept but I feel I'm beginning to get there [img]http://public.gamedev.net/public/style_emoticons/default/tongue.gif[/img]
LazyFoo is where I've been learning SDL, it is indeed the shizzle [img]http://public.gamedev.net/public/style_emoticons/default/cool.gif[/img]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this