This commit is contained in:
Frank DeMarco 2019-05-04 03:25:35 -04:00
parent 845d3bb883
commit 41dbfb8498
12 changed files with 110 additions and 33 deletions

View File

@ -179,8 +179,6 @@ struct Demo : Game
Input *input = new Input(this);
input->print_branch();
mushroom->print_branch();
// Game* root = get_root();
// std::cout << root << std::endl;
}
std::string get_class_name()
@ -343,6 +341,10 @@ struct Demo : Game
get_framerate_indicator_surface(frame_count), GL_LINEAR);
}
void respond(SDL_Event& event)
{
}
void update()
{
// while (SDL_PollEvent(&event))

View File

@ -10,5 +10,6 @@
"keys":
{
"screenshot": ["CTRL", "s"]
}
}

View File

@ -1,6 +1,6 @@
#include "Configuration.hpp"
Configuration::Configuration(Node *parent) : Configuration(parent, "config") {}
Configuration::Configuration(Node *parent) : Configuration(parent, "config.json") {}
Configuration::Configuration(Node *parent, fs::path path) : Node(parent)
{
@ -9,15 +9,20 @@ Configuration::Configuration(Node *parent, fs::path path) : Node(parent)
config_path = path;
set_defaults();
load();
merge();
}
void Configuration::set_defaults()
{
sys_config["keys"] = {
{"record", {"CTRL", "SHIFT", "F10"}},
{"screenshot", "F9"}
{"screenshot", "F9"},
{"action", " "},
{"up", "up"},
{"right", "right"},
{"down", "down"},
{"left", "left"}
};
config = sys_config;
}
void Configuration::load()
@ -27,16 +32,21 @@ void Configuration::load()
void Configuration::load(fs::path path)
{
std::ifstream contents(path);
contents >> game_config;
// config["blob"] = false;
// config["slime"][1] = {420, 69};
// config["goo"] = "yum";
// std::cout << std::setw(4) << config << std::endl;
// std::cout << std::setw(4) << sys_config << std::endl;
config.update(game_config);
if (fs::exists(path))
{
std::ifstream contents(path);
contents >> game_config;
}
}
void Configuration::merge()
{
config = sys_config;
if (not game_config.empty())
{
config["keys"].update(game_config["keys"]);
}
std::cout << std::setw(4) << config << std::endl;
std::cout << std::setw(4) << game_config << std::endl;
}
void Configuration::write()
@ -47,5 +57,5 @@ void Configuration::write()
void Configuration::write(fs::path path)
{
std::ofstream output(path);
output << std::setw(tab_width) << config << std::endl;
output << std::setw(tab_width) << game_config << std::endl;
}

View File

@ -8,6 +8,7 @@
#include "json/json.hpp"
#include "filesystem.hpp"
#include "Node.hpp"
struct Configuration : Node
@ -21,6 +22,7 @@ struct Configuration : Node
void set_defaults();
void load();
void load(fs::path path);
void merge();
void write();
void write(fs::path path);
std::string get_class_name() { return "Configuration"; }

View File

@ -22,7 +22,7 @@ void Delegate::dispatch()
{
for (subscriber s : iter->second)
{
s(&event);
s(event);
}
}
}

View File

@ -9,7 +9,7 @@
#include "SDL.h"
typedef std::function<void(SDL_Event*)> subscriber;
typedef std::function<void(SDL_Event&)> subscriber;
struct Delegate : Node
{

View File

@ -152,9 +152,9 @@ void Game::set_framerate(int fps)
frame_length = 1000.0 / framerate;
}
void Game::handle_quit_event(SDL_Event *event)
void Game::handle_quit_event(SDL_Event &event)
{
if (event->type == SDL_QUIT)
if (event.type == SDL_QUIT)
{
flag_to_end();
}

View File

@ -48,7 +48,7 @@ struct Game : Node
void flag_to_end();
virtual void update() {};
void set_framerate(int);
void handle_quit_event(SDL_Event*);
void handle_quit_event(SDL_Event&);
void quit();
std::string get_class_name() { return "Game"; }

View File

@ -4,17 +4,75 @@ Input::Input(Node *parent) : Node(parent)
{
load_key_map();
get_delegate()->add_subscriber(std::bind(&Input::respond, this, std::placeholders::_1), SDL_KEYDOWN);
for (KeyCombination& combination : key_map)
{
print_key_combination(combination);
}
}
void Input::print_key_combination(KeyCombination &combination)
{
std::cout << "<KeyCombination " << combination.command << ", " <<
combination.key << ", ctrl " << combination.ctrl << ", shift " <<
combination.shift << ", alt " << combination.alt << ">" << std::endl;
}
void Input::load_key_map()
{
nlohmann::json &config = get_configuration();
for (auto& entry : config.at("keys").items())
{
bool ctrl = false, alt = false, shift = false;
int key;
if (not entry.value().is_string())
{
for (std::string part : entry.value())
{
if (part == "CTRL")
{
ctrl = true;
}
else if (part == "SHIFT")
{
shift = true;
}
else if (part == "ALT")
{
alt = true;
}
else
{
key = get_key_code(part);
}
}
}
else
{
key = get_key_code(entry.value());
}
add_to_key_map(entry.key(), key, ctrl, shift, alt);
}
}
void Input::add_to_key_map()
int Input::get_key_code(std::string name)
{
if (key_ids.count(name) > 0)
{
return key_ids[name];
}
else
{
return (SDL_Keycode) name[0];
}
}
void Input::respond(SDL_Event *event)
void Input::add_to_key_map(
std::string command, SDL_Keycode key_code, bool ctrl, bool shift, bool alt)
{
std::cout << event->key.keysym.sym << std::endl;
key_map.push_back((KeyCombination){command, key_code, ctrl, shift, alt});
}
void Input::respond(SDL_Event &event)
{
std::cout << event.key.keysym.sym << std::endl;
}

View File

@ -11,16 +11,17 @@
struct KeyCombination
{
bool alt;
std::string command;
int key;
bool ctrl;
bool shift;
unsigned int key;
bool alt;
};
struct Input : Node
{
std::map<std::string, unsigned int> key_ids
std::map<std::string, SDL_Keycode> key_ids
{
{"up", SDLK_UP},
{"right", SDLK_RIGHT},
@ -42,10 +43,13 @@ struct Input : Node
std::list<KeyCombination> key_map;
Input(Node*);
void respond(SDL_Event*);
void respond(SDL_Event&);
std::string get_class_name() { return "Input"; }
void load_key_map();
void add_to_key_map();
SDL_Keycode get_key_code(std::string);
void add_to_key_map(
std::string, SDL_Keycode, bool = false, bool = false, bool = false);
void print_key_combination(KeyCombination&);
};

View File

@ -9,9 +9,9 @@ Node::Node(Node *parent) : parent(parent)
print_branch();
}
Configuration* Node::get_configuration()
nlohmann::json& Node::get_configuration()
{
return get_root()->configuration;
return get_root()->configuration->config;
}
Delegate* Node::get_delegate()

View File

@ -3,12 +3,12 @@
#include <iostream>
#include "json/json.hpp"
#include "SDL.h"
#include "filesystem.hpp"
struct Game;
struct Configuration;
struct Delegate;
struct Node
@ -18,8 +18,8 @@ struct Node
Node();
Node(Node*);
Game *get_root();
Configuration* get_configuration();
Game* get_root();
nlohmann::json& get_configuration();
Delegate* get_delegate();
void print_branch();
virtual std::string get_class_name() { return "Node"; };