First commit

This commit is contained in:
Lendemor 2015-07-09 22:02:20 +02:00
commit 06b7d19b1a
7 changed files with 277 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
CMakeLists.txt.user
build/

51
CMakeLists.txt Normal file
View File

@ -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}
)

56
input.cpp Normal file
View File

@ -0,0 +1,56 @@
/**
* @author: Thomas Brandého
*/
#include "input.h"
#include <SFML/Window.hpp>
Input::Input(sf::Window* w, std::string keysmappings, int nb_actions): window(w)
{
keysmap= KeysMap(keysmappings, nb_actions);
// action_file = std::queue<int>();
// 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;
}
*/

37
input.h Normal file
View File

@ -0,0 +1,37 @@
/**
* @author: Thomas Brandého
*/
#ifndef INPUT_H
#define INPUT_H
#include "keybindings.h"
#include <queue>
#include <vector>
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<int> action_list);
void setCurrentContext(std::string context_name);
// void updateEvents();
// int getAction();
// void reloadKeyBindings();
/*
*/
private:
sf::Window* window;
KeysMap keysmap;
/*
std::queue<int> action_file;
Context current_context;
std::vector<int> contexts[NB_CONTEXT];
std::vector<Context> contexts;
KeyBindings keybindings[NB_CONTEXT];
*/
};
#endif

71
keybindings.cpp Normal file
View File

@ -0,0 +1,71 @@
/**
* @author: Thomas Brandého
*/
#include "keybindings.h"
#include <iostream>
/* 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<int> action_list, KeysMap* keysmap)
{
for (int action : action_list){
for(int i=0; i<KeysMap::NB_BINDINGS; i++)
setKeyBinding(keysmap->getKeyBinding(action,i),action);
}
}
int KeyBindings::getAction(int key)
{
// return bindings.;
}
void KeyBindings::setKeyBinding(int key, int action)
{
// bindings.set(key,action);
}

40
keybindings.h Normal file
View File

@ -0,0 +1,40 @@
/**
* @author: Thomas Brandého
*/
#ifndef KEYBINDINGS_H
#define KEYBINDINGS_H
#include <SFML/Window.hpp>
#include <unordered_map>
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<int> action_list, KeysMap* keysmap);
int getAction(int key);
private:
std::unordered_map<int,int> bindings;
void setKeyBinding(int key, int action);
};
#endif

20
main.cpp Normal file
View File

@ -0,0 +1,20 @@
#include <iostream>
#include <SFML/Window.hpp>
#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<int> actions;
actions.size();
return 0;
}