fix animation manager

This commit is contained in:
Alfie King 2024-09-06 11:52:36 +01:00
parent 8091651cc7
commit 5813da7e97
5 changed files with 13 additions and 10 deletions

View File

@ -49,7 +49,7 @@ class Animation
class AnimationManager
{
private:
std::map<std::string, Animation> *animations;
std::map<std::string, Animation*> animations;
std::map<std::string, bool> flip;
std::map<std::string, bool> autoRect;
std::string currentAnimation;

View File

@ -14,4 +14,4 @@ private:
public:
Text(TTF_Font* font, SDL_Color color);
void draw(SDL_Renderer* renderer, std::string text, int x, int y);
};
};

View File

@ -13,5 +13,8 @@ $(TARGET): $(OBJ)
build/%.o: src/%.cpp
@$(CXX) -c -o $@ $< $(CXXFLAGS)
dirs:
@mkdir -p build/x86_64
clean:
@rm -f $(OBJ) $(TARGET)

View File

@ -54,7 +54,7 @@ int main(int argc, char* argv[])
// Create sprite and animation
Sprite* sprite = new Sprite(renderer, assets);
AnimationManager* animations = new AnimationManager(sprite);
animations->addAnimation("idle", frames);
animations->addAnimation("idle", frames, 10, false, true, true, true);
animations->setAnimation("idle");
// Main loop
@ -76,8 +76,8 @@ int main(int argc, char* argv[])
animations->update();
// Clear screen
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 100, 100, 255, 255);
SDL_RenderClear(renderer);
// Draw sprite
animations->draw(renderer);

View File

@ -106,10 +106,10 @@ void AnimationManager::addAnimation(std::string name, std::map<std::string, SDL_
animations[name] = new Animation(frames, sprite, frameDuration);
this->flip[name] = flip;
this->autoRect[name] = autoRect;
animations[name].loop = loop;
animations[name].pingpong = pingpong;
animations[name]->loop = loop;
animations[name]->pingpong = pingpong;
}
void autorect(std::string frame);
void AnimationManager::setAnimation(std::string name)
{
currentAnimation = name;
@ -117,10 +117,10 @@ void AnimationManager::setAnimation(std::string name)
void AnimationManager::update()
{
animations[currentAnimation].update();
animations[currentAnimation]->update();
}
void AnimationManager::draw(SDL_Renderer* renderer)
{
animations[currentAnimation].draw(renderer, autoRect[currentAnimation], flip[currentAnimation]);
animations[currentAnimation]->draw(renderer, autoRect[currentAnimation], flip[currentAnimation]);
}