0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 13:43:41 +00:00

implemented actual flipping of textures

This commit is contained in:
Benedikt Galbavy 2024-01-26 23:28:55 +01:00
parent 251d7f6661
commit 96418442f0
8 changed files with 42 additions and 17 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 1.8 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -19,8 +19,7 @@ struct Animation
enum AnimationType enum AnimationType
{ {
IDLE = 0, IDLE = 0,
WALK_R = 1, WALK = 1
WALK_L = 2
}; };

View File

@ -8,6 +8,12 @@
class TransformComponent; class TransformComponent;
enum SpriteDirection
{
LEFT,
RIGHT
};
class SpriteComponent : public Component class SpriteComponent : public Component
{ {
public: public:
@ -23,6 +29,7 @@ private:
bool animated = false; bool animated = false;
int frames = 0; int frames = 0;
int speed = 100; int speed = 100;
bool flipped;
public: public:
SpriteComponent() = default; SpriteComponent() = default;
@ -35,5 +42,6 @@ public:
void init() override; void init() override;
void update() override; void update() override;
void draw() override; void draw() override;
void play(AnimationType type); void playAnimation(AnimationType type);
void setDirection(SpriteDirection direction);
}; };

View File

@ -36,5 +36,6 @@ class TextureManager
SDL_Texture* loadTexture(const char* fileName); SDL_Texture* loadTexture(const char* fileName);
static std::vector<SDL_Rect> splitSpriteSheet(SDL_Texture* spriteSheet, int width, int height, int spritesOnSheet); static std::vector<SDL_Rect> splitSpriteSheet(SDL_Texture* spriteSheet, int width, int height, int spritesOnSheet);
static void draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest); static void draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest); // defaults to flipped false -> legacy
static void draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest, bool flipped);
}; };

View File

@ -3,6 +3,7 @@
#include "Game.h" #include "Game.h"
#include "Components.h" #include "Components.h"
#include "AssetManager.h" #include "AssetManager.h"
#include "SpriteComponent.h"
KeyboardController::KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity) KeyboardController::KeyboardController(SDL_Scancode up, SDL_Scancode down, SDL_Scancode left, SDL_Scancode right, SDL_Scancode fire, Vector2D fireVelocity)
{ {
@ -24,23 +25,25 @@ void KeyboardController::update()
{ {
transform->velocity.x = 0; transform->velocity.x = 0;
transform->velocity.y = 0; transform->velocity.y = 0;
sprite->play(IDLE); sprite->playAnimation(IDLE);
if (keystates[this->up]) { if (keystates[this->up]) {
transform->velocity.y = -1; transform->velocity.y = -1;
sprite->play(WALK_R); sprite->playAnimation(WALK);
} }
if (keystates[this->left]) { if (keystates[this->left]) {
transform->velocity.x = -1; transform->velocity.x = -1;
sprite->play(WALK_L); sprite->playAnimation(WALK);
sprite->setDirection(LEFT);
} }
if (keystates[this->down]) { if (keystates[this->down]) {
transform->velocity.y = 1; transform->velocity.y = 1;
sprite->play(WALK_R); sprite->playAnimation(WALK);
} }
if (keystates[this->right]) { if (keystates[this->right]) {
transform->velocity.x = 1; transform->velocity.x = 1;
sprite->play(WALK_R); sprite->playAnimation(WALK);
sprite->setDirection(RIGHT);
} }
if (keystates[this->fire]) { if (keystates[this->fire]) {

View File

@ -16,14 +16,12 @@ SpriteComponent::SpriteComponent(const char* path, bool isAnimated)
animated = isAnimated; animated = isAnimated;
Animation* idle = new Animation((int)AnimationType::IDLE, 2, 200); Animation* idle = new Animation((int)AnimationType::IDLE, 2, 200);
Animation* walkR = new Animation((int)AnimationType::WALK_R, 2, 200); Animation* walk = new Animation((int)AnimationType::WALK, 2, 200);
Animation* walkL = new Animation((int)AnimationType::WALK_L, 2, 200);
animations.emplace(IDLE, idle); animations.emplace(IDLE, idle);
animations.emplace(WALK_R, walkR); animations.emplace(WALK, walk);
animations.emplace(WALK_L, walkL);
play(IDLE); playAnimation(IDLE);
setTexture(path); setTexture(path);
} }
@ -63,12 +61,22 @@ void SpriteComponent::update()
void SpriteComponent::draw() void SpriteComponent::draw()
{ {
TextureManager::get().draw(this->texture, this->srcRect, this->destRect); TextureManager::get().draw(this->texture, this->srcRect, this->destRect, this->animated && this->flipped);
} }
void SpriteComponent::play(AnimationType type) void SpriteComponent::playAnimation(AnimationType type)
{ {
animationIndex = animations.at(type)->index; animationIndex = animations.at(type)->index;
frames = animations.at(type)->frames; frames = animations.at(type)->frames;
speed = animations.at(type)->speed; speed = animations.at(type)->speed;
} }
void SpriteComponent::setDirection(SpriteDirection direction)
{
if (direction == RIGHT) {
this->flipped = true;
return;
}
this->flipped = false;
}

View File

@ -20,5 +20,11 @@ SDL_Texture* TextureManager::loadTexture(const char* fileName)
void TextureManager::draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest) void TextureManager::draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest)
{ {
SDL_RenderCopy(Game::renderer, texture, &src, &dest); draw(texture, src, dest, false);
}
void TextureManager::draw(SDL_Texture* texture, SDL_Rect src, SDL_Rect dest, bool flipped)
{
SDL_RendererFlip flip = flipped ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE;
SDL_RenderCopyEx(Game::renderer, texture, &src, &dest, 0, NULL, flip);
} }