Tutorial 4 Part 1

In this tutorial we will be looking at joysticks, how to detect if one is attached to the PC and how to actually read input from one.

First we will make sure that a joystick or controller exists.

  • Create a new project in DXC.

  • Put a background image into bank 1 and add the necessary code to create a screen.

  • Add the command "Redirect GDI Output to Bank 1" so that we can type GDI text to our Direct X screen.

    There are a number of new commands that we need to add to the code that I'll now explain.

    DXC has a function for counting the number of Direct X compatible devices on the system:

    The total number of devices found on the system will be at least 1; the keyboard. There will normally be 2 with the mouse as well.

    Each device is given a device number by the system. Given a device number, this command tells you the type of input device it is:

    The result of this command will be an integer number. 2 is a mouse, a keyboard is 3 and a joystick is 4. If a 0 was returned it means that the device was not found, a 1 means that the device is unknown or unsupported by DirectX.

    You can read the name of a device with this command:

    Each detected device has a name associated with it, obtained from its driver. This command returns a string.

  • In your code, add the InputDeviceCount function and print the total number of devices found onto the screen.

  • Next, loop through each device and find out its type and name. Print the results onto the screen, exchanging the device type number with the meaning of that number (eg, mouse instead of 2).

  • If a joystick was found, print this onto the screen.

    Try running your project with and without a joystick (or any other devices you have) and see what results you get. If you have trouble, compare your code to mine below:

    NOTE: If you do not wish to type the code below you may download or open this text file and simply copy and paste the code into your project.


    //TUTORIAL 4 PART 1

    //DEFINE VARIABLES

    Global KEYPRESS$, COUNT, X, TYPE, TYPESTR$, NAME$, JOYSTICK
    //COUNT will be used for the total number of devices
    //X will be used for looping through those devices
    //TYPE will be the Type ID for a device
    //TYPESTR$ will be the Type description
    //NAME$ will be the name of the device (extracted from the device driver)
    //JOYSTICK will be TRUE if a joystick was detected or FALSE if there was none found

    //DEFINE PROCEDURES
    DefProc INIT_DISPLAY()

    //---- START MAIN CODE ----

    //CALL THE PROCEDURE TO CREATE THE SCREEN
    INIT_DISPLAY()

    JOYSTICK = False //MAKE SURE THIS VARIABLE IS SET TO FALSE BEFORE WE BEGIN

    //START LOOP - THIS LOOP WILL REPEAT UNTIL A KEY IS PRESSED
    Repeat

      COUNT=InputDeviceCount() //GET THE NUMBER OF INPUT DEVICES DETECTED BY DIRECTX

      Text(10,150,"Total # of devices = " + Str$(COUNT)) //CONVERT COUNT TO A STRING AND PRINT ONTO THE SCREEN (YOU WOULD NORMALLY EXPECT TO SEE 2 OR MORE)

      For X = 1 To COUNT //LOOP THROUGH EACH DETECTED DEVICE

        TYPE = InputDeviceType(X) //GET THE TYPE ID NUMBER

        //CONSTRUCT A STRING DESCRIBING THE DEVICE TYPE


        If TYPE = 1 Then : TYPESTR$ = "Unknown" : EndIf

        If TYPE = 2 Then : TYPESTR$ = "Mouse" : EndIf

        If TYPE = 3 Then : TYPESTR$ = "Keyboard" : EndIf

        If TYPE = 4 Then : TYPESTR$ = "Joystick or pad" : JOYSTICK = True : EndIf //SET JOYSTICK TO TRUE TO SAY THAT WE HAVE FOUND A JOYSTICK

        NAME$ = InputDeviceName$(X) //GET THE DEVICE NAME (AS IT IS NAMED IN ITS DRIVER)

        //PRINT RESULTS

        Text(10,150+(X*12),"Device #" + " " + Str$(X) + " Type = " + TYPESTR$ + " Name = " + NAME$)

      Next

      //STATE WHETHER OR NOT A JOYSTICK WAS FOUND
      If JOYSTICK = True Then
        Text (10,120,"A Joystick was found.")
      Else
        Text (10,120,"A Joystick was NOT found.")
      EndIf

      KEYPRESS$ = Inkey$()

      Update Display

    Until KEYPRESS$ > ""

    //THE INIT_DISPLAY PROCEDURE CREATES A SCREEN AND A PLANE
    Procedure INIT_DISPLAY()

      Create Map 640,480,1,1 In Bank 50 //CREATE A MAP IN BANK 50
      Set Tile 0,0,1 Using Bank 50 //CREATE A TILE USING A BACKGROUND IMAGE IN BANK 1
      Create Plane 1 Using Bank 50 //CREATE PLANE 1 USING THE MAP IN BANK 50
      ReDirect GDI Output To Bank 1

    EndProc

    EndProg


  • This is what you should get without a joystick...

    Joystick detection


    and then with a joystick....

    Joystick detection


    Next Page