Removed some agame things; Added VSYNC
This commit is contained in:
@@ -7,7 +7,9 @@
|
||||
|
||||
namespace Game::AGame {
|
||||
GAME_ENTITY(Background)
|
||||
private:
|
||||
Object::Sound mSound;
|
||||
public:
|
||||
void onWindowResized(int newWidth, int newHeight) override;
|
||||
private:
|
||||
Object::Sound mSound;
|
||||
END_GAME_ENTITY()
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <object/entity.hpp>
|
||||
#include <renderer/texture.hpp>
|
||||
#include <renderer/font.hpp>
|
||||
#include <object/sound.hpp>
|
||||
|
||||
namespace Game::AGame {
|
||||
GAME_ENTITY(CamController)
|
||||
public:
|
||||
void onWindowResized(int newWidth, int newHeight) override {
|
||||
mScreenW = newWidth;
|
||||
mScreenH = newHeight;
|
||||
}
|
||||
|
||||
private:
|
||||
int mScreenW, mScreenH;
|
||||
END_GAME_ENTITY()
|
||||
}
|
||||
24
include/game/agame/sampletextbox.hpp
Normal file
24
include/game/agame/sampletextbox.hpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
#include <object/ui/uitextbox.hpp>
|
||||
|
||||
namespace Game::AGame {
|
||||
class SampleTextBox : public Object::UITextBox {
|
||||
using Object::UITextBox::UITextBox; // Inherit constructors
|
||||
|
||||
public:
|
||||
~SampleTextBox() override = default;
|
||||
|
||||
void start() override {
|
||||
// Call the base class start to initialize the text box
|
||||
mZIndex = 1000; // Ensure it renders on top of most other entities
|
||||
Object::UITextBox::start();
|
||||
setText("Hello, World!");
|
||||
|
||||
mIsActive = false;
|
||||
}
|
||||
void update(float deltaTime) override {
|
||||
// Call the base class update to handle input and text refreshing
|
||||
Object::UITextBox::update(deltaTime);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -11,9 +11,23 @@
|
||||
#include <game/input.hpp>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
namespace Game {
|
||||
using clock = std::chrono::steady_clock;
|
||||
|
||||
enum class GameStateEnum {
|
||||
RUNNING,
|
||||
PAUSED,
|
||||
STOPPED
|
||||
};
|
||||
|
||||
enum class SharedDataType {
|
||||
STRING,
|
||||
INT,
|
||||
FLOAT,
|
||||
BOOL
|
||||
};
|
||||
|
||||
class GameManager {
|
||||
public:
|
||||
@@ -27,14 +41,77 @@ namespace Game {
|
||||
int getTargetUpdatesPerSecond() { return mTargetUpdatesPerSecond; }
|
||||
float getLastDelta() { return mLastDelta; }
|
||||
|
||||
static void setSharedData(const std::string& key, std::string data);
|
||||
static std::string getSharedData(const std::string& key);
|
||||
static void removeSharedData(const std::string& key);
|
||||
template<typename T>
|
||||
static void setSharedData(const std::string& key, T data);
|
||||
template<typename T>
|
||||
static T getSharedData(const std::string& key);
|
||||
static void removeSharedData(const std::string& key, SharedDataType type);
|
||||
|
||||
static GameStateEnum getCurrentGameState() { return mCurrentGameState; }
|
||||
static void setCurrentGameState(GameStateEnum newState) { mCurrentGameState = newState; }
|
||||
|
||||
private:
|
||||
int mTargetUpdatesPerSecond = TARGET_UPDATE_RATE;
|
||||
clock::time_point mLastUpdate;
|
||||
static std::unordered_map<std::string, std::string> mSharedStrings;
|
||||
static std::unordered_map<std::string, int> mSharedInts;
|
||||
static std::unordered_map<std::string, float> mSharedFloats;
|
||||
static std::unordered_map<std::string, bool> mSharedBools;
|
||||
static GameStateEnum mCurrentGameState;
|
||||
float mLastDelta = 0.f;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
void GameManager::setSharedData(const std::string& key, T data) {
|
||||
if constexpr (std::is_same_v<T, std::string>) {
|
||||
mSharedStrings[key] = data;
|
||||
} else if constexpr (std::is_same_v<T, int>) {
|
||||
mSharedInts[key] = data;
|
||||
} else if constexpr (std::is_same_v<T, float>) {
|
||||
mSharedFloats[key] = data;
|
||||
} else if constexpr (std::is_same_v<T, bool>) {
|
||||
mSharedBools[key] = data;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
T GameManager::getSharedData(const std::string& key) {
|
||||
if constexpr (std::is_same_v<T, std::string>) {
|
||||
auto it = mSharedStrings.find(key);
|
||||
if (it != mSharedStrings.end()) {
|
||||
return it->second;
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
} else if constexpr (std::is_same_v<T, int>) {
|
||||
auto it = mSharedInts.find(key);
|
||||
if (it != mSharedInts.end()) {
|
||||
return it->second;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
} else if constexpr (std::is_same_v<T, float>) {
|
||||
auto it = mSharedFloats.find(key);
|
||||
if (it != mSharedFloats.end()) {
|
||||
return it->second;
|
||||
} else {
|
||||
return 0.0f;
|
||||
}
|
||||
} else if constexpr (std::is_same_v<T, bool>) {
|
||||
auto it = mSharedBools.find(key);
|
||||
if (it != mSharedBools.end()) {
|
||||
return it->second;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
static_assert(
|
||||
std::is_same_v<T, std::string> || std::is_same_v<T, int> ||
|
||||
std::is_same_v<T, float> || std::is_same_v<T, bool>,
|
||||
"Unsupported type for shared data"
|
||||
);
|
||||
|
||||
return T{};
|
||||
}
|
||||
}
|
||||
37
include/object/ui/uitextbox.hpp
Normal file
37
include/object/ui/uitextbox.hpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <object/entity.hpp>
|
||||
#include <renderer/font.hpp>
|
||||
#include <renderer/texture.hpp>
|
||||
#include <utility>
|
||||
#include <game/input.hpp>
|
||||
|
||||
namespace Game::Object {
|
||||
class UITextBox : public Entity {
|
||||
public:
|
||||
UITextBox(const std::string& name, std::shared_ptr<Renderer::Font> font, const Transform& transform, float x = 0.f, float y = 0.f);
|
||||
~UITextBox() override = default;
|
||||
|
||||
void start() override;
|
||||
void update(float deltaTime) override;
|
||||
|
||||
void setText(const std::string& text);
|
||||
std::string getText() const;
|
||||
std::string getValue() const;
|
||||
bool isFocused() const;
|
||||
|
||||
void setPosition(float x, float y) { mX = x; mY = y; }
|
||||
std::pair<float, float> getPosition() const { return {mX, mY}; }
|
||||
|
||||
private:
|
||||
bool isMouseInsideBox() const;
|
||||
void refreshVisualText();
|
||||
|
||||
float mX, mY;
|
||||
std::string mText;
|
||||
bool mIsFocused = false;
|
||||
float mBoxWidth = 0.f;
|
||||
float mBoxHeight = 0.f;
|
||||
bool mNeedsTextRefresh = true;
|
||||
};
|
||||
}
|
||||
@@ -24,11 +24,13 @@ namespace Game::Renderer {
|
||||
void destroy();
|
||||
|
||||
SDL_Renderer* getSDLRenderer() { return mRenderer; }
|
||||
bool isVSyncEnabled() const { return mVSyncEnabled; }
|
||||
|
||||
private:
|
||||
void mClear();
|
||||
void mPresent();
|
||||
|
||||
SDL_Renderer* mRenderer;
|
||||
bool mVSyncEnabled = false;
|
||||
};
|
||||
}
|
||||
@@ -44,4 +44,6 @@
|
||||
#define PI 3.14159265358979323846f
|
||||
#define UNIVERSAL_SCALE_COEFFICIENT 0.25f
|
||||
#define TARGET_FPS 60
|
||||
#define TARGET_UPDATE_RATE 60
|
||||
#define TARGET_UPDATE_RATE 120
|
||||
#define ENABLE_LOW_LATENCY_VSYNC 1
|
||||
#define VSYNC_FPS_OFFSET 2
|
||||
@@ -40,7 +40,11 @@ namespace Game::Window {
|
||||
std::jthread mGameThread;
|
||||
bool mRunning;
|
||||
int mTargetFPS = TARGET_FPS;
|
||||
size_t mFrameCount = 0;
|
||||
std::chrono::steady_clock::time_point mLastFPSTime;
|
||||
int mEffectiveFrameCap = TARGET_FPS;
|
||||
#if DEBUG
|
||||
size_t mTelemetryFrameCount = 0;
|
||||
double mTelemetryFrameTimeMsTotal = 0.0;
|
||||
#endif
|
||||
std::chrono::steady_clock::time_point mTelemetryStart = std::chrono::steady_clock::now();
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user