Advertisement
Guest User

SFML keyboard not working

a guest
Aug 3rd, 2020
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <iostream>
  3.  
  4.  
  5. class Game
  6. {
  7.     public:
  8.         Game();
  9.         void run();
  10.    
  11.     private:
  12.         void proccessEvent();
  13.         void update(sf::Time deltaTime);
  14.         void render();
  15.         void handlePlayerInput(sf::Keyboard::Key key, bool isPressed);
  16.    
  17.     private:
  18.         const float playerSpeed = 0.1f;
  19.         const sf::Time TimePerFrame = sf::seconds(1.0f/60.0f);
  20.    
  21.         sf::RenderWindow mWindow;
  22.         sf::Keyboard::Key key;
  23.         sf::Texture texture;
  24.         sf::RectangleShape rect;
  25.         bool isPressed;
  26.         bool mIsMovingUp;
  27.         bool mIsMovingDown;
  28.         bool mIsMovingRight;
  29.         bool mIsMovingLeft;
  30.        
  31.    
  32.    
  33. };
  34.  
  35. Game::Game()
  36. : mWindow(sf::VideoMode(640, 480), "SFML Application"), rect()
  37. {
  38. //  rect.setTexture(texture);
  39.     rect.setFillColor(sf::Color::Red);
  40.     rect.setSize(sf::Vector2f(20.0f,20.0f));
  41.    
  42.    
  43. }
  44.    
  45. void Game::run()
  46. {
  47.     sf::Clock clock;
  48.     sf::Time timeSinceLastUpdate = sf::Time::Zero;
  49.    
  50.     while(mWindow.isOpen())
  51.     {
  52.         proccessEvent();
  53.         sf::Time elapsedTime = clock.restart();
  54.         timeSinceLastUpdate += elapsedTime;
  55.         while(timeSinceLastUpdate > TimePerFrame)
  56.         {
  57.             timeSinceLastUpdate -= TimePerFrame;
  58.             proccessEvent();
  59.             update(TimePerFrame);
  60.         }
  61.         render();
  62.     }
  63. }
  64.  
  65. void Game::proccessEvent()
  66. {
  67.     sf::Event event;
  68.     while (mWindow.pollEvent(event)) {
  69.         switch (event.type) {
  70.             case sf::Event::Closed:
  71.                 mWindow.close();
  72.                 break;
  73.                
  74.             case sf::Event::KeyPressed:
  75.                 handlePlayerInput(event.key.code, true);
  76.                 break;
  77.                
  78.             case sf::Event::KeyReleased:
  79.                 handlePlayerInput(event.key.code, true);
  80.                 break;
  81.                
  82.             default:
  83.                 break;
  84.         }
  85.     }
  86. }
  87.  
  88.  
  89. void Game::update(sf::Time deltaTime)
  90. {
  91.    
  92.     sf::Vector2<float> movement(0.0f,0.0f);
  93.     if(mIsMovingUp)
  94.     {
  95.         movement.y -= playerSpeed;
  96.     }
  97.     else if(mIsMovingDown)
  98.     {
  99.         movement.y += playerSpeed;
  100.     }
  101.     else if(mIsMovingRight)
  102.     {
  103.         movement.x -= playerSpeed;
  104.     }
  105.     else if(mIsMovingLeft)
  106.     {
  107.         movement.x += playerSpeed;
  108.     }
  109.     rect.move(movement * deltaTime.asSeconds());
  110.    
  111. }
  112.  
  113.  
  114.  
  115. void Game::render()
  116. {
  117.     if(!texture.loadFromFile("/Users/tahajalili/Desktop/Eagle.png"))
  118.     {
  119.         std::cout << "Error occured" << std::endl;
  120.     }
  121.    
  122.     mWindow.clear();
  123.     mWindow.draw(rect);
  124.     mWindow.display();
  125. }
  126.  
  127. void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed)
  128. {
  129.     if(key == sf::Keyboard::W)
  130.     {
  131.         mIsMovingUp = isPressed;
  132.     }
  133.     else if (key == sf::Keyboard::S)
  134.     {
  135.         mIsMovingDown = isPressed;
  136.     }
  137.     else if (key == sf::Keyboard::D)
  138.     {
  139.         mIsMovingRight = isPressed;
  140.     }
  141.     else if (key == sf::Keyboard::A)
  142.     {
  143.         mIsMovingLeft = isPressed;
  144.     }
  145. }
  146.  
  147. int main(int, char const**)
  148. {
  149.     Game game;
  150.     game.run();
  151.    
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement