git repos / blockattack-game

blame: source/code/joypad.cpp

normal view · raw

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