git repos / blockattack-game

blame: source/code/joypad.cpp

normal view · raw

89c4a3a6 sago007 2008-08-29 14:32 1
/*
c53e6443 sago007 2012-04-17 11:04 2
===========================================================================
c53e6443 sago007 2012-04-17 11:04 3
blockattack - Block Attack - Rise of the Blocks
c53e6443 sago007 2012-04-17 11:04 4
Copyright (C) 2005-2012 Poul Sander
89c4a3a6 sago007 2008-08-29 14:32 5
c53e6443 sago007 2012-04-17 11:04 6
This program is free software: you can redistribute it and/or modify
c53e6443 sago007 2012-04-17 11:04 7
it under the terms of the GNU General Public License as published by
c53e6443 sago007 2012-04-17 11:04 8
the Free Software Foundation, either version 2 of the License, or
c53e6443 sago007 2012-04-17 11:04 9
(at your option) any later version.
89c4a3a6 sago007 2008-08-29 14:32 10
c53e6443 sago007 2012-04-17 11:04 11
This program is distributed in the hope that it will be useful,
c53e6443 sago007 2012-04-17 11:04 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
c53e6443 sago007 2012-04-17 11:04 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
c53e6443 sago007 2012-04-17 11:04 14
GNU General Public License for more details.
89c4a3a6 sago007 2008-08-29 14:32 15
c53e6443 sago007 2012-04-17 11:04 16
You should have received a copy of the GNU General Public License
c53e6443 sago007 2012-04-17 11:04 17
along with this program.  If not, see http://www.gnu.org/licenses/
89c4a3a6 sago007 2008-08-29 14:32 18
c53e6443 sago007 2012-04-17 11:04 19
Source information and contacts persons can be found at
c53e6443 sago007 2012-04-17 11:04 20
http://blockattack.sf.net
c53e6443 sago007 2012-04-17 11:04 21
===========================================================================
89c4a3a6 sago007 2008-08-29 14:32 22
*/
89c4a3a6 sago007 2008-08-29 14:32 23
89c4a3a6 sago007 2008-08-29 14:32 24
#include "joypad.h"
89c4a3a6 sago007 2008-08-29 14:32 25
89c4a3a6 sago007 2008-08-29 14:32 26
bool Joypad_init()
89c4a3a6 sago007 2008-08-29 14:32 27
{
c53e6443 sago007 2012-04-17 11:04 28
	if (0==SDL_InitSubSystem(SDL_INIT_JOYSTICK))
c53e6443 sago007 2012-04-17 11:04 29
		return true;
c53e6443 sago007 2012-04-17 11:04 30
	else
c53e6443 sago007 2012-04-17 11:04 31
		return false;
89c4a3a6 sago007 2008-08-29 14:32 32
}
89c4a3a6 sago007 2008-08-29 14:32 33
89c4a3a6 sago007 2008-08-29 14:32 34
Joypad_status Joypad_getStatus(SDL_Joystick *joystick)
89c4a3a6 sago007 2008-08-29 14:32 35
{
c53e6443 sago007 2012-04-17 11:04 36
	Joypad_status status;
c53e6443 sago007 2012-04-17 11:04 37
	for (int i=0; i<NRofPADS; i++)
c53e6443 sago007 2012-04-17 11:04 38
	{
c53e6443 sago007 2012-04-17 11:04 39
		//cout << SDL_JoystickNumAxes(joystick) << endl;
c53e6443 sago007 2012-04-17 11:04 40
		if (i*2>=SDL_JoystickNumAxes(joystick))
c53e6443 sago007 2012-04-17 11:04 41
		{
c53e6443 sago007 2012-04-17 11:04 42
			status.padDown[i]=false;
c53e6443 sago007 2012-04-17 11:04 43
			status.padUp[i] = false;
c53e6443 sago007 2012-04-17 11:04 44
			status.padLeft[i] = false;
c53e6443 sago007 2012-04-17 11:04 45
			status.padRight[i] = false;
c53e6443 sago007 2012-04-17 11:04 46
		}
c53e6443 sago007 2012-04-17 11:04 47
		else
c53e6443 sago007 2012-04-17 11:04 48
		{
c53e6443 sago007 2012-04-17 11:04 49
			//cout << SDL_JoystickGetAxis(joystick,i*2+1)<< endl;
c53e6443 sago007 2012-04-17 11:04 50
			if (SDL_JoystickGetAxis(joystick,i*2)<(-8000))
c53e6443 sago007 2012-04-17 11:04 51
				status.padLeft[i]=1;
c53e6443 sago007 2012-04-17 11:04 52
			else
c53e6443 sago007 2012-04-17 11:04 53
				status.padLeft[i]=0;
c53e6443 sago007 2012-04-17 11:04 54
			if (SDL_JoystickGetAxis(joystick,i*2)>(8000))
c53e6443 sago007 2012-04-17 11:04 55
				status.padRight[i]=1;
c53e6443 sago007 2012-04-17 11:04 56
			else
c53e6443 sago007 2012-04-17 11:04 57
				status.padRight[i]=0;
c53e6443 sago007 2012-04-17 11:04 58
			if (SDL_JoystickGetAxis(joystick,i*2+1)<(-8000))
c53e6443 sago007 2012-04-17 11:04 59
				status.padUp[i]=1;
c53e6443 sago007 2012-04-17 11:04 60
			else
c53e6443 sago007 2012-04-17 11:04 61
				status.padUp[i]=0;
c53e6443 sago007 2012-04-17 11:04 62
			if (SDL_JoystickGetAxis(joystick,i*2+1)>(8000))
c53e6443 sago007 2012-04-17 11:04 63
				status.padDown[i]=1;
c53e6443 sago007 2012-04-17 11:04 64
			else
c53e6443 sago007 2012-04-17 11:04 65
				status.padDown[i]=0;
c53e6443 sago007 2012-04-17 11:04 66
		}
c53e6443 sago007 2012-04-17 11:04 67
	}//NRofPADS
c53e6443 sago007 2012-04-17 11:04 68
	for (int i=0; i<NRofBUTTONS; i++)
c53e6443 sago007 2012-04-17 11:04 69
	{
c53e6443 sago007 2012-04-17 11:04 70
		if (i>=SDL_JoystickNumButtons(joystick))
c53e6443 sago007 2012-04-17 11:04 71
			status.button[i]=false;
c53e6443 sago007 2012-04-17 11:04 72
		else if (1==SDL_JoystickGetButton(joystick,i))
c53e6443 sago007 2012-04-17 11:04 73
			status.button[i] = true;
c53e6443 sago007 2012-04-17 11:04 74
		else
c53e6443 sago007 2012-04-17 11:04 75
			status.button[i] = false;
c53e6443 sago007 2012-04-17 11:04 76
	}
c53e6443 sago007 2012-04-17 11:04 77
	return status;
89c4a3a6 sago007 2008-08-29 14:32 78
}
89c4a3a6 sago007 2008-08-29 14:32 79
89c4a3a6 sago007 2008-08-29 14:32 80
Joypad::Joypad()
89c4a3a6 sago007 2008-08-29 14:32 81
{
c53e6443 sago007 2012-04-17 11:04 82
	up=false;
c53e6443 sago007 2012-04-17 11:04 83
	down=false;
c53e6443 sago007 2012-04-17 11:04 84
	left=false;
c53e6443 sago007 2012-04-17 11:04 85
	right=false;
c53e6443 sago007 2012-04-17 11:04 86
	but1=false;
c53e6443 sago007 2012-04-17 11:04 87
	but2=false;
c53e6443 sago007 2012-04-17 11:04 88
	upREL=true;
c53e6443 sago007 2012-04-17 11:04 89
	downREL=true;
c53e6443 sago007 2012-04-17 11:04 90
	leftREL=true;
c53e6443 sago007 2012-04-17 11:04 91
	rightREL=true;
c53e6443 sago007 2012-04-17 11:04 92
	but1REL=true;
c53e6443 sago007 2012-04-17 11:04 93
	but2REL=true;
c53e6443 sago007 2012-04-17 11:04 94
	int joynum = 0;
c53e6443 sago007 2012-04-17 11:04 95
	while ((SDL_JoystickOpened(joynum))&&(joynum<Joypad_number))
c53e6443 sago007 2012-04-17 11:04 96
		joynum++;
c53e6443 sago007 2012-04-17 11:04 97
	if (joynum>=Joypad_number)
c53e6443 sago007 2012-04-17 11:04 98
		working = false;
c53e6443 sago007 2012-04-17 11:04 99
	else
c53e6443 sago007 2012-04-17 11:04 100
	{
c53e6443 sago007 2012-04-17 11:04 101
		joystick=SDL_JoystickOpen(joynum);
c53e6443 sago007 2012-04-17 11:04 102
		if (joystick==NULL)
c53e6443 sago007 2012-04-17 11:04 103
			working =false;
c53e6443 sago007 2012-04-17 11:04 104
		else
c53e6443 sago007 2012-04-17 11:04 105
			working=true;
c53e6443 sago007 2012-04-17 11:04 106
	}
89c4a3a6 sago007 2008-08-29 14:32 107
}
89c4a3a6 sago007 2008-08-29 14:32 108
89c4a3a6 sago007 2008-08-29 14:32 109
Joypad::~Joypad()
89c4a3a6 sago007 2008-08-29 14:32 110
{
d07b805e sago007 2009-01-27 13:15 111
	if(working)
c53e6443 sago007 2012-04-17 11:04 112
		SDL_JoystickClose(joystick);
89c4a3a6 sago007 2008-08-29 14:32 113
}
89c4a3a6 sago007 2008-08-29 14:32 114
89c4a3a6 sago007 2008-08-29 14:32 115
void Joypad::update()
89c4a3a6 sago007 2008-08-29 14:32 116
{
c53e6443 sago007 2012-04-17 11:04 117
	SDL_JoystickUpdate();
c53e6443 sago007 2012-04-17 11:04 118
	Joypad_status status = Joypad_getStatus(joystick);
c53e6443 sago007 2012-04-17 11:04 119
	if ((upREL)&&((status.padUp[0])||(status.padUp[1])||(status.padUp[2])||(status.padUp[3])))
c53e6443 sago007 2012-04-17 11:04 120
	{
c53e6443 sago007 2012-04-17 11:04 121
		up=true;
c53e6443 sago007 2012-04-17 11:04 122
		upREL=false;
c53e6443 sago007 2012-04-17 11:04 123
	}
c53e6443 sago007 2012-04-17 11:04 124
	else
c53e6443 sago007 2012-04-17 11:04 125
		up=false;
c53e6443 sago007 2012-04-17 11:04 126
	if ((downREL)&&((status.padDown[0])||(status.padDown[1])||(status.padDown[2])||(status.padDown[3])))
c53e6443 sago007 2012-04-17 11:04 127
	{
c53e6443 sago007 2012-04-17 11:04 128
		down=true;
c53e6443 sago007 2012-04-17 11:04 129
		downREL=false;
c53e6443 sago007 2012-04-17 11:04 130
	}
c53e6443 sago007 2012-04-17 11:04 131
	else
c53e6443 sago007 2012-04-17 11:04 132
		down=false;
c53e6443 sago007 2012-04-17 11:04 133
	if ((leftREL)&&((status.padLeft[0])||(status.padLeft[1])||(status.padLeft[2])||(status.padLeft[3])))
c53e6443 sago007 2012-04-17 11:04 134
	{
c53e6443 sago007 2012-04-17 11:04 135
		left=true;
c53e6443 sago007 2012-04-17 11:04 136
		leftREL=false;
c53e6443 sago007 2012-04-17 11:04 137
	}
c53e6443 sago007 2012-04-17 11:04 138
	else
c53e6443 sago007 2012-04-17 11:04 139
		left=false;
c53e6443 sago007 2012-04-17 11:04 140
	if ((rightREL)&&((status.padRight[0])||(status.padRight[1])||(status.padRight[2])||(status.padRight[3])))
c53e6443 sago007 2012-04-17 11:04 141
	{
c53e6443 sago007 2012-04-17 11:04 142
		right=true;
c53e6443 sago007 2012-04-17 11:04 143
		rightREL=false;
c53e6443 sago007 2012-04-17 11:04 144
	}
c53e6443 sago007 2012-04-17 11:04 145
	else
c53e6443 sago007 2012-04-17 11:04 146
		right=false;
c53e6443 sago007 2012-04-17 11:04 147
	if ((but1REL)&&((status.button[0])||(status.button[2])||(status.button[4])||(status.button[6])))
c53e6443 sago007 2012-04-17 11:04 148
	{
c53e6443 sago007 2012-04-17 11:04 149
		but1=true;
c53e6443 sago007 2012-04-17 11:04 150
		but1REL=false;
c53e6443 sago007 2012-04-17 11:04 151
	}
c53e6443 sago007 2012-04-17 11:04 152
	else
c53e6443 sago007 2012-04-17 11:04 153
		but1=false;
c53e6443 sago007 2012-04-17 11:04 154
	if ((but2REL)&&((status.button[1])||(status.button[3])||(status.button[5])||(status.button[7])))
c53e6443 sago007 2012-04-17 11:04 155
	{
c53e6443 sago007 2012-04-17 11:04 156
		but2=true;
c53e6443 sago007 2012-04-17 11:04 157
		but2REL=false;
c53e6443 sago007 2012-04-17 11:04 158
	}
c53e6443 sago007 2012-04-17 11:04 159
	else
c53e6443 sago007 2012-04-17 11:04 160
		but2=false;
c53e6443 sago007 2012-04-17 11:04 161
	//Now testing for up
c53e6443 sago007 2012-04-17 11:04 162
	if (!((status.padUp[0])||(status.padUp[1])||(status.padUp[2])||(status.padUp[3])))
c53e6443 sago007 2012-04-17 11:04 163
		upREL = true;
c53e6443 sago007 2012-04-17 11:04 164
	if (!((status.padDown[0])||(status.padDown[1])||(status.padDown[2])||(status.padDown[3])))
c53e6443 sago007 2012-04-17 11:04 165
		downREL = true;
c53e6443 sago007 2012-04-17 11:04 166
	if (!((status.padLeft[0])||(status.padLeft[1])||(status.padLeft[2])||(status.padLeft[3])))
c53e6443 sago007 2012-04-17 11:04 167
		leftREL = true;
c53e6443 sago007 2012-04-17 11:04 168
	if (!((status.padRight[0])||(status.padRight[1])||(status.padRight[2])||(status.padRight[3])))
c53e6443 sago007 2012-04-17 11:04 169
		rightREL= true;
c53e6443 sago007 2012-04-17 11:04 170
	if (!((status.button[0])||(status.button[2])||(status.button[4])||(status.button[6])))
c53e6443 sago007 2012-04-17 11:04 171
		but1REL = true;
c53e6443 sago007 2012-04-17 11:04 172
	if (!((status.button[1])||(status.button[3])||(status.button[5])||(status.button[7])))
c53e6443 sago007 2012-04-17 11:04 173
		but2REL = true;
89c4a3a6 sago007 2008-08-29 14:32 174
}
1970-01-01 00:00 175