animation with sdl timeing/speed issue

Good Evening,
i seem to be having an issue with my animations using SDL. the animations work, its basically just a static sprite swap, "Witch" for instance is just a lady stiring a big cauldron... anyways, whats hapening is the speeds for the sprites are different, but whichever speed was last set in the second set of code, is the speed that sets for every other animation... any ideas how to fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
 #include "AISprite.h"

extern float gDeltaTime;


namespace {
	//index sizes...
	int numSwapIndices{ 0 };
	//indices...
	int *swapIndices = nullptr;
	//timer for the sprite swap...
	float swapTimer{ 0.f };

	//Animation speeds...
	float animMoveSpeed{ 0 };
	float animSwapSpeed{ 0 };

}

void AISprite::Update() {
	StaticSpriteSwapping();
	Sprite::Update();
}

//functions to set indices and counts for swaps and movements...
void AISprite::SetAnimSwapIndices(int count,float animSpeed,int * indices) {
	numSwapIndices = count;
	swapIndices = indices;
	animSwapSpeed = animSpeed;
}

//swaps sprites staticly..
void AISprite::StaticSpriteSwapping() {
		swapTimer += animSwapSpeed * gDeltaTime;
		int i = (int)swapTimer % numSwapIndices;
		mSpriteClipIndex = swapIndices[i];
	}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void GameManager::animationStates() {

	float witchSpeed = 3.0f;
	float guyInBedSpeed = .02f;
	float sawGuysSpeed = .5f;
	
	
		witch.SetAnimSwapIndices(8, witchSpeed, witchSwapIndices);
		witch.Update();


		sawGuys.SetAnimSwapIndices(3, sawGuysSpeed, sawGuysIndices);
		sawGuys.Update();

		guyInBed.SetAnimSwapIndices(2, guyInBedSpeed, guyInBedIndices);
		guyInBed.Update();

}
I guess that the global variable gDeltaTime is used for calculating the speed? Why isn't it a member variable?

If StaticSpriteSwapping() means it is a static function you can always pass gDeltaTime as a parameter.
yes the gDeltaTime is used as a multiplier with the speed variable. im still new to c++ so if i understand correctly what a member variable is, i could, would that make a difference? essentially all the animations are being run at the one speed vs the different ones.. also static was just a naming convention. the function just iterates through the sprite indices on a timer, i meant static as these animations arent moving around the map.
it looks like it is supposed to swap current with itself repeatedly until the time for the next image comes around, which looks like a fair bit of work to do nothing. It seems like it should short circuit when there is no work to do.


swapTimer += animSwapSpeed * gDeltaTime;

this just does not look correct. I am trying to see what it should be, but what i advise you to do is to do this yourself:

get rid of all the animations except 1.
print swaptimer every iteration for a while, 30 seconds or whatever. Also print the index computed from it and the current time stamp. This should tell you whether this logic is correct or not.


Last edited on
i will try this, however i would like to add, individually they all work. but once they're all running together is when the speed is being overwritten.

EDIT: i agree, the way that is working... i dunno my teacher helped me put that in, im not sure how it even works right at all...
Last edited on
oh. I see it

swaptimer is global, in a namespace, but you ONLY HAVE ONE OF THEM and ALL the animations modify the value!

each animation needs its own swap timer, the way this is coded, if I am understanding correctly. so that variable needs to be in your class.

Honestly you have a weird mix of globals (which cause problems) and objects. Most of the stuff in the namespace probably belongs in a class somewhere.

Last edited on
ill try to stream line this, again im like 4 weeks into learning c++ so im still a noob. ill add the swaptimer to the class, and see if this works out better for me.

EDIT:: everything you see up above is in a class, this is my AISprite class... the gameloop is my GameManager.cpp.. the second set of code in OP is the function thats being called in update in the gamemanager
Last edited on
curious, how would you recommend putting in the timer for each individual animation? i put in the swap timer and passed it through as an argument, but the timer stays at zero now.
the namespace is NOT in the class.

put those variables inside the actual class like this:

class AISprite
{
public:
float swapTimer;
... etc
}

if you pass it as a parameter, you need to put & in front of it if you want to retain the value:

void modifyx(int &x)
{
x = rand();
}
int z = 0;
modifyx(z); //z will change. without the &, it will not.

but that won't solve your problem because you still only have ONE copy of the variable for all the animations, which is not logically correct for this job.

Last edited on
after i put the variables in the class - thank you for that info - its actually working, again probably still very hacky...
Glad it worked.
It takes a while to get past hackery. Hackery isn't all bad, it means you have learned some piece of the language and made it work, even if its not the best way. That stirs the problem solving brain cells around in good ways.

I am guessing, but I think you misunderstood the idea of putting a class in its own cpp/.h files and took that to mean the things in those files belong to the class. Organizing the code into files is for the humans, the compiler really does not care what you put where if its legal, and its ok (to the compiler) to have many classes in 1 set of files or even one huge monolithic main or stuff that isn't part of the class buried in the class cpp file (that is what you had here).


Last edited on
Topic archived. No new replies allowed.