File: joypad.cpp

package info (click to toggle)
blockattack 1.3.1-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 7,180 kB
  • ctags: 1,826
  • sloc: cpp: 6,225; sh: 3,257; ansic: 3,055; makefile: 105
file content (176 lines) | stat: -rw-r--r-- 5,578 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
joypad.cpp - Enables joypad support
Copyright (C) 2005 Poul Sander

    This program is free software; you can redistribute it and/or modify        
    it under the terms of the GNU General Public License as published by        
    the Free Software Foundation; either version 2 of the License, or           
    (at your option) any later version.                                         
                                                                                
    This program is distributed in the hope that it will be useful,       
    but WITHOUT ANY WARRANTY; without even the implied warranty of              
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               
    GNU General Public License for more details.                
                                                                               
    You should have received a copy of the GNU General Public License           
    along with this program; if not, write to the Free Software                 
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   
                                                                                
    Poul Sander
    Rvehjvej 36, V. 1111                                                    
    2800 Kgs. Lyngby
    DENMARK
    poul.sander@tdcadsl.dk       
*/

#include "joypad.h"

bool Joypad_init()
{
    if(0==SDL_InitSubSystem(SDL_INIT_JOYSTICK))
        return true;
    else
        return false;
}

Joypad_status Joypad_getStatus(SDL_Joystick *joystick)
{
    Joypad_status status;
    for(int i=0; i<NRofPADS; i++)
    {
    	//cout << SDL_JoystickNumAxes(joystick) << endl;
        if(i*2>=SDL_JoystickNumAxes(joystick))
        {
            status.padDown[i]=false;
            status.padUp[i] = false;
            status.padLeft[i] = false;
            status.padRight[i] = false;
        }
        else
        {
	//cout << SDL_JoystickGetAxis(joystick,i*2+1)<< endl;
            if(SDL_JoystickGetAxis(joystick,i*2)<(-8000))
                status.padLeft[i]=1;
            else
                status.padLeft[i]=0;
            if(SDL_JoystickGetAxis(joystick,i*2)>(8000))
                status.padRight[i]=1;
            else
                status.padRight[i]=0;
            if(SDL_JoystickGetAxis(joystick,i*2+1)<(-8000))
                status.padUp[i]=1;
            else
                status.padUp[i]=0;
            if(SDL_JoystickGetAxis(joystick,i*2+1)>(8000))
                status.padDown[i]=1;
            else
                status.padDown[i]=0;
        }
    }//NRofPADS
    for(int i=0; i<NRofBUTTONS; i++)
    {
        if(i>=SDL_JoystickNumButtons(joystick))
            status.button[i]=false;
        else
            if(1==SDL_JoystickGetButton(joystick,i))
                status.button[i] = true;
            else
                status.button[i] = false;
    }
    return status;
}

Joypad::Joypad()
{
    up=false;
    down=false;
    left=false;
    right=false;
    but1=false;
    but2=false;
    upREL=true;
    downREL=true;
    leftREL=true;
    rightREL=true;
    but1REL=true;
    but2REL=true;
    int joynum = 0;
    while((SDL_JoystickOpened(joynum))&&(joynum<Joypad_number))
    	joynum++;
    if(joynum>=Joypad_number)
    	working = false;
    else
    {
        joystick=SDL_JoystickOpen(joynum);
	if(joystick==NULL)
		working =false;
	else
        	working=true;
    }
}

Joypad::~Joypad()
{
    SDL_JoystickClose(joystick);
}

void Joypad::update()
{
    SDL_JoystickUpdate();
    Joypad_status status = Joypad_getStatus(joystick);
    if((upREL)&&((status.padUp[0])||(status.padUp[1])||(status.padUp[2])||(status.padUp[3])))
    {
    	up=true;
        upREL=false;
    }
    else
        up=false;
    if((downREL)&&((status.padDown[0])||(status.padDown[1])||(status.padDown[2])||(status.padDown[3])))
    {
        down=true;
        downREL=false;
    }
    else
        down=false;
    if((leftREL)&&((status.padLeft[0])||(status.padLeft[1])||(status.padLeft[2])||(status.padLeft[3])))
    {
        left=true;
        leftREL=false;
    }
    else
        left=false;
    if((rightREL)&&((status.padRight[0])||(status.padRight[1])||(status.padRight[2])||(status.padRight[3])))
    {
        right=true;
        rightREL=false;
    }
    else
        right=false;
    if((but1REL)&&((status.button[0])||(status.button[2])||(status.button[4])||(status.button[6])))
    {
        but1=true;
        but1REL=false;
    }
    else
        but1=false;
    if((but2REL)&&((status.button[1])||(status.button[3])||(status.button[5])||(status.button[7])))
    {
        but2=true;
        but2REL=false;
    }
    else
        but2=false;
    //Now testing for up
    if(!((status.padUp[0])||(status.padUp[1])||(status.padUp[2])||(status.padUp[3])))
        upREL = true;
    if(!((status.padDown[0])||(status.padDown[1])||(status.padDown[2])||(status.padDown[3])))
        downREL = true;
    if(!((status.padLeft[0])||(status.padLeft[1])||(status.padLeft[2])||(status.padLeft[3])))
        leftREL = true;
    if(!((status.padRight[0])||(status.padRight[1])||(status.padRight[2])||(status.padRight[3])))
        rightREL= true;
    if(!((status.button[0])||(status.button[2])||(status.button[4])||(status.button[6])))
        but1REL = true;
    if(!((status.button[1])||(status.button[3])||(status.button[5])||(status.button[7])))
        but2REL = true;
}