This commit is contained in:
2025-06-16 03:14:07 +01:00
parent 675ce0039a
commit 137c4a544b
26 changed files with 550 additions and 788 deletions

59
include/assets.hpp Normal file
View File

@@ -0,0 +1,59 @@
#ifndef ASSETS_HPP
#define ASSETS_HPP
#include <SDL3/SDL.h>
#include <SDL3_image/SDL_image.h>
#include <unordered_map>
#include <string>
// Structs
struct Rect {
int x;
int y;
int width;
int height;
Rect(int x = 0, int y = 0, int width = 0, int height = 0)
: x(x), y(y), width(width), height(height) {}
Rect(const SDL_Rect& rect)
: x(rect.x), y(rect.y), width(rect.w), height(rect.h) {}
};
struct Tile {
Rect srcRect;
std::string assetName;
};
// Classes
class AssetManager {
public:
AssetManager();
~AssetManager();
void load(const std::string& name, const std::string& filePath);
SDL_Surface* getAsset(const std::string& name) const;
void unloadAsset(const std::string& name);
void clearAssets();
private:
std::unordered_map<std::string, SDL_Surface*> assets;
};
struct TileMap {
public:
TileMap(AssetManager& assetManager);
~TileMap();
void addTile(const std::string& name, const Tile& tile);
Tile getTile(const std::string& name) const;
SDL_Surface* getTileAsset(const std::string& name) const;
Rect getTileRect(const std::string& name) const;
void removeTile(const std::string& name);
void clear();
private:
std::unordered_map<std::string, Tile> tiles;
AssetManager& assetManager;
};
#endif

View File

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

50
include/engine.hpp Normal file
View File

@@ -0,0 +1,50 @@
#ifndef RENDER_HPP
#define RENDER_HPP
// Include necessary headers
#include "assets.hpp"
#include "input.hpp"
#include <SDL3/SDL.h>
// Structs
struct Color {
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 a;
Color(Uint8 r = 0, Uint8 g = 0, Uint8 b = 0, Uint8 a = 255) : r(r), g(g), b(b), a(a) {}
};
// Classes
class Engine {
public:
Engine(const char* windowTitle, int windowWidth, int windowHeight, int windowFlags);
Engine(const char* windowTitle, int windowWidth, int windowHeight)
: Engine(windowTitle, windowWidth, windowHeight, 0) {}
~Engine();
void targetFPS(int fps);
void startFrame();
void clearScreen(const Color& color);
void draw(SDL_Surface* surface, Rect destRect);
void draw(SDL_Surface* surface, Rect srcRect, Rect destRect);
void draw(Tile tile, Rect destRect);
void draw(SDL_Surface* surface, Vector2 pos) { draw(surface, Rect(pos.x, pos.y, surface->w, surface->h)); }
void draw(SDL_Surface* surface, Rect srcRect, Vector2 pos) { draw(surface, srcRect, Rect(pos.x, pos.y, srcRect.width, srcRect.height)); }
void draw(Tile tile, Vector2 pos) { draw(tile, Rect(pos.x, pos.y, tile.srcRect.width, tile.srcRect.height)); }
void present();
Input input; // Input handler
AssetManager assets; // Asset manager
private:
SDL_Window* window;
SDL_Renderer* renderer;
int targetFPSValue = 60; // Target frames per second
Uint64 frameStartTime = 0; // Start time of the current frame
Uint64 frameEndTime = 0; // End time of the current frame
Uint64 frameDuration = 0; // Duration of the current frame in milliseconds
};
#endif

View File

@@ -1,61 +0,0 @@
#include <SDL.h>
#include <map>
#include <string>
#include <vector>
enum CBUTTONS
{
DPAD_UP = SDL_CONTROLLER_BUTTON_DPAD_UP,
DPAD_DOWN = SDL_CONTROLLER_BUTTON_DPAD_DOWN,
DPAD_LEFT = SDL_CONTROLLER_BUTTON_DPAD_LEFT,
DPAD_RIGHT = SDL_CONTROLLER_BUTTON_DPAD_RIGHT,
A = SDL_CONTROLLER_BUTTON_A,
B = SDL_CONTROLLER_BUTTON_B,
X = SDL_CONTROLLER_BUTTON_X,
Y = SDL_CONTROLLER_BUTTON_Y,
BACK = SDL_CONTROLLER_BUTTON_BACK,
GUIDE = SDL_CONTROLLER_BUTTON_GUIDE,
START = SDL_CONTROLLER_BUTTON_START,
LEFTSTICK = SDL_CONTROLLER_BUTTON_LEFTSTICK,
RIGHTSTICK = SDL_CONTROLLER_BUTTON_RIGHTSTICK,
LEFTSHOULDER = SDL_CONTROLLER_BUTTON_LEFTSHOULDER,
RIGHTSHOULDER = SDL_CONTROLLER_BUTTON_RIGHTSHOULDER
};
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();
bool keysPressed(std::vector<SDL_Keycode> keys);
bool controllerPressed(std::vector<Uint8> buttons, int controller = 0);
};

45
include/input.hpp Normal file
View File

@@ -0,0 +1,45 @@
#ifndef INPUT_HPP
#define INPUT_HPP
#include <SDL3/SDL.h>
#include <unordered_map>
#include <vector>
// Structs
struct Vector2 {
int x;
int y;
Vector2(int x = 0, int y = 0) : x(x), y(y) {}
};
// Classes
class Input {
public:
Input();
~Input();
void handleEvents();
bool quitRequested() const;
bool isKeyPressed(SDL_Scancode key);
bool isKeyPressed(std::vector<SDL_Scancode> keys);
bool hasKeyBeenPressed(SDL_Scancode key);
bool hasKeyBeenPressed(std::vector<SDL_Scancode> keys);
bool isMouseButtonPressed(Uint8 button);
bool isMouseButtonPressed(std::vector<Uint8> buttons);
bool hasMouseButtonBeenPressed(Uint8 button);
bool hasMouseButtonBeenPressed(std::vector<Uint8> buttons);
Vector2 getMousePosition() const;
private:
SDL_Event event;
std::unordered_map<SDL_Scancode, bool> keyStates;
std::unordered_map<SDL_Scancode, bool> keyFallingEdgeStates;
std::unordered_map<Uint8, bool> mouseButtonStates;
std::unordered_map<Uint8, bool> mouseButtonFallingEdgeStates;
Vector2 mousePosition;
bool quitRequestedFlag = false;
};
#endif

View File

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

View File

@@ -1,68 +0,0 @@
#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);
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 frameDuration = 0;
int frameCounter = 0;
std::string currentFrameName;
public:
bool end = false;
int frameDirection = 1;
int frameIndex = 0;
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);
void reset();
};
class AnimationManager
{
private:
std::map<std::string, Animation*> animations;
std::map<std::string, bool> flip;
std::map<std::string, bool> autoRect;
std::string currentAnimation;
bool playing = false;
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 playAnimation(std::string name);
void update();
void draw(SDL_Renderer* renderer);
};

View File

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

View File

@@ -1,58 +0,0 @@
// 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 };
}
};