git repos / blockattack-game

blame: source/code/gamecontroller.cpp

normal view · raw

826cf176 sago007 2016-03-12 10:53 1
/*
826cf176 sago007 2016-03-12 10:53 2
===========================================================================
826cf176 sago007 2016-03-12 10:53 3
blockattack - Block Attack - Rise of the Blocks
826cf176 sago007 2016-03-12 10:53 4
Copyright (C) 2005-2016 Poul Sander
826cf176 sago007 2016-03-12 10:53 5
826cf176 sago007 2016-03-12 10:53 6
This program is free software: you can redistribute it and/or modify
826cf176 sago007 2016-03-12 10:53 7
it under the terms of the GNU General Public License as published by
826cf176 sago007 2016-03-12 10:53 8
the Free Software Foundation, either version 2 of the License, or
826cf176 sago007 2016-03-12 10:53 9
(at your option) any later version.
826cf176 sago007 2016-03-12 10:53 10
826cf176 sago007 2016-03-12 10:53 11
This program is distributed in the hope that it will be useful,
826cf176 sago007 2016-03-12 10:53 12
but WITHOUT ANY WARRANTY; without even the implied warranty of
826cf176 sago007 2016-03-12 10:53 13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
826cf176 sago007 2016-03-12 10:53 14
GNU General Public License for more details.
826cf176 sago007 2016-03-12 10:53 15
826cf176 sago007 2016-03-12 10:53 16
You should have received a copy of the GNU General Public License
826cf176 sago007 2016-03-12 10:53 17
along with this program.  If not, see http://www.gnu.org/licenses/
826cf176 sago007 2016-03-12 10:53 18
826cf176 sago007 2016-03-12 10:53 19
Source information and contacts persons can be found at
826cf176 sago007 2016-03-12 10:53 20
http://www.blockattack.net
826cf176 sago007 2016-03-12 10:53 21
===========================================================================
826cf176 sago007 2016-03-12 10:53 22
*/
826cf176 sago007 2016-03-12 10:53 23
826cf176 sago007 2016-03-12 10:53 24
#include "gamecontroller.h"
826cf176 sago007 2016-03-12 10:53 25
#include "SDL_gamecontroller.h"
2cb68251 sago007 2016-03-14 18:01 26
#include "sago/platform_folders.h"
826cf176 sago007 2016-03-12 10:53 27
#include <iostream>
a077dceb sago007 2016-03-19 19:19 28
#include <map>
826cf176 sago007 2016-03-12 10:53 29
2cb68251 sago007 2016-03-14 18:01 30
static bool verbose = false;
2cb68251 sago007 2016-03-14 18:01 31
a077dceb sago007 2016-03-19 19:19 32
struct ControllerStatus {
a077dceb sago007 2016-03-19 19:19 33
	std::map<int, bool> AxisInDeadZone;
f2dac7e4 sago007 2016-04-09 19:24 34
	int player = 1;
a077dceb sago007 2016-03-19 19:19 35
};
a077dceb sago007 2016-03-19 19:19 36
a077dceb sago007 2016-03-19 19:19 37
static std::map<SDL_JoystickID, ControllerStatus> controllerStatusMap;
a077dceb sago007 2016-03-19 19:19 38
a077dceb sago007 2016-03-19 19:19 39
2cb68251 sago007 2016-03-14 18:01 40
void GameControllerSetVerbose(bool value) {
2cb68251 sago007 2016-03-14 18:01 41
	verbose = value;
2cb68251 sago007 2016-03-14 18:01 42
}
2cb68251 sago007 2016-03-14 18:01 43
a35ab74b sago007 2016-04-02 10:26 44
static const char* GameControllerGetName(SDL_GameController* gamecontroller) {
2cb68251 sago007 2016-03-14 18:01 45
	const char* result = SDL_GameControllerName(gamecontroller);
2cb68251 sago007 2016-03-14 18:01 46
	if (!result) {
2cb68251 sago007 2016-03-14 18:01 47
		result = "Unnamed";
2cb68251 sago007 2016-03-14 18:01 48
	}
2cb68251 sago007 2016-03-14 18:01 49
	return result;
2cb68251 sago007 2016-03-14 18:01 50
}
2cb68251 sago007 2016-03-14 18:01 51
826cf176 sago007 2016-03-12 10:53 52
void InitGameControllers() {
2cb68251 sago007 2016-03-14 18:01 53
	std::string configFile = sago::getConfigHome()+"/blockattack/gamecontrollerdb.txt";
2cb68251 sago007 2016-03-14 18:01 54
	int errorCode = SDL_GameControllerAddMappingsFromFile(configFile.c_str());
2cb68251 sago007 2016-03-14 18:01 55
	if (errorCode == -1 && verbose) {
2cb68251 sago007 2016-03-14 18:01 56
		std::cerr << "Could not load mapping file: " << configFile << std::endl;
2cb68251 sago007 2016-03-14 18:01 57
	}
2cb68251 sago007 2016-03-14 18:01 58
	if (verbose) {
2cb68251 sago007 2016-03-14 18:01 59
		std::cout << "Number of Game controllers: " << SDL_NumJoysticks() << std::endl;
2cb68251 sago007 2016-03-14 18:01 60
	}
c7f2082f sago007 2016-03-13 11:48 61
	SDL_GameController* controller = nullptr;
826cf176 sago007 2016-03-12 10:53 62
	for (int i = 0; i < SDL_NumJoysticks(); ++i) {
826cf176 sago007 2016-03-12 10:53 63
		if (SDL_IsGameController(i)) {
826cf176 sago007 2016-03-12 10:53 64
			controller = SDL_GameControllerOpen(i);
f2dac7e4 sago007 2016-04-09 19:24 65
			SDL_Joystick *j = SDL_GameControllerGetJoystick(controller);
f2dac7e4 sago007 2016-04-09 19:24 66
			SDL_JoystickID instanceId = SDL_JoystickInstanceID(j);
f2dac7e4 sago007 2016-04-09 19:24 67
			controllerStatusMap[instanceId].player = 1;
2cb68251 sago007 2016-03-14 18:01 68
			if (verbose) {
2cb68251 sago007 2016-03-14 18:01 69
				std::cout << "Supported game controller detected: " << GameControllerGetName(controller) << ", mapping: " << SDL_GameControllerMapping(controller) <<  std::endl;
f2dac7e4 sago007 2016-04-09 19:24 70
				std::cout << "Assigned to player: " << controllerStatusMap[instanceId].player << std::endl;
2cb68251 sago007 2016-03-14 18:01 71
			}
826cf176 sago007 2016-03-12 10:53 72
		}
826cf176 sago007 2016-03-12 10:53 73
	}
826cf176 sago007 2016-03-12 10:53 74
}
ce5aa7d3 sago007 2016-03-13 11:44 75
a077dceb sago007 2016-03-19 19:19 76
void checkDeadZone(const SDL_Event& event) {
a077dceb sago007 2016-03-19 19:19 77
	if (event.type != SDL_CONTROLLERAXISMOTION) {
a077dceb sago007 2016-03-19 19:19 78
		return;  //assert?
a077dceb sago007 2016-03-19 19:19 79
	}
a077dceb sago007 2016-03-19 19:19 80
	int value = event.caxis.value;
a077dceb sago007 2016-03-19 19:19 81
	if (value > -deadZoneLimit && value < deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 82
		controllerStatusMap[event.caxis.which].AxisInDeadZone[event.caxis.axis] = true;
a077dceb sago007 2016-03-19 19:19 83
	}
a077dceb sago007 2016-03-19 19:19 84
}
a077dceb sago007 2016-03-19 19:19 85
a077dceb sago007 2016-03-19 19:19 86
bool getDeadZone(SDL_JoystickID id, int axis) {
a077dceb sago007 2016-03-19 19:19 87
	return controllerStatusMap[id].AxisInDeadZone[axis];
a077dceb sago007 2016-03-19 19:19 88
}
a077dceb sago007 2016-03-19 19:19 89
a077dceb sago007 2016-03-19 19:19 90
void setDeadZone(SDL_JoystickID id, int axis, bool value) {
a077dceb sago007 2016-03-19 19:19 91
	controllerStatusMap[id].AxisInDeadZone[axis] = value;
a077dceb sago007 2016-03-19 19:19 92
}
a077dceb sago007 2016-03-19 19:19 93
f2dac7e4 sago007 2016-04-09 19:24 94
static bool skipThisPlayer(int playerNumber, const SDL_Event& event) {
f2dac7e4 sago007 2016-04-09 19:24 95
f2dac7e4 sago007 2016-04-09 19:24 96
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
f2dac7e4 sago007 2016-04-09 19:24 97
		ControllerStatus& cs = controllerStatusMap[event.cbutton.which];
f2dac7e4 sago007 2016-04-09 19:24 98
		if (cs.player == playerNumber) {
f2dac7e4 sago007 2016-04-09 19:24 99
			return false;
f2dac7e4 sago007 2016-04-09 19:24 100
		}
f2dac7e4 sago007 2016-04-09 19:24 101
	}
