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

Merge branch 'texture-flipping'

This commit is contained in:
Benedikt Galbavy 2024-01-27 18:34:31 +01:00
commit fa480f916f
8 changed files with 44 additions and 17 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 1.9 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 //TODO enum class
{
IDLE = 0,
WALK_R = 1,
WALK_L = 2
WALK = 1
};

View File

@ -8,6 +8,12 @@
class TransformComponent;
enum SpriteDirection
{
LEFT,
RIGHT
};
class SpriteComponent : public Component
{
public:
@ -23,6 +29,7 @@ private:
bool animated = false;
uint8_t frames = 0;
uint8_t speed = 100;
bool flipped;
public:
SpriteComponent() = default;
@ -35,5 +42,6 @@ public:
void init() override;
void update() 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);
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 "Components.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)
{
@ -24,23 +25,25 @@ void KeyboardController::update()
{
transform->velocity.x = 0;
transform->velocity.y = 0;
sprite->play(IDLE);
sprite->playAnimation(IDLE);
if (keystates[this->up]) {
transform->velocity.y = -1;
sprite->play(WALK_R);
sprite->playAnimation(WALK);
}
if (keystates[this->left]) {
transform->velocity.x = -1;
sprite->play(WALK_L);
sprite->playAnimation(WALK);
sprite->setDirection(LEFT);
}
if (keystates[this->down]) {
transform->velocity.y = 1;
sprite->play(WALK_R);
sprite->playAnimation(WALK);
}
if (keystates[this->right]) {
transform->velocity.x = 1;
sprite->play(WALK_R);
sprite->playAnimation(WALK);
sprite->setDirection(RIGHT);
}
if (keystates[this->fire]) {
@ -54,10 +57,12 @@ void KeyboardController::update()
//checks player source via the firing velocity
//TODO: adding actual projectile textures
if (fireVelocity.x > 0) {
sprite->setDirection(RIGHT);
Game::assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity,
false, 1, 180, 1, "assets/egg.png");
}
else {
sprite->setDirection(LEFT);
Game::assets->createProjectile(Vector2D(player->position.x, player->position.y), fireVelocity,
true, 1, 180, 1, "assets/egg.png");
}

View File

@ -16,14 +16,12 @@ SpriteComponent::SpriteComponent(const char* path, bool isAnimated)
animated = isAnimated;
Animation* idle = new Animation((uint8_t)AnimationType::IDLE, 2, 200);
Animation* walkR = new Animation((uint8_t)AnimationType::WALK_R, 2, 200);
Animation* walkL = new Animation((uint8_t)AnimationType::WALK_L, 2, 200);
Animation* walk = new Animation((uint8_t)AnimationType::WALK, 2, 200);
animations.emplace(IDLE, idle);
animations.emplace(WALK_R, walkR);
animations.emplace(WALK_L, walkL);
animations.emplace(WALK, walk);
play(IDLE);
playAnimation(IDLE);
setTexture(path);
}
@ -63,12 +61,22 @@ void SpriteComponent::update()
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)
{
this->animationIndex = animations.at(type)->index;
this->frames = animations.at(type)->frames;
this->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)
{
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);
}