aminmations

This commit is contained in:
2024-09-06 10:34:29 +01:00
parent a756a4f68e
commit 8091651cc7
12 changed files with 421 additions and 0 deletions

14
include/input.h Normal file
View File

@@ -0,0 +1,14 @@
#include <SDL.h>
#include <map>
#include <string>
class Input
{
private:
SDL_Event event;
public:
bool exit;
std::map<SDL_Keycode, bool> activeKeys;
void update();
};

64
include/sprite.h Normal file
View File

@@ -0,0 +1,64 @@
#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);
};

17
include/text.h Normal file
View File

@@ -0,0 +1,17 @@
#include <SDL.h>
#include <SDL_ttf.h>
#include <string>
class Text
{
private:
SDL_Surface* surface;
SDL_Texture* texture;
TTF_Font* font;
SDL_Color color;
SDL_Rect rect;
public:
Text(TTF_Font* font, SDL_Color color);
void draw(SDL_Renderer* renderer, std::string text, int x, int y);
};

49
include/vector.h Normal file
View File

@@ -0,0 +1,49 @@
#pragma once
class float_vec2
{
public:
float x, y;
float_vec2 operator+(float_vec2 vec)
{
return { x + vec.x, y + vec.y };
}
float_vec2 operator*(float v)
{
return { x * v, y * v };
}
float_vec2 operator*(float_vec2 vec)
{
return { x * vec.x, y * vec.y };
}
float_vec2 operator-(float_vec2 vec)
{
return { x - vec.x, y - vec.y };
}
float_vec2 operator-(float v)
{
return { x - v, y - v };
}
};
class int_vec2
{
public:
int x, y;
int_vec2 operator+(int_vec2 vec)
{
return { x + vec.x, y + vec.y };
}
int_vec2 operator*(int v)
{
return { x * v, y * v };
}
int_vec2 operator*(int_vec2 vec)
{
return { x * vec.x, y * vec.y };
}
int_vec2 operator-(int_vec2 vec)
{
return { x - vec.x, y - vec.y };
}
};