Joystick Position abfragen



  • Hallo,
    ich habe im Internet ein fertiges Programm in VisualBasic gefunden um die Position der Joystick-Achsen abzufragen.
    Hier der Link zum Code des fertigen Programms:
    http://www.activevb.de/tipps/vb6tipps/tipp0459.html
    Ich brauch für ein Schulprojekt ein C Programm das mir die Position des Joysticks abfragt. Leider hab ich noch nicht so viel Erfahrung in Programmieren um dieses fertige Programm von VisualBasic in C umzuwandeln.
    Ich habe mal versucht es umzuwandeln, klappt aber noch nich ganz, vll. kann mir jemand sagen was ich falsch gemacht habe..

    Hier mein Programm:

    #include "stdafx.h"
    #include <iostream.h>
    
    #include <windows.h>
    #include <mmSystem.h>
    
    long _stdcall joyGetDevCaps(long id, JOYCAPS lpCaps, long uSize);
    long _stdcall joyGetPos(long uJoyID, JOYINFO pji);
    
    struct JOYINFO{
      long X;
      long Y;
      long Z;
      long Buttons;
    }
    
    struct JOYCAPS{
      long wXmin;
      long wXmax;
      long wYmin;
      long wYmax;
    }
    
    int GetJoyMax(int joy, JOYINFO JI){
      JOYCAPS jc;
      if(joyGetDevCaps(joy, jc) != JOYERR_NOERROR){
       GetJoyMax = 0;
      }
      else{
       JI.X = jc.wXmax;
       JI.Y = jc.wYmax;
       GetJoyMax = 1;
      }
    }
    
    int GetJoyMin(int joy, JOYINFO JI){
      JOYCAPS jc;
      if(joyGetDevCaps(joy, jc, strlen(jc)) != JOYERR_NOERROR){
       GetJoyMin = 0;
      }
      else{
       JI.X = jc.wXmin;
       JI.Y = jc.wYmin;
       GetJoyMax = 1;
      }
    }
    
    int GetJoystick(int joy, JOYINFO JI){
      if(joyGetPos(joy, JI) != JOYERR_NOERROR){
       GetJoystick = 0;
      }
      else{
       GetJoystick = 1;
      }
    }
    
    int main(){
      long JOYERR_NOERR=0;
      long JOYSTICKID1=0;
    
    while(1==1){
       GetJoyMax(JOYSTICKID1, JInfo);
       GetJoyMin(JOYSTICKID1, JInfo);
       GetJoystick(JOYSTICKID1, JInfo);
       printf("Current X: %d\n", JInfo.X);
       printf("Current Y: %d\n", JInfo.Y);
      }
    
    return 0;
    }
    

    Ich brauch nur die x- und y-Positionen.

    Danke.



  • Dieser Thread wurde von Moderator/in HumeSikkins aus dem Forum C++ in das Forum WinAPI verschoben.

    Im Zweifelsfall bitte auch folgende Hinweise beachten:
    C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?

    Dieses Posting wurde automatisch erzeugt.



  • Ein Fehler den ich auf die Schnelle seh: Du verwendest "JOYINFO JI" als Output Parameter, übergibst es aber "by value" - was nicht gehen kann.



  • ich hab schon eine lösung gefunden.
    falls jemand meine lösung haben will werde ich sie mal posten..

    winmm.lib in das projekt einbinden!

    #include "stdafx.h"
    #include <stdlib.h>
    #include <mmSystem.h>
    
    int main(){
      long Xpos;
      long Ypos;
    
    JOYINFO JI;
    
    if (joyGetPos(JOYSTICKID1, &JI)==JOYERR_NOERROR){
              Xpos = JI.wXpos; //Position der X-Achse
       Ypos = JI.wYpos; //Position der Y-Achse
      }
      return 0;
    }
    

    das wars dann auch schon 🙂


Anmelden zum Antworten