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

small changes in input

parent 9f7a5e89
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.cpp for GDW SS15
//****************************************
#ifndef __CONTROLLER_HPP__ #ifndef __CONTROLLER_HPP__
#define __CONTROLLER_HPP__ #define __CONTROLLER_HPP__
......
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.cpp for GDW SS15
//****************************************
#ifndef __CONTROLLER_MANAGER_HPP__ #ifndef __CONTROLLER_MANAGER_HPP__
#define __CONTROLLER_MANAGER_HPP__ #define __CONTROLLER_MANAGER_HPP__
......
//**************************************** //****************************************
// by Benjamin 'Albsi' Albsmeier // by Benjamin 'Albsi' Albsmeier
// input/input.hpp for GDW SS15 // input/input.hpp for GDW SS15
// mouse, keyboard input
//**************************************** //****************************************
#ifndef __INPUT_HPP__ #ifndef __INPUT_HPP__
#define __INPUT_HPP__ #define __INPUT_HPP__
...@@ -33,9 +32,18 @@ namespace gdw{ ...@@ -33,9 +32,18 @@ namespace gdw{
engine &engine_; engine &engine_;
glm::vec2 pos_; glm::vec2 pos_;
glm::vec2 last_pos_;
glm::vec2 relative_pos_;
int window_width_ = 800; int window_width_ = 800;
int window_height_ = 600; int window_height_ = 600;
std::shared_ptr<gdw::keyboard> keyboard() noexcept {return keyboard_;};
std::shared_ptr<gdw::controller> controller(int id = -1);
std::shared_ptr<gdw::controller_manager> controller_manager() noexcept {return controller_manager_;};
std::shared_ptr<gdw::mouse> mouse() noexcept {return mouse_;};
std::shared_ptr<gdw::input_events> events() noexcept {return events_;};
std::shared_ptr<gdw::input_mapping> mapping() noexcept {return mapping_;};
public: public:
input(engine& engine); input(engine& engine);
~input(); ~input();
...@@ -102,18 +110,17 @@ namespace gdw{ ...@@ -102,18 +110,17 @@ namespace gdw{
/**returns y position of the mouse cursor or of the controller cross-hair*/ /**returns y position of the mouse cursor or of the controller cross-hair*/
int position_y(); int position_y();
/**returns the relative x position of the mouse cursor or of the controller cross-hair*/
int relative_position_x();
/**returns the relative y position of the mouse cursor or of the controller cross-hair*/
int relative_position_y();
/**x motion of the mouse wheel*/ /**x motion of the mouse wheel*/
int mouse_wheel_x(); int mouse_wheel_x();
/**y motion of the mouse wheel*/ /**y motion of the mouse wheel*/
int mouse_wheel_y(); int mouse_wheel_y();
std::shared_ptr<gdw::keyboard> keyboard() noexcept {return keyboard_;};
std::shared_ptr<gdw::controller> controller(int id = -1);
std::shared_ptr<gdw::controller_manager> controller_manager() noexcept {return controller_manager_;};
std::shared_ptr<gdw::mouse> mouse() noexcept {return mouse_;};
std::shared_ptr<gdw::input_events> events() noexcept {return events_;};
std::shared_ptr<gdw::input_mapping> mapping() noexcept {return mapping_;};
}; };
} }
......
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.cpp for GDW SS15
//****************************************
#ifndef __INPUT_EVENTS_HPP__ #ifndef __INPUT_EVENTS_HPP__
#define __INPUT_EVENTS_HPP__ #define __INPUT_EVENTS_HPP__
...@@ -13,8 +17,14 @@ namespace gdw{ ...@@ -13,8 +17,14 @@ namespace gdw{
class input_events{ class input_events{
private: private:
input *input_; input *input_;
std::shared_ptr<gdw::keyboard> keyboard_;
std::shared_ptr<gdw::mouse> mouse_;
std::shared_ptr<gdw::controller_manager> controller_manager_;
public: public:
input_events(input *input); input_events(input *input,
std::shared_ptr<gdw::keyboard> keyboard,
std::shared_ptr<gdw::mouse> mouse,
std::shared_ptr<gdw::controller_manager> controller_manager);
~input_events(); ~input_events();
void update(const SDL_Event &e); void update(const SDL_Event &e);
......
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.cpp for GDW SS15
//****************************************
#ifndef __INPUT_MAPPING_HPP__ #ifndef __INPUT_MAPPING_HPP__
#define __INPUT_MAPPING_HPP__ #define __INPUT_MAPPING_HPP__
#include <map> #include <unordered_map>
#include <SDL.h> #include <SDL.h>
#include <input/input.hpp> #include <input/input.hpp>
#include <input/keyboard.hpp> #include <input/keyboard.hpp>
...@@ -29,7 +33,7 @@ namespace gdw{ ...@@ -29,7 +33,7 @@ namespace gdw{
static unsigned int constexpr mouse_button_ = 0x04; static unsigned int constexpr mouse_button_ = 0x04;
static unsigned int constexpr controller_button_ = 0x08; static unsigned int constexpr controller_button_ = 0x08;
static unsigned int constexpr controller_axis_ = 0x10; static unsigned int constexpr controller_axis_ = 0x10;
std::map<std::string, gdw::mapping> mappings_; std::unordered_map<std::string, gdw::mapping> mappings_;
public: public:
input_mapping(input *input, engine& engine); input_mapping(input *input, engine& engine);
~input_mapping(); ~input_mapping();
......
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.cpp for GDW SS15
//****************************************
#ifndef __INPUT_UTIL_HPP__ #ifndef __INPUT_UTIL_HPP__
#define __INPUT_UTIL_HPP__ #define __INPUT_UTIL_HPP__
......
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.cpp for GDW SS15
//****************************************
#ifndef __KEYBOARD_HPP__ #ifndef __KEYBOARD_HPP__
#define __KEYBOARD_HPP__ #define __KEYBOARD_HPP__
......
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.cpp for GDW SS15
//****************************************
#ifndef __MOUSE_HPP__ #ifndef __MOUSE_HPP__
#define __MOUSE_HPP__ #define __MOUSE_HPP__
......
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.cpp for GDW SS15
//****************************************
#include <input/controller.hpp> #include <input/controller.hpp>
namespace gdw{ namespace gdw{
......
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.cpp for GDW SS15
//****************************************
#include <input/controller_manager.hpp> #include <input/controller_manager.hpp>
namespace gdw{ namespace gdw{
......
...@@ -14,13 +14,15 @@ ...@@ -14,13 +14,15 @@
namespace gdw{ namespace gdw{
input::input(engine& engine):engine_(engine), input::input(engine& engine):engine_(engine),
pos_(0,0){ pos_(0,0),
last_pos_(0,0),
relative_pos_(0,0){
mapping_ = std::make_shared<gdw::input_mapping>(this, engine_);
keyboard_ = std::make_shared<gdw::keyboard>(this); keyboard_ = std::make_shared<gdw::keyboard>(this);
mouse_ = std::make_shared<gdw::mouse>(this); mouse_ = std::make_shared<gdw::mouse>(this);
events_ = std::make_shared<gdw::input_events>(this);
mapping_ = std::make_shared<gdw::input_mapping>(this, engine_);
controller_manager_ = std::make_shared<gdw::controller_manager>(this, engine_); controller_manager_ = std::make_shared<gdw::controller_manager>(this, engine_);
events_ = std::make_shared<gdw::input_events>(this, keyboard_, mouse_, controller_manager_);//must be last
window_width_ = engine_.getConfig().get<int>("width",800); window_width_ = engine_.getConfig().get<int>("width",800);
window_height_ = engine_.getConfig().get<int>("height",600); window_height_ = engine_.getConfig().get<int>("height",600);
...@@ -35,6 +37,10 @@ namespace gdw{ ...@@ -35,6 +37,10 @@ namespace gdw{
void input::update(float delta){ void input::update(float delta){
pos_.x = mouse_->position_x(); pos_.x = mouse_->position_x();
pos_.y = mouse_->position_y(); pos_.y = mouse_->position_y();
last_pos_.x = mouse_->last_position_x();
last_pos_.y = mouse_->last_position_y();
relative_pos_.x = last_pos_.x - pos_.x;
relative_pos_.y = last_pos_.y - pos_.y;
controller_manager_->update(delta); controller_manager_->update(delta);
} }
...@@ -122,6 +128,14 @@ namespace gdw{ ...@@ -122,6 +128,14 @@ namespace gdw{
return pos_.y; return pos_.y;
} }
int input::relative_position_x(){
return relative_pos_.x;
}
int input::relative_position_y(){
return relative_pos_.y;
}
int input::mouse_wheel_x(){ int input::mouse_wheel_x(){
return mouse_->wheel_x(); return mouse_->wheel_x();
} }
......
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.cpp for GDW SS15
//****************************************
#include <input/input_events.hpp> #include <input/input_events.hpp>
namespace gdw{ namespace gdw{
input_events::input_events(input *input):input_(input){} input_events::input_events(input *input,
std::shared_ptr<gdw::keyboard> keyboard,
std::shared_ptr<gdw::mouse> mouse,
std::shared_ptr<gdw::controller_manager> controller_manager):
input_(input),
keyboard_(keyboard),
mouse_(mouse),
controller_manager_(controller_manager){}
input_events::~input_events(){} input_events::~input_events(){}
void input_events::update(const SDL_Event &e){ void input_events::update(const SDL_Event &e){
switch(e.type){ switch(e.type){
//KEYBOARD //KEYBOARD
case SDL_KEYDOWN: input_->keyboard()->down_event(e.key); break; case SDL_KEYDOWN: keyboard_->down_event(e.key); break;
case SDL_KEYUP: input_->keyboard()->up_event(e.key); break; case SDL_KEYUP: keyboard_->up_event(e.key); break;
//MOUSE //MOUSE
case SDL_MOUSEBUTTONDOWN: input_->mouse()->down_event(e.button); break; case SDL_MOUSEBUTTONDOWN: mouse_->down_event(e.button); break;
case SDL_MOUSEBUTTONUP: input_->mouse()->up_event(e.button); break; case SDL_MOUSEBUTTONUP: mouse_->up_event(e.button); break;
case SDL_MOUSEMOTION: input_->mouse()->motion(e.motion); break; case SDL_MOUSEMOTION: mouse_->motion(e.motion); break;
case SDL_MOUSEWHEEL: input_->mouse()->wheel(e.wheel); break; case SDL_MOUSEWHEEL: mouse_->wheel(e.wheel); break;
//CONTROLLER //CONTROLLER
case SDL_CONTROLLERBUTTONDOWN: input_->controller_manager()->down_event(e.cbutton); break; case SDL_CONTROLLERBUTTONDOWN: controller_manager_->down_event(e.cbutton); break;
case SDL_CONTROLLERBUTTONUP: input_->controller_manager()->up_event(e.cbutton); break; case SDL_CONTROLLERBUTTONUP: controller_manager_->up_event(e.cbutton); break;
case SDL_CONTROLLERDEVICEADDED: input_->controller_manager()->added(e.cdevice); break; case SDL_CONTROLLERDEVICEADDED: controller_manager_->added(e.cdevice); break;
case SDL_CONTROLLERDEVICEREMOVED: input_->controller_manager()->removed(e.cdevice); break; case SDL_CONTROLLERDEVICEREMOVED: controller_manager_->removed(e.cdevice); break;
case SDL_CONTROLLERDEVICEREMAPPED: input_->controller_manager()->remapped(e.cdevice); break; case SDL_CONTROLLERDEVICEREMAPPED: controller_manager_->remapped(e.cdevice); break;
case SDL_CONTROLLERAXISMOTION: input_->controller_manager()->axis_event(e.caxis); break; case SDL_CONTROLLERAXISMOTION: controller_manager_->axis_event(e.caxis); break;
//JOYSTICK > CONTROLLER //JOYSTICK > CONTROLLER
case SDL_JOYDEVICEADDED: input_->controller_manager()->joy_added(e.jdevice); break; case SDL_JOYDEVICEADDED: controller_manager_->joy_added(e.jdevice); break;
case SDL_JOYDEVICEREMOVED: input_->controller_manager()->joy_removed(e.jdevice); break; case SDL_JOYDEVICEREMOVED: controller_manager_->joy_removed(e.jdevice); break;
case SDL_JOYHATMOTION: input_->controller_manager()->joy_hat(e.jhat); break; case SDL_JOYHATMOTION: controller_manager_->joy_hat(e.jhat); break;
case SDL_JOYBUTTONDOWN: input_->controller_manager()->joy_down(e.jbutton); break; case SDL_JOYBUTTONDOWN: controller_manager_->joy_down(e.jbutton); break;
case SDL_JOYBUTTONUP: input_->controller_manager()->joy_up(e.jbutton); break; case SDL_JOYBUTTONUP: controller_manager_->joy_up(e.jbutton); break;
case SDL_JOYBALLMOTION: input_->controller_manager()->joy_ball(e.jball); break; case SDL_JOYBALLMOTION: controller_manager_->joy_ball(e.jball); break;
case SDL_JOYAXISMOTION: input_->controller_manager()->joy_axis(e.jaxis); break; case SDL_JOYAXISMOTION: controller_manager_->joy_axis(e.jaxis); break;
//TEXTINPUT unused //TEXTINPUT unused
case SDL_TEXTEDITING: text_edit(e.edit); break; case SDL_TEXTEDITING: text_edit(e.edit); break;
case SDL_TEXTINPUT: text_input(e.text); break; case SDL_TEXTINPUT: text_input(e.text); break;
......
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.cpp for GDW SS15
//****************************************
#include <input/input_mapping.hpp> #include <input/input_mapping.hpp>
namespace gdw{ namespace gdw{
......
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.cpp for GDW SS15
//****************************************
#include <input/input_util.hpp> #include <input/input_util.hpp>
namespace gdw{ namespace gdw{
......
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.cpp for GDW SS15
//****************************************
#include <input/keyboard.hpp> #include <input/keyboard.hpp>
namespace gdw{ namespace gdw{
......
//****************************************
// by Benjamin 'Albsi' Albsmeier
// input/input.cpp for GDW SS15
//****************************************
#include <input/mouse.hpp> #include <input/mouse.hpp>
namespace gdw{ namespace gdw{
......
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