f2dac7e4 sago007 2016-04-09 19:24 102
	if (event.type == SDL_CONTROLLERAXISMOTION ) {
f2dac7e4 sago007 2016-04-09 19:24 103
		ControllerStatus& cs = controllerStatusMap[event.caxis.which];
f2dac7e4 sago007 2016-04-09 19:24 104
		if (cs.player == playerNumber) {
f2dac7e4 sago007 2016-04-09 19:24 105
			return false;
f2dac7e4 sago007 2016-04-09 19:24 106
		}
f2dac7e4 sago007 2016-04-09 19:24 107
	}
f2dac7e4 sago007 2016-04-09 19:24 108
	return true;
f2dac7e4 sago007 2016-04-09 19:24 109
}
f2dac7e4 sago007 2016-04-09 19:24 110
ce5aa7d3 sago007 2016-03-13 11:44 111
bool isPlayerDownEvent(int playerNumber, const SDL_Event& event) {
f2dac7e4 sago007 2016-04-09 19:24 112
	if (skipThisPlayer(playerNumber, event)) {
ce5aa7d3 sago007 2016-03-13 11:44 113
		return false;
ce5aa7d3 sago007 2016-03-13 11:44 114
	}
ce5aa7d3 sago007 2016-03-13 11:44 115
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
ce5aa7d3 sago007 2016-03-13 11:44 116
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN ) {
ce5aa7d3 sago007 2016-03-13 11:44 117
			return true;
ce5aa7d3 sago007 2016-03-13 11:44 118
		}
