Skip to main content
added 35 characters in body; added 1 character in body
Source Link
Jeff L
  • 161
  • 1
  • 9

Okay so I'm writing a controller class to expand the SFML joystick library capabilities. Since the different controllers I want to use for this game have different integer values mapped to different buttons, I want to be able to pass integer values to the class to map these values to buttons I have defined. As well, SFML can detect whether a button is pressed with sf::Joystick::isButtonPressed(unsigned int joystick, unsigned int button). However, this only detects if a button is currently pressed and thus holding the button will just register as repeated presses. So I wrote my own code to detect for a button press without a hold.

So here's the issue: What is the proper way for the player (outside the class) to access the set of buttons without exposing data? I could have getters for the buttons, but then the buttonPressed method couldn't change the value of held. The only way I could see this working is if I gave full access to the Button's. Or should I go about this a different way? Any help would be greatly appreciated. Here's the relevant code:

// Controller.h

class Controller
{
public:
    struct Button
    {
        unsigned int id;
        bool held;
    };

    // constructor which assigns a controller ID
    // and a set of buttons
    Controller(...); 

    // Checks each frame for each button if 
    // any are not held and sets held to false
    void update();

    bool buttonPressed(Button& button);
private:
    unsigned int controllerId_;
    Button A_, B_, X_, Y_, Z_, R_, L_, 
    START_, D_PAD_UP_, D_PAD_LEFT_, D_PAD_DOWN_, D_PAD_RIGHT_;
};

// Controller.cpp

bool Controller::buttonPressed(Button& button)
{
    if (sf::Joystick::isButtonPressed(controllerId_, button.id) && !button.held)
    {
        button.held = true;
        return true;
    }
    else
    {
        return false;
    }
}

Okay so I'm writing a controller class to expand the SFML joystick library capabilities. Since the different controllers I want to use for this game have different integer values mapped to different buttons, I want to be able to pass integer values to the class to map these values to buttons I have defined. As well, SFML can detect whether a button is pressed with sf::Joystick::isButtonPressed(unsigned int joystick, unsigned int button). However, this only detects if a button is currently pressed and thus holding the button will just register as repeated presses. So I wrote my own code to detect for a button press without a hold.

So here's the issue: What is the proper way for the player (outside the class) to access the set of buttons without exposing data? I could have getters for the buttons, but then the buttonPressed method couldn't change the value of held. The only way I could see this working is if I gave full access to the Button's. Or should I go about this a different way? Any help would be greatly appreciated. Here's the relevant code:

// Controller.h

class Controller
{
public:
    struct Button
    {
        unsigned int id;
        bool held;
    };

    // constructor which assigns a controller ID
    // and a set of buttons
    Controller(...); 

    // Checks each frame for each button if 
    // any are not held and sets held to false
    void update();

    bool buttonPressed(Button& button);
private:
    Button A_, B_, X_, Y_, Z_, R_, L_, 
    START_, D_PAD_UP_, D_PAD_LEFT_, D_PAD_DOWN_, D_PAD_RIGHT_;
};

// Controller.cpp

bool Controller::buttonPressed(Button& button)
{
    if (sf::Joystick::isButtonPressed(controllerId_, button.id) && !button.held)
    {
        button.held = true;
        return true;
    }
    else
    {
        return false;
    }
}

Okay so I'm writing a controller class to expand the SFML joystick library capabilities. Since the different controllers I want to use for this game have different integer values mapped to different buttons, I want to be able to pass integer values to the class to map these values to buttons I have defined. As well, SFML can detect whether a button is pressed with sf::Joystick::isButtonPressed(unsigned int joystick, unsigned int button). However, this only detects if a button is currently pressed and thus holding the button will just register as repeated presses. So I wrote my own code to detect for a button press without a hold.

So here's the issue: What is the proper way for the player (outside the class) to access the set of buttons without exposing data? I could have getters for the buttons, but then the buttonPressed method couldn't change the value of held. The only way I could see this working is if I gave full access to the Button's. Or should I go about this a different way? Any help would be greatly appreciated. Here's the relevant code:

// Controller.h

class Controller
{
public:
    struct Button
    {
        unsigned int id;
        bool held;
    };

    // constructor which assigns a controller ID
    // and a set of buttons
    Controller(...); 

    // Checks each frame for each button if 
    // any are not held and sets held to false
    void update();

    bool buttonPressed(Button& button);
private:
    unsigned int controllerId_;
    Button A_, B_, X_, Y_, Z_, R_, L_, 
    START_, D_PAD_UP_, D_PAD_LEFT_, D_PAD_DOWN_, D_PAD_RIGHT_;
};

// Controller.cpp

bool Controller::buttonPressed(Button& button)
{
    if (sf::Joystick::isButtonPressed(controllerId_, button.id) && !button.held)
    {
        button.held = true;
        return true;
    }
    else
    {
        return false;
    }
}
Source Link
Jeff L
  • 161
  • 1
  • 9

Having trouble writing a controller class

Okay so I'm writing a controller class to expand the SFML joystick library capabilities. Since the different controllers I want to use for this game have different integer values mapped to different buttons, I want to be able to pass integer values to the class to map these values to buttons I have defined. As well, SFML can detect whether a button is pressed with sf::Joystick::isButtonPressed(unsigned int joystick, unsigned int button). However, this only detects if a button is currently pressed and thus holding the button will just register as repeated presses. So I wrote my own code to detect for a button press without a hold.

So here's the issue: What is the proper way for the player (outside the class) to access the set of buttons without exposing data? I could have getters for the buttons, but then the buttonPressed method couldn't change the value of held. The only way I could see this working is if I gave full access to the Button's. Or should I go about this a different way? Any help would be greatly appreciated. Here's the relevant code:

// Controller.h

class Controller
{
public:
    struct Button
    {
        unsigned int id;
        bool held;
    };

    // constructor which assigns a controller ID
    // and a set of buttons
    Controller(...); 

    // Checks each frame for each button if 
    // any are not held and sets held to false
    void update();

    bool buttonPressed(Button& button);
private:
    Button A_, B_, X_, Y_, Z_, R_, L_, 
    START_, D_PAD_UP_, D_PAD_LEFT_, D_PAD_DOWN_, D_PAD_RIGHT_;
};

// Controller.cpp

bool Controller::buttonPressed(Button& button)
{
    if (sf::Joystick::isButtonPressed(controllerId_, button.id) && !button.held)
    {
        button.held = true;
        return true;
    }
    else
    {
        return false;
    }
}