Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Input manager help (SFML2.0)  (Read 2338 times)

0 Members and 1 Guest are viewing this topic.

Nickalus

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Input manager help (SFML2.0)
« on: August 08, 2012, 06:37:35 pm »
I've been working on an input manager for a day or two. For some reason it isn't working like it should be. It works fine when using the arrow keys, but when using the wasd keys it acts like it isn't getting any input.

InputManager.hpp
#ifndef INPUTMANAGER_H
#define INPUTMANAGER_H

#include <SFML/Graphics.hpp>

class InputManager
{
        private:
                sf::Event inputEvent;
        public:
                InputManager(sf::Event);
                bool IsKeyPressed(sf::Keyboard::Key);
};

#endif
 

InputManager.cpp
#include "InputManager.hpp"
#include <SFML/Graphics.hpp>
#include <iostream>

InputManager::InputManager(sf::Event event)
{
        inputEvent = event;
}

bool InputManager::IsKeyPressed(sf::Keyboard::Key key)
{
        if(inputEvent.type == sf::Event::KeyPressed)
        {
                if(inputEvent.key.code == key)
                {
                        return true;
                }
        }
        return false;
}
 

Main loop code
while(window.isOpen())
    {
        sf::Event Event;
        InputManager *input = new InputManager(Event);
        while(window.pollEvent(Event))
        {
            //if(Event.Type == sf::Event::Closed || Event.Key.Code == sf::Key::Escape)
            if(Event.type == sf::Event::Closed || input->IsKeyPressed(sf::Keyboard::Escape))
                window.close();
        }

        //if(Window.GetInput().IsKeyDown(sf::Key::Right))
        if(input->IsKeyPressed(sf::Keyboard::D))
            playerVelocity.x = moveSpeed;
        else if(input->IsKeyPressed(sf::Keyboard::A))
            playerVelocity.x = -moveSpeed;
        else
            playerVelocity.x = 0;

        if(input->IsKeyPressed(sf::Keyboard::W) && jump)
        {
            playerVelocity.y = -jumpSpeed;
            jump = false;
        }
       
        if(input->IsKeyPressed(sf::Keyboard::Space))
        {
                        rect.setFillColor(sf::Color::Red);
        }

        if(!jump)
            playerVelocity.y += gravity;
        else
            playerVelocity.y = 0;

        playerPosition += playerVelocity;

        jump = playerPosition.y + 10 >= groundHeight;

        if(jump)
            playerPosition.y = groundHeight - 10;

        rect.setPosition(playerPosition);

        window.clear();
        level->DrawMap(window);
        window.draw(rect);
        window.display();
        delete input;
    }
 

I'm using Xubuntu 12.04 if that matters and everything is being linked dynamically.
Any help on this will be appreciated. Thanks!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Input manager help (SFML2.0)
« Reply #1 on: August 08, 2012, 07:07:15 pm »
Out of curiosity why are you reinventing the wheel when you just could've used sf::Keyboard::isKeyPressed(sf::Keyboard::Key)? ;)
It's often helpfull to read the tutorials/documentation.

I wonder how you managed to get the arrow keys to work. ;D
Whenever you pass the event to your input manager, you're coping the event class and since you create a new event object every iteration the event object in your manager won't contain any event polled by window.pollEvent(). You'd need to use a const reference and if you add a update event function and call it within the event loop you won't need new and delete and everything should work fine. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything