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>
826cf176 sago007 2016-03-12 10:53 28
2cb68251 sago007 2016-03-14 18:01 29
static bool verbose = false;
2cb68251 sago007 2016-03-14 18:01 30
2cb68251 sago007 2016-03-14 18:01 31
void GameControllerSetVerbose(bool value) {
2cb68251 sago007 2016-03-14 18:01 32
	verbose = value;
2cb68251 sago007 2016-03-14 18:01 33
}
2cb68251 sago007 2016-03-14 18:01 34
2cb68251 sago007 2016-03-14 18:01 35
static const char* GameControllerGetName(SDL_GameController *gamecontroller) {
2cb68251 sago007 2016-03-14 18:01 36
	const char* result = SDL_GameControllerName(gamecontroller);
2cb68251 sago007 2016-03-14 18:01 37
	if (!result) {
2cb68251 sago007 2016-03-14 18:01 38
		result = "Unnamed";
2cb68251 sago007 2016-03-14 18:01 39
	}
2cb68251 sago007 2016-03-14 18:01 40
	return result;
2cb68251 sago007 2016-03-14 18:01 41
}
2cb68251 sago007 2016-03-14 18:01 42
826cf176 sago007 2016-03-12 10:53 43
void InitGameControllers() {
2cb68251 sago007 2016-03-14 18:01 44
	std::string configFile = sago::getConfigHome()+"/blockattack/gamecontrollerdb.txt";
2cb68251 sago007 2016-03-14 18:01 45
	int errorCode = SDL_GameControllerAddMappingsFromFile(configFile.c_str());
2cb68251 sago007 2016-03-14 18:01 46
	if (errorCode == -1 && verbose) {
2cb68251 sago007 2016-03-14 18:01 47
		std::cerr << "Could not load mapping file: " << configFile << std::endl;
2cb68251 sago007 2016-03-14 18:01 48
	}
2cb68251 sago007 2016-03-14 18:01 49
	if (verbose) {
2cb68251 sago007 2016-03-14 18:01 50
		std::cout << "Number of Game controllers: " << SDL_NumJoysticks() << std::endl;
2cb68251 sago007 2016-03-14 18:01 51
	}
c7f2082f sago007 2016-03-13 11:48 52
	SDL_GameController* controller = nullptr;
826cf176 sago007 2016-03-12 10:53 53
	for (int i = 0; i < SDL_NumJoysticks(); ++i) {
826cf176 sago007 2016-03-12 10:53 54
		if (SDL_IsGameController(i)) {
826cf176 sago007 2016-03-12 10:53 55
			controller = SDL_GameControllerOpen(i);
2cb68251 sago007 2016-03-14 18:01 56
			if (verbose) {
2cb68251 sago007 2016-03-14 18:01 57
				std::cout << "Supported game controller detected: " << GameControllerGetName(controller) << ", mapping: " << SDL_GameControllerMapping(controller) <<  std::endl;
2cb68251 sago007 2016-03-14 18:01 58
			}
826cf176 sago007 2016-03-12 10:53 59
		}
826cf176 sago007 2016-03-12 10:53 60
	}
826cf176 sago007 2016-03-12 10:53 61
}
ce5aa7d3 sago007 2016-03-13 11:44 62
ce5aa7d3 sago007 2016-03-13 11:44 63
bool isPlayerDownEvent(int playerNumber, const SDL_Event& event) {
ce5aa7d3 sago007 2016-03-13 11:44 64
	if (playerNumber != 1) {
ce5aa7d3 sago007 2016-03-13 11:44 65
		return false;
ce5aa7d3 sago007 2016-03-13 11:44 66
	}
ce5aa7d3 sago007 2016-03-13 11:44 67
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
ce5aa7d3 sago007 2016-03-13 11:44 68
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN ) {
ce5aa7d3 sago007 2016-03-13 11:44 69
			return true;
ce5aa7d3 sago007 2016-03-13 11:44 70
		}
ce5aa7d3 sago007 2016-03-13 11:44 71
	}
ce5aa7d3 sago007 2016-03-13 11:44 72
	return false;
