basic movement

This commit is contained in:
2026-03-14 22:27:54 +01:00
parent b19f595daf
commit 2983b919cd
24 changed files with 368 additions and 57 deletions

View File

@@ -0,0 +1,20 @@
#pragma once
#include <object/entity.hpp>
#include <renderer/texture.hpp>
#include <renderer/font.hpp>
#include <object/sound.hpp>
namespace Game::AGame {
class Background : public Object::Entity {
using Object::Entity::Entity;
public:
~Background() override = default;
void start() override;
void update(float deltaTime) override;
private:
Object::Sound mSound;
};
}

View File

@@ -0,0 +1,26 @@
#pragma once
#include <object/entity.hpp>
#include <renderer/texture.hpp>
#include <renderer/font.hpp>
#include <object/sound.hpp>
namespace Game::AGame {
class CamController : public Object::Entity {
using Object::Entity::Entity;
public:
~CamController() override = default;
void start() override;
void update(float deltaTime) override;
void onWindowResized(int newWidth, int newHeight) override {
mScreenW = newWidth;
mScreenH = newHeight;
}
private:
float mSpeed = 200.f; // Pixels per second
int mScreenW, mScreenH;
int mEdgeTolerance = 200;
};
}

View File

@@ -16,5 +16,6 @@ namespace Game::AGame {
private:
Object::Sound mSound;
float mSpeed = 200.f; // Pixels per second
};
}

View File

@@ -7,6 +7,7 @@
#include <mutex>
#include <chrono>
#include <functional>
#include <object/camera.hpp>
namespace Game {
using clock = std::chrono::steady_clock;
@@ -22,7 +23,7 @@ namespace Game {
void setTargetUpdatesPerSecond(int target) { mTargetUpdatesPerSecond = target; }
int getTargetUpdatesPerSecond() { return mTargetUpdatesPerSecond; }
private:
int mTargetUpdatesPerSecond = 60;
int mTargetUpdatesPerSecond = TARGET_UPDATE_RATE;
clock::time_point mLastUpdate;
};
}

24
include/object/camera.hpp Normal file
View File

@@ -0,0 +1,24 @@
#pragma once
#include <utils.hpp>
#include <mutex>
namespace Game::Object {
class Camera {
public:
Camera() = default;
DISABLE_COPY_AND_MOVE(Camera)
~Camera() = default;
static Camera& getInstance();
void setPosition(float x, float y);
void getPosition(float& x, float& y) const;
void move(float deltaX, float deltaY);
private:
mutable std::mutex mMutex;
float mX = 0.0f;
float mY = 0.0f;
};
}

View File

@@ -7,6 +7,7 @@
#include <SDL3_image/SDL_image.h>
#include <utility>
#include <memory>
#include <renderer/renderer.hpp>
namespace Game::Renderer {
class Renderer;
@@ -24,9 +25,12 @@ namespace Game::Object {
Entity& operator=(Entity&&) noexcept;
virtual ~Entity() = 0;
// Start is called when the entity is spawned
virtual void start() = 0;
// Update is called every update cycle; deltaTime is the time (in seconds) since the last update call
virtual void update(float deltaTime) = 0;
void render(Game::Renderer::Renderer* renderer);
virtual void onWindowResized(int newWidth, int newHeight) {} // Called when the window is resized, with the new width and height in pixels
void render(Game::Renderer::Renderer* renderer, Game::Renderer::RendererConfig config);
// Setters and getters
void setTexture(std::shared_ptr<Game::Renderer::Texture> tex) { mTex = tex; }
@@ -37,12 +41,15 @@ namespace Game::Object {
std::string getName() { return mName; }
Transform* getTransform() { return &mTransform; }
bool isActive() { return mIsActive; }
int getZIndex() const { return mZIndex; }
protected:
std::string mName;
std::shared_ptr<Game::Renderer::Texture> mTex;
Transform mTransform;
bool mIsActive;
int mZIndex = 0; // For rendering order; higher zIndex renders on top of lower zIndex
float mTiledScale = 1.f; // Only used if the texture is tiled, to determine how much to scale the texture when rendering (since the entire texture is rendered as a single tile, this is necessary to be able to have different sized tiles)
private:
float mScaleConstant = 0.25f;
};
}

View File

@@ -5,6 +5,8 @@ namespace Game::Object {
float x, y;
float rotation; // In degrees, clockwise
float scaleX, scaleY;
float adjustedScaleX() const { return scaleX * UNIVERSAL_SCALE_COEFFICIENT; }
float adjustedScaleY() const { return scaleY * UNIVERSAL_SCALE_COEFFICIENT; }
} Transform;
constexpr Transform DEFAULT_TRANSFORM{0.f, 0.f, 0.f, 1.f, 1.f};

View File

@@ -6,6 +6,13 @@
#include <utils.hpp>
namespace Game::Renderer {
typedef struct {
float camX;
float camY;
int screenW;
int screenH;
} RendererConfig;
class Renderer {
public:
Renderer();

View File

@@ -19,9 +19,13 @@ namespace Game::Renderer {
std::string getId();
float getWidth();
float getHeight();
bool isTiled() { return mIsTiled; }
void setTiled(bool tiled) { mIsTiled = tiled; }
protected:
SDL_Texture* mTex;
std::string mId;
private:
bool mIsTiled = false; // Whether the texture is a tileset that should be rendered as a single tile or not
};
}

View File

@@ -2,6 +2,7 @@
#include <vector>
#include <memory>
#include <functional>
#include <utils.hpp>
#include <object/entity.hpp>
#include <mutex>
@@ -18,6 +19,10 @@ namespace Game::State {
fn(mEntities);
}
void sort(); // Sort entities by zIndex for correct rendering order
Object::Entity* getEntityByName(const std::string& name); // Get an entity by name, returns nullptr if no entity with the name exists
std::vector<Object::Entity*> getEntitiesSnapshot(bool sortByZIndex = false); // Get a stable snapshot of entity pointers for iteration outside the lock
// Update entity at index, by REFERENCE.
Object::Entity* getAtIndex(size_t at);
// Add an entity to the gamestate.

View File

@@ -34,4 +34,7 @@
#define RUNNING_TIME() \
SDL_GetTicks() / 1000.f
#define PI 3.14159265358979323846f
#define PI 3.14159265358979323846f
#define UNIVERSAL_SCALE_COEFFICIENT 0.25f
#define TARGET_FPS 60
#define TARGET_UPDATE_RATE 60

View File

@@ -11,6 +11,7 @@
#include <game/gamemanager.hpp>
#include <audio/audio.hpp>
#include <chrono>
#include <mutex>
namespace Game::Window {
class Window {
@@ -25,18 +26,20 @@ namespace Game::Window {
void setTargetFPS(int fps) { mTargetFPS = fps; }
int getTargetFPS() { return mTargetFPS; }
static SDL_Window* getSDLWindowBackend() { return sWindowBackend; }
static SDL_Window* getSDLWindowBackend() { std::scoped_lock lock(sMutex); return sWindowBackend; }
Renderer::Renderer* getRenderer() { return &mRenderer; }
Renderer::Renderer* getRenderer() { std::scoped_lock lock(mMutex); return &mRenderer; }
private:
mutable std::mutex mMutex;
static std::mutex sMutex;
static inline SDL_Window* sWindowBackend = nullptr;
SDL_Window* mWindow;
Renderer::Renderer mRenderer;
Game::GameManager mGameManager;
std::jthread mGameThread;
bool mRunning;
int mTargetFPS = 60;
int mTargetFPS = TARGET_FPS;
size_t mFrameCount = 0;
std::chrono::steady_clock::time_point mLastFPSTime;
};