67 lines
1.8 KiB
C++
67 lines
1.8 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, SDL_RendererFlip flip = SDL_FLIP_NONE);
|
|
void addFrame(std::string name, SDL_Rect rect);
|
|
void move(int_vec2 position);
|
|
void setPos(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 reset(bool ifFinish = false);
|
|
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 play(std::string name);
|
|
void draw(SDL_Renderer* renderer);
|
|
}; |