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;
a077dceb sago007 2016-03-19 19:19 34
};
a077dceb sago007 2016-03-19 19:19 35
a077dceb sago007 2016-03-19 19:19 36
static std::map<SDL_JoystickID, ControllerStatus> controllerStatusMap;
a077dceb sago007 2016-03-19 19:19 37
a077dceb sago007 2016-03-19 19:19 38
2cb68251 sago007 2016-03-14 18:01 39
void GameControllerSetVerbose(bool value) {
2cb68251 sago007 2016-03-14 18:01 40
	verbose = value;
2cb68251 sago007 2016-03-14 18:01 41
}
2cb68251 sago007 2016-03-14 18:01 42
2cb68251 sago007 2016-03-14 18:01 43
static const char* GameControllerGetName(SDL_GameController *gamecontroller) {
2cb68251 sago007 2016-03-14 18:01 44
	const char* result = SDL_GameControllerName(gamecontroller);
2cb68251 sago007 2016-03-14 18:01 45
	if (!result) {
2cb68251 sago007 2016-03-14 18:01 46
		result = "Unnamed";
2cb68251 sago007 2016-03-14 18:01 47
	}
2cb68251 sago007 2016-03-14 18:01 48
	return result;
2cb68251 sago007 2016-03-14 18:01 49
}
2cb68251 sago007 2016-03-14 18:01 50
826cf176 sago007 2016-03-12 10:53 51
void InitGameControllers() {
2cb68251 sago007 2016-03-14 18:01 52
	std::string configFile = sago::getConfigHome()+"/blockattack/gamecontrollerdb.txt";
2cb68251 sago007 2016-03-14 18:01 53
	int errorCode = SDL_GameControllerAddMappingsFromFile(configFile.c_str());
2cb68251 sago007 2016-03-14 18:01 54
	if (errorCode == -1 && verbose) {
2cb68251 sago007 2016-03-14 18:01 55
		std::cerr << "Could not load mapping file: " << configFile << std::endl;
2cb68251 sago007 2016-03-14 18:01 56
	}
2cb68251 sago007 2016-03-14 18:01 57
	if (verbose) {
2cb68251 sago007 2016-03-14 18:01 58
		std::cout << "Number of Game controllers: " << SDL_NumJoysticks() << std::endl;
2cb68251 sago007 2016-03-14 18:01 59
	}
c7f2082f sago007 2016-03-13 11:48 60
	SDL_GameController* controller = nullptr;
826cf176 sago007 2016-03-12 10:53 61
	for (int i = 0; i < SDL_NumJoysticks(); ++i) {
826cf176 sago007 2016-03-12 10:53 62
		if (SDL_IsGameController(i)) {
826cf176 sago007 2016-03-12 10:53 63
			controller = SDL_GameControllerOpen(i);
2cb68251 sago007 2016-03-14 18:01 64
			if (verbose) {
2cb68251 sago007 2016-03-14 18:01 65
				std::cout << "Supported game controller detected: " << GameControllerGetName(controller) << ", mapping: " << SDL_GameControllerMapping(controller) <<  std::endl;
2cb68251 sago007 2016-03-14 18:01 66
			}
826cf176 sago007 2016-03-12 10:53 67
		}
826cf176 sago007 2016-03-12 10:53 68
	}
826cf176 sago007 2016-03-12 10:53 69
}
ce5aa7d3 sago007 2016-03-13 11:44 70
a077dceb sago007 2016-03-19 19:19 71
void checkDeadZone(const SDL_Event& event) {
a077dceb sago007 2016-03-19 19:19 72
	if (event.type != SDL_CONTROLLERAXISMOTION) {
a077dceb sago007 2016-03-19 19:19 73
		return;  //assert?
a077dceb sago007 2016-03-19 19:19 74
	}
a077dceb sago007 2016-03-19 19:19 75
	int value = event.caxis.value;
a077dceb sago007 2016-03-19 19:19 76
	if (value > -deadZoneLimit && value < deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 77
		controllerStatusMap[event.caxis.which].AxisInDeadZone[event.caxis.axis] = true;
a077dceb sago007 2016-03-19 19:19 78
	}
a077dceb sago007 2016-03-19 19:19 79
}
a077dceb sago007 2016-03-19 19:19 80
a077dceb sago007 2016-03-19 19:19 81
bool getDeadZone(SDL_JoystickID id, int axis) {
a077dceb sago007 2016-03-19 19:19 82
	return controllerStatusMap[id].AxisInDeadZone[axis];
a077dceb sago007 2016-03-19 19:19 83
}
a077dceb sago007 2016-03-19 19:19 84
a077dceb sago007 2016-03-19 19:19 85
void setDeadZone(SDL_JoystickID id, int axis, bool value) {
a077dceb sago007 2016-03-19 19:19 86
	controllerStatusMap[id].AxisInDeadZone[axis] = value;
a077dceb sago007 2016-03-19 19:19 87
}
a077dceb sago007 2016-03-19 19:19 88
ce5aa7d3 sago007 2016-03-13 11:44 89
bool isPlayerDownEvent(int playerNumber, const SDL_Event& event) {
ce5aa7d3 sago007 2016-03-13 11:44 90
	if (playerNumber != 1) {
ce5aa7d3 sago007 2016-03-13 11:44 91
		return false;
ce5aa7d3 sago007 2016-03-13 11:44 92
	}
ce5aa7d3 sago007 2016-03-13 11:44 93
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
ce5aa7d3 sago007 2016-03-13 11:44 94
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN ) {
ce5aa7d3 sago007 2016-03-13 11:44 95
			return true;
ce5aa7d3 sago007 2016-03-13 11:44 96
		}
