SDL Timer help

Started by
2 comments, last by TaylorMckinven 12 years, 6 months ago
[font="arial, verdana, tahoma, sans-serif"]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.[/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 smile.gif


//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;
}
}
Advertisement
Have you tried using SDL's timers? They would probably work better for you than rolling your own. The following page explains how to use SDL's timers:

SDL Timers
Mark Szymczyk
Author of Mac Game Programming and Xcode Tools Sensei
http://www.meandmark.com
well you could probably make a timer class, eg


class Timer

{

public:

int start, newtime;

void startTimer();

int getTicks();

};



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.
Bit of a late reply I know, just saying thanks :D



well you could probably make a timer class, eg


class Timer

{

public:

int start, newtime;

void startTimer();

int getTicks();

};



This is what I did,




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


} worldAnimation;


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 cool.gif

My main, the timers in logic, not sure if it should be? seems to work fine for now anyway
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
}





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]

}




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.


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 tongue.gif
LazyFoo is where I've been learning SDL, it is indeed the shizzle cool.gif

This topic is closed to new replies.

Advertisement