diff --git a/includes/mlx.h b/includes/mlx.h index fc5616d..ec3d2f4 100644 --- a/includes/mlx.h +++ b/includes/mlx.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */ -/* Updated: 2023/04/11 20:31:38 by maldavid ### ########.fr */ +/* Updated: 2023/04/12 11:12:00 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,15 @@ extern "C" { #endif +typedef enum +{ + MLX_KEYDOWN = 0, + MLX_KEYUP = 1, + MLX_MOUSEDOWN = 2, + MLX_MOUSEUP = 3, + MLX_WINDOW_EVENT = 4 +} mlx_event_type; + void* mlx_init(); void* mlx_new_window(void* mlx, int w, int h, const char* title); @@ -29,9 +38,7 @@ int mlx_mouse_hide(); int mlx_mouse_move(void* mlx, void* win, int x, int y); int mlx_mouse_get_pos(void* mlx, int* x, int* y); -int mlx_mouse_hook(void* mlx, int (*funct_ptr)(), void* param); -int mlx_key_hook(void* mlx, int (*funct_ptr)(), void* param); -int mlx_expose_hook(void* mlx, int (*funct_ptr)(), void* param); +int mlx_on_event(void* mlx, mlx_event_type event, int (*f)(), void* param); int mlx_do_key_autorepeaton(void* mlx); int mlx_do_key_autorepeatoff(void* mlx); diff --git a/src/core/application.cpp b/src/core/application.cpp index c699100..ae8ca9e 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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(); diff --git a/src/core/application.h b/src/core/application.h index b1e3cb3..5e0ea13 100644 --- a/src/core/application.h +++ b/src/core/application.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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()) {} 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 _texture_ids; std::vector> _graphics; std::function _loop_hook; + std::unique_ptr _in; void* _param = nullptr; bool _is_loop_running = false; }; diff --git a/src/core/application.inl b/src/core/application.inl index 69c1bf7..3b9eadb 100644 --- a/src/core/application.inl +++ b/src/core/application.inl @@ -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(); } } diff --git a/src/core/bridge.cpp b/src/core/bridge.cpp index 643f40b..ab0c6af 100644 --- a/src/core/bridge.cpp +++ b/src/core/bridge.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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)->mouseHook(funct_ptr, param); - return 0; - } - - int mlx_key_hook(void* mlx, int (*funct_ptr)(const char*, void*), void* param) - { - static_cast(mlx)->keyHook(funct_ptr, param); - return 0; - } - - int mlx_expose_hook(void* mlx, int (*funct_ptr)(const char*, void*), void* param) - { - static_cast(mlx)->exposeHook(funct_ptr, param); + static_cast(mlx)->onEvent(event, funct_ptr, param); return 0; } diff --git a/src/platform/inputs.cpp b/src/platform/inputs.cpp index cd7fafc..f12dc18 100644 --- a/src/platform/inputs.cpp +++ b/src/platform/inputs.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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(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(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(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(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; } diff --git a/src/platform/inputs.h b/src/platform/inputs.h index 6de50e6..ba0bbac 100644 --- a/src/platform/inputs.h +++ b/src/platform/inputs.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 hook; + std::function 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 _keys; SDL_Event _event; std::array _mouse; + std::vector _window; - Hook _mouse_hook; - Hook _key_hook; - Hook _expose_hook; + std::array _events_hooks; int _x = 0; int _y = 0; diff --git a/src/renderer/text_library.cpp b/src/renderer/text_library.cpp index 432149e..9c94610 100644 --- a/src/renderer/text_library.cpp +++ b/src/renderer/text_library.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 #include #include +#include namespace mlx { - void TextData::init(std::vector vbo_data, std::vector ibo_data) + void TextData::init(std::string text, std::vector vbo_data, std::vector 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>& v) { - return v.second == text; + return v.second->getText() == text->getText(); }); if(it != _cache.end()) return it->first; diff --git a/src/renderer/text_library.h b/src/renderer/text_library.h index 76ac73f..fd1079f 100644 --- a/src/renderer/text_library.h +++ b/src/renderer/text_library.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 vbo_data, std::vector ibo_data); + void init(std::string text, std::vector vbo_data, std::vector 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 diff --git a/src/renderer/text_pipeline.cpp b/src/renderer/text_pipeline.cpp index a6c303b..35458ec 100644 --- a/src/renderer/text_pipeline.cpp +++ b/src/renderer/text_pipeline.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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& 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& cdata) noexcept { std::vector vertexData; std::vector indexData; @@ -53,9 +58,8 @@ namespace mlx indexData.emplace_back(index + 3); indexData.emplace_back(index + 0); } - std::shared_ptr text_data = std::make_shared(); - 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(*res.first).init(_library, _cdata); } void TextPutPipeline::render() diff --git a/src/renderer/text_pipeline.h b/src/renderer/text_pipeline.h index b2f55c0..ea6c57c 100644 --- a/src/renderer/text_pipeline.h +++ b/src/renderer/text_pipeline.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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& 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& 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 { - size_t operator()(const mlx::TextDrawData& d) const noexcept + std::size_t operator()(const mlx::TextDrawData& d) const noexcept { - return std::hash()(d.id) + std::hash()(d.x) + std::hash()(d.y) + std::hash()(d.color); + return std::hash()(d.text) + std::hash()(d.x) + std::hash()(d.y) + std::hash()(d.color); } }; } diff --git a/src/renderer/texture_library.cpp b/src/renderer/texture_library.cpp index dd0d9e4..751d135 100644 --- a/src/renderer/texture_library.cpp +++ b/src/renderer/texture_library.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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>& v) { - return v.second == texture; + return v.second.get() == texture.get(); }); if(it != _cache.end()) return it->first; diff --git a/test/main.c b/test/main.c index a1a235c..e3463bd 100644 --- a/test/main.c +++ b/test/main.c @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */ -/* Updated: 2023/04/11 23:33:03 by maldavid ### ########.fr */ +/* Updated: 2023/04/12 13:44:38 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ int update(t_mlx *mlx) int j; mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo, 100, 100); + mlx_string_put(mlx->mlx, mlx->win, 20, 50, 0xFFFFFFFF, "that's a text"); j = 0; while (j < 400) { @@ -36,8 +37,6 @@ int update(t_mlx *mlx) i++; if (i == 5000) mlx_clear_window(mlx->mlx, mlx->win); - //if (i > 10000) - // mlx_loop_end(mlx->mlx); return (0); } @@ -65,10 +64,17 @@ void *create_image(t_mlx *mlx) return (img); } -int key_hook(const char *key, void *param) +int key_hook(int key, t_mlx *param) +{ + if (key == 41) + mlx_loop_end(param->mlx); + return (0); +} + +int window_hook(int event, t_mlx *param) { (void)param; - puts(key); + printf("%d\n", event); return (0); } @@ -81,13 +87,13 @@ int main(void) mlx.mlx = mlx_init(); mlx.win = mlx_new_window(mlx.mlx, 400, 400, "My window"); - mlx_key_hook(mlx.mlx, key_hook, NULL); + mlx_on_event(mlx.mlx, MLX_KEYDOWN, key_hook, &mlx); mlx.logo = mlx_png_file_to_image(mlx.mlx, "42_logo.png", &w, &h); mlx_pixel_put(mlx.mlx, mlx.win, 200, 10, 0xFFFF00FF); mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo, 200, 200); img = create_image(&mlx); - mlx_string_put(mlx.mlx, mlx.win, 20, 20, 0xFFFF2000, "this is a text"); - mlx_string_put(mlx.mlx, mlx.win, 20, 50, 0xFFFFFFFF, "that's another text"); + mlx_string_put(mlx.mlx, mlx.win, 20, 20, 0xFFFF2000, \ + "that text will disappear"); mlx_put_image_to_window(mlx.mlx, mlx.win, img, 200, 20); mlx_loop_hook(mlx.mlx, update, &mlx); mlx_loop(mlx.mlx);