Thread: Reading game port with a simple compiler like BuilderX?

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    6

    Question Reading game port with a simple compiler like BuilderX?

    I'm not really good with C++ and if I open a compiler like C++ Builder 6 I have no idea what's going on. I wrote a program in C++ BuilderX to control a robot and I built a parallel port controller for the motors but I need feedback for the position of the parts.
    I had an old analog joystick that I wanted to take apart and use the potentiometers from but I don't know how to read the game port. I can do the parallel port with inpout32.
    Question about the port: Is it just the game port that can accept analog input? I have to build an Analog to Digital converter for COM1-COM4 right?

    I downloaded this source code but I get these errors:

    aggregate 'REGS regs' has incomplete type and cannot be defined
    'int86' undeclared

    Code:
         /* +++Date last modified: 05-Jul-1997 */
    
    /*
    **  JOYSTICK.C
    **
    **  Joystick support functions
    **
    **  Public domain demo by Bob Stout
    */
    
    #include <dos.h>
    #include "snpdosys.h"
    
    struct joystick JoyStick;
    
    
    main()
    {
      read_joystick();
    }
    /*
    **  read_joystick()
    **
    **  returns Success_ or Error_
    **
    **  fills in global JoyStick structure
    */
    
    Boolean_T read_joystick(void)
    {
            union REGS regs;
    
            regs.h.ah = 0x84;                       /* Read the switches    */
            regs.x.dx = 0;
            int86(0x15, &regs, &regs);
            if (regs.x.cflag)
                    return Error_;
            JoyStick.switch_0 = TOBOOL(regs.h.al & 0x10);
            JoyStick.switch_1 = TOBOOL(regs.h.al & 0x20);
            JoyStick.switch_2 = TOBOOL(regs.h.al & 0x40);
            JoyStick.switch_3 = TOBOOL(regs.h.al & 0x80);
    
            regs.h.ah = 0x84;                       /* Read positions       */
            regs.x.dx = 1;
            int86(0x15, &regs, &regs);
            if (regs.x.cflag)
                    return Error_;
            JoyStick.pos_Ax = regs.x.ax;
            JoyStick.pos_Ay = regs.x.bx;
            JoyStick.pos_Bx = regs.x.cx;
            JoyStick.pos_By = regs.x.dx;
    
            return Success_;
    }
    It's funny, I have no idea what's going on. Sorry to post it here but I haven't had any time to teach myself. I really want to get the hardware operational.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    The problem is, you're using a win32 compiler to compile ancient 16-bit DOS code.
    So there's a whole bunch of stuff which is no longer applicable.

    As for what to do about it, that's harder to say.
    Looking for "win32 joystick" brings up various graphics library options (like DirectX), which you may not want to have just to read the joystick positions.

    http://www-2.cs.cmu.edu/afs/cs/user/...WWW/files.html
    Or you could rummage through here to find out which ports the joystick is on, and use the inpout32 method you're using already.

    Or find an old 16-bit compiler and hope the windows console compatibility layer takes care of enough stuff for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    OK. I have it where it reads the buttons using inpout32 but it won't input the stick position. In my device manager the I/O Range of the game port is 0200 - 0207 (0x200 - 0x207 in inpout32 right?). Anyway, I made a small loop to check the port 10 times per second and then increment the port address. As it ran, I moved the stick all over but got no different readings all through each address.

    Code:
    #include <iostream.h>
    #include <conio.h>
    #include <stdio.h>
    #include <windows.h>
    
    typedef short _stdcall( * inpfuncPtr ) ( short portaddr );
    typedef void _stdcall( * oupfuncPtr ) ( short portaddr, short datum );
    
    main()
    {
      HINSTANCE hLib;
      inpfuncPtr inp32;
      oupfuncPtr oup32;
    
      short InValue; //Value
      int OutAddress = 0x378; //Port Address
    
      // Load the library
      hLib = LoadLibrary( "inpout32.dll" );
    
      if ( hLib == NULL ) //If Load Fails
      {
        printf( "LoadLibrary Failed.\n" );
        return -1;
      }
    
      // Get the address of the function
      inp32 = ( inpfuncPtr )GetProcAddress( hLib, "Inp32" );
    
      if ( inp32 == NULL )
      {
        printf( "GetProcAddress for Inp32 Failed.\n" );
        return -1;
      }
    
    
      oup32 = ( oupfuncPtr )GetProcAddress( hLib, "Out32" );
    
      if ( oup32 == NULL )
      {
        printf( "GetProcAddress for Oup32 Failed.\n" );
        return -1;
      }
    
      //------------------------------------------------------------
    
      short i;
      int G;
    
      cout << "Press a key when ready to begin." << endl;
    
      for ( i = 0x200; i <= 0x207; i-- )
      {
        for ( G = 10; G > 0; G-- )
        {
          InValue = ( inp32 )( i );
          Sleep(100);
          cout << i << '\t' << InValue << endl;
        }
    
      }
    
      //0200 - 0207
    
      getch();
      return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    From my quick reading of RBIL, I thought those addresses were for digital joysticks (not analog ones like you're using). That is they only recognise ON/OFF states for each direction.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    Hmm...How do I read the analog inputs? I really don't want to make an A/D converter.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well they're on another port somewhere. They should be listed somewhere in RBIL
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    YYEEEEEEEEEEEESSSSSSSSS!! It works. The address was 0x201 but I had to write to the port to get the values. THANK YOU!!
    BTW, is there a statement that says if (X is within Y). Like if X is Y units either way instead of
    Code:
    if ((X <= (X+Y)) && (X >= (X- Y))
    One more question...How can I show a variable (Maybe...Used for percent complete) change without repeatedly outputing it?

    Not:
    51 % Complete
    52 % Complete
    53 % Complete

    But:
    51 % Complete all on teh same line 0 - 100 %

    THANK YOU again!!
    Last edited by MaxxMan-X; 02-27-2005 at 10:42 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Something like
    Code:
    printf( "Percent complete 00%%" );  // initial line
    Then to print a new percentage, do
    Code:
    printf( "\b\b\b%02d%%", new_percent );  // backspace 3 chars, and print 3 more
    fflush( stdout );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple game over network
    By Tommo in forum Networking/Device Communication
    Replies: 2
    Last Post: 10-12-2007, 03:07 PM
  2. 2D Game project requires extra C++ programmers, new or experienced
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 05-16-2007, 10:46 AM
  3. a simple Dev-C++ game graphics system
    By shintaro in forum Game Programming
    Replies: 1
    Last Post: 08-11-2005, 05:49 PM
  4. A simple game using RNG
    By amirahasanen1 in forum C++ Programming
    Replies: 6
    Last Post: 03-12-2005, 11:05 AM
  5. Problems with a simple console game
    By DZeek in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2005, 02:02 PM