ce5aa7d3 sago007 2016-03-13 11:44 73
}
ce5aa7d3 sago007 2016-03-13 11:44 74
ce5aa7d3 sago007 2016-03-13 11:44 75
bool isPlayerUpEvent(int playerNumber, const SDL_Event& event) {
ce5aa7d3 sago007 2016-03-13 11:44 76
	if (playerNumber != 1) {
ce5aa7d3 sago007 2016-03-13 11:44 77
		return false;
ce5aa7d3 sago007 2016-03-13 11:44 78
	}
ce5aa7d3 sago007 2016-03-13 11:44 79
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
ce5aa7d3 sago007 2016-03-13 11:44 80
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP ) {
ce5aa7d3 sago007 2016-03-13 11:44 81
			return true;
ce5aa7d3 sago007 2016-03-13 11:44 82
		}
ce5aa7d3 sago007 2016-03-13 11:44 83
	}
ce5aa7d3 sago007 2016-03-13 11:44 84
	return false;
ce5aa7d3 sago007 2016-03-13 11:44 85
}
ce5aa7d3 sago007 2016-03-13 11:44 86
ce5aa7d3 sago007 2016-03-13 11:44 87
bool isPlayerLeftEvent(int playerNumber, const SDL_Event& event) {
ce5aa7d3 sago007 2016-03-13 11:44 88
	if (playerNumber != 1) {
ce5aa7d3 sago007 2016-03-13 11:44 89
		return false;
ce5aa7d3 sago007 2016-03-13 11:44 90
	}
ce5aa7d3 sago007 2016-03-13 11:44 91
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
ce5aa7d3 sago007 2016-03-13 11:44 92
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_LEFT ) {
ce5aa7d3 sago007 2016-03-13 11:44 93
			return true;
ce5aa7d3 sago007 2016-03-13 11:44 94
		}
ce5aa7d3 sago007 2016-03-13 11:44 95
	}
ce5aa7d3 sago007 2016-03-13 11:44 96
	return false;
ce5aa7d3 sago007 2016-03-13 11:44 97
}
ce5aa7d3 sago007 2016-03-13 11:44 98
ce5aa7d3 sago007 2016-03-13 11:44 99
bool isPlayerRightEvent(int playerNumber, const SDL_Event& event) {
ce5aa7d3 sago007 2016-03-13 11:44 100
	if (playerNumber != 1) {
ce5aa7d3 sago007 2016-03-13 11:44 101
		return false;
ce5aa7d3 sago007 2016-03-13 11:44 102
	}
ce5aa7d3 sago007 2016-03-13 11:44 103
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
ce5aa7d3 sago007 2016-03-13 11:44 104
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_RIGHT ) {
ce5aa7d3 sago007 2016-03-13 11:44 105
			return true;
ce5aa7d3 sago007 2016-03-13 11:44 106
		}
ce5aa7d3 sago007 2016-03-13 11:44 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 isPlayerSwitchEvent(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_A || event.cbutton.button == SDL_CONTROLLER_BUTTON_B ) {
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
	}
ce5aa7d3 sago007 2016-03-13 11:44 120
	return false;
ce5aa7d3 sago007 2016-03-13 11:44 121
}
ce5aa7d3 sago007 2016-03-13 11:44 122
ce5aa7d3 sago007 2016-03-13 11:44 123
bool isPlayerPushEvent(int playerNumber, const SDL_Event& event) {
ce5aa7d3 sago007 2016-03-13 11:44 124
	if (playerNumber != 1) {
ce5aa7d3 sago007 2016-03-13 11:44 125
		return false;
ce5aa7d3 sago007 2016-03-13 11:44 126
	}
ce5aa7d3 sago007 2016-03-13 11:44 127
	if (event.type == SDL_CONTROLLERBUTTONDOWN) {
ce5aa7d3 sago007 2016-03-13 11:44 128
		if (event.cbutton.button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER || event.cbutton.button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER ) {
ce5aa7d3 sago007 2016-03-13 11:44 129
			return true;
ce5aa7d3 sago007 2016-03-13 11:44 130
		}
ce5aa7d3 sago007 2016-03-13 11:44 131
	}
ce5aa7d3 sago007 2016-03-13 11:44 132
	return false;
ce5aa7d3 sago007 2016-03-13 11:44 133
}