adding graphics support class, refactoring the code

This commit is contained in:
2023-04-02 17:37:38 +02:00
parent 7eea6ea1d5
commit aaf7e861d5
14 changed files with 318 additions and 148 deletions

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 22:10:52 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()) while(_in.is_running())
{ {
_in.update(); _in.update();
for(auto win : _wins) for(auto& gs : _graphics)
win->beginFrame(); gs->beginRender();
if(_loop_hook) if(_loop_hook)
_loop_hook(_param); _loop_hook(_param);
for(auto win : _wins) for(auto& gs : _graphics)
win->endFrame(); 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> texture = std::make_shared<Texture>(stb_texture_load(file, w, h)); std::shared_ptr<Texture> texture = std::make_shared<Texture>(stbTextureLoad(file, w, h));
TextureID id = _texture_lib.addTextureToLibrary(texture); TextureID id = _texture_lib.addTextureToLibrary(texture);
_texture_ids.push_back(id); _texture_ids.push_back(id);
return &_texture_ids.back(); return &_texture_ids.back();
} }
void Application::texture_put(void* win, void* img, int x, int y) void Application::destroyTexture(void* ptr)
{
std::shared_ptr<Texture> texture = _texture_lib.getTexture(*static_cast<TextureID*>(img));
_wins[*static_cast<int*>(win)]->texture_put(texture, x, y);
}
void Application::destroy_texture(void* ptr)
{ {
vkDeviceWaitIdle(Render_Core::get().getDevice().get()); vkDeviceWaitIdle(Render_Core::get().getDevice().get());
TextureID id = *static_cast<TextureID*>(ptr); TextureID id = *static_cast<TextureID*>(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/04 21:49:46 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 <utility> #include <utility>
#include <functional> #include <functional>
#include "errors.h" #include <core/errors.h>
#include <core/graphics.h>
#include <platform/inputs.h> #include <platform/inputs.h>
#include <platform/window.h>
#include <renderer/texture_library.h> #include <renderer/texture_library.h>
@@ -32,37 +32,22 @@ namespace mlx::core
public: public:
Application() : _in() {} Application() : _in() {}
inline void* new_window(std::size_t w, std::size_t h, std::string title) inline void getMousePos(int* x, int* y) noexcept;
{ inline void mouseMove(void* win_ptr, int x, int y) noexcept;
_wins.emplace_back(std::make_shared<MLX_Window>(w, h, std::move(title), _wins.size()));
return static_cast<void*>(&_wins.back()->get_id());
}
inline void get_mouse_pos(int* x, int* y) noexcept inline void* newGraphicsSuport(std::size_t w, std::size_t h, std::string title);
{ inline void clearGraphicsSupport(void* win_ptr);
*x = _in.getX(); inline void destroyGraphicsSupport(void* win_ptr);
*y = _in.getY();
}
inline void mouse_move(void* win_ptr, int x, int y) noexcept inline void pixelPut(void* win_ptr, int x, int y, int color) const 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; } void* newStbTexture(char* file, int* w, int* h); // stb textures are format managed by stb image (png, jpg, bpm, ...)
inline void loop_end() noexcept { _in.finish(); } 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<int*>(win_ptr)]->pixel_put(x, y, color); } inline void loopHook(int (*f)(void*), void* param);
inline void loopEnd() noexcept;
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 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(); }
void run() noexcept; void run() noexcept;
~Application() = default; ~Application() = default;
@@ -71,11 +56,13 @@ namespace mlx::core
Input _in; Input _in;
TextureLibrary _texture_lib; TextureLibrary _texture_lib;
std::vector<TextureID> _texture_ids; std::vector<TextureID> _texture_ids;
std::vector<std::shared_ptr<MLX_Window>> _wins; std::vector<std::unique_ptr<GraphicsSupport>> _graphics;
std::function<int(void*)> _loop_hook; std::function<int(void*)> _loop_hook;
void* _param = nullptr; void* _param = nullptr;
bool _is_loop_running = false; bool _is_loop_running = false;
}; };
} }
#include <core/application.inl>
#endif // __MLX_APPLICATION__ #endif // __MLX_APPLICATION__

67
src/core/application.inl git.filemode.normal_file
View File

