fixing performance issue in text pipeline, working on event system

This commit is contained in:
2023-04-12 15:15:25 +02:00
parent ec0f2eaa60
commit fe45d0953d
13 changed files with 100 additions and 84 deletions

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */
/* Updated: 2023/04/06 15:35:31 by maldavid ### ########.fr */
/* Updated: 2023/04/12 11:03:57 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -20,9 +20,9 @@ namespace mlx::core
{
void Application::run() noexcept
{
while(_in.is_running())
while(_in->is_running())
{
_in.update();
_in->update();
for(auto& gs : _graphics)
gs->beginRender();

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */
/* Updated: 2023/04/11 21:43:13 by maldavid ### ########.fr */
/* Updated: 2023/04/12 11:14:24 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -32,14 +32,12 @@ namespace mlx::core
class Application
{
public:
Application() : _in() {}
Application() : _in(std::make_unique<Input>()) {}
inline void getMousePos(int* x, int* y) noexcept;
inline void mouseMove(void* win, int x, int y) noexcept;
inline void mouseHook(int (*funct_ptr)(const char*, void*), void* param) noexcept;
inline void keyHook(int (*funct_ptr)(const char*, void*), void* param) noexcept;
inline void exposeHook(int (*funct_ptr)(const char*, void*), void* param) noexcept;
inline void onEvent(int event, int (*funct_ptr)(int, void*), void* param) noexcept;
inline constexpr void enableAutoRepeat() noexcept;
inline constexpr void disableAutoRepeat() noexcept;
@@ -67,11 +65,11 @@ namespace mlx::core
~Application() = default;
private:
Input _in;
TextureLibrary _texture_lib;
std::list<TextureID> _texture_ids;
std::vector<std::unique_ptr<GraphicsSupport>> _graphics;
std::function<int(void*)> _loop_hook;
std::unique_ptr<Input> _in;
void* _param = nullptr;
bool _is_loop_running = false;
};

View File

@@ -16,8 +16,8 @@ namespace mlx::core
{
void Application::getMousePos(int* x, int* y) noexcept
{
*x = _in.getX();
*y = _in.getY();
*x = _in->getX();
*y = _in->getY();
}
void Application::mouseMove(void* win, int x, int y) noexcept
@@ -27,29 +27,19 @@ namespace mlx::core
SDL_FlushEvent(SDL_MOUSEMOTION);
}
void Application::mouseHook(int (*funct_ptr)(const char*, void*), void* param) noexcept
void Application::onEvent(int event, int (*funct_ptr)(int, void*), void* param) noexcept
{
_in.mouseHook(funct_ptr, param);
}
void Application::keyHook(int (*funct_ptr)(const char*, void*), void* param) noexcept
{
_in.keyHook(funct_ptr, param);
}
void Application::exposeHook(int (*funct_ptr)(const char*, void*), void* param) noexcept
{
_in.exposeHook(funct_ptr, param);
_in->onEvent(event, funct_ptr, param);
}
constexpr void Application::enableAutoRepeat() noexcept
{
_in.enableAutoRepeat();
_in->enableAutoRepeat();
}
constexpr void Application::disableAutoRepeat() noexcept
{
_in.disableAutoRepeat();
_in->disableAutoRepeat();
}
void Application::getScreenSize(int* w, int* h) noexcept
@@ -101,6 +91,6 @@ namespace mlx::core
void Application::loopEnd() noexcept
{
_in.finish();
_in->finish();
}
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
/* Updated: 2023/04/11 21:42:56 by maldavid ### ########.fr */
/* Updated: 2023/04/12 11:16:52 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -69,21 +69,9 @@ extern "C"
return 0;
}
int mlx_mouse_hook(void* mlx, int (*funct_ptr)(const char*, void*), void* param)
int mlx_on_event(void* mlx, int event, int (*funct_ptr)(int, void*), void* param)
{
static_cast<mlx::core::Application*>(mlx)->mouseHook(funct_ptr, param);
return 0;
}
int mlx_key_hook(void* mlx, int (*funct_ptr)(const char*, void*), void* param)
{
static_cast<mlx::core::Application*>(mlx)->keyHook(funct_ptr, param);
return 0;
}
int mlx_expose_hook(void* mlx, int (*funct_ptr)(const char*, void*), void* param)
{
static_cast<mlx::core::Application*>(mlx)->exposeHook(funct_ptr, param);
static_cast<mlx::core::Application*>(mlx)->onEvent(event, funct_ptr, param);
return 0;
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */
/* Updated: 2023/04/11 21:32:43 by maldavid ### ########.fr */
/* Updated: 2023/04/12 11:31:28 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -39,28 +39,43 @@ namespace mlx
case SDL_KEYDOWN:
{
_keys[_event.key.keysym.scancode] = static_cast<uint8_t>(action::down);
if(_events_hooks[0].hook)
_events_hooks[0].hook(_event.key.keysym.scancode, _events_hooks[0].param);
break;
}
case SDL_KEYUP:
{
_keys[_event.key.keysym.scancode] = static_cast<uint8_t>(action::up);
if(_key_hook.hook)
_key_hook.hook(SDL_GetScancodeName(_event.key.keysym.scancode), _key_hook.param);
if(_events_hooks[1].hook)
_events_hooks[1].hook(_event.key.keysym.scancode, _events_hooks[1].param);
break;
}
case SDL_MOUSEBUTTONDOWN:
{
_mouse[_event.button.button] = static_cast<uint8_t>(action::down);
if(_events_hooks[2].hook)
_events_hooks[2].hook(_event.button.button, _events_hooks[2].param);
break;
}
case SDL_MOUSEBUTTONUP:
{
_mouse[_event.button.button] = static_cast<uint8_t>(action::up);
if(_mouse_hook.hook)
_mouse_hook.hook(std::string("mouse").c_str(), _mouse_hook.param);
if(_events_hooks[3].hook)
_events_hooks[3].hook(_event.button.button, _events_hooks[3].param);
break;
}
case SDL_WINDOWEVENT:
{
switch(_event.window.event)
{
//case
default : break;
}
break;
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */
/* Updated: 2023/04/11 21:41:33 by maldavid ### ########.fr */
/* Updated: 2023/04/12 13:47:02 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -25,7 +25,7 @@ namespace mlx
struct Hook
{
std::function<int(const char*, void*)> hook;
std::function<int(int, void*)> hook;
void* param = nullptr;
};
@@ -53,9 +53,11 @@ namespace mlx
inline constexpr void enableAutoRepeat() noexcept { _auto_repeat = true; }
inline constexpr void disableAutoRepeat() noexcept { _auto_repeat = false; }
inline void mouseHook(int (*funct_ptr)(const char*, void*), void* param) noexcept { _mouse_hook.hook = funct_ptr; _mouse_hook.param = param; }
inline void keyHook(int (*funct_ptr)(const char*, void*), void* param) noexcept { _key_hook.hook = funct_ptr; _key_hook.param = param; }
inline void exposeHook(int (*funct_ptr)(const char*, void*), void* param) noexcept { _expose_hook.hook = funct_ptr; _expose_hook.param = param; }
inline void onEvent(int event, int (*funct_ptr)(int, void*), void* param) noexcept
{
_events_hooks[event].hook = funct_ptr;
_events_hooks[event].param = param;
}
~Input() = default;
@@ -63,10 +65,9 @@ namespace mlx
std::array<uint8_t, SDL_NUM_SCANCODES> _keys;
SDL_Event _event;
std::array<uint8_t, 8> _mouse;
std::vector<class* MLX_Window> _window;
Hook _mouse_hook;
Hook _key_hook;
Hook _expose_hook;
std::array<Hook, 5> _events_hooks;
int _x = 0;
int _y = 0;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/10 11:59:57 by maldavid #+# #+# */
/* Updated: 2023/04/11 18:30:09 by maldavid ### ########.fr */
/* Updated: 2023/04/12 13:24:19 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,11 +14,13 @@
#include <core/errors.h>
#include <renderer/renderer.h>
#include <algorithm>
#include <iostream>
namespace mlx
{
void TextData::init(std::vector<Vertex> vbo_data, std::vector<uint16_t> ibo_data)
void TextData::init(std::string text, std::vector<Vertex> vbo_data, std::vector<uint16_t> ibo_data)
{
_text = std::move(text);
_vbo.create(sizeof(Vertex) * vbo_data.size(), vbo_data.data());
_ibo.create(sizeof(uint16_t) * ibo_data.size(), ibo_data.data());
}
@@ -46,7 +48,7 @@ namespace mlx
{
auto it = std::find_if(_cache.begin(), _cache.end(), [=](const std::pair<TextID, std::shared_ptr<TextData>>& v)
{
return v.second == text;
return v.second->getText() == text->getText();
});
if(it != _cache.end())
return it->first;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/10 11:52:30 by maldavid #+# #+# */
/* Updated: 2023/04/11 12:28:08 by maldavid ### ########.fr */
/* Updated: 2023/04/12 11:38:57 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -31,9 +31,10 @@ namespace mlx
public:
TextData() = default;
void init(std::vector<Vertex> vbo_data, std::vector<uint16_t> ibo_data);
void init(std::string text, std::vector<Vertex> vbo_data, std::vector<uint16_t> ibo_data);
void bind(class Renderer& renderer) noexcept;
inline uint32_t getIBOsize() noexcept { return _ibo.getSize(); }
inline const std::string& getText() const { return _text; }
void destroy() noexcept;
~TextData() = default;
@@ -41,7 +42,7 @@ namespace mlx
private:
C_VBO _vbo;
C_IBO _ibo;
std::string _text;
};
class TextLibrary

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */
/* Updated: 2023/04/11 23:27:45 by maldavid ### ########.fr */
/* Updated: 2023/04/12 13:21:42 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -23,7 +23,12 @@
namespace mlx
{
TextDrawData::TextDrawData(std::string text, int _color, int _x, int _y, TextLibrary& library, std::array<stbtt_bakedchar, 96>& cdata) : x(_x), y(_y), color(_color)
TextDrawData::TextDrawData(std::string _text, int _color, int _x, int _y) :
x(_x), y(_y), color(_color),
text(std::move(_text))
{}
void TextDrawData::init(TextLibrary& library, std::array<stbtt_bakedchar, 96>& cdata) noexcept
{
std::vector<Vertex> vertexData;
std::vector<uint16_t> indexData;
@@ -53,9 +58,8 @@ namespace mlx
indexData.emplace_back(index + 3);
indexData.emplace_back(index + 0);
}
std::shared_ptr<TextData> text_data = std::make_shared<TextData>();
text_data->init(std::move(vertexData), std::move(indexData));
text_data->init(text, std::move(vertexData), std::move(indexData));
id = library.addTextToLibrary(text_data);
}
@@ -78,7 +82,9 @@ namespace mlx
void TextPutPipeline::put(int x, int y, int color, std::string str)
{
_drawlist.emplace(std::move(str), color, x, y, _library, _cdata);
auto res = _drawlist.emplace(std::move(str), color, x, y);
if(res.second)
const_cast<TextDrawData&>(*res.first).init(_library, _cdata);
}
void TextPutPipeline::render()

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/06 16:24:11 by maldavid #+# #+# */
/* Updated: 2023/04/11 18:36:53 by maldavid ### ########.fr */
/* Updated: 2023/04/12 13:25:33 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -29,9 +29,11 @@ namespace mlx
int x;
int y;
int color;
std::string text;
TextDrawData(std::string text, int _color, int _x, int _y, TextLibrary& library, std::array<stbtt_bakedchar, 96>& cdata);
bool operator==(const TextDrawData& rhs) const { return id == rhs.id && x == rhs.x && y == rhs.y && color == rhs.color; }
TextDrawData(std::string text, int _color, int _x, int _y);
void init(TextLibrary& library, std::array<stbtt_bakedchar, 96>& cdata) noexcept;
bool operator==(const TextDrawData& rhs) const { return text == rhs.text && x == rhs.x && y == rhs.y && color == rhs.color; }
};
}
@@ -40,9 +42,9 @@ namespace std
template <>
struct hash<mlx::TextDrawData>
{
size_t operator()(const mlx::TextDrawData& d) const noexcept
std::size_t operator()(const mlx::TextDrawData& d) const noexcept
{
return std::hash<mlx::TextID>()(d.id) + std::hash<int>()(d.x) + std::hash<int>()(d.y) + std::hash<int>()(d.color);
return std::hash<std::string>()(d.text) + std::hash<int>()(d.x) + std::hash<int>()(d.y) + std::hash<int>()(d.color);
}
};
}

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/01 14:24:00 by maldavid #+# #+# */
/* Updated: 2023/04/11 18:30:30 by maldavid ### ########.fr */
/* Updated: 2023/04/12 13:26:59 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -27,7 +27,7 @@ namespace mlx
{
auto it = std::find_if(_cache.begin(), _cache.end(), [=](const std::pair<TextureID, std::shared_ptr<Texture>>& v)
{
return v.second == texture;
return v.second.get() == texture.get();
});
if(it != _cache.end())
return it->first;