Irrlicht 0.11 - Patch for basic Win32 Joystick Support

Announce new projects or updates of Irrlicht Engine related tools, games, and applications.
Also check the Wiki
Post Reply
evolutional
Posts: 16
Joined: Mon Apr 11, 2005 2:32 pm
Location: LEEDS, England
Contact:

Irrlicht 0.11 - Patch for basic Win32 Joystick Support

Post by evolutional »

This is more of an experiment than a full blown addition, but I've just extended the basic Irrlicht IEventListener and Win32 Irrlicht Device to add basic joystick support.

http://www.evolutional.co.uk/irrlicht/I ... ick_01.zip

I apologise for the cross-post, I posted this in the wrong forum - if a mod could delete the original post it would be cool.

Basics
The Windows Multimedia library, winmm.lib, allows windows applications to access attached joysticks, using the message pump to convey actions. By altering the CIrrDeviceWin32 class and adding some extra IEventListener interface, Irrlicht 0.11 now receives basic joystick events from 2 joystick devices (with a max of 4 buttons each).

Changes
Here's a brief outline to the changed Event structure:-

New enumeration:

Code: Select all

//! Enumeration for all joystick input events
     enum EJOYSTICK_INPUT_EVENT
    {
        //! Joystick button 1 was pressed down.
        EJIE_BUTTON1_PRESSED_DOWN = 0,
    
        //! Joystick button 1 was left up
        EJIE_BUTTON1_LEFT_UP,
        
        //! Joystick button 2 was pressed down.
       EJIE_BUTTON2_PRESSED_DOWN,
    
        //! Joystick button 2 was left up
        EJIE_BUTTON2_LEFT_UP,
        
        //! Joystick button 3 was pressed down.
       EJIE_BUTTON3_PRESSED_DOWN,
    
        //! Joystick button 3 was left up
        EJIE_BUTTON3_LEFT_UP,
        
        //! Joystick button 4 was pressed down.
        EJIE_BUTTON4_PRESSED_DOWN,
    
        //! Joystick button 4 was left up
        EJIE_BUTTON4_LEFT_UP,
        
        //! Joystick was moved
        EJIE_JOYSTICK_MOVED,
				
};
As you can see, you get an event if the joystick was moved or if a button was pressed or released.

Within the SEvent structure, there's a new struct:-

Code: Select all

struct
{
   //! Joystick Device ID
   u32 DeviceId;
				
   //! X position of joystick
   s32 X;

   //! Y position of joystick
   s32 Y;
                                    
   //! type of joystick event
   EJOYSTICK_INPUT_EVENT Event;
} JoystickInput;
As you can see, you get the X/Y position and the DeviceId of the joystick in question (first stick is device 0).


Usage
Using it is pretty simple; within your normal Event handler function:-

Code: Select all

    if ( event.EventType == irr::EET_JOYSTICK_INPUT_EVENT )
    {
        if (event.JoystickInput.Event == irr::EJIE_BUTTON1_PRESSED_DOWN)
        {
        	std::cout << "Device: " << event.JoystickInput.DeviceId << " - Button 1 pressed\n";
            return true;
        }       
        
        if (event.JoystickInput.Event == irr::EJIE_BUTTON1_LEFT_UP)
        {
        	std::cout << "Device: " << event.JoystickInput.DeviceId << " - Button 1 released\n";
            return true;
        }
        
        if (event.JoystickInput.Event == irr::EJIE_BUTTON2_PRESSED_DOWN)
        {
        	std::cout << "Device: " << event.JoystickInput.DeviceId << " - Button 2 pressed\n";
            return true;
        }       
        
        if (event.JoystickInput.Event == irr::EJIE_BUTTON2_LEFT_UP)
        {
        	std::cout << "Device: " << event.JoystickInput.DeviceId << " - Button 2 released\n";
            return true;
        }   	
    }
Installation
Unpack the Irrlicht source distribution and replace the following files with the ones from this patch:-

CIrrDeviceWin32.h
CIrrDeviceWin32.cpp
include/IEventReceiver.h (remember to replace your SDK version too!)

Compile the Irrlicht.dll as normal, linking with winmm.lib/winmm.a

Other things
So far there is NO capabilities information for the joysticks, so it's up to your own application to work out the calibrations and number of buttons present. I've also hardcoded the limit of 2 joysticks, but this can easily be raised if needed.

If you find this useful, let me know. If anyone wishes to offer any further advice or improvements to this (very simple) implementation, feel free to drop me a line. If I get round to it, I'll also attempt to add the same functionality to the Linux Device.

Download
http://www.evolutional.co.uk/irrlicht/I ... ick_01.zip


Enjoy!

- Oli
evolutional
Posts: 16
Joined: Mon Apr 11, 2005 2:32 pm
Location: LEEDS, England
Contact:

Post by evolutional »

I'm thinking that for the next 'version' of this, I'll kill the individual numbered button pressed events and just have a single EJIE_BUTTON_LEFT_UP / EJIE_BUTTON_PRESSED_DOWN event and have a u8 ButtonId in the Event data section. This way, it allows for as many buttons as possible without having to handle each event in turn (tedious).
evolutional
Posts: 16
Joined: Mon Apr 11, 2005 2:32 pm
Location: LEEDS, England
Contact:

Post by evolutional »

I've made the update and it is better to work with.

Get it here:- http://www.evolutional.co.uk/irrlicht/I ... ick_02.zip

The SEvent::JoystickInput struct now has a ButtonId member and the input events are simply EJIE_BUTTON_LEFT_UP and EJIE_BUTTON_PRESSED_DOWN. Note the ButtonIds always start from zero and there is currently a max of 4 buttons supported (easy to increase if needed).

If anyone's interested, I'm working on some specialist GameController classes (Mouse and Joystick so far) that wrap the Irrlicht input event handlers. They are buffered and will normalise the input, which makes it easier to use in (action) games. Because they're classes, it allows you to split your input handling from outside of your main game core. I'll post more when I've done the keyboard input (Mouse/Joystick is complete).
Electron
Posts: 874
Joined: Sun Mar 14, 2004 12:05 am
Location: Massachusetts USA

Post by Electron »

cool
You do a lot of programming? Really? I try to get some in, but the debugging keeps me pretty busy.

Crucible of Stars
Soy1Bonus
Posts: 31
Joined: Mon Jun 21, 2004 8:41 am
Location: Spain (Cantabria)
Contact:

Post by Soy1Bonus »

I'm going to try this right now, because I'll buy a gamepad tomorrow. I'll try to use it in my game.

I'll post about it when it's done! Thank you very much!
lord_Revlis
Posts: 1
Joined: Sat Aug 26, 2006 12:40 am
Location: My room; basking in the glow of my monitor.

Post by lord_Revlis »

Yeah, I'm using Dev-Cpp, so what do I do with CIrrDeviceWin32.h and CIrrDeviceWin32.cpp?

Sorry, I'm good at programming, bad at file handling.
What the wicked dreads will overtake him, what the righteous desire will be granted. -Proverbs 10:24
Cristian
Posts: 55
Joined: Fri Nov 25, 2005 12:02 pm

Post by Cristian »

Thanks for the patch. Applied it on to Irrlicht and managed to add D-pad support (by adding it in my event receiver).
Post Reply