Commit 180633e0 authored by Benjamin 'Albsi' Albsmeier's avatar Benjamin 'Albsi' Albsmeier
Browse files

input is now working - keyboard key pressed, down, released - mouse pos x,y ,...

input is now working - keyboard key pressed, down, released - mouse pos x,y , wheel x,y , button pressed, down, released
parent 76bd1425
......@@ -13,10 +13,7 @@ namespace gdw{
~input();
void update(const SDL_Event &event);
int isKeyDown(SDL_Scancode key);
bool isKeyPressed(SDL_Scancode key);
bool isKeyReleased(SDL_Scancode key);
void reset();
int isKeyDown(SDL_Keycode key);
bool isKeyPressed(SDL_Keycode key);
......@@ -34,16 +31,15 @@ namespace gdw{
int mousePosX()noexcept{return mousePos_.x;};
int mousePosY()noexcept{return mousePos_.y;};
private:
int lastTimeStamp_;
const Uint8 *keyState_;
glm::vec2 mousePos_;
glm::vec2 mouseWheel_;
std::map<int, int> mouseMap_;
std::map<int, int> mouseReleasedMap_;
std::map<int, int>::iterator mouseIt_;
std::map<SDL_Scancode, int> keyMap_;
std::map<SDL_Scancode, int> keyReleasedMap_;
std::map<SDL_Scancode, int>::iterator keyIt_;
std::map<int, int> keyMap_;
std::map<int, int> keyReleasedMap_;
std::map<int, int>::iterator keyIt_;
};
}
......
......@@ -15,8 +15,6 @@
#include <util/logger.hpp>
#include <util/make_unique.hpp>
#include <iostream>
namespace gdw {
engine::engine()
: quit_(false), config_manager_(*this), entity_manager_(*this) {
......@@ -55,18 +53,16 @@ namespace gdw {
if (event.type == SDL_QUIT) {
quit_ = true;
}
input_->update(event);
}
duration = std::chrono::high_resolution_clock::now().time_since_epoch();
last_time = current_time;
current_time = std::chrono::duration_cast<std::chrono::duration<float>>(duration).count();
delta_time = current_time - last_time;
input_->update(event);
if(input_->isKeyDown(SDL_SCANCODE_H)){
std::cout << "h down:" << input_->isKeyDown(SDL_SCANCODE_H) << std::endl;
}
update(delta_time);
entity_manager_.clean_up();
input_->reset();
}
}
}
#include "input/input.hpp"
#include <iostream>
namespace gdw{
input::input(engine& engine):/*engine_(engine),*/mousePos_(0,0),mouseWheel_(0,0),lastTimeStamp_(0){
keyState_ = SDL_GetKeyboardState(0);
input::input(engine& engine):/*engine_(engine),*/mousePos_(0,0),mouseWheel_(0,0){
keyState_ = SDL_GetKeyboardState(NULL);
}
input::~input(){}
void input::update(const SDL_Event &event){
void input::update(const SDL_Event &e){
switch(e.type){
case SDL_KEYDOWN:
if((keyMap_.find(e.key.keysym.sym)) == keyMap_.end()){
keyMap_.emplace(e.key.keysym.sym, 0);
}
++keyMap_[e.key.keysym.sym];
break;
case SDL_KEYUP:
keyMap_[e.key.keysym.sym] = 0;
keyReleasedMap_.emplace(e.key.keysym.sym, 1);
break;
case SDL_MOUSEBUTTONDOWN:
if((mouseMap_.find(e.button.button)) == mouseMap_.end()){
mouseMap_.emplace(e.button.button, 0);
}
++mouseMap_[e.button.button];
break;
case SDL_MOUSEBUTTONUP:
mouseMap_[e.button.button] = 0;
mouseReleasedMap_.emplace(e.button.button, 1);
break;
case SDL_MOUSEMOTION:
mousePos_.x = e.motion.x;
mousePos_.y = e.motion.y;
break;
case SDL_MOUSEWHEEL:
mouseWheel_.x = e.wheel.x;
mouseWheel_.y = e.wheel.y;
break;
}
}
void input::reset(){
mouseWheel_.x = 0;
mouseWheel_.y = 0;
if(!keyReleasedMap_.empty()){keyReleasedMap_.clear();}
if(!mouseReleasedMap_.empty()){mouseReleasedMap_.clear();}
for (mouseIt_ = mouseMap_.begin(); mouseIt_ != mouseMap_.end(); mouseIt_++){
if(mouseIt_->second){
if(mouseIt_->second == 1){
++mouseIt_->second;
}
}
for (keyIt_ = keyMap_.begin(); keyIt_ != keyMap_.end(); keyIt_++){
if(keyIt_->second){
if(keyIt_->second == 1){
++keyIt_->second;
}
}
if(event.type == SDL_MOUSEMOTION){
mousePos_.x = event.motion.x;
mousePos_.y = event.motion.y;
}
if(event.type == SDL_MOUSEWHEEL){
mouseWheel_.x = event.wheel.x;
mouseWheel_.y = event.wheel.y;
}
if(event.type == SDL_MOUSEBUTTONDOWN){
if((mouseMap_.find(event.button.button)) == mouseMap_.end()){
mouseMap_.emplace(event.button.button,1);
}
++mouseMap_[event.button.button];
}
if(event.type == SDL_MOUSEBUTTONUP){
mouseMap_[event.button.button] = 0;
mouseReleasedMap_[event.button.button] = 1;
}
if(event.type == SDL_KEYDOWN){
if((keyMap_.find(event.key.keysym.scancode)) == keyMap_.end()){
keyMap_.emplace(event.key.keysym.scancode,1);
}
++keyMap_[event.key.keysym.scancode];
}
if(event.type == SDL_KEYUP){
keyMap_[event.key.keysym.scancode] = 0;
}
}
int isKeyDown(SDL_Keycode key){
return isKeyDown(SDL_GetScancodeFromKey(key));
}
bool isKeyPressed(SDL_Keycode key){
return isKeyPressed(SDL_GetScancodeFromKey(key));
}
bool isKeyReleased(SDL_Keycode key){
return isKeyReleased(SDL_GetScancodeFromKey(key));
}
int input::isKeyDown(SDL_Scancode key){
return keyMap_[key];
int input::isKeyDown(SDL_Keycode key){
return keyState_[SDL_GetScancodeFromKey(key)];
}
bool input::isKeyPressed(SDL_Scancode key){
bool input::isKeyPressed(SDL_Keycode key){//TODO
return keyMap_[key]==1;
}
bool input::isKeyReleased(SDL_Scancode key){
return keyReleasedMap_[key];
bool input::isKeyReleased(SDL_Keycode key){
return keyReleasedMap_[key] && !keyState_[SDL_GetScancodeFromKey(key)];
}
int input::isMouseButtonDown(int button){
return mouseMap_[button];
return SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(button);
}
bool input::isMouseButtonPressed(int button){
......@@ -91,6 +86,6 @@ namespace gdw{
}
bool input::isMouseButtonReleased(int button){
return mouseReleasedMap_[button];
return mouseReleasedMap_[button] && !SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(button);
}
}
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