ce5aa7d3 sago007 2016-03-13 11:44 119
	}
a077dceb sago007 2016-03-19 19:19 120
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) {
a35ab74b sago007 2016-04-02 10:26 121
		const SDL_ControllerAxisEvent& a = event.caxis;
f2dac7e4 sago007 2016-04-09 19:24 122
		checkDeadZone(event);
a077dceb sago007 2016-03-19 19:19 123
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 124
			if (event.caxis.value > deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 125
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 126
				return true;
a077dceb sago007 2016-03-19 19:19 127
			}
a077dceb sago007 2016-03-19 19:19 128
		}
a077dceb sago007 2016-03-19 19:19 129
	}
ce5aa7d3 sago007 2016-03-13 11:44 130
	return false;
ce5aa7d3 sago007 2016-03-13 11:44 131
}
ce5aa7d3 sago007 2016-03-13 11:44 132
ce5aa7d3 sago007 2016-03-13 11:44 133
bool isPlayerUpEvent(int playerNumber, const SDL_Event& event) {
f2dac7e4 sago007 2016-04-09 19:24 134
	if (skipThisPlayer(playerNumber, event)) {
ce5aa7d3 sago007 2016-03-13 11:44 135
		return false;
ce5aa7d3 sago007 2016-03-13 11:44 136
	}
ce5aa7d3 sago007 2016-03-13 11:44 137
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
ce5aa7d3 sago007 2016-03-13 11:44 138
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP ) {
ce5aa7d3 sago007 2016-03-13 11:44 139
			return true;
ce5aa7d3 sago007 2016-03-13 11:44 140
		}
ce5aa7d3 sago007 2016-03-13 11:44 141
	}
a077dceb sago007 2016-03-19 19:19 142
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) {
a077dceb sago007 2016-03-19 19:19 143
		checkDeadZone(event);
a35ab74b sago007 2016-04-02 10:26 144
		const SDL_ControllerAxisEvent& a = event.caxis;
a077dceb sago007 2016-03-19 19:19 145
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 146
			if (event.caxis.value < -deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 147
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 148
				return true;
a077dceb sago007 2016-03-19 19:19 149
			}
a077dceb sago007 2016-03-19 19:19 150
		}
a077dceb sago007 2016-03-19 19:19 151
	}
ce5aa7d3 sago007 2016-03-13 11:44 152
	return false;
ce5aa7d3 sago007 2016-03-13 11:44 153
}
ce5aa7d3 sago007 2016-03-13 11:44 154
ce5aa7d3 sago007 2016-03-13 11:44 155
bool isPlayerLeftEvent(int playerNumber, const SDL_Event& event) {
f2dac7e4 sago007 2016-04-09 19:24 156
	if (skipThisPlayer(playerNumber, event)) {
ce5aa7d3 sago007 2016-03-13 11:44 157
		return false;
ce5aa7d3 sago007 2016-03-13 11:44 158
	}
ce5aa7d3 sago007 2016-03-13 11:44 159
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
ce5aa7d3 sago007 2016-03-13 11:44 160
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT ) {
ce5aa7d3 sago007 2016-03-13 11:44 161
			return true;
ce5aa7d3 sago007 2016-03-13 11:44 162
		}
