From 534bea942c1bd65220a12f91c8a12ec2a4f89573 Mon Sep 17 00:00:00 2001 From: kbz_8 Date: Sat, 1 Apr 2023 17:51:29 +0200 Subject: [PATCH] adding clear window --- README.md | 2 +- includes/mlx.h | 8 +++++--- src/core/application.cpp | 9 +++------ src/core/application.h | 18 +++++++++++++----- src/core/bridge.cpp | 14 +++++++++++--- src/platform/inputs.cpp | 2 +- src/platform/window.cpp | 7 ++++++- src/platform/window.h | 3 ++- src/renderer/images/vk_image.cpp | 3 ++- src/renderer/pixel_put.cpp | 10 +++++++++- src/renderer/pixel_put.h | 4 +++- src/renderer/renderer.cpp | 2 +- test/main.c | 6 +++++- 13 files changed, 62 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 79798ad..d1ad42e 100644 --- a/README.md +++ b/README.md @@ -30,5 +30,5 @@ For Arch based distros 3. Compile your project ```bash -gcc myApp.c MacroLibX/libmlx.so -lSDL2 +clang myApp.c MacroLibX/libmlx.so -lSDL2 ``` diff --git a/includes/mlx.h b/includes/mlx.h index 871f9a5..d64434a 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/01 13:50:52 by maldavid ### ########.fr */ +/* Updated: 2023/04/01 17:25:25 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,8 +26,8 @@ int mlx_loop_end(void* mlx); int mlx_mouse_show(); int mlx_mouse_hide(); -int mlx_mouse_move(void* win_ptr, int x, int y); -int mlx_mouse_get_pos(void* win_ptr, int* x, int* y); +int mlx_mouse_move(void* mlx, void* win_ptr, int x, int y); +int mlx_mouse_get_pos(void* mlx, int* x, int* y); int mlx_pixel_put(void* mlx, void* win_ptr, int x, int y, int color); @@ -36,6 +36,8 @@ int mlx_destroy_image(void* mlx_ptr, void* img_ptr); void* mlx_png_file_to_image(void* mlx_ptr, char* filename, int* width, int* height); +int mlx_clear_window(void* mlx_ptr, void* win_ptr); + int mlx_destroy_window(void* mlx, void* win_ptr); int mlx_destroy_display(void* mlx); diff --git a/src/core/application.cpp b/src/core/application.cpp index 0015b9f..840f31e 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -6,12 +6,13 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */ -/* Updated: 2023/04/01 15:34:00 by maldavid ### ########.fr */ +/* Updated: 2023/04/01 16:03:45 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #include "application.h" #include +#include namespace mlx::core { @@ -47,12 +48,8 @@ namespace mlx::core void Application::destroy_texture(void* ptr) { + vkDeviceWaitIdle(Render_Core::get().getDevice().get()); TextureID id = *static_cast(ptr); _texture_lib.removeTextureFromLibrary(id); } - - Application::~Application() - { - _texture_lib.clearLibrary(); - } } diff --git a/src/core/application.h b/src/core/application.h index 86484d6..2d98d0d 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/01 14:46:14 by maldavid ### ########.fr */ +/* Updated: 2023/04/01 17:26:10 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,10 +38,17 @@ namespace mlx::core return static_cast(&_wins.back()->get_id()); } - inline int get_mouse_pos(void* win_ptr, int* x, int* y) noexcept + inline void get_mouse_pos(int* x, int* y) noexcept { - if(*static_cast(win_ptr) > _wins.size()) - return -1; + *x = _in.getX(); + *y = _in.getY(); + } + + inline void mouse_move(void* win_ptr, int x, int y) noexcept + { + SDL_WarpMouseInWindow(_wins[*static_cast(win_ptr)]->getNativeWindow(), x, y); + SDL_PumpEvents(); + SDL_FlushEvent(SDL_MOUSEMOTION); } inline void loop_hook(int (*f)(void*), void* param) { _loop_hook = f; _param = param; } @@ -53,11 +60,12 @@ namespace mlx::core void texture_put(void* win, void* img, int x, int y); void destroy_texture(void* ptr); + inline void clear_window(void* win_ptr) { _wins[*static_cast(win_ptr)]->clear(); } inline void destroy_window(void* win_ptr) { _wins[*static_cast(win_ptr)].reset(); } void run() noexcept; - ~Application(); + ~Application() = default; private: Input _in; diff --git a/src/core/bridge.cpp b/src/core/bridge.cpp index 85ec142..171efd1 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/01 15:32:57 by maldavid ### ########.fr */ +/* Updated: 2023/04/01 17:48:21 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,13 +56,15 @@ extern "C" return SDL_ShowCursor(SDL_DISABLE); } - int mlx_mouse_move(void* win_ptr, int x, int y) + int mlx_mouse_move(void* mlx, void* win_ptr, int x, int y) { + static_cast(mlx)->mouse_move(win_ptr, x, y); return 0; } - int mlx_mouse_get_pos(void* win_ptr, int* x, int* y) + int mlx_mouse_get_pos(void* mlx, int* x, int* y) { + static_cast(mlx)->get_mouse_pos(x, y); return 0; } @@ -89,6 +91,12 @@ extern "C" return 0; } + int mlx_clear_window(void* mlx_ptr, void* win_ptr) + { + static_cast(mlx_ptr)->clear_window(win_ptr); + return 0; + } + int mlx_destroy_window(void* mlx, void* win_ptr) { static_cast(mlx)->destroy_window(win_ptr); diff --git a/src/platform/inputs.cpp b/src/platform/inputs.cpp index 88f3b8b..fe36f10 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: 2022/12/18 23:09:55 by maldavid ### ########.fr */ +/* Updated: 2023/04/01 16:46:13 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/platform/window.cpp b/src/platform/window.cpp index dd29fb8..53a9ad3 100644 --- a/src/platform/window.cpp +++ b/src/platform/window.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:36:44 by maldavid #+# #+# */ -/* Updated: 2023/04/01 15:40:30 by maldavid ### ########.fr */ +/* Updated: 2023/04/01 17:50:22 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,6 +57,11 @@ namespace mlx texture->render(*_renderer, x, y); } + void MLX_Window::clear() + { + _renderer->getPixelPutPipeline().clear(); + } + void MLX_Window::endFrame() { auto cmd_buff = _renderer->getActiveCmdBuffer().get(); diff --git a/src/platform/window.h b/src/platform/window.h index 4a675bb..67b33e6 100644 --- a/src/platform/window.h +++ b/src/platform/window.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 21:53:12 by maldavid #+# #+# */ -/* Updated: 2023/04/01 15:27:51 by maldavid ### ########.fr */ +/* Updated: 2023/04/01 17:26:18 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,6 +31,7 @@ namespace mlx inline SDL_Window* getNativeWindow() const noexcept { return _win; } bool beginFrame(); + void clear(); void endFrame(); void pixel_put(int x, int y, int color); diff --git a/src/renderer/images/vk_image.cpp b/src/renderer/images/vk_image.cpp index 38f22f5..369c4df 100644 --- a/src/renderer/images/vk_image.cpp +++ b/src/renderer/images/vk_image.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/01/25 11:59:07 by maldavid #+# #+# */ -/* Updated: 2023/03/31 12:26:08 by maldavid ### ########.fr */ +/* Updated: 2023/04/01 16:05:03 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,7 @@ namespace mlx return format; } core::error::report(e_kind::fatal_error, "Vulkan : failed to find image format"); + return VK_FORMAT_R8G8B8A8_UNORM; // to avoid warning; } void Image::create(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties) diff --git a/src/renderer/pixel_put.cpp b/src/renderer/pixel_put.cpp index 155e8e5..cb860f3 100644 --- a/src/renderer/pixel_put.cpp +++ b/src/renderer/pixel_put.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/31 15:14:50 by maldavid #+# #+# */ -/* Updated: 2023/04/01 15:32:23 by maldavid ### ########.fr */ +/* Updated: 2023/04/01 17:30:54 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,6 +53,14 @@ namespace mlx *reinterpret_cast(mem) = color; } + void PixelPutPipeline::clear() + { + if(!_impl->buffer.isMapped()) + _impl->buffer.mapMem(&_impl->map); + unsigned char* mem = static_cast(_impl->map); + std::memset(mem, 0, sizeof(uint32_t) * (_impl->width * _impl->height)); + } + void PixelPutPipeline::present() noexcept { if(_impl->buffer.isMapped()) diff --git a/src/renderer/pixel_put.h b/src/renderer/pixel_put.h index 54116f5..c9a875c 100644 --- a/src/renderer/pixel_put.h +++ b/src/renderer/pixel_put.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/31 13:18:50 by maldavid #+# #+# */ -/* Updated: 2023/04/01 15:32:06 by maldavid ### ########.fr */ +/* Updated: 2023/04/01 17:26:46 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,6 +29,8 @@ namespace mlx void render(class Renderer& renderer) noexcept; VkDescriptorSet getDescriptorSet() noexcept; + void clear(); + void destroy() noexcept; ~PixelPutPipeline(); diff --git a/src/renderer/renderer.cpp b/src/renderer/renderer.cpp index 99addd7..54a61bb 100644 --- a/src/renderer/renderer.cpp +++ b/src/renderer/renderer.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/12/18 17:25:16 by maldavid #+# #+# */ -/* Updated: 2023/03/31 18:54:23 by maldavid ### ########.fr */ +/* Updated: 2023/04/01 17:50:28 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/test/main.c b/test/main.c index 640f8f7..445737f 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/01 13:49:21 by maldavid ### ########.fr */ +/* Updated: 2023/04/01 17:50:46 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,6 +34,8 @@ int update(t_mlx *mlx) j++; } i++; + if (i == 5000) + mlx_clear_window(mlx->mlx, mlx->win); if (i > 10000) mlx_loop_end(mlx->mlx); return (0); @@ -48,8 +50,10 @@ int main(void) mlx.mlx = mlx_init(); mlx.win = mlx_new_window(mlx.mlx, 400, 400, "My window"); 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_loop_hook(mlx.mlx, update, &mlx); mlx_loop(mlx.mlx); + mlx_destroy_image(mlx.mlx, mlx.logo); mlx_destroy_window(mlx.mlx, mlx.win); mlx_destroy_display(mlx.mlx); return (0);