Commit 29198a36 authored by Benjamin 'Albsi' Albsmeier's avatar Benjamin 'Albsi' Albsmeier
Browse files

input now with the stuff for a controller cursor, but without the bug

parent 12789100
......@@ -17,11 +17,18 @@
#define __INPUT_HPP__
namespace gdw{
struct axisStruct{
bool changed = false;
int down = 0;
bool released = false;
};
struct controller {
SDL_GameController *sdlController;
int controllerNr = -1;
std::unordered_map<int, bool> controllerMap;
std::unordered_map<int, bool> controllerReleasedMap;
std::unordered_map<int, axisStruct> axisMap;
bool active = false;
};
......@@ -46,12 +53,19 @@ namespace gdw{
SDL_GameControllerAxis caxisCode = SDL_CONTROLLER_AXIS_INVALID;
};
enum class controllerCursor {
none,
left,
right
};
class engine;
class input{
private:
std::string controllerDBPath_ = "input/gamecontrollerdb.txt";
static float constexpr controllerAxisMax_ = 32767.f;
static float constexpr controllerAxisDeadZone_ = .3f;
static float constexpr controllerSpeed_ = 800.f;
/**inputMappingStruct.type for key*/
static unsigned int constexpr key1 = 0x01;
......@@ -74,6 +88,9 @@ namespace gdw{
* 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);
......@@ -175,11 +192,24 @@ namespace gdw{
SDL_GameControllerButton getCButtonFromName(std::string name);
SDL_GameControllerAxis getCAxisFromName(std::string name);
int firstController();
std::unordered_map<int, bool> usedControllers_;
controller controller_;
engine &engine_;
glm::vec2 mousePos_;
glm::vec2 oldMousePos_;
glm::vec2 mouseWheel_;
glm::vec2 pos_;
int windowWidth_ = 800;
int windowHeight_ = 600;
bool controllerUse = false;
controllerCursor getCCursorFromName(std::string name);
controllerCursor ccursor = controllerCursor::none;
const Uint8 *keyState_;
......@@ -190,6 +220,7 @@ namespace gdw{
std::unordered_map<int, int>::iterator mouseIt_;
std::unordered_map<int, int>::iterator keyIt_;
std::unordered_map<int, bool>::iterator controllerIt_;
std::unordered_map<int, axisStruct>::iterator caxisIt_;
//KEYBOARD
void keyDown(const SDL_KeyboardEvent &e);
......
......@@ -10,6 +10,15 @@ namespace gdw{
keyState_ = SDL_GetKeyboardState(NULL);
controllerDB_ = engine.asset_manager().load(engine.asset_manager().native_name(controllerDBPath_));
makeMappings();
ccursor = getCCursorFromName(engine_.getConfig().get<std::string>("controller_cursor","RIGHT"));
controllerUse = engine_.getConfig().get<bool>("use_controller",false);
windowWidth_ = engine_.getConfig().get<int>("width",800);
windowHeight_ = engine_.getConfig().get<int>("height",600);
//SDL_SetCursor(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR));
if(controllerUse)SDL_ShowCursor(SDL_FALSE);
}
/** if you add here, add also in enum inputMapping in input.hpp
......@@ -176,11 +185,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());
......@@ -188,13 +197,62 @@ 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){
for(int i=0;i<controllers_.size();++i){
for(int j=SDL_CONTROLLER_AXIS_INVALID+1;j<SDL_CONTROLLER_AXIS_MAX;++j){
float f = SDL_GameControllerGetAxis(controllers_[i].sdlController, SDL_GameControllerAxis(j)) / controllerAxisMax_;
if(!(f < controllerAxisDeadZone_ && f > -controllerAxisDeadZone_)){
if(controllers_[i].axisMap.find(j) == controllers_[j].axisMap.end()){
axisStruct as;
as.down = 1;
as.changed = true;
controllers_[i].axisMap.emplace(j, as);
}else{
++controllers_[i].axisMap[j].down;
controllers_[i].axisMap[j].changed = true;
}
}
}
}
if(ccursor != controllerCursor::none){
pos_.x += mousePos_.x - oldMousePos_.x;
pos_.y += mousePos_.y - oldMousePos_.y;
if(controllerUse){
float speed = controllerSpeed_ * delta;
if(ccursor == controllerCursor::left){
pos_.x += controllerAxis(SDL_CONTROLLER_AXIS_LEFTX) * speed;
pos_.y += controllerAxis(SDL_CONTROLLER_AXIS_LEFTY) * speed;;
}else if(ccursor == controllerCursor::right){
pos_.x += controllerAxis(SDL_CONTROLLER_AXIS_RIGHTX) * speed;;
pos_.y += controllerAxis(SDL_CONTROLLER_AXIS_RIGHTY) * speed;;
}
}
pos_.x = pos_.x <= windowWidth_ ? pos_.x : windowWidth_;
pos_.x = pos_.x >= 0.f ? pos_.x : 0.f;
pos_.y = pos_.y <= windowHeight_ ? pos_.y : windowHeight_;
pos_.y = pos_.y >= 0.f ? pos_.y : 0.f;
}
}
int input::firstController(){
if(usedControllers_.size() < 1)return -1;
for (controllerIt_ = usedControllers_.begin(); controllerIt_ != usedControllers_.end(); controllerIt_++){
if(controllerIt_->second == true){
return controllerIt_->first;
}
}
return -1;
}
//RESET
......@@ -208,6 +266,15 @@ namespace gdw{
for(int i=0;i<controllers_.size();++i){
controllers_[i].controllerReleasedMap.clear();
controllers_[i].controllerMap.clear();
for (caxisIt_ = controllers_[i].axisMap.begin(); caxisIt_ != controllers_[i].axisMap.end(); caxisIt_++){
if(!caxisIt_->second.changed && caxisIt_->second.down > 0){
caxisIt_->second.released = true;
caxisIt_->second.down = 0;
}else{
if(caxisIt_->second.released)caxisIt_->second.released = false;
}
caxisIt_->second.changed = false;
}
}
for (mouseIt_ = mouseMap_.begin(); mouseIt_ != mouseMap_.end(); mouseIt_++){
......@@ -221,6 +288,8 @@ namespace gdw{
++keyIt_->second;
}
}
oldMousePos_.x = mousePos_.x;
oldMousePos_.y = mousePos_.y;
}
//MAPPING
......@@ -282,6 +351,15 @@ namespace gdw{
return SDL_GameControllerGetAxisFromString(name.c_str());
}
controllerCursor input::getCCursorFromName(std::string name){
if(name == "LEFT" || name == "left" || name == "Left"){
return controllerCursor::left;
}else if(name == "RIGHT" || name == "right" || name == "Right"){
return controllerCursor::right;
}
return controllerCursor::none;
}
//RETURN
bool input::isPressed(inputMapping id){
if((mappings_[id].type & key1) != 0){
......@@ -304,11 +382,13 @@ namespace gdw{
return true;
}
}
/*if((mappings_[id].type & caxis) != 0){
if(controllerAxis(mappings_[id].caxisCode)){
return true;
if(controllers_.size() > 0){
if((mappings_[id].type & caxis) != 0){
if(controllers_[firstController()].axisMap[mappings_[id].caxisCode].down == 1){
return true;
}
}
}*/
}
return false;
}
......@@ -368,11 +448,13 @@ namespace gdw{
return true;
}
}
/*if((mappings_[id].type & caxis) != 0){
if(controllerAxis(mappings_[id].caxisCode)){
return true;
if(controllers_.size() > 0){
if((mappings_[id].type & caxis) != 0){
if(controllers_[firstController()].axisMap[mappings_[id].caxisCode].released){
return true;
}
}
}*/
}
return false;
}
......@@ -392,6 +474,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);
......@@ -401,6 +485,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];
......@@ -410,6 +496,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];
......@@ -419,6 +507,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_;
......
Markdown is supported
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