@@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* application.inl :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */
/* Updated: 2023/04/02 14:56:27 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <core/application.h>
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<int*>(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<GraphicsSupport>(w, h, std::move(title), _graphics.size()));
return static_cast<void*>(&_graphics.back()->getID());
}
void Application::clearGraphicsSupport(void* win_ptr)
{
_graphics[*static_cast<int*>(win_ptr)]->clearRenderData();
}
void Application::destroyGraphicsSupport(void* win_ptr)
{
_graphics[*static_cast<int*>(win_ptr)].reset();
}
void Application::pixelPut(void* win_ptr, int x, int y, int color) const noexcept
{
_graphics[*static_cast<int*>(win_ptr)]->pixelPut(x, y, color);
}
void Application::texturePut(void* win_ptr, void* img, int x, int y)
{
std::shared_ptr<Texture> texture = _texture_lib.getTexture(*static_cast<TextureID*>(img));
_graphics[*static_cast<int*>(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();
}
}

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 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) void* mlx_new_window(void* mlx, int w, int h, const char* title)
{ {
return static_cast<mlx::core::Application*>(mlx)->new_window(w, h, title); return static_cast<mlx::core::Application*>(mlx)->newGraphicsSuport(w, h, title);
} }
int mlx_loop_hook(void* mlx, int (*f)(void*), void* param) int mlx_loop_hook(void* mlx, int (*f)(void*), void* param)
{ {
static_cast<mlx::core::Application*>(mlx)->loop_hook(f, param); static_cast<mlx::core::Application*>(mlx)->loopHook(f, param);
return 0; return 0;
} }
@@ -42,7 +42,7 @@ extern "C"
int mlx_loop_end(void* mlx) int mlx_loop_end(void* mlx)
{ {
static_cast<mlx::core::Application*>(mlx)->loop_end(); static_cast<mlx::core::Application*>(mlx)->loopEnd();
return 0; return 0;
} }
@@ -58,48 +58,48 @@ extern "C"
int mlx_mouse_move(void* mlx, 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); static_cast<mlx::core::Application*>(mlx)->mouseMove(win_ptr, x, y);
return 0; return 0;
} }
int mlx_mouse_get_pos(void* mlx, 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); static_cast<mlx::core::Application*>(mlx)->getMousePos(x, y);
return 0; return 0;
} }
int mlx_put_image_to_window(void* mlx_ptr, void* win_ptr, void* img_ptr, int x, int y) int mlx_put_image_to_window(void* mlx_ptr, void* win_ptr, void* img_ptr, int x, int y)
{ {
static_cast<mlx::core::Application*>(mlx_ptr)->texture_put(win_ptr, img_ptr, x, y); static_cast<mlx::core::Application*>(mlx_ptr)->texturePut(win_ptr, img_ptr, x, y);
return 0; return 0;
} }
int mlx_destroy_image(void* mlx_ptr, void* img_ptr) int mlx_destroy_image(void* mlx_ptr, void* img_ptr)
{ {
static_cast<mlx::core::Application*>(mlx_ptr)->destroy_texture(img_ptr); static_cast<mlx::core::Application*>(mlx_ptr)->destroyTexture(img_ptr);
return 0; return 0;
} }
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)
{ {
return static_cast<mlx::core::Application*>(mlx_ptr)->new_stb_texture(filename, width, height); return static_cast<mlx::core::Application*>(mlx_ptr)->newStbTexture(filename, width, height);
} }
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)
{ {
static_cast<mlx::core::Application*>(mlx)->pixel_put(win_ptr, x, y, color); static_cast<mlx::core::Application*>(mlx)->pixelPut(win_ptr, x, y, color);
return 0; return 0;
} }
int mlx_clear_window(void* mlx_ptr, void* win_ptr) int mlx_clear_window(void* mlx_ptr, void* win_ptr)
{ {
static_cast<mlx::core::Application*>(mlx_ptr)->clear_window(win_ptr); static_cast<mlx::core::Application*>(mlx_ptr)->clearGraphicsSupport(win_ptr);
return 0; 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)->destroyGraphicsSupport(win_ptr);
return 0; return 0;
} }

64
src/core/graphics.cpp git.filemode.normal_file
View File

@@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* graphics.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */
/* Updated: 2023/04/02 17:33:47 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <core/graphics.h>
namespace mlx
{
GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id) :
_window(std::make_shared<MLX_Window>(w, h, std::move(title))),
_renderer(std::make_unique<Renderer>()),
_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<VkDescriptorSet> 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();
}
}

59
src/core/graphics.h git.filemode.normal_file
View File

@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* graphics.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <memory>
#include <unordered_set>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <platform/window.h>
#include <renderer/renderer.h>
#include <utils/non_copyable.h>
#include <renderer/images/texture.h>
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<MLX_Window> 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> texture, int x, int y);
void endRender() noexcept;
~GraphicsSupport();
private:
std::unordered_set<TextureRenderData> _textures_to_render;
glm::mat4 _proj = glm::mat4(1.0);
std::shared_ptr<MLX_Window> _window;
PixelPutPipeline _pixel_put_pipeline;
std::unique_ptr<Renderer> _renderer;
int _id;
};
}
#include <core/graphics.inl>
#endif

43
src/core/graphics.inl git.filemode.normal_file
View File

@@ -0,0 +1,43 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* graphics.inl :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 15:13:55 by maldavid #+# #+# */
/* Updated: 2023/04/02 15:26:16 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <core/graphics.h>
namespace mlx
{
int& GraphicsSupport::getID() noexcept { return _id; }
std::shared_ptr<MLX_Window> GraphicsSupport::getWindow() { return _window; }
void GraphicsSupport::beginRender() noexcept
{
if(!_renderer->beginFrame())
return;
_proj = glm::ortho<float>(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> texture, int x, int y)
{
_textures_to_render.emplace(texture, x, y);
}
}

View File

@@ -6,80 +6,24 @@
/* 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 17:50:22 by maldavid ### ########.fr */ /* Updated: 2023/04/02 15:39:59 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "window.h" #include <platform/window.h>
#include <core/errors.h> #include <core/errors.h>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <cstring>
namespace mlx 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); _win = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_VULKAN | SDL_WINDOW_SHOWN);
if(!_win) if(!_win)
core::error::report(e_kind::fatal_error, std::string("unable to open a new window, ") + SDL_GetError()); 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<float>(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> texture, int x, int y)
{
if(texture->getSet() == VK_NULL_HANDLE)
texture->setDescriptor(_renderer->getFragDescriptorSet().duplicate());
texture->updateSet(0);
std::vector<VkDescriptorSet> 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<VkDescriptorSet> 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() MLX_Window::~MLX_Window()
{ {
_renderer->destroy();
if(_win) if(_win)
SDL_DestroyWindow(_win); SDL_DestroyWindow(_win);
} }

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 17:26:18 by maldavid ### ########.fr */ /* Updated: 2023/04/02 15:39:53 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -15,37 +15,24 @@
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <string> #include <string>
#include <memory>
#include <renderer/renderer.h>
#include <renderer/images/texture.h>
#include <renderer/buffers/vk_buffer.h>
namespace mlx namespace mlx
{ {
class MLX_Window class MLX_Window
{ {
public: 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 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> texture, int x, int y);
~MLX_Window(); ~MLX_Window();
private: private:
glm::mat4 _proj = glm::mat4(1.0);
std::unique_ptr<Renderer> _renderer;
SDL_Window* _win = nullptr; SDL_Window* _win = nullptr;
int _width = 0; int _width = 0;
int _height = 0; int _height = 0;
int _id;
}; };
} }

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 18:03:35 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) void Texture::render(Renderer& renderer, int x, int y)
{ {
auto cmd = renderer.getActiveCmdBuffer().get();
_vbo.bind(renderer); _vbo.bind(renderer);
_ibo.bind(renderer); _ibo.bind(renderer);
auto cmd = renderer.getActiveCmdBuffer().get();
glm::vec2 translate(x, y); glm::vec2 translate(x, y);
vkCmdPushConstants(cmd, renderer.getPipeline().getPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(translate), &translate); vkCmdPushConstants(cmd, renderer.getPipeline().getPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(translate), &translate);
vkCmdDrawIndexed(cmd, static_cast<uint32_t>(_ibo.getSize() / sizeof(uint16_t)), 1, 0, 0, 0); vkCmdDrawIndexed(cmd, static_cast<uint32_t>(_ibo.getSize() / sizeof(uint16_t)), 1, 0, 0, 0);
@@ -70,7 +70,7 @@ namespace mlx
_ibo.destroy(); _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; Texture texture;
int channels; int channels;

View File

@@ -6,7 +6,7 @@
/* By: maldavid <marvin@42.fr> +#+ +:+ +#+ */ /* By: maldavid <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/08 02:24:58 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__ #define __MLX_TEXTURE__
#include <filesystem> #include <filesystem>
#include <memory>
#include <functional>
#include <renderer/images/vk_image.h> #include <renderer/images/vk_image.h>
#include <renderer/descriptors/vk_descriptor_set.h> #include <renderer/descriptors/vk_descriptor_set.h>
#include <renderer/buffers/vk_ibo.h> #include <renderer/buffers/vk_ibo.h>
@@ -32,7 +34,9 @@ namespace mlx
inline void setDescriptor(DescriptorSet set) noexcept { _set = std::move(set); } inline void setDescriptor(DescriptorSet set) noexcept { _set = std::move(set); }
inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; } 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; ~Texture() = default;
@@ -40,9 +44,32 @@ namespace mlx
C_VBO _vbo; C_VBO _vbo;
C_IBO _ibo; C_IBO _ibo;
DescriptorSet _set; 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> texture;
int x;
int y;
TextureRenderData(std::shared_ptr<Texture> _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<mlx::TextureRenderData>
{
size_t operator()(const mlx::TextureRenderData& td) const noexcept
{
return std::hash<std::shared_ptr<mlx::Texture>>()(td.texture) + std::hash<int>()(td.x) + std::hash<int>()(td.y);
}
};
} }
#endif #endif

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/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++) for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
_cmd_buffers[i].destroy(); _cmd_buffers[i].destroy();
_pixel_put_pipeline.destroy();
_pipeline.destroy(); _pipeline.destroy();
_uniform_buffer->destroy(); _uniform_buffer->destroy();
_vert_layout.destroy(); _vert_layout.destroy();

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:14:45 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 DescriptorSet& getFragDescriptorSet() noexcept { return _frag_set; }
inline DescriptorSetLayout& getVertDescriptorSetLayout() noexcept { return _vert_layout; } inline DescriptorSetLayout& getVertDescriptorSetLayout() noexcept { return _vert_layout; }
inline DescriptorSetLayout& getFragDescriptorSetLayout() noexcept { return _frag_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 getActiveImageIndex() noexcept { return _active_image_index; }
inline uint32_t getImageIndex() noexcept { return _image_index; } inline uint32_t getImageIndex() noexcept { return _image_index; }
@@ -113,7 +112,6 @@ namespace mlx
~Renderer() = default; ~Renderer() = default;
private: private:
PixelPutPipeline _pixel_put_pipeline;
GraphicPipeline _pipeline; GraphicPipeline _pipeline;
RenderPass _pass; RenderPass _pass;
Surface _surface; Surface _surface;
@@ -130,7 +128,7 @@ namespace mlx
DescriptorSet _frag_set; DescriptorSet _frag_set;
std::array<CmdBuffer, MAX_FRAMES_IN_FLIGHT> _cmd_buffers; std::array<CmdBuffer, MAX_FRAMES_IN_FLIGHT> _cmd_buffers;
std::unique_ptr<UBO> _uniform_buffer = nullptr; std::unique_ptr<UBO> _uniform_buffer;
class MLX_Window* _window = nullptr; class MLX_Window* _window = nullptr;

View File

@@ -6,14 +6,14 @@
/* 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 17:50:46 by maldavid ### ########.fr */ /* Updated: 2023/04/02 15:50:40 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdio.h> #include <stdio.h>
#include "../includes/mlx.h" #include "../includes/mlx.h"
typedef struct typedef struct s_mlx
{ {
void *mlx; void *mlx;
void *win; void *win;
@@ -25,7 +25,6 @@ int update(t_mlx *mlx)
static int i = 0; static int i = 0;
int j; int j;
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo, 100, 100);
j = 0; j = 0;
while (j < 400) while (j < 400)
{ {
@@ -46,11 +45,13 @@ int main(void)
t_mlx mlx; t_mlx mlx;
int w; int w;
int h; int h;
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_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_hook(mlx.mlx, update, &mlx);
mlx_loop(mlx.mlx); mlx_loop(mlx.mlx);
mlx_destroy_image(mlx.mlx, mlx.logo); mlx_destroy_image(mlx.mlx, mlx.logo);