CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
  1. #1
    Join Date
    Apr 2009
    Posts
    1,355

    [win32] - about joystick

    i'm trying calculate the button selection... i'm mean using 1,2,3,4,5,6,7....
    what isn't right with these calculation:
    Code:
    lngButton = (log(b.dwButtons) / log(2)) + 1;
    some numbers are the same with diferent buttons.
    so what i'm doing wrong with calculation?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: [win32] - about joystick

    Don't understand what you are trying to do??

    What values can b.dwButtons take and what corresponding vakue do you want lngButton to be?? What is the type of lngButton?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [win32] - about joystick

    Quote Originally Posted by 2kaud View Post
    Don't understand what you are trying to do??

    What values can b.dwButtons take and what corresponding vakue do you want lngButton to be?? What is the type of lngButton?
    i know the 2^x=buttonvalue, but i want to know the x. i forget some math... but i'm read more.
    the lngButton is long

  4. #4
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [win32] - about joystick

    more easy than i thot:
    Code:
    long lngButton=0;
    
                            if (b.dwButtons!=0)
                            {
                                lngButton = log2(b.dwButtons);
                            }
    and now works.
    how can i test how many joysticks are connect to pc?
    how i know that was joystick 1 be used?

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: [win32] - about joystick

    What you want to know is the bit position that is set - yes?

    Have a look at _BitScanForward()
    http://msdn.microsoft.com/en-us/libr...vs.120%29.aspx
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [win32] - about joystick

    Quote Originally Posted by 2kaud View Post
    What you want to know is the bit position that is set - yes?

    Have a look at _BitScanForward()
    http://msdn.microsoft.com/en-us/libr...vs.120%29.aspx
    please see my last post

  7. #7
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [win32] - about joystick

    sorry... you have right... the JOYINFOEX.dwButtons it's a bit buttons
    let me ask: how can i combine the buttons?
    Code:
    if(b.dwButtons==JOY_BUTTON1 &  JOY_BUTTON2)
                                MessageBox(NULL,"hi", "hello",MB_OK);
    isn't the same of button 1 and 2 beeing pressed in same time?

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: [win32] - about joystick

    try this
    Code:
    if (((b.dwButtons & JOY_BUTTON1) == JOY_BUTTON1) && ((b.dwButtons & JOY_BUTTON2) == JOY_BUTTON2)) {
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [win32] - about joystick

    Quote Originally Posted by 2kaud View Post
    try this
    Code:
    if (((b.dwButtons & JOY_BUTTON1) == JOY_BUTTON1) && ((b.dwButtons & JOY_BUTTON2) == JOY_BUTTON2)) {
    works fine thanks.... i'm sorry, but that line can be simplificated?

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: [win32] - about joystick

    Depending...

    It might simplify to
    Code:
    if ((b.dwButtons & JOY_BUTTON1) && (b.dwButtons & JOY_BUTTON2)) {
    or even
    Code:
    if (b.dwButtons & (JOY_BUTTON1 | JOY_BUTTON2)) {
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [win32] - about joystick

    Quote Originally Posted by 2kaud View Post
    Depending...

    It might simplify to
    Code:
    if ((b.dwButtons & JOY_BUTTON1) && (b.dwButtons & JOY_BUTTON2)) {
    or even
    Code:
    if (b.dwButtons & (JOY_BUTTON1 | JOY_BUTTON2)) {
    Thanks for all

  12. #12
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [win32] - about joystick

    sorry, but can i do something like these:
    if(buttons==(JOY_BUTTON1 | JOY_BUTTON2))
    //do something
    ???
    i mean convert the b.dwButtons to buttons

  13. #13
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [win32] - about joystick

    i did these:

    Code:
    JOYINFOEX  b;
                            b.dwSize=sizeof(JOYINFOEX );
                            b.dwFlags=JOY_RETURNALL;
    
                            joyGetPosEx(JOYSTICKID1,&b);
                            long h =(b.dwButtons & b.dwButtons);
    
                            if (h==(JOY_BUTTON1 | JOY_BUTTON2))
    
                                MessageBox(NULL,"hi beaury", "hello",MB_OK);
    works... but tell me what you think about the conversion?
    i have 1 personal reason for do these, but tell me something please
    Last edited by Cambalinho; August 1st, 2014 at 01:16 PM.

  14. #14
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: [win32] - about joystick

    Code:
    long h =(b.dwButtons & b.dwButtons);
    this is just
    Code:
    long h = b.dwButtons;
    Code:
    if (h==(JOY_BUTTON1 | JOY_BUTTON2))
    yes providing no other bits in h are set. If other bits in h are set then the compare will fail. That is why I used bitwise and. If the only bits in b.dwButtons that can be set at the same time are JOY_BUTTON1 JOY_BUTTON2 then
    Code:
    if (b.dwButtons == (JOY_BUTTON1 | JOY_BUTTON2))
    should be OK but IMO I would not recommend doing it like this. If in the future there was a change in the spec and bits other than JOY_BUTTON1 JOY_BUTTON2 could be set at the same time then this code would fail.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  15. #15
    Join Date
    Apr 2009
    Posts
    1,355

    Re: [win32] - about joystick

    Quote Originally Posted by 2kaud View Post
    Code:
    long h =(b.dwButtons & b.dwButtons);
    this is just
    Code:
    long h = b.dwButtons;
    Code:
    if (h==(JOY_BUTTON1 | JOY_BUTTON2))
    yes providing no other bits in h are set. If other bits in h are set then the compare will fail. That is why I used bitwise and. If the only bits in b.dwButtons that can be set at the same time are JOY_BUTTON1 JOY_BUTTON2 then
    Code:
    if (b.dwButtons == (JOY_BUTTON1 | JOY_BUTTON2))
    should be OK but IMO I would not recommend doing it like this. If in the future there was a change in the spec and bits other than JOY_BUTTON1 JOY_BUTTON2 could be set at the same time then this code would fail.
    see what i'm doing with joystick:
    Code:
    else if(wParam==JoystickTimer)
                        {
                            JOYINFOEX  b;
                            b.dwSize=sizeof(JOYINFOEX );
                            b.dwFlags=JOY_RETURNALL;
                            
                            //cicle all possible joysticks
                            for (int i=0, i<16; i++)
                            {
                                int direction=0;
                                if(joyGetPosEx(i,&b)!= JOYERR_NOERROR) //if theres any error then continue to next joystick
                                    continue;
                                
                                long h =b.dwButtons;
                                
                                //testing the directions
                                //will be 8 directions(inclued diagonals)
                                if (b.dwXpos == 0 && b.dwYpos == 0)
                                {
                                    direction=1
                                }
                                else if (b.dwXpos == 0 && b.dwYpos == 65535)
                                {
                                    direction=2;
                                }
                                Joystick(i, direction,h);
                            }
    i'm doing something wrong with directionals... but i will do some consts\enum
    thanks

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured