Quellcode durchsuchen

SFML: ignore input if window is not focused

Streetwalrus Einstein vor 10 Jahren
Ursprung
Commit
571183a9f9
3 geänderte Dateien mit 16 neuen und 6 gelöschten Zeilen
  1. 1 0
      platform/sfml/Graphics.cpp
  2. 7 6
      platform/sfml/Input.cpp
  3. 8 0
      platform/sfml/sfwindow.h

+ 1 - 0
platform/sfml/Graphics.cpp

@@ -1,4 +1,5 @@
 #include "Graphics.h"
+#include "sfwindow.h"
 #include "engine/main.h"
 #include <SFML/Graphics.hpp>
 #include "utility/misc.h"

+ 7 - 6
platform/sfml/Input.cpp

@@ -1,34 +1,35 @@
 #include "Input.h"
+#include "sfwindow.h"
 #include <SFML/Window/Keyboard.hpp>
 
 #define INPUT WalrusRPG::Input
 
 bool INPUT::key_a()
 {
-    return sf::Keyboard::isKeyPressed(sf::Keyboard::Return);
+    return window.hasFocus() && sf::Keyboard::isKeyPressed(sf::Keyboard::Return);
 }
 
 bool INPUT::key_b()
 {
-    return sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace);
+    return window.hasFocus() && sf::Keyboard::isKeyPressed(sf::Keyboard::BackSpace);
 }
 
 bool INPUT::key_up()
 {
-    return sf::Keyboard::isKeyPressed(sf::Keyboard::W);
+    return window.hasFocus() && sf::Keyboard::isKeyPressed(sf::Keyboard::W);
 }
 
 bool INPUT::key_down()
 {
-    return sf::Keyboard::isKeyPressed(sf::Keyboard::S);
+    return window.hasFocus() && sf::Keyboard::isKeyPressed(sf::Keyboard::S);
 }
 
 bool INPUT::key_left()
 {
-    return sf::Keyboard::isKeyPressed(sf::Keyboard::A);
+    return window.hasFocus() && sf::Keyboard::isKeyPressed(sf::Keyboard::A);
 }
 
 bool INPUT::key_right()
 {
-    return sf::Keyboard::isKeyPressed(sf::Keyboard::D);
+    return window.hasFocus() && sf::Keyboard::isKeyPressed(sf::Keyboard::D);
 }

+ 8 - 0
platform/sfml/sfwindow.h

@@ -0,0 +1,8 @@
+#ifndef INCLUDE_SFWINDOW_H
+#define INCLUDE_SFWINDOW_H
+
+#include <SFML/Graphics/RenderWindow.hpp>
+
+extern sf::RenderWindow window;
+
+#endif