SFML community forums

Help => Window => Topic started by: Sheygox on December 10, 2012, 03:17:36 am

Title: Can't make Joystick work
Post by: Sheygox on December 10, 2012, 03:17:36 am
Hi. I'm really sorry for my noob questions, but I'm pretty new to all this. The thing is I donĀ“t know how to make Joystick works. For Example, I have this code:

int parsekey() {
    char k;
    if (_kbhit() == 0) return 0;
        k = _getch();
    switch (k){
    case 'w': if (movedir != 3) {movedir = 1;} return 0; break;
    case 'a': if (movedir != 4) {movedir = 2;} return 0; break;
    case 's': if (movedir != 1) {movedir = 3;} return 0; break;
    case 'd': if (movedir != 2) {movedir = 4;} return 0; break;
    case 'p':
        gotoxy(console_x / 2 - 5, console_y / 2 - 1);
        printf ("P A U S E D");
        do { k = _getch(); } while (k != 'p');
        pos p; p.y = (world_y) / 2 - 1;

        for (   p.x  = (world_x) / 2 - 5;
                p.x <= (world_x) / 2 + 5;
                p.x++)

                render_pos(p);
        break;
    case 27: // esc
        return 1;
        break;
    }
    return 0;
}

This is an actual code intended to move a character, and it is functionally working. What I want to do is make use of the joystick function. I have Code::Blocks and SFML 1.6 already working and well configured, but maybe I'm missing something (I have followed THIS (http://sfml-dev.org/tutorials/1.6/start-cb.php) tutorial).
So, what I started doing is adding this lines just to make sure it is compilling.

    float x;
    x =  sf::Input::GetJoystickAxis(1, sf::Joy::AxisX);

And I have the following error:
cannot call member function 'float sf::Input::GetJoystickAxis(unsigned int, sf::Joy::Axis) const' without object
As I said, I'm pretty noob with all this, so I don't even know what's that error meaning. Please, if you could help I would be really gratefull. If you need to know something more, please tell. I need to have this working.
Title: Re: Can't make Joystick work
Post by: FRex on December 10, 2012, 03:39:52 am
I'm not even going to comment about the crazy contraption that you have going on there using kbhit and getch.....

Quote
cannot call member function 'float sf::Input::GetJoystickAxis(unsigned int, sf::Joy::Axis) const' without object
That says it all, you need sf::Input object to get inputs from it, you can get reference to sf::Input of a window using this function:
http://www.sfml-dev.org/documentation/1.6/classsf_1_1Window.php#ace1eacb2f3ba2ed15dcc5028d0c5df6d
in 2.0 you could do that without a need for window.
Title: Re: Can't make Joystick work
Post by: Sheygox on December 10, 2012, 05:43:42 am
Thanks for the Answer  :) - But unfortunately, I'm stuck again with another error. I changed the code of that line and now it is:

x =  sf::Window::GetInput.GetJoystickAxis(0,sf::Joy::AxisX);

The error is:
request for member 'GetJoystickAxis' in 'sf::Window::GetInput', which is of non-class type 'const sf::Input& (sf::Window::)()const'

