Keyboard events management

This commit is contained in:
JackCarterSmith 2024-10-12 14:35:59 +02:00
parent 1f91a08465
commit 50b1a15589
Signed by: JackCarterSmith
GPG Key ID: 832E52F4E23F8F24
3 changed files with 78 additions and 7 deletions

View File

@ -2,7 +2,7 @@
#include <numeric>
#include <SFML/System.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Window/Keyboard.hpp>
#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);

View File

@ -36,6 +36,8 @@ private:
static Game* smInstance;
void Update();
void KeyboardInputsCheck();
void Render();
bool mbDbgModeEnabled;

View File

@ -1,4 +1,5 @@
#include <stdexcept>
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/ContextSettings.hpp>
@ -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)