From 50b1a15589e35951db112bea6c02071a048d51fc Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Sat, 12 Oct 2024 14:35:59 +0200 Subject: [PATCH] Keyboard events management --- Game.cpp | 43 ++++++++++++++++++++++++++++++++++++------- Game.hpp | 2 ++ ProtoTank.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 7 deletions(-) diff --git a/Game.cpp b/Game.cpp index 6288154..33c1a43 100644 --- a/Game.cpp +++ b/Game.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include "Engine/Utils/Perfs.hpp" @@ -48,12 +48,6 @@ GAME_STATUS Game::Tick() { } } - sf::Event event; - while (mMainWindow->pollEvent(event)) { - if (event.type == sf::Event::Closed) - mMainWindow->close(); - } - // Update game stats and internal stuff Update(); @@ -84,11 +78,46 @@ GAME_STATUS Game::Tick() { } void Game::Update() { + // Refresh keyboard inputs + KeyboardInputsCheck(); + // Game logic calls should go here... mWorldUI->Update(); mCockpitUI->Update(); } +void Game::KeyboardInputsCheck() { + const float deltaTimeS = mSysTimer.GetDeltaTime() / 1000; + + if (mMainWindow->hasFocus()) { + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z)) { + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::LShift)) + mWorld3D->UpdateCamera(CAMERA_MOVE_WALK, 15.0f * deltaTimeS); + else + mWorld3D->UpdateCamera(CAMERA_MOVE_WALK, 10.0f * deltaTimeS); + } + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::LShift)) + mWorld3D->UpdateCamera(CAMERA_MOVE_WALK, -15.0f * deltaTimeS); + else + mWorld3D->UpdateCamera(CAMERA_MOVE_WALK, -10.0f * deltaTimeS); + } + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) + mWorld3D->UpdateCamera(CAMERA_MOVE_STRAFE, -10.0f * deltaTimeS); + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) + mWorld3D->UpdateCamera(CAMERA_MOVE_STRAFE, 10.0f * deltaTimeS); + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) + mWorld3D->UpdateCamera(CAMERA_MOVE_FLY, 5.0f * deltaTimeS); + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)) + mWorld3D->UpdateCamera(CAMERA_MOVE_FLY, -5.0f * deltaTimeS); + } +} + void Game::Render() { // Clear the draw buffer mMainWindow->clear(sf::Color::Black); diff --git a/Game.hpp b/Game.hpp index 5311e86..1586911 100644 --- a/Game.hpp +++ b/Game.hpp @@ -36,6 +36,8 @@ private: static Game* smInstance; void Update(); + void KeyboardInputsCheck(); + void Render(); bool mbDbgModeEnabled; diff --git a/ProtoTank.cpp b/ProtoTank.cpp index 734e8b2..4458d87 100644 --- a/ProtoTank.cpp +++ b/ProtoTank.cpp @@ -1,4 +1,5 @@ #include +#include #include #include @@ -27,6 +28,45 @@ int main(int argc, char** argv) { GAME_STATUS status = GAME_INIT; for ( ;; ) { + sf::Event event; + while (mainWindow->pollEvent(event)) { + if (event.type == sf::Event::Closed) + switch (event.type) { + case sf::Event::Closed: + mainWindow->close(); + break; + + case sf::Event::Resized: + //TODO: Recreate window related resource + //event.size.width + //event.size.height + break; + + //case sf::Event::LostFocus: + // pause(); + // break; + + //if (event.type == sf::Event::GainedFocus) + // resume(); + // break; + + default: + break; + } + + // Focus only actions + if (mainWindow->hasFocus()) { + switch (event.type) { + case sf::Event::MouseMoved: + //if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) + break; + + default: + break; + } + } + } + status = arcadeGame.Tick(); if (status == GAME_QUIT)