re-adding render to texture

This commit is contained in:
2024-12-20 01:19:15 +01:00
parent 952e48a05c
commit 4e326db10d
7 changed files with 77 additions and 42 deletions

View File

@@ -5,7 +5,7 @@ if [ -e a.out ]; then
fi fi
if [ $(uname -s) = 'Darwin' ]; then if [ $(uname -s) = 'Darwin' ]; then
clang main.c ../../libmlx.dylib -L /opt/homebrew/lib -lSDL2 -g; clang main.c ../../libmlx.dylib -L /opt/homebrew/lib -lSDL2 -lm -g;
else else
clang main.c ../../libmlx.so -lSDL2 -g -Wall -Wextra -Werror; clang main.c ../../libmlx.so -lSDL2 -g -Wall -Wextra -Werror -lm;
fi fi

View File

@@ -1,61 +1,62 @@
#include <math.h>
#include <stddef.h>
#include "../../includes/mlx.h" #include "../../includes/mlx.h"
#include "../../includes/mlx_extended.h"
typedef struct typedef struct
{ {
void* mlx; mlx_context mlx;
void* win; mlx_window win;
void* render_target; mlx_image render_target;
void* render_target_win; mlx_window render_target_window;
} mlx_t; } mlx_t;
int update(void* param) void update(void* param)
{ {
mlx_t* mlx = (mlx_t*)param; mlx_t* mlx = (mlx_t*)param;
mlx_clear_window(mlx->mlx, mlx->win, 0xFF334D4D); mlx_clear_window(mlx->mlx, mlx->win, (mlx_color){ .rgba = 0x334D4DFF });
mlx_string_put(mlx->mlx, mlx->win, 160, 120, 0xFFFF2066, "text"); mlx_string_put(mlx->mlx, mlx->win, 160, 120, (mlx_color){ .rgba = 0xFF2066FF }, "text");
mlx_string_put(mlx->mlx, mlx->win, 20, 50, 0xFFFFFFFF, "that's a text"); mlx_string_put(mlx->mlx, mlx->win, 20, 50, (mlx_color){ .rgba = 0xFFFFFFFF }, "that's a text");
for(int j = 0, color = 0; j < 400; j++) for(int j = 0, color = 0; j < 400; j++)
{ {
mlx_pixel_put(mlx->mlx, mlx->win, j, j, 0xFFFF0000 + color); mlx_pixel_put(mlx->mlx, mlx->win, j, j, (mlx_color){ .rgba = 0xFF0000FF + (color << 8) });
mlx_pixel_put(mlx->mlx, mlx->win, 399 - j, j, 0xFF0000FF); mlx_pixel_put(mlx->mlx, mlx->win, 399 - j, j, (mlx_color){ .rgba = 0x0000FFFF });
color += (color < 255); color += (color < 255);
} }
for(int j = 0; j < 20; j++) for(int j = 0; j < 20; j++)
{ {
for(int k = 0; k < 20; k++) for(int k = 0; k < 20; k++)
mlx_pixel_put(mlx->mlx, mlx->win, 220 + j, 160 + k, 0xFFFF0000); mlx_pixel_put(mlx->mlx, mlx->win, 220 + j, 160 + k, (mlx_color){ .rgba = 0xFF0000FF });
} }
mlx_string_put(mlx->mlx, mlx->render_target_win, 20, 20, 0xFFAF2BFF, "yippeeee"); mlx_string_put(mlx->mlx, mlx->render_target_window, 20, 20, (mlx_color){ .rgba = 0xAF2BFFFF }, "yippeeee");
for(int j = 0, color = 0; j < 200; j++) for(int j = 0, color = 0; j < 200; j++)
{ {
mlx_pixel_put(mlx->mlx, mlx->render_target_win, j, j, 0xFFFF0000 + color); mlx_pixel_put(mlx->mlx, mlx->render_target_window, j, j, (mlx_color){ .rgba = 0xFF0000FF + (color << 8) });
mlx_pixel_put(mlx->mlx, mlx->render_target_win, 199 - j, j, 0xFF0000FF); mlx_pixel_put(mlx->mlx, mlx->render_target_window, 199 - j, j, (mlx_color){ .rgba = 0x0000FFFF });
color += (color < 255); color += (color < 255);
} }
mlx_transform_put_image_to_window(mlx->mlx, mlx->win, mlx->render_target, 5, 250, 0.5f, 33.0f); static int i = 0;
i++;
return 0; mlx_put_transformed_image_to_window(mlx->mlx, mlx->win, mlx->render_target, 5, 100, 1.0f, 1.0f, i);
} }
int key_hook(int key, void* param) void key_hook(int key, void* param)
{ {
mlx_t* mlx = (mlx_t*)param; mlx_t* mlx = (mlx_t*)param;
if(key == 41) if(key == 41)
mlx_loop_end(mlx->mlx); mlx_loop_end(mlx->mlx);
return 0;
} }
int window_hook(int event, void* param) void window_hook(int event, void* param)
{ {
if(event == 0) if(event == 0)
mlx_loop_end(((mlx_t*)param)->mlx); mlx_loop_end(((mlx_t*)param)->mlx);
return 0;
} }
int main(void) int main(void)
@@ -63,24 +64,36 @@ int main(void)
mlx_t mlx; mlx_t mlx;
mlx.mlx = mlx_init(); mlx.mlx = mlx_init();
mlx.win = mlx_new_resizable_window(mlx.mlx, 400, 400, "My window");
mlx_set_fps_goal(mlx.mlx, 60);
mlx_window_create_info info = { 0 };
info.title = "My window";
info.width = 400;
info.height = 400;
info.is_resizable = true;
mlx.win = mlx_new_window(mlx.mlx, &info);
mlx.render_target = mlx_new_image(mlx.mlx, 200, 200); mlx.render_target = mlx_new_image(mlx.mlx, 200, 200);
mlx.render_target_win = mlx_new_window(mlx.mlx, 200, 200, (char*)mlx.render_target); info.render_target = mlx.render_target;
mlx_clear_window(mlx.mlx, mlx.render_target_win, 0xFFC16868); info.title = NULL;
info.width = 200;
info.height = 200;
mlx.render_target_window = mlx_new_window(mlx.mlx, &info);
mlx_clear_window(mlx.mlx, mlx.render_target_window, (mlx_color){ .rgba = 0xC16868FF });
mlx_on_event(mlx.mlx, mlx.win, MLX_KEYDOWN, key_hook, &mlx); mlx_on_event(mlx.mlx, mlx.win, MLX_KEYDOWN, key_hook, &mlx);
mlx_on_event(mlx.mlx, mlx.win, MLX_WINDOW_EVENT, window_hook, &mlx); mlx_on_event(mlx.mlx, mlx.win, MLX_WINDOW_EVENT, window_hook, &mlx);
mlx_loop_hook(mlx.mlx, update, &mlx); mlx_add_loop_hook(mlx.mlx, update, &mlx);
mlx_loop(mlx.mlx); mlx_loop(mlx.mlx);
mlx_destroy_window(mlx.mlx, mlx.win); mlx_destroy_window(mlx.mlx, mlx.win);
mlx_destroy_window(mlx.mlx, mlx.render_target_win); mlx_destroy_window(mlx.mlx, mlx.render_target_window);
mlx_destroy_image(mlx.mlx, mlx.render_target); mlx_destroy_image(mlx.mlx, mlx.render_target);
mlx_destroy_display(mlx.mlx); mlx_destroy_context(mlx.mlx);
return 0; return 0;
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <contact@kbz8.me> +#+ +:+ +#+ */ /* By: maldavid <contact@kbz8.me> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */ /* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */
/* Updated: 2024/12/17 02:58:07 by maldavid ### ########.fr */ /* Updated: 2024/12/20 00:42:01 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -97,9 +97,18 @@ MLX_API void mlx_destroy_context(mlx_context mlx);
/** /**
* @brief Descriptor structure for window creation * @brief Descriptor structure for window creation
*
* Note: if a valid mlx_image is passed as render_target, this window will not be a real system window
* and will rather act as a gate to use any draw function to draw directly on an image.
*
* Ex: you could use mlx_string_put or mlx_pixel_put to draw on a given image and then use this image
* with mlx_put_image_to_window to render it on a real window.
*
* See experimental/RenderToTexture/main.c for a concrete example.
*/ */
typedef struct mlx_window_create_info typedef struct mlx_window_create_info
{ {
mlx_image render_target;
const char* title; const char* title;
int width; int width;
int height; int height;

View File

@@ -67,6 +67,7 @@ namespace mlx
catch(...) { return nullptr; } catch(...) { return nullptr; }
m_graphics.emplace_back(std::make_unique<GraphicsSupport>(info, m_graphics.size())); m_graphics.emplace_back(std::make_unique<GraphicsSupport>(info, m_graphics.size()));
if(m_graphics.back()->HasWindow())
m_in.RegisterWindow(m_graphics.back()->GetWindow()); m_in.RegisterWindow(m_graphics.back()->GetWindow());
m_graphics.back()->GetScene().BindFont(p_last_font_bound); m_graphics.back()->GetScene().BindFont(p_last_font_bound);
window->id = m_graphics.back()->GetID(); window->id = m_graphics.back()->GetID();

View File

@@ -1,9 +1,6 @@
#ifndef __MLX_HANDLES__ #ifndef __MLX_HANDLES__
#define __MLX_HANDLES__ #define __MLX_HANDLES__
#include <Core/Application.h>
#include <Renderer/Image.h>
extern "C" extern "C"
{ {
struct mlx_context_handler struct mlx_context_handler

View File

@@ -1,5 +1,7 @@
#include <PreCompiled.h> #include <PreCompiled.h>
#include <Core/Graphics.h> #include <Core/Graphics.h>
#include <Core/Application.h>
#include <Core/Handles.h>
namespace mlx namespace mlx
{ {
@@ -9,11 +11,24 @@ namespace mlx
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
if(info->render_target == nullptr)
{
p_window = std::make_shared<Window>(info); p_window = std::make_shared<Window>(info);
m_has_window = true; m_has_window = true;
}
else
m_has_window = false;
if(info->render_target != nullptr)
{
m_renderer.Init(info->render_target->texture);
m_scene_renderer.Init(info->render_target->texture);
}
else
{
m_renderer.Init(p_window.get()); m_renderer.Init(p_window.get());
m_scene_renderer.Init(nullptr); m_scene_renderer.Init(nullptr);
}
p_scene = std::make_unique<Scene>(); p_scene = std::make_unique<Scene>();
} }
@@ -25,7 +40,7 @@ namespace mlx
m_scene_renderer.Render(*p_scene, m_renderer); m_scene_renderer.Render(*p_scene, m_renderer);
m_renderer.EndFrame(); m_renderer.EndFrame();
#ifdef GRAPHICS_MEMORY_DUMP #ifdef GRAPHICS_MEMORY_DUMP
// dump memory to file every two seconds // Dump memory usage to file every two seconds
using namespace std::chrono_literals; using namespace std::chrono_literals;
static std::int64_t timer = static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()); static std::int64_t timer = static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
if(std::chrono::duration<std::uint64_t>{ static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()) - timer } >= 1s) if(std::chrono::duration<std::uint64_t>{ static_cast<std::uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count()) - timer } >= 1s)

View File

@@ -29,9 +29,9 @@ namespace mlx
else else
extent = kvfGetSwapchainImagesSize(renderer.GetSwapchain().Get()); extent = kvfGetSwapchainImagesSize(renderer.GetSwapchain().Get());
#ifdef DEBUG #ifdef DEBUG
m_main_render_texture.Init({}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_renderpasses_target"); m_main_render_texture.Init({}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_UNORM, false, "mlx_renderpasses_target");
#else #else
m_main_render_texture.Init({}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_SRGB, false, {}); m_main_render_texture.Init({}, extent.width, extent.height, VK_FORMAT_R8G8B8A8_UNORM, false, {});
#endif #endif
m_main_render_texture.TransitionLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL); m_main_render_texture.TransitionLayout(VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
} }