1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-26 12:22:41 +01:00

Only accept user input when the window is in focus!

This commit is contained in:
Timmy Sjöstedt 2013-07-04 11:56:23 +02:00
parent c033c877cf
commit ef104054a0

View File

@ -27,6 +27,7 @@ GTAEngine* gta = nullptr;
glm::vec3 plyPos;
glm::vec2 plyLook;
float moveSpeed = 20.0f;
bool inFocus = false;
void handleEvent(sf::Event &event)
{
@ -49,6 +50,12 @@ void handleEvent(sf::Event &event)
break;
}
break;
case sf::Event::GainedFocus:
inFocus = true;
break;
case sf::Event::LostFocus:
inFocus = false;
break;
default: break;
}
}
@ -87,50 +94,48 @@ void init(std::string gtapath)
void update(float dt)
{
static int i = 0;
if (inFocus) {
sf::Vector2i screenCenter{sf::Vector2i{window.getSize()} / 2};
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
sf::Vector2i deltaMouse = mousePos - screenCenter;
sf::Mouse::setPosition(screenCenter, window);
sf::Vector2i screenCenter{sf::Vector2i{window.getSize()} / 2};
sf::Vector2i mousePos = sf::Mouse::getPosition(window);
sf::Vector2i deltaMouse = mousePos - screenCenter;
sf::Mouse::setPosition(screenCenter, window);
plyLook.x += deltaMouse.x / 10.0;
plyLook.y += deltaMouse.y / 10.0;
plyLook.x += deltaMouse.x / 10.0;
plyLook.y += deltaMouse.y / 10.0;
if (plyLook.y > 90)
plyLook.y = 90;
else if (plyLook.y < -90)
plyLook.y = -90;
if (plyLook.y > 90)
plyLook.y = 90;
else if (plyLook.y < -90)
plyLook.y = -90;
glm::vec3 movement;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
movement.z = -1;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
movement.z = 1;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
movement.x = -1;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
movement.x = 1;
}
glm::vec3 movement;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
movement.z = -1;
glm::mat4 view;
view = glm::rotate(view, -90.f, glm::vec3(1, 0, 0));
view = glm::rotate(view, plyLook.y, glm::vec3(1, 0, 0));
view = glm::rotate(view, plyLook.x, glm::vec3(0, 0, 1));
if (glm::length(movement) > 0.f) {
plyPos += dt * moveSpeed * (glm::inverse(glm::mat3(view)) * movement);
}
view = glm::translate(view, -plyPos);
gta->renderer.camera.worldPos = plyPos;
gta->renderer.camera.frustum.view = view;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
movement.z = 1;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
movement.x = -1;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
movement.x = 1;
}
glm::mat4 view;
view = glm::rotate(view, -90.f, glm::vec3(1, 0, 0));
view = glm::rotate(view, plyLook.y, glm::vec3(1, 0, 0));
view = glm::rotate(view, plyLook.x, glm::vec3(0, 0, 1));
if (glm::length(movement) > 0.f) {
plyPos += dt * moveSpeed * (glm::inverse(glm::mat3(view)) * movement);
}
view = glm::translate(view, -plyPos);
gta->renderer.camera.worldPos = plyPos;
gta->renderer.camera.frustum.view = view;
i++;
}
void render()