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

Author Topic: [solved] axis and buttons  (Read 2620 times)

0 Members and 1 Guest are viewing this topic.

yarikonline

  • Newbie
  • *
  • Posts: 3
    • View Profile
[solved] axis and buttons
« on: January 20, 2016, 09:36:22 am »
Hi all, I'm new here, and I need your help (pls).

Yes, I tried to search, but found nothing usefull (but mb I just did it wrong).

OK, the problem is with sf::Joystick axis and buttons. For now I work at simple arcade project, where core functionality is about using controller. Without it game will be awefull. So this problem is critical.

So, when I try to cath pressed button on joystick with event, it works as intended, buttons return and all works. BUT (here is problem) when I move sticks, event also returns pressed buttons, and they are absolutely same, as normal buttons.

Shortly, I get same event.JoystickButton.button from button itself and from axis, and this issue prevent any kind of normal controls.

P.S. using standart x360 wired controller, win10 x64
P.S.2 sent it to friend, asking to test buttons, waitng for results

upd: check event type before grabbing any values from it
« Last Edit: January 20, 2016, 06:44:07 pm by yarikonline »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: axis and buttons
« Reply #1 on: January 20, 2016, 10:08:09 am »
Can you show the relevant piece of code?
Laurent Gomila - SFML developer

yarikonline

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: axis and buttons
« Reply #2 on: January 20, 2016, 03:40:33 pm »
this for some reason, may be it will be usefull
sf::RenderWindow mainWin(sf::VideoMode(800, 600), "JoySlime");
mainWin.setKeyRepeatEnabled(false);
mainWin.setFramerateLimit(240);
mainWin.setMouseCursorVisible(false);
mainWin.setJoystickThreshold(5);
sf::Event mwEvent;
 

and this is actually part with issue, it is wrapped in WHILE (mainWin.isOpen()), don't think this should be here
//main menu
//event handler
while (mainWin.pollEvent(mwEvent)) {
        //close window
        if (mwEvent.type == sf::Event::Closed) {
                GameState.isInMenu = false;
                mainWin.close();
        }
        //start new game while player press A (button 0) or SPACEBAR
        if (mwEvent.joystickButton.button == 0 || mwEvent.key.code == sf::Keyboard::Space) {
                GameState.isInMenu = false;
                GameState.isInGame = true;
                th_input.launch();                        //thread that handle input while in game
                mClock.restart();
        }
}
 

as I sad, all this works almost write, but this axis issue...
also noticed that while cursor is over window, event.joystickbutton returns mouse coords... is it intended, or I do something wrong?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: axis and buttons
« Reply #3 on: January 20, 2016, 03:45:06 pm »
Quote from: Event tutorial
Before dealing with events, it is important to understand what the sf::Event type is, and how to correctly use it. sf::Event is a union, which means that only one of its members is valid at a time (remember your C++ lesson: all the members of a union share the same memory space). The valid member is the one that matches the event type, for example event.key for a KeyPressed event. Trying to read any other member will result in an undefined behavior (most likely: random or invalid values). It it important to never try to use an event member that doesn't match its type.

[...]

Read the above paragraph once again and make sure that you fully understand it, the sf::Event union is the cause of many problems for inexperienced programmers.
Laurent Gomila - SFML developer

yarikonline

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: axis and buttons
« Reply #4 on: January 20, 2016, 06:43:03 pm »
oh my... it was so easy, and I didn't figure it out :(
shame on my head

thx, by the way