ce5aa7d3 sago007 2016-03-13 11:44 163
	}
a077dceb sago007 2016-03-19 19:19 164
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX ) {
a077dceb sago007 2016-03-19 19:19 165
		checkDeadZone(event);
a35ab74b sago007 2016-04-02 10:26 166
		const SDL_ControllerAxisEvent& a = event.caxis;
a077dceb sago007 2016-03-19 19:19 167
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 168
			if (event.caxis.value < -deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 169
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 170
				return true;
a077dceb sago007 2016-03-19 19:19 171
			}
a077dceb sago007 2016-03-19 19:19 172
		}
a077dceb sago007 2016-03-19 19:19 173
	}
ce5aa7d3 sago007 2016-03-13 11:44 174
	return false;
ce5aa7d3 sago007 2016-03-13 11:44 175
}
ce5aa7d3 sago007 2016-03-13 11:44 176
ce5aa7d3 sago007 2016-03-13 11:44 177
bool isPlayerRightEvent(int playerNumber, const SDL_Event& event) {
f2dac7e4 sago007 2016-04-09 19:24 178
	if (skipThisPlayer(playerNumber, event)) {
ce5aa7d3 sago007 2016-03-13 11:44 179
		return false;
ce5aa7d3 sago007 2016-03-13 11:44 180
	}
ce5aa7d3 sago007 2016-03-13 11:44 181
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
ce5aa7d3 sago007 2016-03-13 11:44 182
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT ) {
ce5aa7d3 sago007 2016-03-13 11:44 183
			return true;
ce5aa7d3 sago007 2016-03-13 11:44 184
		}
ce5aa7d3 sago007 2016-03-13 11:44 185
	}
a077dceb sago007 2016-03-19 19:19 186
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTX ) {
a077dceb sago007 2016-03-19 19:19 187
		checkDeadZone(event);
a35ab74b sago007 2016-04-02 10:26 188
		const SDL_ControllerAxisEvent& a = event.caxis;
a077dceb sago007 2016-03-19 19:19 189
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 190
			if (event.caxis.value > deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 191
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 192
				return true;
a077dceb sago007 2016-03-19 19:19 193
			}
a077dceb sago007 2016-03-19 19:19 194
		}
a077dceb sago007 2016-03-19 19:19 195
	}
ce5aa7d3 sago007 2016-03-13 11:44 196
	return false;
ce5aa7d3 sago007 2016-03-13 11:44 197
}
ce5aa7d3 sago007 2016-03-13 11:44 198
ce5aa7d3 sago007 2016-03-13 11:44 199
bool isPlayerSwitchEvent(int playerNumber, const SDL_Event& event) {
f2dac7e4 sago007 2016-04-09 19:24 200
	if (skipThisPlayer(playerNumber, event)) {
ce5aa7d3 sago007 2016-03-13 11:44 201
		return false;
ce5aa7d3 sago007 2016-03-13 11:44 202
	}
ce5aa7d3 sago007 2016-03-13 11:44 203
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
ce5aa7d3 sago007 2016-03-13 11:44 204
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_A || event.cbutton.button == SDL_CONTROLLER_BUTTON_B ) {
ce5aa7d3 sago007 2016-03-13 11:44 205
			return true;
ce5aa7d3 sago007 2016-03-13 11:44 206
		}
ce5aa7d3 sago007 2016-03-13 11:44 207
	}
ce5aa7d3 sago007 2016-03-13 11:44 208
	return false;
ce5aa7d3 sago007 2016-03-13 11:44 209
}
ce5aa7d3 sago007 2016-03-13 11:44 210
ce5aa7d3 sago007 2016-03-13 11:44 211
bool isPlayerPushEvent(int playerNumber, const SDL_Event& event) {
f2dac7e4 sago007 2016-04-09 19:24 212
	if (skipThisPlayer(playerNumber, event)) {
ce5aa7d3 sago007 2016-03-13 11:44 213
		return false;
ce5aa7d3 sago007 2016-03-13 11:44 214
	}
ce5aa7d3 sago007 2016-03-13 11:44 215
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
ce5aa7d3 sago007 2016-03-13 11:44 216
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER || event.cbutton.button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ) {
ce5aa7d3 sago007 2016-03-13 11:44 217
			return true;
ce5aa7d3 sago007 2016-03-13 11:44 218
		}
ce5aa7d3 sago007 2016-03-13 11:44 219
	}
ce5aa7d3 sago007 2016-03-13 11:44 220
	return false;
ce5aa7d3 sago007 2016-03-13 11:44 221
}