This commit is contained in:
2025-05-06 15:08:30 +01:00
commit e2e27c3677
23 changed files with 863 additions and 0 deletions

30
include/engine.h Normal file
View File

@@ -0,0 +1,30 @@
#include "input.h"
#include "text.h"
#include "sprite.h"
#include "vector.h"
#include <SDL.h>
#include <string>
#include <vector>
#include "sound.h"
class Engine
{
private:
const char* ctitle;
int frameStart = 0;
bool soundInitialized = false;
public:
Input input;
int targetFrameRate = 60;
SDL_Window *window;
SDL_Renderer *renderer;
Engine(std::string title, int width, int height);
void initSound();
void clear(SDL_Color color);
void startFrame();
void render();
void update();
~Engine();
};

39
include/input.h Normal file
View File

@@ -0,0 +1,39 @@
#include <SDL.h>
#include <map>
#include <string>
struct Controller
{
SDL_GameController* controller;
std::string name;
std::map<Uint8, bool> buttons;
Sint16 leftStickX = 0;
Sint16 leftStickY = 0;
Sint16 rightStickX = 0;
Sint16 rightStickY = 0;
Sint16 leftTrigger = 0;
Sint16 rightTrigger = 0;
};
struct Mouse
{
int x;
int y;
int wheel;
std::map<int, bool> buttons;
};
class Input
{
private:
SDL_Event event;
public:
bool exit;
std::map<int, Controller> controllers;
std::map<SDL_Keycode, bool> activeKeys;
Mouse mouse;
Input();
void update();
};

26
include/sound.h Normal file
View File

@@ -0,0 +1,26 @@
#include <SDL_mixer.h>
class Music
{
private:
Mix_Music* music;
public:
Music(const char* path);
void play(int loops = -1);
void pause();
void stop();
void resume();
void setVolume(int volume);
~Music();
};
class Effect
{
private:
Mix_Chunk* effect;
public:
Effect(const char* path);
void setVolume(int volume);
void play(int loops = 0, int channel = -1);
~Effect();
};

67
include/sprite.h Normal file
View File

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

19
include/text.h Normal file
View File

@@ -0,0 +1,19 @@
#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(std::string font, int size, SDL_Color color);
~Text();
void update(std::string text, SDL_Renderer* renderer);
void draw(SDL_Renderer* renderer, int x, int y);
};

58
include/vector.h Normal file
View File

@@ -0,0 +1,58 @@
// prevent multiple inclusion
#pragma once
// class for 2D float vector
class float_vec2
{
// public members
public:
// x and y coordinates
float x, y;
// operator functions
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 for 2D integer vector
class int_vec2
{
// public members
public:
// x and y coordinates
int x, y;
// operator functions
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 };
}
};