commit 06b7d19b1af469daf6f6ff41d54a2c65d1696bb2 Author: Lendemor Date: Thu Jul 9 22:02:20 2015 +0200 First commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9542729 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +CMakeLists.txt.user +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..86d745b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,51 @@ +project(SparrowInput) +cmake_minimum_required(VERSION 2.8) + +if(WIN32) + set(SYSTEM_LIB_PATH "win32") +else(WIN32) + set(SYSTEM_LIB_PATH "linux") +endif(WIN32) + +set(LIB_SRC_LIST input.cpp keybindings.cpp) + +set(EXECUTABLE_NAME "test${PROJECT_NAME}") +set(LIBRARY_NAME ${PROJECT_NAME}) + +set(DEPENDENCIES_ROOT ${PROJECT_SOURCE_DIR}/../cpp_dependencies) +set(INCLUDE_ROOT ${DEPENDENCIES_ROOT}/include) +set(LIB_ROOT ${DEPENDENCIES_ROOT}/lib/${SYSTEM_LIB_PATH}) + + +add_library(${LIBRARY_NAME} SHARED ${LIB_SRC_LIST}) +add_executable(${EXECUTABLE_NAME} main.cpp) +add_definitions(-std=c++11) + +include_directories( + ${INCLUDE_ROOT} +) + +find_library(SFML_LIBRARY_WINDOW + NAMES + sfml-window + PATHS + ${LIB_ROOT} +) + +find_library(SFML_LIBRARY_SYSTEM + NAMES + sfml-system + PATHS + ${LIB_ROOT} +) + +target_link_libraries( + ${LIBRARY_NAME} + ${SFML_LIBRARY_WINDOW} + ${SFML_LIBRARY_SYSTEM} +) + +target_link_libraries( + ${EXECUTABLE_NAME} + ${LIBRARY_NAME} +) diff --git a/input.cpp b/input.cpp new file mode 100644 index 0000000..ed37585 --- /dev/null +++ b/input.cpp @@ -0,0 +1,56 @@ +/** +* @author: Thomas Brandého +*/ + +#include "input.h" + +#include + +Input::Input(sf::Window* w, std::string keysmappings, int nb_actions): window(w) +{ + keysmap= KeysMap(keysmappings, nb_actions); +// action_file = std::queue(); +// keysmap = KeysMap(false); +// context_maps.push_back(new Context()); +} + +int Input::getKeyBinding(int action,int num){ + return keysmap.getKeyBinding(action,num-1); //offset num between 0 and 1 +} + +void Input::setkeyBinding(int action, int num, sf::Keyboard::Key key){ + keysmap.setKeyBinding(action,num-1,key); +} + + +/* +void Input::updateEvents(){ + sf::Event event; + //WARNING : may cause lag if continuously fed with event? + while(window->pollEvent(event)){ + if (event.type == sf::Event::KeyPressed){ + switch(event.KeyPressed){ + // handle keypressed + default: + break; + } + }else{ + //handle other kind of event + } + } +} + +int Input::getAction(){ + //return action_file.first(); +} + +void Input::reloadKeyBindings(){ + for (int i = 0; i < NB_CONTEXT; i++){ + keybindings[i] = KeyBindings(contexts[i],&keysmap); + } +} + +void Input::setContext(Context context){ + current_context = context; +} +*/ diff --git a/input.h b/input.h new file mode 100644 index 0000000..15bbfc0 --- /dev/null +++ b/input.h @@ -0,0 +1,37 @@ +/** +* @author: Thomas Brandého +*/ + +#ifndef INPUT_H +#define INPUT_H +#include "keybindings.h" +#include +#include + +class Input{ + public: + //enum Context {MENU, EDITOR, GAME, DEBUG, NB_CONTEXT}; + Input(sf::Window* w, std::string keysmappings, int nb_actions); + int getKeyBinding(int action, int num); + void setkeyBinding(int action, int num, sf::Keyboard::Key key); + bool createContext(std::string context_name, std::vector action_list); + void setCurrentContext(std::string context_name); + + // void updateEvents(); + // int getAction(); + // void reloadKeyBindings(); + /* +*/ + private: + sf::Window* window; + KeysMap keysmap; + /* + std::queue action_file; + Context current_context; + std::vector contexts[NB_CONTEXT]; + std::vector contexts; + KeyBindings keybindings[NB_CONTEXT]; +*/ +}; + +#endif diff --git a/keybindings.cpp b/keybindings.cpp new file mode 100644 index 0000000..7f170e2 --- /dev/null +++ b/keybindings.cpp @@ -0,0 +1,71 @@ +/** +* @author: Thomas Brandého +*/ + +#include "keybindings.h" +#include + +/* Implementation of KeysMap class + * @author: Thomas Brandého + * @info: This class register all the association between key and action. + */ + +KeysMap::KeysMap() +{ +} + +KeysMap::KeysMap(std::string keysmapping, int nb_actions):keysmapping_file(keysmapping), size_keys_map(nb_actions*2) +{ + keys_map = (int*) malloc(size_keys_map*sizeof(int)); + loadKeysMap(); +} + +int KeysMap::getKeyBinding(int action,int num) +{ + return keys_map[action*NB_BINDINGS+num]; +} + +void KeysMap::setKeyBinding(int action, int num, int key) +{ + keys_map[(action*NB_BINDINGS)+num] = key; +} + +void KeysMap::saveKeysMap() +{ + //TODO: Serialize map_keys + //use keysmapping_file +} + +void KeysMap::loadKeysMap() +{ + //TODO: Load map_keys + //use keysmapping_file +} + +/* Implementation of KeyBindings class + * @author: Thomas Brandého + * @info: This class map a list of action with the associated keys, for a quick access in-game. + * The map is read-only, so if you want to add of remove action from the map, you have to create a new instance of KeyBindings + */ + +KeyBindings::KeyBindings() +{ +} + +KeyBindings::KeyBindings(std::vector action_list, KeysMap* keysmap) +{ + for (int action : action_list){ + for(int i=0; igetKeyBinding(action,i),action); + } +} + +int KeyBindings::getAction(int key) +{ +// return bindings.; +} + +void KeyBindings::setKeyBinding(int key, int action) +{ +// bindings.set(key,action); +} diff --git a/keybindings.h b/keybindings.h new file mode 100644 index 0000000..2b555b5 --- /dev/null +++ b/keybindings.h @@ -0,0 +1,40 @@ +/** +* @author: Thomas Brandého +*/ + +#ifndef KEYBINDINGS_H +#define KEYBINDINGS_H + +#include + +#include + +class KeysMap { + public: + enum {PRIMARY,SECONDARY,NB_BINDINGS}; + + KeysMap(); + KeysMap(std::string keysmapping, int nb_actions); + + int getKeyBinding(int action,int num); + void setKeyBinding(int action, int num, int key); + + void saveKeysMap(); + void loadKeysMap(); + private: + std::string keysmapping_file; + int size_keys_map = 0; + int* keys_map; +}; + +class KeyBindings { + public: + KeyBindings(); + KeyBindings(const std::vector action_list, KeysMap* keysmap); + int getAction(int key); + private: + std::unordered_map bindings; + void setKeyBinding(int key, int action); +}; + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..a88cf43 --- /dev/null +++ b/main.cpp @@ -0,0 +1,20 @@ +#include +#include +#include "input.h" + +using namespace std; + +int main() +{ + cout << "Hello World!" << endl; + sf::Window* window= new sf::Window(sf::VideoMode(400,200),"testSparrowInput"); + Input myInput(window, "test.esk",5); + bool run = true; + while(run){ + //myInput.updateEvents(); + } + vector actions; + actions.size(); + return 0; +} +