~ubuntu-branches/ubuntu/natty/spring/natty

« back to all changes in this revision

Viewing changes to rts/System/Input/Joystick.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Ritchie
  • Date: 2010-09-23 18:56:03 UTC
  • mfrom: (3.1.9 experimental)
  • Revision ID: james.westby@ubuntu.com-20100923185603-st97s5chplo42y7w
Tags: 0.82.5.1+dfsg1-1ubuntu1
* Latest upstream version for online play
* debian/control: Replace (rather than conflict) spring-engine
  - spring-engine will be a dummy package (LP: #612905)
  - also set maintainer to MOTU

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
 
2
 
 
3
#include "StdAfx.h"
 
4
#include "Joystick.h"
 
5
 
 
6
#include <SDL.h>
 
7
 
 
8
#include "InputHandler.h"
 
9
#include "ConfigHandler.h"
 
10
#include "LogOutput.h"
 
11
#include "EventHandler.h"
 
12
 
 
13
Joystick* stick = NULL;
 
14
 
 
15
void InitJoystick()
 
16
{
 
17
        const bool useJoystick = configHandler->Get("JoystickEnabled", true);
 
18
        if (useJoystick)
 
19
        {
 
20
                const int err = SDL_InitSubSystem(SDL_INIT_JOYSTICK);
 
21
                if (err == -1)
 
22
                {
 
23
                        LogObject() << "Could not initialise joystick subsystem: " << SDL_GetError();
 
24
                        return;
 
25
                }
 
26
                else
 
27
                {
 
28
                        stick = new Joystick();
 
29
                }
 
30
        };
 
31
}
 
32
 
 
33
Joystick::Joystick()
 
34
{
 
35
        const int numSticks = SDL_NumJoysticks();
 
36
        LogObject() << "Joysticks found: " << numSticks;
 
37
        
 
38
        const int stickNum = configHandler->Get("JoystickUse", 0);
 
39
        myStick =  SDL_JoystickOpen(stickNum);
 
40
        
 
41
        if (myStick)
 
42
        {
 
43
                LogObject() << "Using joystick " << stickNum << ": " << SDL_JoystickName(stickNum);
 
44
                inputCon = input.AddHandler(boost::bind(&Joystick::HandleEvent, this, _1));
 
45
        }
 
46
        else
 
47
        {
 
48
                LogObject() << "Joystick " << stickNum << " not found";
 
49
        }
 
50
}
 
51
 
 
52
Joystick::~Joystick()
 
53
{
 
54
        if (myStick)
 
55
                SDL_JoystickClose(myStick);
 
56
}
 
57
 
 
58
bool Joystick::HandleEvent(const SDL_Event& event)
 
59
{
 
60
        switch (event.type) {
 
61
                case SDL_JOYAXISMOTION:
 
62
                {
 
63
                        eventHandler.JoystickEvent("JoyAxis", event.jaxis.axis, event.jaxis.value);
 
64
                        break;
 
65
                }
 
66
                case SDL_JOYHATMOTION:
 
67
                {
 
68
                        eventHandler.JoystickEvent("JoyHat", event.jhat.hat, event.jhat.value);
 
69
                        break;
 
70
                }
 
71
                case SDL_JOYBUTTONDOWN:
 
72
                {
 
73
                        eventHandler.JoystickEvent("JoyButtonDown", event.jbutton.button, event.jbutton.state);
 
74
                        break;
 
75
                }
 
76
                case SDL_JOYBUTTONUP:
 
77
                {
 
78
                        eventHandler.JoystickEvent("JoyButtonUp", event.jbutton.button, event.jbutton.state);
 
79
                        break;
 
80
                }
 
81
                default:
 
82
                {
 
83
                }
 
84
        }
 
85
        return false;
 
86
}