From 552168dab31e353112b2ee31e22070fdd7ade042 Mon Sep 17 00:00:00 2001 From: ineslelin Date: Tue, 23 Jan 2024 20:16:17 +0100 Subject: [PATCH 1/3] 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; From 76f4ae79ed75037e8010a3602eed280b88842a08 Mon Sep 17 00:00:00 2001 From: ineslelin Date: Tue, 23 Jan 2024 22:26:26 +0100 Subject: [PATCH 2/3] spritesheets and correct game.cpp --- assets/chicken_knight_spritesheet.png | Bin 0 -> 1818 bytes assets/chicken_spritesheet.png | Bin 0 -> 1433 bytes src/Game.cpp | 2 +- 3 files changed, 1 insertion(+), 1 deletion(-) create mode 100644 assets/chicken_knight_spritesheet.png create mode 100644 assets/chicken_spritesheet.png diff --git a/assets/chicken_knight_spritesheet.png b/assets/chicken_knight_spritesheet.png new file mode 100644 index 0000000000000000000000000000000000000000..9e6939d3985a3a8dbb10cd323a7b6702c72dd8c0 GIT binary patch literal 1818 zcmV+#2j%#QP)Px*x=BPqRCt{2nr~BR_#G2YBO-!4d&Fw|J z>*e3&lDjl^eA zpFG;325|7(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DSQ0D^K1_5204efP=w z2B8>@XDXUn94ZOJ62K7wlCq0nWHSJPP_(0sUdYkVSBFBvum#v`7hFoF2u3z{E!>2Z zRM1N$P>XKkJAx^I^20aX1*n&ary#R1XSgLnI0;tjI>E%4vm_G<;;C^rv2D>VKra!b zq(We8%2Ny>0cJrW_1ASKZSj;uFN{I}ApxoV#{gLQ{UWu+S!#>303^0gx(?(8qYywy zfO_*9+Am*2F;DeLg=!^4ZE=>;@?F<~+#<6uXLu3dzdP4mEsTVdpr#gyA3V*I&n^Ox zzC7cmZwu)t#H_Ih*2|Cx=J*4wmlQ9>n@eD54g)(BZr!kkO{K1r+;;$Y@scLW z!8WhpBn_S zSI-*U|Jmw4b^P+Cj`Jij7qd-ZHrB}C_j9eWhg?+@_(0pR-se18D={(xpX-yh)KA0Q+^Q;S3no_4i8-R&!`XnR@& zw*pAX<|EM!gxllb-X9Q8jq^(Q0|0z~fbS2$>a-Vpe}L}~ct!mI=7uLg^za!+E3#L{ zACSFt!ia2-S;y7oGSS0lng`(*Lt-QS0fA7ImH98M%NW=)2x? zl>8|C0hQ~sFrI$VxR~$AuP=WxFE>o74fXpcZIe}MFKvGd30B)Ad4wxLNx zuf6wH5(Zto9l@>J;$Hm$qy4Dq0d@qd^QVkzEzXXU-@o*=`0()(;(yWyWmE&lA?pAD n01jnXNoGw=04e|g00;m8000000Mb*F00000NkvXXu0mjf6i}G9 literal 0 HcmV?d00001 diff --git a/src/Game.cpp b/src/Game.cpp index faa6f6e..8ab07db 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -69,7 +69,7 @@ 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", true); //adds sprite (32x32px), path needed + player.addComponent("assets/chicken_knight_spritesheet.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 From 3766fefb132330d42c3717a64cf622b2339123a8 Mon Sep 17 00:00:00 2001 From: Benedikt Galbavy Date: Wed, 24 Jan 2024 01:50:33 +0100 Subject: [PATCH 3/3] fixed walk/idle animation --- include/AnimationHandler.h | 4 ++-- include/Defines.h | 3 --- include/SpriteComponent.h | 33 +++++++++++++++++---------------- src/KeyboardController.cpp | 4 ++++ 4 files changed, 23 insertions(+), 21 deletions(-) diff --git a/include/AnimationHandler.h b/include/AnimationHandler.h index ea77855..0473651 100644 --- a/include/AnimationHandler.h +++ b/include/AnimationHandler.h @@ -18,6 +18,6 @@ struct Animation enum AnimationType { - idle = 0, - walk = 1 + IDLE = 0, + WALK = 1 }; \ No newline at end of file diff --git a/include/Defines.h b/include/Defines.h index 3ee280a..da72770 100644 --- a/include/Defines.h +++ b/include/Defines.h @@ -10,6 +10,3 @@ #define MAP_SIZE_X 25 #define MAP_SIZE_Y 20 -#define IDLE "idle" -#define WALK "walk" - diff --git a/include/SpriteComponent.h b/include/SpriteComponent.h index c3b272e..e3fadb1 100644 --- a/include/SpriteComponent.h +++ b/include/SpriteComponent.h @@ -11,8 +11,18 @@ class SpriteComponent : public Component public: int animationIndex = 0; - std::map animations; + std::map animations; + private: + TransformComponent* transform; + SDL_Texture* texture; + SDL_Rect srcRect, destRect; + + bool animated = false; + int frames = 0; + int speed = 100; + + public: SpriteComponent() = default; SpriteComponent(const char* path) { @@ -23,8 +33,8 @@ class SpriteComponent : public Component { animated = isAnimated; - Animation idle = Animation((int)AnimationType::idle, 2, 200); - Animation walk = Animation((int)AnimationType::walk, 2, 200); + Animation* idle = new Animation((int)AnimationType::IDLE, 2, 200); + Animation* walk = new Animation((int)AnimationType::WALK, 2, 200); animations.emplace(IDLE, idle); animations.emplace(WALK, walk); @@ -75,19 +85,10 @@ class SpriteComponent : public Component TextureManager::get().draw(this->texture, this->srcRect, this->destRect); } - void play(const char* animationName) + void play(AnimationType type) { - animationIndex = animations[animationName].index; - frames = animations[animationName].frames; - speed = animations[animationName].speed; + animationIndex = animations.at(type)->index; + frames = animations.at(type)->frames; + speed = animations.at(type)->speed; } - - private: - TransformComponent* transform; - SDL_Texture* texture; - SDL_Rect srcRect, destRect; - - bool animated = false; - int frames = 0; - int speed = 100; }; diff --git a/src/KeyboardController.cpp b/src/KeyboardController.cpp index e5e367e..8603df5 100644 --- a/src/KeyboardController.cpp +++ b/src/KeyboardController.cpp @@ -28,6 +28,7 @@ void KeyboardController::update() { transform->velocity.x = 0; transform->velocity.y = 0; + sprite->play(IDLE); if (keystates[this->up]) { transform->velocity.y = -1; @@ -35,11 +36,14 @@ void KeyboardController::update() } if (keystates[this->left]) { transform->velocity.x = -1; + sprite->play(WALK); } if (keystates[this->down]) { transform->velocity.y = 1; + sprite->play(WALK); } if (keystates[this->right]) { transform->velocity.x = 1; + sprite->play(WALK); } } \ No newline at end of file