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

input is not working - but no errors

parent f746f63a
......@@ -8,6 +8,7 @@
namespace gdw {
class audio;
class graphics_system;
class input;
class rendering_system;
}
......@@ -19,6 +20,7 @@ namespace gdw {
gdw::config_manager config_manager_;
std::unique_ptr<gdw::audio> audio_;
std::unique_ptr<gdw::graphics_system> graphics_system_;
std::unique_ptr<gdw::input> input_;
std::unique_ptr<gdw::rendering_system> rendering_system_;
// needs to be the last one that will be destroyed
......@@ -61,6 +63,10 @@ namespace gdw {
return *graphics_system_;
}
gdw::input& input() noexcept {
return *input_;
}
gdw::rendering_system& rendering_system() noexcept {
return *rendering_system_;
}
......
#include <map>
#include <SDL.h>
#include <glm/glm.hpp>
#ifndef __INPUT_HPP__
#define __INPUT_HPP__
namespace gdw{
class engine;
class input{
public:
input(engine& engine);
~input();
void update(const SDL_Event &event);
bool isKeyDown(SDL_Keycode key);
bool isKeyPressed(SDL_Keycode key);
bool isKeyReleased(SDL_Keycode key);
bool isMouseButtonDown(int button);
bool isMouseButtonPressed(int button);
bool isMouseButtonReleased(int button);
glm::vec2 mouseWheel()noexcept{return mousePos_;};
int mouseWheelX()noexcept{return mouseWheel_.x;};
int mouseWheelY()noexcept{return mouseWheel_.y;};
glm::vec2 mousePos()noexcept{return mousePos_;};
int mousePosX()noexcept{return mousePos_.x;};
int mousePosY()noexcept{return mousePos_.y;};
private:
const Uint8 *keyState_;
glm::vec2 mousePos_;
glm::vec2 mouseWheel_;
std::map<int, int> mouseMap_;
std::map<int, int> mouseReleasedMap_;
std::map<SDL_Keycode, int> keyMap_;
std::map<SDL_Keycode, int> keyReleasedMap_;
};
}
#endif
......@@ -9,11 +9,14 @@
#include <ecs/component.hpp>
#include <ecs/entity.hpp>
#include <graphics/graphics_system.hpp>
#include <input/input.hpp>
#include <rendering/rendering_system.hpp>
#include <util/config.hpp>
#include <util/logger.hpp>
#include <util/make_unique.hpp>
#include <iostream>
namespace gdw {
engine::engine()
: quit_(false), config_manager_(*this), entity_manager_(*this) {
......@@ -26,6 +29,7 @@ namespace gdw {
}
audio_ = make_unique<gdw::audio>(*this);
graphics_system_ = make_unique<gdw::graphics_system>(*this);
input_ = make_unique<gdw::input>(*this);
rendering_system_ = make_unique<gdw::rendering_system>(*this);
}
......@@ -57,6 +61,11 @@ namespace gdw {
current_time = std::chrono::duration_cast<std::chrono::duration<float>>(duration).count();
delta_time = current_time - last_time;
input_->update(event);
if(input_->isKeyDown(SDLK_f))quit_ = true;
update(delta_time);
entity_manager_.clean_up();
}
......
#include "input/input.hpp"
#include <iostream>
namespace gdw{
input::input(engine& engine):/*engine_(engine),*/mousePos_(0,0),mouseWheel_(0,0){
keyState_ = SDL_GetKeyboardState(0);
}
input::~input(){}
void input::update(const SDL_Event &event){
keyReleasedMap_.clear();
mouseReleasedMap_.clear();
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_[event.button.button]){
++mouseMap_[event.button.button];
}else{
mouseMap_[event.button.button] = 1;
}
}
if(event.type == SDL_MOUSEBUTTONUP){
mouseMap_[event.button.button] = 0;
mouseReleasedMap_[event.button.button] = 1;
}
if(event.type == SDL_KEYDOWN){
if(keyMap_[event.key.keysym.sym]){
++keyMap_[event.key.keysym.sym];
}else{
keyMap_[event.key.keysym.sym] = 1;
}
}
if(event.type == SDL_KEYUP){
keyMap_[event.key.keysym.sym] = 0;
}
}
bool input::isKeyDown(SDL_Keycode key){
std::cout << "kd:"<<keyMap_[key]<<std::endl;
return keyMap_[key];
}
bool input::isKeyPressed(SDL_Keycode key){
return keyMap_[key]==1;
}
bool input::isKeyReleased(SDL_Keycode key){
return keyReleasedMap_[key];
}
bool input::isMouseButtonDown(int button){
return mouseMap_[button];
}
bool input::isMouseButtonPressed(int button){
return mouseMap_[button]==1;
}
bool input::isMouseButtonReleased(int button){
return mouseReleasedMap_[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