bootleg-game-engine/include/sprite.h
2024-09-06 10:34:29 +01:00

64 lines
1.7 KiB
C++

#include <SDL.h>
#include <map>
#include <string>
#include <vector>
#include "vector.h"
#pragma once
class Sprite
{
private:
std::map<std::string, SDL_Rect> frames;
SDL_Surface* surface;
SDL_Texture* Texture;
int_vec2 size;
public:
SDL_Rect dstrect;
int_vec2 scale = { 1, 1 };
double angle = 0;
Sprite(SDL_Renderer* renderer, SDL_Surface* surface);
void draw(SDL_Renderer* renderer, std::string frame, bool autoRect = true, bool flip = false);
void addFrame(std::string name, SDL_Rect rect);
void move(int_vec2 position);
int_vec2 getSize(std::string frame);
void autorect(std::string frame);
~Sprite();
};
class Animation
{
private:
std::vector<std::string> frames;
Sprite* sprite;
int currentFrame = 0;
int frameDuration = 0;
int frameCounter = 0;
std::string currentFrameName;
int frameIndex = 0;
int frameDirection = 1;
public:
bool loop = true;
bool pingpong = false;
Animation(std::map<std::string, SDL_Rect> frames, Sprite* sprite, int frameDuration);
void update();
void draw(SDL_Renderer* renderer, bool autoRect = true, bool flip = false);
};
class AnimationManager
{
private:
std::map<std::string, Animation> *animations;
std::map<std::string, bool> flip;
std::map<std::string, bool> autoRect;
std::string currentAnimation;
Sprite* sprite;
public:
AnimationManager(Sprite* sprite);
void addAnimation(std::string name, std::map<std::string, SDL_Rect> animation, int frameDuration = 10, bool flip = false, bool autoRect = true, bool loop = true, bool pingpong = false);
void setAnimation(std::string name);
void update();
void draw(SDL_Renderer* renderer);
};