From aaf7e861d5954655626581fd193d6467ea1f9ce8 Mon Sep 17 00:00:00 2001 From: kbz_8 Date: Sun, 2 Apr 2023 17:37:38 +0200 Subject: [PATCH] adding graphics support class, refactoring the code --- src/core/application.cpp | 22 ++++------- src/core/application.h | 47 +++++++++-------------- src/core/application.inl | 67 +++++++++++++++++++++++++++++++++ src/core/bridge.cpp | 24 ++++++------ src/core/graphics.cpp | 64 +++++++++++++++++++++++++++++++ src/core/graphics.h | 59 +++++++++++++++++++++++++++++ src/core/graphics.inl | 43 +++++++++++++++++++++ src/platform/window.cpp | 62 ++---------------------------- src/platform/window.h | 21 ++--------- src/renderer/images/texture.cpp | 6 +-- src/renderer/images/texture.h | 33 ++++++++++++++-- src/renderer/renderer.cpp | 3 +- src/renderer/renderer.h | 6 +-- test/main.c | 9 +++-- 14 files changed, 318 insertions(+), 148 deletions(-) create mode 100644 src/core/application.inl create mode 100644 src/core/graphics.cpp create mode 100644 src/core/graphics.h create mode 100644 src/core/graphics.inl diff --git a/src/core/application.cpp b/src/core/application.cpp index 840f31e..cf7f9b2 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/01 16:03:45 by maldavid ### ########.fr */ +/* Updated: 2023/04/02 15:11:53 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,32 +21,26 @@ namespace mlx::core while(_in.is_running()) { _in.update(); - for(auto win : _wins) - win->beginFrame(); + for(auto& gs : _graphics) + gs->beginRender(); if(_loop_hook) _loop_hook(_param); - for(auto win : _wins) - win->endFrame(); + for(auto& gs : _graphics) + gs->endRender(); } } - void* Application::new_stb_texture(char* file, int* w, int* h) + void* Application::newStbTexture(char* file, int* w, int* h) { - std::shared_ptr texture = std::make_shared(stb_texture_load(file, w, h)); + std::shared_ptr texture = std::make_shared(stbTextureLoad(file, w, h)); TextureID id = _texture_lib.addTextureToLibrary(texture); _texture_ids.push_back(id); return &_texture_ids.back(); } - void Application::texture_put(void* win, void* img, int x, int y) - { - std::shared_ptr texture = _texture_lib.getTexture(*static_cast(img)); - _wins[*static_cast(win)]->texture_put(texture, x, y); - } - - void Application::destroy_texture(void* ptr) + void Application::destroyTexture(void* ptr) { vkDeviceWaitIdle(Render_Core::get().getDevice().get()); TextureID id = *static_cast(ptr); diff --git a/src/core/application.h b/src/core/application.h index 2d98d0d..b5fa048 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 17:26:10 by maldavid ### ########.fr */ +/* Updated: 2023/04/02 15:38:47 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,10 +18,10 @@ #include #include -#include "errors.h" +#include +#include #include -#include #include @@ -32,37 +32,22 @@ namespace mlx::core public: Application() : _in() {} - inline void* new_window(std::size_t w, std::size_t h, std::string title) - { - _wins.emplace_back(std::make_shared(w, h, std::move(title), _wins.size())); - return static_cast(&_wins.back()->get_id()); - } + inline void getMousePos(int* x, int* y) noexcept; + inline void mouseMove(void* win_ptr, int x, int y) noexcept; - inline void get_mouse_pos(int* x, int* y) noexcept - { - *x = _in.getX(); - *y = _in.getY(); - } + inline void* newGraphicsSuport(std::size_t w, std::size_t h, std::string title); + inline void clearGraphicsSupport(void* win_ptr); + inline void destroyGraphicsSupport(void* win_ptr); - 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 pixelPut(void* win_ptr, int x, int y, int color) const noexcept; - inline void loop_hook(int (*f)(void*), void* param) { _loop_hook = f; _param = param; } - inline void loop_end() noexcept { _in.finish(); } + void* newStbTexture(char* file, int* w, int* h); // stb textures are format managed by stb image (png, jpg, bpm, ...) + inline void texturePut(void* win_ptr, void* img, int x, int y); + void destroyTexture(void* ptr); - inline void pixel_put(void* win_ptr, int x, int y, int color) const noexcept { _wins[*static_cast(win_ptr)]->pixel_put(x, y, color); } - - void* new_stb_texture(char* file, int* w, int* h); // stb textures are format managed by stb image (png, jpg, bpm, ...) - void texture_put(void* win, void* img, int x, int y); - void destroy_texture(void* ptr); + inline void loopHook(int (*f)(void*), void* param); + inline void loopEnd() noexcept; - 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() = default; @@ -71,11 +56,13 @@ namespace mlx::core Input _in; TextureLibrary _texture_lib; std::vector _texture_ids; - std::vector> _wins; + std::vector> _graphics; std::function _loop_hook; void* _param = nullptr; bool _is_loop_running = false; }; } +#include + #endif // __MLX_APPLICATION__ diff --git a/src/core/application.inl b/src/core/application.inl new file mode 100644 index 0000000..4dc0eea --- /dev/null +++ b/src/core/application.inl @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* application.inl :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */ +/* Updated: 2023/04/02 14:56:27 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +namespace mlx::core +{ + void Application::getMousePos(int* x, int* y) noexcept + { + *x = _in.getX(); + *y = _in.getY(); + } + + void Application::mouseMove(void* win_ptr, int x, int y) noexcept + { + SDL_WarpMouseInWindow(_graphics[*static_cast(win_ptr)]->getWindow()->getNativeWindow(), x, y); + SDL_PumpEvents(); + SDL_FlushEvent(SDL_MOUSEMOTION); + } + + void* Application::newGraphicsSuport(std::size_t w, std::size_t h, std::string title) + { + _graphics.emplace_back(std::make_unique(w, h, std::move(title), _graphics.size())); + return static_cast(&_graphics.back()->getID()); + } + + void Application::clearGraphicsSupport(void* win_ptr) + { + _graphics[*static_cast(win_ptr)]->clearRenderData(); + } + + void Application::destroyGraphicsSupport(void* win_ptr) + { + _graphics[*static_cast(win_ptr)].reset(); + } + + void Application::pixelPut(void* win_ptr, int x, int y, int color) const noexcept + { + _graphics[*static_cast(win_ptr)]->pixelPut(x, y, color); + } + + void Application::texturePut(void* win_ptr, void* img, int x, int y) + { + std::shared_ptr texture = _texture_lib.getTexture(*static_cast(img)); + _graphics[*static_cast(win_ptr)]->texturePut(texture, x, y); + } + + void Application::loopHook(int (*f)(void*), void* param) + { + _loop_hook = f; + _param = param; + } + + void Application::loopEnd() noexcept + { + _in.finish(); + } +} diff --git a/src/core/bridge.cpp b/src/core/bridge.cpp index 171efd1..053ee13 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 17:48:21 by maldavid ### ########.fr */ +/* Updated: 2023/04/02 15:10:44 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,12 +25,12 @@ extern "C" void* mlx_new_window(void* mlx, int w, int h, const char* title) { - return static_cast(mlx)->new_window(w, h, title); + return static_cast(mlx)->newGraphicsSuport(w, h, title); } int mlx_loop_hook(void* mlx, int (*f)(void*), void* param) { - static_cast(mlx)->loop_hook(f, param); + static_cast(mlx)->loopHook(f, param); return 0; } @@ -42,7 +42,7 @@ extern "C" int mlx_loop_end(void* mlx) { - static_cast(mlx)->loop_end(); + static_cast(mlx)->loopEnd(); return 0; } @@ -58,48 +58,48 @@ extern "C" int mlx_mouse_move(void* mlx, void* win_ptr, int x, int y) { - static_cast(mlx)->mouse_move(win_ptr, x, y); + static_cast(mlx)->mouseMove(win_ptr, x, y); return 0; } int mlx_mouse_get_pos(void* mlx, int* x, int* y) { - static_cast(mlx)->get_mouse_pos(x, y); + static_cast(mlx)->getMousePos(x, y); return 0; } int mlx_put_image_to_window(void* mlx_ptr, void* win_ptr, void* img_ptr, int x, int y) { - static_cast(mlx_ptr)->texture_put(win_ptr, img_ptr, x, y); + static_cast(mlx_ptr)->texturePut(win_ptr, img_ptr, x, y); return 0; } int mlx_destroy_image(void* mlx_ptr, void* img_ptr) { - static_cast(mlx_ptr)->destroy_texture(img_ptr); + static_cast(mlx_ptr)->destroyTexture(img_ptr); return 0; } void* mlx_png_file_to_image(void* mlx_ptr, char* filename, int* width, int* height) { - return static_cast(mlx_ptr)->new_stb_texture(filename, width, height); + return static_cast(mlx_ptr)->newStbTexture(filename, width, height); } int mlx_pixel_put(void* mlx, void* win_ptr, int x, int y, int color) { - static_cast(mlx)->pixel_put(win_ptr, x, y, color); + static_cast(mlx)->pixelPut(win_ptr, x, y, color); return 0; } int mlx_clear_window(void* mlx_ptr, void* win_ptr) { - static_cast(mlx_ptr)->clear_window(win_ptr); + static_cast(mlx_ptr)->clearGraphicsSupport(win_ptr); return 0; } int mlx_destroy_window(void* mlx, void* win_ptr) { - static_cast(mlx)->destroy_window(win_ptr); + static_cast(mlx)->destroyGraphicsSupport(win_ptr); return 0; } diff --git a/src/core/graphics.cpp b/src/core/graphics.cpp new file mode 100644 index 0000000..c689afd --- /dev/null +++ b/src/core/graphics.cpp @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* graphics.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */ +/* Updated: 2023/04/02 17:33:47 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +namespace mlx +{ + GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id) : + _window(std::make_shared(w, h, std::move(title))), + _renderer(std::make_unique()), + _id(id) + { + _renderer->setWindow(_window.get()); + _renderer->init(); + _pixel_put_pipeline.init(w, h, *_renderer); + } + + void GraphicsSupport::endRender() noexcept + { + auto cmd_buff = _renderer->getActiveCmdBuffer().get(); + + std::vector sets; + sets.push_back(_renderer->getVertDescriptorSet().get()); + + for(auto& data : _textures_to_render) + { + if(data.texture->getSet() == VK_NULL_HANDLE) + data.texture->setDescriptor(_renderer->getFragDescriptorSet().duplicate()); + if(!data.texture->hasBeenUpdated()) + data.texture->updateSet(0); + sets.push_back(data.texture->getSet()); + vkCmdBindDescriptorSets(cmd_buff, VK_PIPELINE_BIND_POINT_GRAPHICS, _renderer->getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr); + data.texture->render(*_renderer, data.x, data.y); + sets.pop_back(); + } + + _pixel_put_pipeline.present(); + + sets.push_back(_pixel_put_pipeline.getDescriptorSet()); + vkCmdBindDescriptorSets(cmd_buff, VK_PIPELINE_BIND_POINT_GRAPHICS, _renderer->getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr); + _pixel_put_pipeline.render(*_renderer); + + _renderer->endFrame(); + + for(auto& data : _textures_to_render) + data.texture->resetUpdate(); + } + + GraphicsSupport::~GraphicsSupport() + { + vkDeviceWaitIdle(Render_Core::get().getDevice().get()); + _pixel_put_pipeline.destroy(); + _renderer->destroy(); + } +} diff --git a/src/core/graphics.h b/src/core/graphics.h new file mode 100644 index 0000000..8c0d3cb --- /dev/null +++ b/src/core/graphics.h @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* graphics.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/02 14:49:49 by maldavid #+# #+# */ +/* Updated: 2023/04/02 15:36:49 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_GRAPHICS__ +#define __MLX_GRAPHICS__ + +#include +#include + +#include +#include + +#include +#include +#include +#include + +namespace mlx +{ + class GraphicsSupport : public non_copyable + { + public: + GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id); + + inline int& getID() noexcept; + inline std::shared_ptr getWindow(); + + inline void beginRender() noexcept; + + inline void clearRenderData() noexcept; + inline void pixelPut(int x, int y, int color) noexcept; + inline void texturePut(std::shared_ptr texture, int x, int y); + + void endRender() noexcept; + + ~GraphicsSupport(); + + private: + std::unordered_set _textures_to_render; + glm::mat4 _proj = glm::mat4(1.0); + std::shared_ptr _window; + PixelPutPipeline _pixel_put_pipeline; + std::unique_ptr _renderer; + int _id; + }; +} + +#include + +#endif diff --git a/src/core/graphics.inl b/src/core/graphics.inl new file mode 100644 index 0000000..4cc0abd --- /dev/null +++ b/src/core/graphics.inl @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* graphics.inl :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */ +/* Updated: 2023/04/02 15:26:16 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +namespace mlx +{ + int& GraphicsSupport::getID() noexcept { return _id; } + std::shared_ptr GraphicsSupport::getWindow() { return _window; } + + void GraphicsSupport::beginRender() noexcept + { + if(!_renderer->beginFrame()) + return; + _proj = glm::ortho(0, _window->getWidth(), 0, _window->getHeight()); + _renderer->getUniformBuffer()->setData(sizeof(_proj), &_proj); + } + + void GraphicsSupport::clearRenderData() noexcept + { + _textures_to_render.clear(); + _pixel_put_pipeline.clear(); + } + + void GraphicsSupport::pixelPut(int x, int y, int color) noexcept + { + _pixel_put_pipeline.setPixel(x, y, color); + } + + void GraphicsSupport::texturePut(std::shared_ptr texture, int x, int y) + { + _textures_to_render.emplace(texture, x, y); + } +} diff --git a/src/platform/window.cpp b/src/platform/window.cpp index 53a9ad3..34adf03 100644 --- a/src/platform/window.cpp +++ b/src/platform/window.cpp @@ -6,80 +6,24 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:36:44 by maldavid #+# #+# */ -/* Updated: 2023/04/01 17:50:22 by maldavid ### ########.fr */ +/* Updated: 2023/04/02 15:39:59 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ -#include "window.h" +#include #include -#include -#include - -#include -#include - namespace mlx { - MLX_Window::MLX_Window(std::size_t w, std::size_t h, std::string title, int id) : _id(id), _renderer(new Renderer), _width(w), _height(h) + MLX_Window::MLX_Window(std::size_t w, std::size_t h, std::string title) : _width(w), _height(h) { _win = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN); if(!_win) core::error::report(e_kind::fatal_error, std::string("unable to open a new window, ") + SDL_GetError()); - _renderer->setWindow(this); - _renderer->init(); - _renderer->getPixelPutPipeline().init(w, h, *_renderer); - } - - bool MLX_Window::beginFrame() - { - if(!_renderer->beginFrame()) - return false; - _proj = glm::ortho(0, _width, 0, _height); - _renderer->getUniformBuffer()->setData(sizeof(_proj), &_proj); - return true; - } - - void MLX_Window::pixel_put(int x, int y, int color) - { - _renderer->getPixelPutPipeline().setPixel(x, y, color); - } - - void MLX_Window::texture_put(std::shared_ptr texture, int x, int y) - { - if(texture->getSet() == VK_NULL_HANDLE) - texture->setDescriptor(_renderer->getFragDescriptorSet().duplicate()); - texture->updateSet(0); - std::vector sets; - sets.push_back(_renderer->getVertDescriptorSet().get()); - sets.push_back(texture->getSet()); - vkCmdBindDescriptorSets(_renderer->getActiveCmdBuffer().get(), VK_PIPELINE_BIND_POINT_GRAPHICS, _renderer->getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr); - texture->render(*_renderer, x, y); - } - - void MLX_Window::clear() - { - _renderer->getPixelPutPipeline().clear(); - } - - void MLX_Window::endFrame() - { - auto cmd_buff = _renderer->getActiveCmdBuffer().get(); - - _renderer->getPixelPutPipeline().present(); - - std::vector sets; - sets.push_back(_renderer->getVertDescriptorSet().get()); - sets.push_back(_renderer->getPixelPutPipeline().getDescriptorSet()); - vkCmdBindDescriptorSets(cmd_buff, VK_PIPELINE_BIND_POINT_GRAPHICS, _renderer->getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr); - _renderer->getPixelPutPipeline().render(*_renderer); - - _renderer->endFrame(); } MLX_Window::~MLX_Window() { - _renderer->destroy(); if(_win) SDL_DestroyWindow(_win); } diff --git a/src/platform/window.h b/src/platform/window.h index 67b33e6..07355cf 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 17:26:18 by maldavid ### ########.fr */ +/* Updated: 2023/04/02 15:39:53 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,37 +15,24 @@ #include #include -#include -#include -#include -#include namespace mlx { class MLX_Window { public: - MLX_Window(std::size_t w, std::size_t h, std::string title, int id); + MLX_Window(std::size_t w, std::size_t h, std::string title); - inline int& get_id() noexcept { return _id; } inline SDL_Window* getNativeWindow() const noexcept { return _win; } + inline int getWidth() const noexcept { return _width; } + inline int getHeight() const noexcept { return _height; } - bool beginFrame(); - void clear(); - void endFrame(); - - void pixel_put(int x, int y, int color); - void texture_put(std::shared_ptr texture, int x, int y); - ~MLX_Window(); private: - glm::mat4 _proj = glm::mat4(1.0); - std::unique_ptr _renderer; SDL_Window* _win = nullptr; int _width = 0; int _height = 0; - int _id; }; } diff --git a/src/renderer/images/texture.cpp b/src/renderer/images/texture.cpp index 02d4b49..a7fa89d 100644 --- a/src/renderer/images/texture.cpp +++ b/src/renderer/images/texture.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/31 18:03:35 by maldavid #+# #+# */ -/* Updated: 2023/04/01 15:39:59 by maldavid ### ########.fr */ +/* Updated: 2023/04/02 15:47:49 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,9 +55,9 @@ namespace mlx void Texture::render(Renderer& renderer, int x, int y) { + auto cmd = renderer.getActiveCmdBuffer().get(); _vbo.bind(renderer); _ibo.bind(renderer); - auto cmd = renderer.getActiveCmdBuffer().get(); glm::vec2 translate(x, y); vkCmdPushConstants(cmd, renderer.getPipeline().getPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(translate), &translate); vkCmdDrawIndexed(cmd, static_cast(_ibo.getSize() / sizeof(uint16_t)), 1, 0, 0, 0); @@ -70,7 +70,7 @@ namespace mlx _ibo.destroy(); } - Texture stb_texture_load(std::filesystem::path file, int* w, int* h) + Texture stbTextureLoad(std::filesystem::path file, int* w, int* h) { Texture texture; int channels; diff --git a/src/renderer/images/texture.h b/src/renderer/images/texture.h index d51e886..f8a241e 100644 --- a/src/renderer/images/texture.h +++ b/src/renderer/images/texture.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/08 02:24:58 by maldavid #+# #+# */ -/* Updated: 2023/04/01 15:31:26 by maldavid ### ########.fr */ +/* Updated: 2023/04/02 17:33:26 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ #define __MLX_TEXTURE__ #include +#include +#include #include #include #include @@ -32,7 +34,9 @@ namespace mlx inline void setDescriptor(DescriptorSet set) noexcept { _set = std::move(set); } inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; } - inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, getImageView(), getSampler());} + inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, getImageView(), getSampler()); _has_been_updated = true; } + inline bool hasBeenUpdated() const noexcept { return _has_been_updated; } + inline constexpr void resetUpdate() noexcept { _has_been_updated = false; } ~Texture() = default; @@ -40,9 +44,32 @@ namespace mlx C_VBO _vbo; C_IBO _ibo; DescriptorSet _set; + bool _has_been_updated = false; }; - Texture stb_texture_load(std::filesystem::path file, int* w, int* h); + Texture stbTextureLoad(std::filesystem::path file, int* w, int* h); + + struct TextureRenderData + { + std::shared_ptr texture; + int x; + int y; + + TextureRenderData(std::shared_ptr _texture, int _x, int _y) : texture(_texture), x(_x), y(_y) {} + bool operator==(const TextureRenderData& rhs) const { return texture == rhs.texture && x == rhs.x && y == rhs.y; } + }; +} + +namespace std +{ + template <> + struct hash + { + size_t operator()(const mlx::TextureRenderData& td) const noexcept + { + return std::hash>()(td.texture) + std::hash()(td.x) + std::hash()(td.y); + } + }; } #endif diff --git a/src/renderer/renderer.cpp b/src/renderer/renderer.cpp index 54a61bb..c5c2bb4 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/04/01 17:50:28 by maldavid ### ########.fr */ +/* Updated: 2023/04/02 15:24:40 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -153,7 +153,6 @@ namespace mlx for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) _cmd_buffers[i].destroy(); - _pixel_put_pipeline.destroy(); _pipeline.destroy(); _uniform_buffer->destroy(); _vert_layout.destroy(); diff --git a/src/renderer/renderer.h b/src/renderer/renderer.h index 3ec159f..0ec5fc2 100644 --- a/src/renderer/renderer.h +++ b/src/renderer/renderer.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/12/18 17:14:45 by maldavid #+# #+# */ -/* Updated: 2023/04/01 15:37:40 by maldavid ### ########.fr */ +/* Updated: 2023/04/02 15:26:59 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -104,7 +104,6 @@ namespace mlx inline DescriptorSet& getFragDescriptorSet() noexcept { return _frag_set; } inline DescriptorSetLayout& getVertDescriptorSetLayout() noexcept { return _vert_layout; } inline DescriptorSetLayout& getFragDescriptorSetLayout() noexcept { return _frag_layout; } - inline PixelPutPipeline& getPixelPutPipeline() noexcept { return _pixel_put_pipeline; } inline uint32_t getActiveImageIndex() noexcept { return _active_image_index; } inline uint32_t getImageIndex() noexcept { return _image_index; } @@ -113,7 +112,6 @@ namespace mlx ~Renderer() = default; private: - PixelPutPipeline _pixel_put_pipeline; GraphicPipeline _pipeline; RenderPass _pass; Surface _surface; @@ -130,7 +128,7 @@ namespace mlx DescriptorSet _frag_set; std::array _cmd_buffers; - std::unique_ptr _uniform_buffer = nullptr; + std::unique_ptr _uniform_buffer; class MLX_Window* _window = nullptr; diff --git a/test/main.c b/test/main.c index 445737f..ab2c444 100644 --- a/test/main.c +++ b/test/main.c @@ -6,14 +6,14 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */ -/* Updated: 2023/04/01 17:50:46 by maldavid ### ########.fr */ +/* Updated: 2023/04/02 15:50:40 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ #include #include "../includes/mlx.h" -typedef struct +typedef struct s_mlx { void *mlx; void *win; @@ -25,7 +25,6 @@ int update(t_mlx *mlx) static int i = 0; int j; - mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo, 100, 100); j = 0; while (j < 400) { @@ -46,11 +45,13 @@ int main(void) t_mlx mlx; int w; int h; - + 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_put_image_to_window(mlx.mlx, mlx.win, mlx.logo, 100, 100); + mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo, 200, 200); mlx_loop_hook(mlx.mlx, update, &mlx); mlx_loop(mlx.mlx); mlx_destroy_image(mlx.mlx, mlx.logo);