Large refactoring, using the Entity-Component model.

This commit is contained in:
2016-11-30 19:50:01 +02:00
parent bcd0a359fc
commit 9c8cbf8518
53 changed files with 1616 additions and 675 deletions

40
src/input/GameKey.h Normal file
View File

@@ -0,0 +1,40 @@
/*
* GameKeys.h
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#ifndef INPUT_GAMEKEY_H_
#define INPUT_GAMEKEY_H_
namespace farmlands {
namespace input {
enum GameKey
{
Right,
Up,
Left,
Down,
Action,
Action2,
Run,
AltRight,
AltUp,
AltLeft,
AltDown,
Debug_ZoomIn,
Debug_ZoomOut,
_Count
};
}
}
#endif /* INPUT_GAMEKEY_H_ */

View File

@@ -0,0 +1,40 @@
/*
* GameKeyConfiguration.cpp
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#include <input/GameKeyConfiguration.h>
namespace farmlands {
namespace input {
/**
* Default keyboard configuration
*/
const GameKeyConfiguration GameKeyConfiguration::defaultConfig =
{
.keys =
{
SDL_SCANCODE_RIGHT, // Right
SDL_SCANCODE_UP, // Up
SDL_SCANCODE_LEFT, // Left
SDL_SCANCODE_DOWN, // Down
SDL_SCANCODE_SPACE, // Action
SDL_SCANCODE_LCTRL, // Action2
SDL_SCANCODE_LSHIFT, // Run
SDL_SCANCODE_D, // AltRight
SDL_SCANCODE_W, // AltUp
SDL_SCANCODE_A, // AltLeft
SDL_SCANCODE_S, // AltRight
SDL_SCANCODE_KP_PLUS, // Debug_ZoomIn,
SDL_SCANCODE_KP_MINUS, // Debug_ZoomOut,
}
};
}
}

View File

@@ -0,0 +1,30 @@
/*
* GameKeyConfiguration.h
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#ifndef INPUT_GAMEKEYCONFIGURATION_H_
#define INPUT_GAMEKEYCONFIGURATION_H_
#include <input/GameKey.h>
#include <SDL2/SDL_scancode.h>
namespace farmlands {
namespace input {
/**
* Defines a keyboard configuration
*/
struct GameKeyConfiguration
{
SDL_Scancode keys[GameKey::_Count];
static const GameKeyConfiguration defaultConfig;
};
} /* namespace input */
} /* namespace farmlands */
#endif /* INPUT_GAMEKEYCONFIGURATION_H_ */

81
src/input/Input.cpp Normal file
View File

@@ -0,0 +1,81 @@
/*
* Input.cpp
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#include <input/Input.h>
#include <SDL2/SDL.h>
namespace farmlands {
namespace input {
Input Input::s_instance;
Input& Input::instance()
{
return s_instance;
}
Input::Input()
: m_config(GameKeyConfiguration::defaultConfig)
{
}
Input::~Input()
{
}
void Input::initialize()
{
}
bool Input::pressed(GameKey key)
{
const Uint8* keys = SDL_GetKeyboardState(NULL);
SDL_Scancode code = m_config.keys[key];
return keys[code];
}
bool Input::down(GameKey key, SDL_Event& event)
{
if (event.type == SDL_KEYDOWN)
{
SDL_Scancode code = m_config.keys[key];
return event.key.keysym.scancode == code;
}
return false;
}
float Input::getX()
{
// Keyboard
if (pressed(GameKey::Right) || pressed(GameKey::AltRight))
return 1.0f;
else if (pressed(GameKey::Left) || pressed(GameKey::AltLeft))
return -1.0f;
// TODO: controller
return 0.0f;
}
float Input::getY()
{
// Keyboard
if (pressed(GameKey::Down) || pressed(GameKey::AltDown))
return 1.0f;
else if (pressed(GameKey::Up) || pressed(GameKey::AltUp))
return -1.0f;
// TODO: controller
return 0.0f;
}
} /* namespace input */
} /* namespace farmlands */

42
src/input/Input.h Normal file
View File

@@ -0,0 +1,42 @@
/*
* Input.h
*
* Created on: Nov 30, 2016
* Author: tibi
*/
#ifndef INPUT_INPUT_H_
#define INPUT_INPUT_H_
#include <input/GameKey.h>
#include <input/GameKeyConfiguration.h>
#include <SDL2/SDL.h>
namespace farmlands {
namespace input {
class Input
{
public:
static Input& instance();
virtual ~Input();
void initialize();
bool pressed(GameKey key);
bool down(GameKey key, SDL_Event& event);
float getX();
float getY();
private:
Input();
static Input s_instance;
GameKeyConfiguration m_config;
};
} /* namespace input */
} /* namespace farmlands */
#endif /* INPUT_INPUT_H_ */