keyboard events.

main
bog 2023-10-26 23:49:45 +02:00
parent bb78d60407
commit 6c10e63d45
12 changed files with 229 additions and 48 deletions

View File

@ -1,14 +1,19 @@
<?xml version="1" encoding="utf-8"?> <?xml version="1" encoding="utf-8"?>
<manifest>
<resources>
<!-- SHADERS -->
<resource type="text" name="vertex-shader"
path="shaders/default_vertex.glsl"/>
<resources> <resource type="text" name="fragment-shader"
<!-- SHADERS --> path="shaders/default_fragment.glsl"/>
<resource type="text" name="vertex-shader"
path="shaders/default_vertex.glsl"/>
<resource type="text" name="fragment-shader" <!-- IMAGES -->
path="shaders/default_fragment.glsl"/> <resource type="image" name="demo" path="images/demo.png"/>
<!-- IMAGES --> </resources>
<resource type="image" name="demo" path="images/demo.png"/>
</resources> <actions>
<action name="hello" key="UP"/>
</actions>
</manifest>

View File

@ -28,6 +28,7 @@ executable(
'src/Services.cpp', 'src/Services.cpp',
'src/Service.cpp', 'src/Service.cpp',
'src/Sprite.cpp', 'src/Sprite.cpp',
'src/Manifest.cpp',
# Logs # Logs
'src/logs/Logs.cpp', 'src/logs/Logs.cpp',
@ -45,6 +46,9 @@ executable(
'src/gfx/Texture.cpp', 'src/gfx/Texture.cpp',
'src/gfx/Shape.cpp', 'src/gfx/Shape.cpp',
# Inputs
'src/inputs/Inputs.cpp',
# Resources # Resources
'src/res/Resources.cpp', 'src/res/Resources.cpp',
'src/res/BaseRes.cpp', 'src/res/BaseRes.cpp',

View File

@ -3,6 +3,8 @@
#include "events/Events.hpp" #include "events/Events.hpp"
#include "logs/Logs.hpp" #include "logs/Logs.hpp"
#include "gfx/Graphics.hpp" #include "gfx/Graphics.hpp"
#include "inputs/Inputs.hpp"
#include "Sprite.hpp" #include "Sprite.hpp"
namespace ml namespace ml
@ -36,6 +38,11 @@ namespace ml
} }
} }
if (ML_SYS(Inputs)->is_action_just_pressed("hello"))
{
ML_SYS(FileLogs)->log(LEVEL_DEBUG, "hello triggered");
}
ML_SYS(Graphics)->clear(4, 139, 154); ML_SYS(Graphics)->clear(4, 139, 154);
ML_SYS(Graphics)->draw(sprite); ML_SYS(Graphics)->draw(sprite);
ML_SYS(Graphics)->update(); ML_SYS(Graphics)->update();

71
src/Manifest.cpp Normal file
View File

@ -0,0 +1,71 @@
#include "Manifest.hpp"
#include "ml.hpp"
namespace ml
{
/*explicit*/ Manifest::Manifest()
{
}
/*virtual*/ Manifest::~Manifest()
{
}
void Manifest::load()
{
xml::XMLDocument doc;
doc.LoadFile(ML_ASSET("manifest.xml").c_str());
auto root = doc.RootElement();
resources(root->FirstChildElement("resources"));
actions(root->FirstChildElement("actions"));
}
void Manifest::resources(xml::XMLElement* resources)
{
xml::XMLElement* itr = resources->FirstChildElement();
while (itr)
{
std::string type = itr->Attribute("type");
std::filesystem::path path = itr->Attribute("path");
std::string name = itr->Attribute("name");
if (type == "text")
{
ML_SYS(Resources)->load<TextRes>(name, path);
}
else if (type == "image")
{
ML_SYS(Resources)->load<ImageRes>(name, path);
}
else
{
throw resource_error {"unknown resource '"
+ name
+ "' of type '" + type + "'"};
}
itr = itr->NextSiblingElement();
}
}
void Manifest::actions(xml::XMLElement* actions)
{
auto itr = actions->FirstChildElement();
while (itr)
{
std::string name = itr->Attribute("name");
if (auto key = itr->Attribute("key", nullptr);
key)
{
SDL_Keycode code = SDL_GetKeyFromName(key);
ML_SYS(Inputs)->bind(std::string(name), code);
}
itr = itr->NextSiblingElement();
}
}
}

28
src/Manifest.hpp Normal file
View File

@ -0,0 +1,28 @@
#ifndef ml_MANIFEST_HPP
#define ml_MANIFEST_HPP
#include <tinyxml2.h>
#include "commons.hpp"
namespace xml = tinyxml2;
namespace ml
{
/**
* Main game configuration file.
**/
class Manifest
{
public:
explicit Manifest();
virtual ~Manifest();
void load();
private:
void resources(xml::XMLElement* resources);
void actions(xml::XMLElement* actions);
};
}
#endif

