From 552168dab31e353112b2ee31e22070fdd7ade042 Mon Sep 17 00:00:00 2001 From: ineslelin Date: Tue, 23 Jan 2024 20:16:17 +0100 Subject: [PATCH] animation wip --- include/AnimationHandler.h | 23 +++++++++++++++++++ include/Defines.h | 3 +++ include/KeyboardController.h | 3 +++ include/SpriteComponent.h | 43 ++++++++++++++++++++++++++++++++++-- include/TextureManager.h | 1 + src/Game.cpp | 4 ++-- src/KeyboardController.cpp | 2 ++ 7 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 include/AnimationHandler.h diff --git a/include/AnimationHandler.h b/include/AnimationHandler.h new file mode 100644 index 0000000..ea77855 --- /dev/null +++ b/include/AnimationHandler.h @@ -0,0 +1,23 @@ +#pragma once + +struct Animation +{ + int index; + int frames; + int speed; + + Animation() {} + + Animation(int i, int f, int s) + { + index = i; + frames = f; + speed = s; + } +}; + +enum AnimationType +{ + idle = 0, + walk = 1 +}; \ No newline at end of file diff --git a/include/Defines.h b/include/Defines.h index da72770..3ee280a 100644 --- a/include/Defines.h +++ b/include/Defines.h @@ -10,3 +10,6 @@ #define MAP_SIZE_X 25 #define MAP_SIZE_Y 20 +#define IDLE "idle" +#define WALK "walk" + diff --git a/include/KeyboardController.h b/include/KeyboardController.h index dbd75ff..f6c2631 100644 --- a/include/KeyboardController.h +++ b/include/KeyboardController.h @@ -2,6 +2,7 @@ #include "Game.h" #include "ECS.h" #include "Components.h" +#include "Defines.h" class KeyboardController : public Component { @@ -14,6 +15,8 @@ public: SDL_Scancode right; //SDL_Scancode action; + SpriteComponent* sprite; + KeyboardController(); KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right/*, SDL_Scancode action*/); diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index 9a5667c..c3b272e 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -2,16 +2,38 @@ #include "Components.h" #include "SDL.h" #include "TextureManager.h" +#include "AnimationHandler.h" +#include "Defines.h" +#include class SpriteComponent : public Component { public: + int animationIndex = 0; + + std::map animations; + SpriteComponent() = default; SpriteComponent(const char* path) { setTexture(path); } + SpriteComponent(const char* path, bool isAnimated) + { + animated = isAnimated; + + Animation idle = Animation((int)AnimationType::idle, 2, 200); + Animation walk = Animation((int)AnimationType::walk, 2, 200); + + animations.emplace(IDLE, idle); + animations.emplace(WALK, walk); + + play(IDLE); + + setTexture(path); + } + ~SpriteComponent() { SDL_DestroyTexture(this->texture); @@ -29,12 +51,19 @@ class SpriteComponent : public Component this->srcRect.x = this->srcRect.y = 0; this->srcRect.w = transform->width; this->srcRect.h = transform->height; -; - } void update() override { + if (animated) + { + srcRect.x = srcRect.w * static_cast((SDL_GetTicks() / speed) % frames); + } + + srcRect.y = animationIndex * transform->height; + + + this->destRect.x = this->transform->position.x; this->destRect.y = this->transform->position.y; this->destRect.w = transform->width * transform->scale; @@ -46,9 +75,19 @@ class SpriteComponent : public Component TextureManager::get().draw(this->texture, this->srcRect, this->destRect); } + void play(const char* animationName) + { + animationIndex = animations[animationName].index; + frames = animations[animationName].frames; + speed = animations[animationName].speed; + } + private: TransformComponent* transform; SDL_Texture* texture; SDL_Rect srcRect, destRect; + bool animated = false; + int frames = 0; + int speed = 100; }; diff --git a/include/TextureManager.h b/include/TextureManager.h index 85e6bbb..cd3fffc 100644 --- a/include/TextureManager.h +++ b/include/TextureManager.h @@ -35,5 +35,6 @@ class TextureManager std::map texture_cache; SDL_Texture* loadTexture(const char* fileName); + static std::vector splitSpriteSheet(SDL_Texture* spriteSheet, int width, int height, int spritesOnSheet); static void draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest); }; \ No newline at end of file diff --git a/src/Game.cpp b/src/Game.cpp index 622f521..faa6f6e 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -69,13 +69,13 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo //ecs implementation player.addComponent(0,0,2); //posx, posy, scale - player.addComponent("assets/chicken_neutral_knight.png"); //adds sprite (32x32px), path needed + player.addComponent("assets/chicken_neutral_knight.png", true); //adds sprite (32x32px), path needed player.addComponent(SDL_SCANCODE_W, SDL_SCANCODE_S, SDL_SCANCODE_A, SDL_SCANCODE_D);//custom keycontrols can be added player.addComponent("player"); //adds tag (for further use, reference tag) player.addGroup(GROUP_PLAYERS); //tell programm what group it belongs to for rendering order enemy.addComponent(600, 500, 2); - enemy.addComponent("assets/chicken_neutral.png"); + enemy.addComponent("assets/chicken_spritesheet.png", true); enemy.addComponent(SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT); enemy.addComponent("enemy"); enemy.addGroup(GROUP_ENEMIES); diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index 1d71e02..e5e367e 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -20,6 +20,7 @@ KeyboardController::~KeyboardController() void KeyboardController::init() { + sprite = &entity->getComponent(); transform = &entity->getComponent(); } @@ -30,6 +31,7 @@ void KeyboardController::update() if (keystates[this->up]) { transform->velocity.y = -1; + sprite->play(WALK); } if (keystates[this->left]) { transform->velocity.x = -1;