adding clear window

This commit is contained in:
2023-04-01 17:51:29 +02:00
parent 28850a6cb8
commit 7eea6ea1d5
13 changed files with 62 additions and 26 deletions

View File

@@ -30,5 +30,5 @@ For Arch based distros
3. Compile your project 3. Compile your project
```bash ```bash
gcc myApp.c MacroLibX/libmlx.so -lSDL2 clang myApp.c MacroLibX/libmlx.so -lSDL2
``` ```

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 16:56:35 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_show();
int mlx_mouse_hide(); int mlx_mouse_hide();
int mlx_mouse_move(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* 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); 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); 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_window(void* mlx, void* win_ptr);
int mlx_destroy_display(void* mlx); int mlx_destroy_display(void* mlx);

View File

@@ -6,12 +6,13 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 22:10:52 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 "application.h"
#include <renderer/images/texture.h> #include <renderer/images/texture.h>
#include <renderer/core/render_core.h>
namespace mlx::core namespace mlx::core
{ {
@@ -47,12 +48,8 @@ namespace mlx::core
void Application::destroy_texture(void* ptr) void Application::destroy_texture(void* ptr)
{ {
vkDeviceWaitIdle(Render_Core::get().getDevice().get());
TextureID id = *static_cast<TextureID*>(ptr); TextureID id = *static_cast<TextureID*>(ptr);
_texture_lib.removeTextureFromLibrary(id); _texture_lib.removeTextureFromLibrary(id);
} }
Application::~Application()
{
_texture_lib.clearLibrary();
}
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:49:46 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<void*>(&_wins.back()->get_id()); return static_cast<void*>(&_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<int*>(win_ptr) > _wins.size()) *x = _in.getX();
return -1; *y = _in.getY();
}
inline void mouse_move(void* win_ptr, int x, int y) noexcept
{
SDL_WarpMouseInWindow(_wins[*static_cast<int*>(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; } 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 texture_put(void* win, void* img, int x, int y);
void destroy_texture(void* ptr); void destroy_texture(void* ptr);
inline void clear_window(void* win_ptr) { _wins[*static_cast<int*>(win_ptr)]->clear(); }
inline void destroy_window(void* win_ptr) { _wins[*static_cast<int*>(win_ptr)].reset(); } inline void destroy_window(void* win_ptr) { _wins[*static_cast<int*>(win_ptr)].reset(); }
void run() noexcept; void run() noexcept;
~Application(); ~Application() = default;
private: private:
Input _in; Input _in;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:35:20 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); 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::core::Application*>(mlx)->mouse_move(win_ptr, x, y);
return 0; 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::core::Application*>(mlx)->get_mouse_pos(x, y);
return 0; return 0;
} }
@@ -89,6 +91,12 @@ extern "C"
return 0; return 0;
} }
int mlx_clear_window(void* mlx_ptr, void* win_ptr)
{
static_cast<mlx::core::Application*>(mlx_ptr)->clear_window(win_ptr);
return 0;
}
int mlx_destroy_window(void* mlx, void* win_ptr) int mlx_destroy_window(void* mlx, void* win_ptr)
{ {
static_cast<mlx::core::Application*>(mlx)->destroy_window(win_ptr); static_cast<mlx::core::Application*>(mlx)->destroy_window(win_ptr);

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:30:19 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:36:44 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); texture->render(*_renderer, x, y);
} }
void MLX_Window::clear()
{
_renderer->getPixelPutPipeline().clear();
}
void MLX_Window::endFrame() void MLX_Window::endFrame()
{ {
auto cmd_buff = _renderer->getActiveCmdBuffer().get(); auto cmd_buff = _renderer->getActiveCmdBuffer().get();

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:53:12 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; } inline SDL_Window* getNativeWindow() const noexcept { return _win; }
bool beginFrame(); bool beginFrame();
void clear();
void endFrame(); void endFrame();
void pixel_put(int x, int y, int color); void pixel_put(int x, int y, int color);

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/25 11:59:07 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; return format;
} }
core::error::report(e_kind::fatal_error, "Vulkan : failed to find image 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) void Image::create(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties)

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/31 15:14:50 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<uint32_t*>(mem) = color; *reinterpret_cast<uint32_t*>(mem) = color;
} }
void PixelPutPipeline::clear()
{
if(!_impl->buffer.isMapped())
_impl->buffer.mapMem(&_impl->map);
unsigned char* mem = static_cast<unsigned char*>(_impl->map);
std::memset(mem, 0, sizeof(uint32_t) * (_impl->width * _impl->height));
}
void PixelPutPipeline::present() noexcept void PixelPutPipeline::present() noexcept
{ {
if(_impl->buffer.isMapped()) if(_impl->buffer.isMapped())

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/31 13:18:50 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; void render(class Renderer& renderer) noexcept;
VkDescriptorSet getDescriptorSet() noexcept; VkDescriptorSet getDescriptorSet() noexcept;
void clear();
void destroy() noexcept; void destroy() noexcept;
~PixelPutPipeline(); ~PixelPutPipeline();

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/18 17:25:16 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:55:21 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++; j++;
} }
i++; i++;
if (i == 5000)
mlx_clear_window(mlx->mlx, mlx->win);
if (i > 10000) if (i > 10000)
mlx_loop_end(mlx->mlx); mlx_loop_end(mlx->mlx);
return (0); return (0);
@@ -48,8 +50,10 @@ int main(void)
mlx.mlx = mlx_init(); mlx.mlx = mlx_init();
mlx.win = mlx_new_window(mlx.mlx, 400, 400, "My window"); 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.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_hook(mlx.mlx, update, &mlx);
mlx_loop(mlx.mlx); mlx_loop(mlx.mlx);
mlx_destroy_image(mlx.mlx, mlx.logo);
mlx_destroy_window(mlx.mlx, mlx.win); mlx_destroy_window(mlx.mlx, mlx.win);
mlx_destroy_display(mlx.mlx); mlx_destroy_display(mlx.mlx);
return (0); return (0);