ce5aa7d3 sago007 2016-03-13 11:44 97
	}
a077dceb sago007 2016-03-19 19:19 98
	if (event.type == SDL_CONTROLLERAXISMOTION  && event.caxis.axis == SDL_CONTROLLER_AXIS_LEFTY ) {
a077dceb sago007 2016-03-19 19:19 99
		checkDeadZone(event);
a077dceb sago007 2016-03-19 19:19 100
		const SDL_ControllerAxisEvent &a = event.caxis;
a077dceb sago007 2016-03-19 19:19 101
		if (getDeadZone(a.which, a.axis)) {
a077dceb sago007 2016-03-19 19:19 102
			if (event.caxis.value > deadZoneLimit) {
a077dceb sago007 2016-03-19 19:19 103
				setDeadZone(a.which,a.axis,false);
a077dceb sago007 2016-03-19 19:19 104
				return true;
a077dceb sago007 2016-03-19 19:19 105
			}
a077dceb sago007 2016-03-19 19:19 106
		}
a077dceb sago007 2016-03-19 19:19 107
	}
ce5aa7d3 sago007 2016-03-13 11:44 108
	return false;
ce5aa7d3 sago007 2016-03-13 11:44 109
}
ce5aa7d3 sago007 2016-03-13 11:44 110
ce5aa7d3 sago007 2016-03-13 11:44 111
bool isPlayerUpEvent(int playerNumber, const SDL_Event& event) {
ce5aa7d3 sago007 2016-03-13 11:44 112
	if (playerNumber != 1) {
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_UP ) {
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 ) {
a077dceb sago007 2016-03-19 19:19 121
		checkDeadZone(event);
a077dceb sago007 2016-03-19 19:19 122
		const SDL_ControllerAxisEvent &a = event.caxis;
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 isPlayerLeftEvent(int playerNumber, const SDL_Event& event) {
ce5aa7d3 sago007 2016-03-13 11:44 134
	if (playerNumber != 1) {
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_LEFT ) {
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_LEFTX ) {
a077dceb sago007 2016-03-19 19:19 143
		checkDeadZone(event);
a077dceb sago007 2016-03-19 19:19 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 isPlayerRightEvent(int playerNumber, const SDL_Event& event) {
ce5aa7d3 sago007 2016-03-13 11:44 156
	if (playerNumber != 1) {
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_RIGHT ) {
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);
a077dceb sago007 2016-03-19 19:19 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 isPlayerSwitchEvent(int playerNumber, const SDL_Event& event) {
ce5aa7d3 sago007 2016-03-13 11:44 178
	if (playerNumber != 1) {
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_A || event.cbutton.button == SDL_CONTROLLER_BUTTON_B ) {
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
	}
ce5aa7d3 sago007 2016-03-13 11:44 186
	return false;
ce5aa7d3 sago007 2016-03-13 11:44 187
}
ce5aa7d3 sago007 2016-03-13 11:44 188
ce5aa7d3 sago007 2016-03-13 11:44 189
bool isPlayerPushEvent(int playerNumber, const SDL_Event& event) {
ce5aa7d3 sago007 2016-03-13 11:44 190
	if (playerNumber != 1) {
ce5aa7d3 sago007 2016-03-13 11:44 191
		return false;
ce5aa7d3 sago007 2016-03-13 11:44 192
	}
ce5aa7d3 sago007 2016-03-13 11:44 193
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
ce5aa7d3 sago007 2016-03-13 11:44 194
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER || event.cbutton.button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ) {
ce5aa7d3 sago007 2016-03-13 11:44 195
			return true;
ce5aa7d3 sago007 2016-03-13 11:44 196
		}
ce5aa7d3 sago007 2016-03-13 11:44 197
	}
ce5aa7d3 sago007 2016-03-13 11:44 198
	return false;
ce5aa7d3 sago007 2016-03-13 11:44 199
}