diff --git a/includes/mlx.h b/includes/mlx.h index b12abde..fc5616d 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 18:37:29 by maldavid ### ########.fr */ +/* Updated: 2023/04/11 20:31:38 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,10 @@ 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_do_key_autorepeaton(void* mlx); int mlx_do_key_autorepeatoff(void* mlx); diff --git a/src/core/application.h b/src/core/application.h index bc63b78..b1e3cb3 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 18:37:05 by maldavid ### ########.fr */ +/* Updated: 2023/04/11 21:43:13 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,6 +37,10 @@ namespace mlx::core 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 constexpr void enableAutoRepeat() noexcept; inline constexpr void disableAutoRepeat() noexcept; diff --git a/src/core/application.inl b/src/core/application.inl index c917782..69c1bf7 100644 --- a/src/core/application.inl +++ b/src/core/application.inl @@ -27,6 +27,21 @@ namespace mlx::core SDL_FlushEvent(SDL_MOUSEMOTION); } + void Application::mouseHook(int (*funct_ptr)(const char*, 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); + } + constexpr void Application::enableAutoRepeat() noexcept { _in.enableAutoRepeat(); diff --git a/src/core/bridge.cpp b/src/core/bridge.cpp index eb99721..643f40b 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/06 16:09:47 by maldavid ### ########.fr */ +/* Updated: 2023/04/11 21:42:56 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,6 +69,24 @@ extern "C" return 0; } + int mlx_mouse_hook(void* mlx, int (*funct_ptr)(const char*, 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); + return 0; + } + int mlx_do_key_autorepeaton(void* mlx) { static_cast(mlx)->enableAutoRepeat(); diff --git a/src/platform/inputs.cpp b/src/platform/inputs.cpp index fac051b..cd7fafc 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/03 12:40:10 by maldavid ### ########.fr */ +/* Updated: 2023/04/11 21:32:43 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,10 +36,33 @@ namespace mlx switch(_event.type) { - case SDL_KEYDOWN: _keys[_event.key.keysym.scancode] = static_cast(action::down); break; - case SDL_KEYUP: _keys[_event.key.keysym.scancode] = static_cast(action::up); break; - case SDL_MOUSEBUTTONDOWN: _mouse[_event.button.button] = static_cast(action::down); break; - case SDL_MOUSEBUTTONUP: _mouse[_event.button.button] = static_cast(action::up); break; + case SDL_KEYDOWN: + { + _keys[_event.key.keysym.scancode] = static_cast(action::down); + 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); + break; + } + + case SDL_MOUSEBUTTONDOWN: + { + _mouse[_event.button.button] = static_cast(action::down); + 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); + break; + } default: break; } diff --git a/src/platform/inputs.h b/src/platform/inputs.h index dde699a..6de50e6 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/03 14:19:11 by maldavid ### ########.fr */ +/* Updated: 2023/04/11 21:41:33 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #include #include #include +#include #include #include "window.h" @@ -22,6 +23,12 @@ namespace mlx { enum class action : uint8_t { up = (1 << 1), down = (1 << 2) }; + struct Hook + { + std::function hook; + void* param = nullptr; + }; + class Input { public: @@ -46,13 +53,21 @@ 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; } + ~Input() = default; private: - SDL_Event _event; std::array _keys; + SDL_Event _event; std::array _mouse; + Hook _mouse_hook; + Hook _key_hook; + Hook _expose_hook; + int _x = 0; int _y = 0; int _xRel = 0; diff --git a/src/renderer/text_pipeline.cpp b/src/renderer/text_pipeline.cpp index 2e93078..a6c303b 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 19:16:08 by maldavid ### ########.fr */ +/* Updated: 2023/04/11 23:27:45 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,8 +23,6 @@ namespace mlx { - uint8_t tmp_bitmap[512 * 512]; - TextDrawData::TextDrawData(std::string text, int _color, int _x, int _y, TextLibrary& library, std::array& cdata) : x(_x), y(_y), color(_color) { std::vector vertexData; @@ -64,8 +62,17 @@ namespace mlx void TextPutPipeline::init(Renderer* renderer) noexcept { _renderer = renderer; + uint8_t tmp_bitmap[512 * 512]; + uint8_t vulkan_bitmap[(512 * 512) * 4]; stbtt_BakeFontBitmap(dogica_ttf, 0, 6.0f, tmp_bitmap, 512, 512, 32, 96, _cdata.data()); - _atlas.create(tmp_bitmap, 512, 512, VK_FORMAT_R8_UNORM); + for(int i = 0, j = 0; i < 512 * 512; i++, j += 4) + { + vulkan_bitmap[j + 0] = tmp_bitmap[i]; + vulkan_bitmap[j + 1] = tmp_bitmap[i]; + vulkan_bitmap[j + 2] = tmp_bitmap[i]; + vulkan_bitmap[j + 3] = tmp_bitmap[i]; + } + _atlas.create(vulkan_bitmap, 512, 512, VK_FORMAT_R8G8B8A8_UNORM); _atlas.setDescriptor(renderer->getFragDescriptorSet().duplicate()); } diff --git a/test/main.c b/test/main.c index ab28ece..a1a235c 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 19:15:35 by maldavid ### ########.fr */ +/* Updated: 2023/04/11 23:33:03 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,8 +36,8 @@ int update(t_mlx *mlx) i++; if (i == 5000) mlx_clear_window(mlx->mlx, mlx->win); - if (i > 10000) - mlx_loop_end(mlx->mlx); + //if (i > 10000) + // mlx_loop_end(mlx->mlx); return (0); } @@ -65,6 +65,13 @@ void *create_image(t_mlx *mlx) return (img); } +int key_hook(const char *key, void *param) +{ + (void)param; + puts(key); + return (0); +} + int main(void) { t_mlx mlx; @@ -74,12 +81,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.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, 0xFF0000FF, "this is a text"); - mlx_string_put(mlx.mlx, mlx.win, 20, 50, 0xFFFFFFFF, "this is another text"); + 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_put_image_to_window(mlx.mlx, mlx.win, img, 200, 20); mlx_loop_hook(mlx.mlx, update, &mlx); mlx_loop(mlx.mlx);