This time, I'm more disoriented than before. I didn't use v2.0 because I thought it was relatively new, and it wouldn't have enough documentation to learn. I will give it a try, but if you could figure out why I keep getting that error, you would really help me.
By the way, thanks again!
Title: Re: Can't make Joystick work
Post by: thePyro_13 on December 10, 2012, 07:10:03 am
You have the same problem as you did before. GetInput is a non-static member function. You need to call GetInput on an actual instance of sf::Window.
Title: Re: Can't make Joystick work
Post by: G. on December 10, 2012, 09:09:09 am
What I want to do is make use of the joystick function. I have Code::Blocks and SFML 1.6 already working and well configured, but maybe I'm missing something (I have followed THIS (http://sfml-dev.org/tutorials/1.6/start-cb.php) tutorial).
Good, but why didn't you also read the other tutorials then? ;)
Read [SFML 1.6] Handling events (http://sfml-dev.org/tutorials/1.6/window-events.php), especially the "Getting real-time inputs" paragraph.

And yes you need a window to get inputs, do you have one?
Title: Re: Can't make Joystick work
Post by: Sheygox on December 10, 2012, 11:08:40 pm
Thanks a lot for your answers. I am having a headache with this. Every time I think I am getting close to what I want, a nwe problem appears. First of all, this is the final code.

   x =  sf::Window().GetInput().GetJoystickAxis(0, sf::Joy::AxisX);

And it seems it is working, because I didn't get any errors when I was compiling.
The big problem: Code::Blocks never finish the compilation. Check this out:

(http://imageshack.us/a/img40/6762/99745598.jpg)
Link to the image (http://k36.kn3.net/48FD45B53.png)

It is just freezing there. The only thing I can do is abort the compilation.

Maybe I'm misunderstanding this whole library. I don't want to open any other window. My game it is just a Snake in ascii.

(http://imageshack.us/a/img5/5959/snakeko.jpg)
Link to the picture (http://k39.kn3.net/08DF1082F.jpg)

I just want to be able to control its movement with 4 buttons in a Joystick. Does the Version 2.0 allows me to do this? without opening any window?
Title: Re: Can't make Joystick work
Post by: FRex on December 10, 2012, 11:28:27 pm
These pictures have error 403 on them.
Yes, 2.0 allows you to get keyboard, mouse and joystick input without a window.
Title: Re: Can't make Joystick work
Post by: Laurent on December 10, 2012, 11:30:38 pm
Quote
Does the Version 2.0 allows me to do this? without opening any window?
Absolutely, switching to SFML 2 is the solution to your problem.

You should have said earlier that you wanted to do that without a window. So that we could understand why you made so many syntax errors ;)
Title: Re: Can't make Joystick work
Post by: Sheygox on December 11, 2012, 12:13:52 am
You should have said earlier that you wanted to do that without a window. So that we could understand why you made so many syntax errors ;)

Yeah, sorry for that. As I said, I'm a newbie. I will try v2.0 and keep you updated with my future problems ;D
Thank you for all the help, you are an awesome community
Title: Re: Can't make Joystick work
Post by: Sheygox on December 11, 2012, 02:36:29 am
I finally made the transition to v2.0 - I have followed this guide (http://www.sfml-dev.org/tutorials/2.0/window-inputs.php) and noticed that the Joystick function is Static, so I configured my build options according the basic tutorial (http://www.sfml-dev.org/tutorials/2.0/start-cb.php)

I just wrote the line sf::Joystick::update(); just to test if I can compile the program, so my function is this:

int parsekey() {
    sf::Joystick::update();
    char k;
    if (_kbhit() == 0) return 0;
        k = _getch();
    switch (k){
    case 'w': if (movedir != 3) {movedir = 1;} return 0; break;
    case 'a': if (movedir != 4) {movedir = 2;} return 0; break;
    case 's': if (movedir != 1) {movedir = 3;} return 0; break;
    case 'd': if (movedir != 2) {movedir = 4;} return 0; break;
    case 'p':
        gotoxy(console_x / 2 - 5, console_y / 2 - 1);
        printf ("P A U S E D");
        do { k = _getch(); } while (k != 'p');
        pos p; p.y = (world_y) / 2 - 1;

        for (   p.x  = (world_x) / 2 - 5;
                p.x <= (world_x) / 2 + 5;
                p.x++)

                render_pos(p);
        break;
    case 27: // esc
        return 1;
        break;
    }
    return 0;
}

Now, the error I'm having it's the next one:
(http://img855.imageshack.us/img855/8610/capturequh.png)

I don't need to say I don't get this:
undefined reference to `_imp___ZN2sf8Joystick6updateEv'
What? I have to define the sf::joystick somewhere? Isn't is enough with just including <SFML/Window.hpp>?
Title: Re: Can't make Joystick work
Post by: Laurent on December 11, 2012, 08:07:49 am
You must link to sfml-window.
Title: Re: Can't make Joystick work
Post by: Sheygox on December 11, 2012, 03:55:24 pm
Laurent, as I said, I have followed step by step from here (http://www.sfml-dev.org/tutorials/2.0/start-cb.php) (with Static options).

This is how my build options are:
(http://imageshack.us/a/img208/2994/52532142.jpg)

RELEASE
(http://imageshack.us/a/img534/1082/11597545.jpg)

DEBUG
(http://imageshack.us/a/img836/8907/89551713.jpg)

And I also tried simply writing sfml-window and sfml-system, but I'm getting the same error
Title: Re: Can't make Joystick work
Post by: Laurent on December 11, 2012, 04:10:39 pm
SFML_STATIC (with an underscore), not SFML-STATIC.
Title: Re: Can't make Joystick work
Post by: Sheygox on December 11, 2012, 05:08:31 pm
Thanks for the answer, I changed that, but I'm still getting the same error. It's really weird though. I have also tried to find a finished project using sf::Joystick but with no luck at all.
I have had a great response from this community, I'm really considerating money donation right now.
Laurent, if you think of something that could help me solve this, please let me know
Title: Re: Can't make Joystick work
Post by: Laurent on December 11, 2012, 05:48:52 pm
You can't get a linker error that contains "_imp___" if you set SFML_STATIC right.

Have you tried a complete rebuild?