From f6a1e59ebb53a3d79c67a00b2eb51041a05ff3e1 Mon Sep 17 00:00:00 2001 From: DcruBro Date: Mon, 16 Mar 2026 08:21:04 +0100 Subject: [PATCH] input, changes --- include/game/agame/camcontroller.hpp | 2 -- include/game/gamemanager.hpp | 9 ++++++++ include/game/input.hpp | 19 +++++++++++++++++ include/object/transform.hpp | 4 ++-- include/utils.hpp | 3 +++ src/game/agame/background.cpp | 14 ++++++++++-- src/game/agame/camcontroller.cpp | 32 ++++------------------------ src/game/agame/player.cpp | 20 ++++++++--------- src/game/gamemanager.cpp | 20 +++++++++++++++++ src/game/input.cpp | 27 +++++++++++++++++++++++ src/main.cpp | 2 ++ src/state/gamestate.cpp | 1 + 12 files changed, 109 insertions(+), 44 deletions(-) create mode 100644 include/game/input.hpp create mode 100644 src/game/input.cpp diff --git a/include/game/agame/camcontroller.hpp b/include/game/agame/camcontroller.hpp index 6e39913..c4bb579 100644 --- a/include/game/agame/camcontroller.hpp +++ b/include/game/agame/camcontroller.hpp @@ -19,8 +19,6 @@ namespace Game::AGame { } private: - float mSpeed = 200.f; // Pixels per second int mScreenW, mScreenH; - int mEdgeTolerance = 200; }; } \ No newline at end of file diff --git a/include/game/gamemanager.hpp b/include/game/gamemanager.hpp index a6327cb..900a298 100644 --- a/include/game/gamemanager.hpp +++ b/include/game/gamemanager.hpp @@ -8,6 +8,9 @@ #include #include #include +#include +#include +#include namespace Game { using clock = std::chrono::steady_clock; @@ -22,8 +25,14 @@ namespace Game { void run(std::stop_token stopToken); void setTargetUpdatesPerSecond(int target) { mTargetUpdatesPerSecond = target; } int getTargetUpdatesPerSecond() { return mTargetUpdatesPerSecond; } + + 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); + private: int mTargetUpdatesPerSecond = TARGET_UPDATE_RATE; clock::time_point mLastUpdate; + static std::unordered_map mSharedStrings; }; } \ No newline at end of file diff --git a/include/game/input.hpp b/include/game/input.hpp new file mode 100644 index 0000000..fdc5a33 --- /dev/null +++ b/include/game/input.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include + +namespace Game { + class Input { + public: + Input() = delete; + + static void update(); + static bool isKeyPressed(SDL_Scancode key); + static bool isKeyJustPressed(SDL_Scancode key); + static bool isKeyJustReleased(SDL_Scancode key); + private: + static const bool* mCurrentKeyStates; + static bool* mPreviousKeyStates; + static int mNumKeys; + }; +} \ No newline at end of file diff --git a/include/object/transform.hpp b/include/object/transform.hpp index bc288c7..b626d30 100644 --- a/include/object/transform.hpp +++ b/include/object/transform.hpp @@ -1,13 +1,13 @@ #pragma once namespace Game::Object { - typedef struct { + struct Transform { 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}; } \ No newline at end of file diff --git a/include/utils.hpp b/include/utils.hpp index f51de91..7f2471d 100644 --- a/include/utils.hpp +++ b/include/utils.hpp @@ -24,6 +24,9 @@ #define ERROR(Msg) \ std::cout << "\033[31m[ERROR] " << __PRETTY_FUNCTION__ << ' ' << Msg << "\033[0m\n"; +#define PLNIMP(Msg) \ + std::cout << "\033[92m" << Msg << "\033[0m\n"; + #define GAME_ENTITY(ClassName) \ class ClassName : public Object::Entity { \ using Object::Entity::Entity; diff --git a/src/game/agame/background.cpp b/src/game/agame/background.cpp index f91d49c..342cfe2 100644 --- a/src/game/agame/background.cpp +++ b/src/game/agame/background.cpp @@ -9,8 +9,8 @@ namespace Game::AGame { int w, h; SDL_GetWindowSizeInPixels(Window::Window::getSDLWindowBackend(), &w, &h); - mTransform.scaleX *= 10.f; - mTransform.scaleY *= 10.f; + mTransform.scaleX *= 5.f; + mTransform.scaleY *= 5.f; mTransform.x -= mTex->getWidth() * mTransform.adjustedScaleX() / 2.f; mTransform.y -= mTex->getHeight() * mTransform.adjustedScaleY() / 2.f; @@ -22,6 +22,16 @@ namespace Game::AGame { void Background::update(float deltaTime) { if (!mIsActive) return; + + /*const bool* state = SDL_GetKeyboardState(nullptr); + if (state[SDL_SCANCODE_P]) { + mTransform.scaleX *= 2.f; + mTransform.scaleY *= 2.f; + } + if (state[SDL_SCANCODE_L]) { + mTransform.scaleX *= 0.5f; + mTransform.scaleY *= 0.5f; + }*/ //mTransform.rotation += 1.f; // Rotate clockwise for testing //mTransform.scaleX = 1.f + 1.f * std::sin(RUNNING_TIME() / 0.5f); // Pulsate scale for testing //mTransform.scaleY = 1.f + 0.5f * std::cos(RUNNING_TIME() / 0.5f); // Pulsate scale for testing diff --git a/src/game/agame/camcontroller.cpp b/src/game/agame/camcontroller.cpp index ab9ca99..36cc346 100644 --- a/src/game/agame/camcontroller.cpp +++ b/src/game/agame/camcontroller.cpp @@ -3,42 +3,18 @@ #include #include #include +#include namespace Game::AGame { void CamController::start() { mTex = nullptr; // No texture SDL_GetWindowSizeInPixels(Window::Window::getSDLWindowBackend(), &mScreenW, &mScreenH); + + mTransform.x = 0.f; + mTransform.y = 0.f; } void CamController::update(float deltaTime) { if (!mIsActive) return; - Object::Entity* player = Game::State::GameState::getInstance().getEntityByName("Player"); // We get the pointer every frame, otherwise we might get screwed by vector reallocs - if (!player) return; // If the player doesn't exist, don't do anything - - float playerX = player->getTransform()->x; - float playerY = player->getTransform()->y; - float camX, camY; - Object::Camera::getInstance().getPosition(camX, camY); - - // Apply tolerance from the edges of the screen, so the camera doesn't immediately start moving when the player moves a little bit - float leftBound = camX - mScreenW / 2.f + mEdgeTolerance; - float rightBound = camX + mScreenW / 2.f - mEdgeTolerance; - float topBound = camY - mScreenH / 2.f + mEdgeTolerance; - float bottomBound = camY + mScreenH / 2.f - mEdgeTolerance; - - if (playerX < leftBound) { - Object::Camera::getInstance().move(-mSpeed * deltaTime, 0.f); - } else if (playerX > rightBound) { - Object::Camera::getInstance().move(mSpeed * deltaTime, 0.f); - } - - if (playerY < topBound) { - Object::Camera::getInstance().move(0.f, -mSpeed * deltaTime); - } else if (playerY > bottomBound) { - Object::Camera::getInstance().move(0.f, mSpeed * deltaTime); - } - - const bool* state = SDL_GetKeyboardState(nullptr); - mSpeed = state[SDL_SCANCODE_LSHIFT] ? 400.f : 200.f; } } \ No newline at end of file diff --git a/src/game/agame/player.cpp b/src/game/agame/player.cpp index d9a387b..a9ef9b0 100644 --- a/src/game/agame/player.cpp +++ b/src/game/agame/player.cpp @@ -1,11 +1,14 @@ #include #include #include +#include namespace Game::AGame { void Player::start() { - mSound = Object::Sound("../resources/example.wav", Object::Format::WAV); + //mSound = Object::Sound("../resources/example.wav", Object::Format::WAV); //mSound.play(); + + mZIndex = 100; int w, h; SDL_GetWindowSizeInPixels(Window::Window::getSDLWindowBackend(), &w, &h); @@ -15,7 +18,7 @@ namespace Game::AGame { LOG("W: " << w << " H: " << h); - mSound.~Sound(); + //mSound.~Sound(); } void Player::update(float deltaTime) { @@ -23,16 +26,13 @@ namespace Game::AGame { //mTransform.rotation += 1.f; // Rotate clockwise for testing //mTransform.scaleX = 1.f + 1.f * std::sin(RUNNING_TIME() / 0.5f); // Pulsate scale for testing //mTransform.scaleY = 1.f + 0.5f * std::cos(RUNNING_TIME() / 0.5f); // Pulsate scale for testing - //Object::Camera::getInstance().move(1.f, 0.f); // Simple movement - const bool* state = SDL_GetKeyboardState(nullptr); - - if (state[SDL_SCANCODE_W]) mTransform.y -= mSpeed * deltaTime; - if (state[SDL_SCANCODE_S]) mTransform.y += mSpeed * deltaTime; - if (state[SDL_SCANCODE_A]) mTransform.x -= mSpeed * deltaTime; - if (state[SDL_SCANCODE_D]) mTransform.x += mSpeed * deltaTime; - mSpeed = state[SDL_SCANCODE_LSHIFT] ? 400.f : 200.f; + if (Input::isKeyPressed(SDL_SCANCODE_W)) mTransform.y -= mSpeed * deltaTime; + if (Input::isKeyPressed(SDL_SCANCODE_S)) mTransform.y += mSpeed * deltaTime; + if (Input::isKeyPressed(SDL_SCANCODE_A)) mTransform.x -= mSpeed * deltaTime; + if (Input::isKeyPressed(SDL_SCANCODE_D)) mTransform.x += mSpeed * deltaTime; + mSpeed = Input::isKeyPressed(SDL_SCANCODE_LSHIFT) ? 400.f : 200.f; } } \ No newline at end of file diff --git a/src/game/gamemanager.cpp b/src/game/gamemanager.cpp index 94b34a0..8cf2794 100644 --- a/src/game/gamemanager.cpp +++ b/src/game/gamemanager.cpp @@ -19,6 +19,7 @@ namespace Game { const auto frameStart = std::chrono::steady_clock::now(); try { + Input::update(); // Update input states at the start of each frame auto entities = State::GameState::getInstance().getEntitiesSnapshot(); for (auto* entity : entities) { if (entity) { @@ -38,4 +39,23 @@ namespace Game { } } } + + // Statics + std::unordered_map GameManager::mSharedStrings; + + void GameManager::setSharedData(const std::string& key, std::string data) { + mSharedStrings[key] = std::move(data); + } + + std::string GameManager::getSharedData(const std::string& key) { + auto it = mSharedStrings.find(key); + if (it != mSharedStrings.end()) { + return it->second; + } + return ""; // Key not found + } + + void GameManager::removeSharedData(const std::string& key) { + mSharedStrings.erase(key); + } } \ No newline at end of file diff --git a/src/game/input.cpp b/src/game/input.cpp new file mode 100644 index 0000000..86b2518 --- /dev/null +++ b/src/game/input.cpp @@ -0,0 +1,27 @@ +#include + +namespace Game { + const bool* Input::mCurrentKeyStates = nullptr; + bool* Input::mPreviousKeyStates = nullptr; + int Input::mNumKeys = 0; + + void Input::update() { + mPreviousKeyStates = const_cast(mCurrentKeyStates); + mCurrentKeyStates = SDL_GetKeyboardState(&mNumKeys); + } + + bool Input::isKeyPressed(SDL_Scancode key) { + if (key < 0 || key >= mNumKeys) return false; + return mCurrentKeyStates[key]; + } + + bool Input::isKeyJustPressed(SDL_Scancode key) { + if (key < 0 || key >= mNumKeys) return false; + return mCurrentKeyStates[key] && (!mPreviousKeyStates || !mPreviousKeyStates[key]); + } + + bool Input::isKeyJustReleased(SDL_Scancode key) { + if (key < 0 || key >= mNumKeys) return false; + return (!mCurrentKeyStates[key]) && mPreviousKeyStates && mPreviousKeyStates[key]; + } +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f540bd1..a53558a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,6 +13,8 @@ using namespace Game; int main() { + PLNIMP("Letnik3Zadnja - Licenca: LGPLv2.1-only, CC BY-SA 4.0"); + Window::Window window = Window::Window(); window.init(1280, 720, "Game Window"); diff --git a/src/state/gamestate.cpp b/src/state/gamestate.cpp index 888998c..3ce673a 100644 --- a/src/state/gamestate.cpp +++ b/src/state/gamestate.cpp @@ -1,4 +1,5 @@ #include +#include #include namespace Game::State {