60
src/inputs/Inputs.cpp Normal file
View File

@ -0,0 +1,60 @@
#include "Inputs.hpp"
#include "SDL_keycode.h"
namespace ml
{
/*explicit*/ Inputs::Inputs()
{
}
/*virtual*/ Inputs::~Inputs()
{
}
void Inputs::bind(std::string const& action, SDL_Keycode code)
{
if (auto itr=m_actions.find(action);
itr != std::end(m_actions))
{
itr->second.push_back(code);
}
else
{
m_actions.insert({action, {code}});
}
}
bool Inputs::is_action_pressed(std::string const& action)
{
int len = 0;
Uint8 const* keys = SDL_GetKeyboardState(&len);
if (auto itr = m_actions.find(action);
itr != std::end(m_actions))
{
for (SDL_Keycode key: itr->second)
{
if (keys[SDL_GetScancodeFromKey(key)])
{
return true;
}
}
}
return false;
}
bool Inputs::is_action_just_pressed(std::string const& action)
{
static bool prev = false;
bool pressed = is_action_pressed(action);
if (pressed != prev)
{
prev = pressed;
return pressed;
}
return false;
}
}

27
src/inputs/Inputs.hpp Normal file
View File

@ -0,0 +1,27 @@
#ifndef ml_INPUTS_HPP
#define ml_INPUTS_HPP
#include <SDL2/SDL.h>
#include "../commons.hpp"
#include "../Service.hpp"
namespace ml
{
class Inputs: public Service
{
public:
explicit Inputs();
virtual ~Inputs();
void bind(std::string const& action, SDL_Keycode code);
bool is_action_pressed(std::string const& action);
bool is_action_just_pressed(std::string const& action);
private:
std::unordered_map<std::string, std::vector<SDL_Keycode>>
m_actions;
};
}
#endif

View File

@ -36,6 +36,6 @@ namespace ml
<< time_str << " " << time_str << " "
<< ("\033[" + color + "m[" + level_str + "]\033[0m ") << ("\033[" + color + "m[" + level_str + "]\033[0m ")
<< message << message
<< "\n"; << std::endl;
} }
} }

View File

@ -9,8 +9,9 @@
#include "events/Events.hpp" #include "events/Events.hpp"
#include "gfx/Graphics.hpp" #include "gfx/Graphics.hpp"
#include "res/Resources.hpp" #include "res/Resources.hpp"
#include "inputs/Inputs.hpp"
#include "Manifest.hpp"
#include "Game.hpp" #include "Game.hpp"
int main(int, char**) int main(int, char**)
@ -22,8 +23,12 @@ int main(int, char**)
ML->add<ml::Events>(); ML->add<ml::Events>();
ML->add<ml::Graphics>("Mornelune", 1024, 768); ML->add<ml::Graphics>("Mornelune", 1024, 768);
ML->add<ml::Inputs>();
ML->init(); ML->init();
ml::Manifest man;
man.load();
ml::Game game; ml::Game game;
game.start(); game.start();
game.stop(); game.stop();

9
src/ml.hpp Normal file
View File

@ -0,0 +1,9 @@
#ifndef ml_ML_HPP
#define ml_ML_HPP
#include "res/Resources.hpp"
#include "inputs/Inputs.hpp"
#include "gfx/Graphics.hpp"
#include "events/Events.hpp"
#endif

View File

@ -16,38 +16,4 @@ namespace ml
/*virtual*/ Resources::~Resources() /*virtual*/ Resources::~Resources()
{ {
} }
void Resources::init() /*override*/
{
xml::XMLDocument doc;
doc.LoadFile(ML_ASSET("manifest.xml").string().c_str());
auto* root = doc.RootElement();
xml::XMLElement* itr = root->FirstChildElement();
while (itr)
{
std::string type = itr->Attribute("type");
std::filesystem::path path = itr->Attribute("path");
std::string name = itr->Attribute("name");
if (type == "text")
{
ML_SYS(Resources)->load<TextRes>(name, path);
}
else if (type == "image")
{
ML_SYS(Resources)->load<ImageRes>(name, path);
}
else
{
throw resource_error {"unknown resource '"
+ name
+ "' of type '" + type + "'"};
}
itr = itr->NextSiblingElement();
}
}
} }

View File

@ -3,7 +3,8 @@
#include "../commons.hpp" #include "../commons.hpp"
#include "../Service.hpp" #include "../Service.hpp"
#include "BaseRes.hpp" #include "TextRes.hpp"
#include "ImageRes.hpp"
#include <memory> #include <memory>
namespace ml namespace ml
@ -14,8 +15,6 @@ namespace ml
explicit Resources(); explicit Resources();
virtual ~Resources(); virtual ~Resources();
void init() override;
template <typename T> template <typename T>
void load(std::string const& name, void load(std::string const& name,
std::filesystem::path const& path); std::filesystem::path const& path);