Commit e78eeb05 authored by Benjamin 'Albsi' Albsmeier's avatar Benjamin 'Albsi' Albsmeier
Browse files

now controllers can reconnect a will work (ok only one of the - no multi controller support)

parent 3ceea4db
......@@ -72,6 +72,7 @@ namespace gdw{
/**resets some data
* should called once per game loop, at the end*/
void reset();
void calcPosition(float delta);
/**Returns true every frame, as long as it is down - NOT FOR MULTI CONTROLLER*/
bool isPressed(inputMapping id);
......@@ -129,27 +130,27 @@ namespace gdw{
/**Returns true every frame, as long as the controller button is down.*/
private:
bool isControllerButtonDown(SDL_GameControllerButton button, int nr = 0);
bool isControllerButtonDown(SDL_GameControllerButton button, int nr = -1);
public:
bool isControllerButtonDown(inputMapping button, int nr = 0) noexcept {return isControllerButtonDown(getCButtonCodeByMapping(button), nr);};
bool isControllerButtonDown(inputMapping button, int nr = -1) noexcept {return isControllerButtonDown(getCButtonCodeByMapping(button), nr);};
/**Returns true once the controller button is pressed*/
private:
bool isControllerButtonPressed(SDL_GameControllerButton button, int nr = 0);
bool isControllerButtonPressed(SDL_GameControllerButton button, int nr = -1);
public:
bool isControllerButtonPressed(inputMapping button, int nr = 0) noexcept {return isControllerButtonPressed(getCButtonCodeByMapping(button), nr);};
bool isControllerButtonPressed(inputMapping button, int nr = -1) noexcept {return isControllerButtonPressed(getCButtonCodeByMapping(button), nr);};
/**Returns true once the controller button is released*/
private:
bool isControllerButtonReleased(SDL_GameControllerButton button, int nr = 0);
bool isControllerButtonReleased(SDL_GameControllerButton button, int nr = -1);
public:
bool isControllerButtonReleased(inputMapping button, int nr = 0) noexcept {return isControllerButtonReleased(getCButtonCodeByMapping(button), nr);};
bool isControllerButtonReleased(inputMapping button, int nr = -1) noexcept {return isControllerButtonReleased(getCButtonCodeByMapping(button), nr);};
/**Returns the value of that axis as a float between -1 and 1*/
private:
float controllerAxis(SDL_GameControllerAxis axis, int nr = 0);
float controllerAxis(SDL_GameControllerAxis axis, int nr = -1);
public:
float controllerAxis(inputMapping axis, int nr = 0) noexcept {return controllerAxis(getCAxisCodeByMapping(axis), nr);};
float controllerAxis(inputMapping axis, int nr = -1) noexcept {return controllerAxis(getCAxisCodeByMapping(axis), nr);};
/**Returns number of aktiv controllers*/
int controllerCount() noexcept {return controllers_.size();};
......@@ -174,6 +175,10 @@ namespace gdw{
SDL_GameControllerButton getCButtonFromName(std::string name);
SDL_GameControllerAxis getCAxisFromName(std::string name);
int firstController();
//controller findController(int sdlId);
std::unordered_map<int, bool> usedControllers_;
controller controller_;
engine &engine_;
......
......@@ -56,13 +56,13 @@ namespace gdw {
void engine::update(float delta_time) {
physics_system_->update(1.f/60.f); //Update fix 60 Hz
game_state_machine_->update(delta_time);
graphics_system_->begin();
rendering_system_->update(delta_time);
ui_system_->render();
graphics_system_->end(delta_time);
input_->calcPosition(delta_time);
}
void engine::run() {
......
......@@ -6,7 +6,7 @@
#include <util/config.hpp>
namespace gdw{
input::input(engine& engine):engine_(engine),mousePos_(0,0),mouseWheel_(0,0),controllers_(),mappings_(){
input::input(engine& engine):engine_(engine),mousePos_(0,0),mouseWheel_(0,0),controllers_(),mappings_(),usedControllers_(){
keyState_ = SDL_GetKeyboardState(NULL);
controllerDB_ = engine.asset_manager().load(engine.asset_manager().native_name(controllerDBPath_));
makeMappings();
......@@ -170,11 +170,11 @@ namespace gdw{
}
void input::controllerAdded(const SDL_ControllerDeviceEvent &e){
log << "[input] controllerAdded was triggerd" << std::endl;
if(SDL_IsGameController(e.which)){
controller c;
c.controllerNr = e.which;
c.sdlController = SDL_GameControllerOpen(e.which);
c.controllerNr = SDL_JoystickInstanceID(SDL_GameControllerGetJoystick(c.sdlController));
usedControllers_.emplace(c.controllerNr, true);
c.active = true;
if(controllerDB_){
std::vector<char> controllerDBVec(controllerDB_->content());
......@@ -182,13 +182,28 @@ namespace gdw{
log << "[input] can't load controllerDB" << std::endl << SDL_GetError() << std::endl;
}
}
controllers_.emplace(e.which, c);
controllers_.emplace(c.controllerNr, c);
}
}
void input::controllerRemoved(const SDL_ControllerDeviceEvent &e){
log << "[input] controllerRemove was triggerd but is not handelt" << std::endl;
usedControllers_.emplace(e.which, false);
SDL_GameControllerClose(controllers_[e.which].sdlController);
controllers_.erase(controllers_.find(e.which));
}
void input::calcPosition(float delta){
}
int input::firstController(){
if(usedControllers_.size() == 0)return -1;
for (controllerIt_ = usedControllers_.begin(); controllerIt_ != usedControllers_.end(); controllerIt_++){
if(controllerIt_->second == true){
return controllerIt_->first;
}
}
return -1;
}
//RESET
......@@ -386,6 +401,8 @@ namespace gdw{
}
bool input::isControllerButtonDown(SDL_GameControllerButton button, int nr){
if(nr == -1)nr = firstController();
if(nr == -1)return false;
if(controllers_.size()>0){
if(controllers_[nr].active){
return SDL_GameControllerGetButton(controllers_[nr].sdlController, button);
......@@ -395,6 +412,8 @@ namespace gdw{
}
bool input::isControllerButtonPressed(SDL_GameControllerButton button, int nr){
if(nr == -1)nr = firstController();
if(nr == -1)return false;
if(controllers_.size()>0){
if(controllers_[nr].active){
return controllers_[nr].controllerMap[button];
......@@ -404,6 +423,8 @@ namespace gdw{
}
bool input::isControllerButtonReleased(SDL_GameControllerButton button, int nr){
if(nr == -1)nr = firstController();
if(nr == -1)return false;
if(controllers_.size()>0){
if(controllers_[nr].active){
return controllers_[nr].controllerReleasedMap[button];
......@@ -413,6 +434,8 @@ namespace gdw{
}
float input::controllerAxis(SDL_GameControllerAxis axis, int nr){
if(nr == -1)nr = firstController();
if(nr == -1)return 0.f;
if(controllers_.size() > 0){
if(controllers_[nr].active){
float f = SDL_GameControllerGetAxis(controllers_[nr].sdlController, axis) / controllerAxisMax_;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment