Archived
1
0

what it workkks now way \!

This commit is contained in:
Adam Joly
2024-01-19 15:41:43 +01:00
parent cb8be13f46
commit 47a6fe08ff
492 changed files with 928 additions and 344 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 30 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

@ -7,6 +7,6 @@ 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 -g;
else else
clang main.c ../libmlx.so -lSDL2 -g; clang main.c ../libmlx.so -lSDL2 -g -Wall -Wextra -Werror;
fi fi

View File

@ -6,118 +6,162 @@
/* 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: 2024/01/11 15:03:14 by maldavid ### ########.fr */ /* Updated: 2024/01/18 15:23:35 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "../includes/mlx.h" #include "../includes/mlx.h"
typedef struct s_mlx typedef struct
{ {
void* mlx; void* mlx;
void* win; void* win;
void *logo; void* logo_png;
void* logo_jpg;
void* logo_bmp;
void* img; void* img;
} t_mlx; } mlx_t;
int update(void* param) int update(void* param)
{ {
static int i = 0; static int i = 0;
int j; mlx_t* mlx = (mlx_t*)param;
int k;
t_mlx *mlx;
mlx = (t_mlx *)param; mlx_set_font_scale(mlx->mlx, mlx->win, "default", 6.f);
mlx_string_put(mlx->mlx, mlx->win, 160, 120, 0xFFFF2066, "this text should be hidden"); mlx_string_put(mlx->mlx, mlx->win, 160, 120, 0xFFFF2066, "this text should be hidden");
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo, 100, 100);
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_png, 100, 100);
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_jpg, 210, 150);
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_bmp, 220, 40);
mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->img, 150, 60); mlx_put_image_to_window(mlx->mlx, mlx->win, mlx->img, 150, 60);
if (i == 0)
mlx_set_font_scale(mlx->mlx, mlx->win, "font.ttf", 16.f);
mlx_string_put(mlx->mlx, mlx->win, 20, 50, 0xFFFFFFFF, "that's a text"); mlx_string_put(mlx->mlx, mlx->win, 20, 50, 0xFFFFFFFF, "that's a text");
j = 0;
k = 0; int color = 0;
while (j++ < 400) for(int j = 0; j < 400; j++)
{ {
mlx_pixel_put(mlx->mlx, mlx->win, j, j, 0xFFFF0000 + k); mlx_pixel_put(mlx->mlx, mlx->win, j, j, 0xFFFF0000 + color);
mlx_pixel_put(mlx->mlx, mlx->win, 399 - j, j, 0xFF0000FF); mlx_pixel_put(mlx->mlx, mlx->win, 399 - j, j, 0xFF0000FF);
k += (k < 255); color += (color < 255);
} }
if(++i == 5000) if(++i == 5000)
mlx_clear_window(mlx->mlx, mlx->win); mlx_clear_window(mlx->mlx, mlx->win);
if(i == 7000) if(i == 7000)
mlx_set_font_scale(mlx->mlx, mlx->win, "default", 16.f); mlx_set_font_scale(mlx->mlx, mlx->win, "default", 16.f);
return (0);
return 0;
} }
void *create_image(t_mlx *mlx) void* create_image(mlx_t* mlx)
{ {
unsigned char pixel[4]; unsigned char pixel[4];
int i[3]; void* img = mlx_new_image(mlx->mlx, 100, 100);
void *img; for(int i = 0, j = 0, k = 0; i < (100 * 100) * 4; i += 4, j++)
memset(i, 0, sizeof(int) * 3);
img = mlx_new_image(mlx->mlx, 100, 100);
while (i[0] < (100 * 100) * 4)
{ {
if (i[0] < 10000 || i[0] > 20000) if(j >= 100)
{ {
pixel[0] = i[0]; j = 0;
pixel[1] = i[1]; k++;
pixel[2] = i[2]; }
if(i < 10000 || i > 20000)
{
pixel[0] = i;
pixel[1] = j;
pixel[2] = k;
pixel[3] = 0x99; pixel[3] = 0x99;
mlx_set_image_pixel(mlx->mlx, img, i[1], i[2], *((int *)pixel)); mlx_set_image_pixel(mlx->mlx, img, j, k, *((int *)pixel));
}
i[0] += 4;
i[1]++;
if (i[1] >= 100)
{
i[1] = 0;
i[2]++;
} }
} }
return (img); return img;
} }
int key_hook(int key, void* param) int key_hook(int key, void* param)
{ {
if (key == 41) int x;
mlx_loop_end(((t_mlx *)param)->mlx); int y;
return (0); mlx_t* mlx = (mlx_t*)param;
mlx_mouse_get_pos(mlx->mlx, &x, &y);
switch(key)
{
case 41 : // ESCAPE
mlx_loop_end(mlx->mlx);
break;
case 22 : // (S)how
mlx_mouse_show();
break;
case 11 : // (H)ide
mlx_mouse_hide();
break;
case 6 : // (C)lear
mlx_clear_window(mlx->mlx, mlx->win);
break;
case 79 : // RIGHT KEY
mlx_mouse_move(mlx->mlx, mlx->win, x + 10, y);
break;
case 80 : // LEFT KEY
mlx_mouse_move(mlx->mlx, mlx->win, x - 10, y);
break;
case 81 : // UP KEY
mlx_mouse_move(mlx->mlx, mlx->win, x, y + 10);
break;
case 82 : // DOWN KEY
mlx_mouse_move(mlx->mlx, mlx->win, x, y - 10);
break;
default : break;
}
return 0;
} }
int window_hook(int event, void* param) int window_hook(int event, void* param)
{ {
if(event == 0) if(event == 0)
mlx_loop_end(((t_mlx *)param)->mlx); mlx_loop_end(((mlx_t*)param)->mlx);
return (0); return 0;
} }
int main(int argc, char **argv) int main(void)
{ {
t_mlx mlx; mlx_t mlx;
void *img;
int w; int w;
int h; int h;
int dummy;
(void)argc;
(void)argv;
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_set_fps_goal(mlx.mlx, 60);
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.logo = mlx_png_file_to_image(mlx.mlx, "42_logo.png", &w, &h);
mlx.logo_png = mlx_png_file_to_image(mlx.mlx, "42_logo.png", &dummy, &dummy);
mlx.logo_bmp = mlx_bmp_file_to_image(mlx.mlx, "42_logo.bmp", &dummy, &dummy);
mlx.logo_jpg = mlx_jpg_file_to_image(mlx.mlx, "42_logo.jpg", &dummy, &dummy);
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, 10, 190); mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo_png, 10, 190);
mlx.img = create_image(&mlx); mlx.img = create_image(&mlx);
mlx_string_put(mlx.mlx, mlx.win, 20, 20, 0xFF0020FF, \
"that text will disappear"); mlx_set_font_scale(mlx.mlx, mlx.win, "font.ttf", 16.f);
mlx_string_put(mlx.mlx, mlx.win, 20, 20, 0xFF0020FF, "that text will disappear");
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_get_screens_size(mlx.mlx, mlx.win, &w, &h);
printf("screen size : %dx%d\n", w, h);
mlx_destroy_image(mlx.mlx, mlx.logo_png);
mlx_destroy_image(mlx.mlx, mlx.logo_jpg);
mlx_destroy_image(mlx.mlx, mlx.logo_bmp);
mlx_destroy_image(mlx.mlx, mlx.img); mlx_destroy_image(mlx.mlx, mlx.img);
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;
} }

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: 2024/01/05 19:53:13 by maldavid ### ########.fr */ /* Updated: 2024/01/18 14:36:12 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -349,15 +349,27 @@ MLX_API int mlx_destroy_display(void* mlx);
/** /**
* @brief Get screen size * @brief Get the size of the screen the given window is on
* *
* @param mlx Internal MLX application * @param mlx Internal MLX application
* @param win Internal window
* @param w Get width size * @param w Get width size
* @param h Get height size * @param h Get height size
* *
* @return (int) Always return 0, made this to copy the behaviour of the original MLX * @return (int) Always return 0, made this to copy the behaviour of the original MLX
*/ */
MLX_API int mlx_get_screens_size(void* mlx, int* w, int* h); MLX_API int mlx_get_screens_size(void* mlx, void* win, int* w, int* h);
/**
* @brief Caps the FPS
*
* @param mlx Internal MLX application
* @param fps The FPS cap
*
* @return (int) Always return 0
*/
MLX_API int mlx_set_fps_goal(void* mlx, int fps);
#ifdef __cplusplus #ifdef __cplusplus
} }

0
MacroLibX/scripts/fetch_dependencies.sh Normal file → Executable file
View File

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: 2024/01/11 05:08:42 by maldavid ### ########.fr */ /* Updated: 2024/01/18 15:19:58 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "application.h" #include "application.h"
#include <renderer/texts/text_library.h> #include <renderer/texts/text_library.h>
#include <renderer/texts/font_library.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <renderer/images/texture.h> #include <renderer/images/texture.h>
#include <renderer/core/render_core.h> #include <renderer/core/render_core.h>
@ -23,8 +24,9 @@
namespace mlx::core namespace mlx::core
{ {
static bool __drop_sdl_responsability = false; static bool __drop_sdl_responsability = false;
Application::Application() : _in(std::make_unique<Input>()) Application::Application() : _fps(), _in(std::make_unique<Input>())
{ {
_fps.init();
__drop_sdl_responsability = SDL_WasInit(SDL_INIT_VIDEO); __drop_sdl_responsability = SDL_WasInit(SDL_INIT_VIDEO);
if(__drop_sdl_responsability) // is case the mlx is running in a sandbox like MacroUnitTester where SDL is already init if(__drop_sdl_responsability) // is case the mlx is running in a sandbox like MacroUnitTester where SDL is already init
return; return;
@ -37,6 +39,8 @@ namespace mlx::core
{ {
while(_in->is_running()) while(_in->is_running())
{ {
if(!_fps.update())
continue;
_in->update(); _in->update();
if(_loop_hook) if(_loop_hook)
@ -84,6 +88,7 @@ namespace mlx::core
Application::~Application() Application::~Application()
{ {
TextLibrary::get().clearLibrary(); TextLibrary::get().clearLibrary();
FontLibrary::get().clearLibrary();
if(__drop_sdl_responsability) if(__drop_sdl_responsability)
return; return;
SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS); SDL_QuitSubSystem(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_EVENTS);

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: 2024/01/10 19:57:12 by maldavid ### ########.fr */ /* Updated: 2024/01/18 14:59:47 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,6 +26,7 @@
#include <platform/inputs.h> #include <platform/inputs.h>
#include <mlx_profile.h> #include <mlx_profile.h>
#include <core/profiler.h> #include <core/profiler.h>
#include <core/fps.h>
namespace mlx::core namespace mlx::core
{ {
@ -39,7 +40,9 @@ namespace mlx::core
inline void onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept; inline void onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept;
inline void getScreenSize(int* w, int* h) noexcept; inline void getScreenSize(void* win, int* w, int* h) noexcept;
inline void setFPSCap(uint32_t fps) noexcept;
inline void* newGraphicsSuport(std::size_t w, std::size_t h, const char* title); inline void* newGraphicsSuport(std::size_t w, std::size_t h, const char* title);
inline void clearGraphicsSupport(void* win); inline void clearGraphicsSupport(void* win);
@ -65,6 +68,7 @@ namespace mlx::core
~Application(); ~Application();
private: private:
FpsManager _fps;
std::list<Texture> _textures; std::list<Texture> _textures;
std::vector<std::unique_ptr<GraphicsSupport>> _graphics; std::vector<std::unique_ptr<GraphicsSupport>> _graphics;
std::function<int(void*)> _loop_hook; std::function<int(void*)> _loop_hook;

View File

@ -42,7 +42,6 @@ namespace mlx::core
} }
SDL_WarpMouseInWindow(_graphics[*static_cast<int*>(win)]->getWindow()->getNativeWindow(), x, y); SDL_WarpMouseInWindow(_graphics[*static_cast<int*>(win)]->getWindow()->getNativeWindow(), x, y);
SDL_PumpEvents(); SDL_PumpEvents();
SDL_FlushEvent(SDL_MOUSEMOTION);
} }
void Application::onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept void Application::onEvent(void* win, int event, int (*funct_ptr)(int, void*), void* param) noexcept
@ -56,14 +55,20 @@ namespace mlx::core
_in->onEvent(_graphics[*static_cast<int*>(win)]->getWindow()->getID(), event, funct_ptr, param); _in->onEvent(_graphics[*static_cast<int*>(win)]->getWindow()->getID(), event, funct_ptr, param);
} }
void Application::getScreenSize(int* w, int* h) noexcept void Application::getScreenSize(void* win, int* w, int* h) noexcept
{ {
CHECK_WINDOW_PTR(win);
SDL_DisplayMode DM; SDL_DisplayMode DM;
SDL_GetDesktopDisplayMode(0, &DM); SDL_GetDesktopDisplayMode(SDL_GetWindowDisplayIndex(_graphics[*static_cast<int*>(win)]->getWindow()->getNativeWindow()), &DM);
*w = DM.w; *w = DM.w;
*h = DM.h; *h = DM.h;
} }
void Application::setFPSCap(uint32_t fps) noexcept
{
_fps.setMaxFPS(fps);
}
void* Application::newGraphicsSuport(std::size_t w, std::size_t h, const char* title) void* Application::newGraphicsSuport(std::size_t w, std::size_t h, const char* title)
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();

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: 2024/01/10 19:54:51 by maldavid ### ########.fr */ /* Updated: 2024/01/18 15:02:06 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -47,6 +47,11 @@ 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)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if(w <= 0 || h <= 0)
{
mlx::core::error::report(e_kind::fatal_error, "invalid window size (%d x %d)", w, h);
return NULL; // not nullptr for the C compatibility
}
return static_cast<mlx::core::Application*>(mlx)->newGraphicsSuport(w, h, title); return static_cast<mlx::core::Application*>(mlx)->newGraphicsSuport(w, h, title);
} }
@ -73,12 +78,14 @@ extern "C"
int mlx_mouse_show() int mlx_mouse_show()
{ {
return SDL_ShowCursor(SDL_ENABLE); SDL_ShowCursor(SDL_ENABLE);
return 0;
} }
int mlx_mouse_hide() int mlx_mouse_hide()
{ {
return SDL_ShowCursor(SDL_DISABLE); SDL_ShowCursor(SDL_DISABLE);
return 0;
} }
int mlx_mouse_move(void* mlx, void* win, int x, int y) int mlx_mouse_move(void* mlx, void* win, int x, int y)
@ -105,6 +112,8 @@ extern "C"
void* mlx_new_image(void* mlx, int width, int height) void* mlx_new_image(void* mlx, int width, int height)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (width <= 0 || height <= 0)
mlx::core::error::report(e_kind::fatal_error, "invalid image size (%d x %d)", width, height);
return static_cast<mlx::core::Application*>(mlx)->newTexture(width, height); return static_cast<mlx::core::Application*>(mlx)->newTexture(width, height);
} }
@ -142,6 +151,8 @@ extern "C"
void* mlx_png_file_to_image(void* mlx, char* filename, int* width, int* height) void* mlx_png_file_to_image(void* mlx, char* filename, int* width, int* height)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (filename == nullptr)
mlx::core::error::report(e_kind::fatal_error, "PNG loader : filename is NULL");
std::filesystem::path file(filename); std::filesystem::path file(filename);
if(file.extension() != ".png") if(file.extension() != ".png")
{ {
@ -154,6 +165,8 @@ extern "C"
void* mlx_jpg_file_to_image(void* mlx, char* filename, int* width, int* height) void* mlx_jpg_file_to_image(void* mlx, char* filename, int* width, int* height)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (filename == nullptr)
mlx::core::error::report(e_kind::fatal_error, "JPG loader : filename is NULL");
std::filesystem::path file(filename); std::filesystem::path file(filename);
if(file.extension() != ".jpg" && file.extension() != ".jpeg") if(file.extension() != ".jpg" && file.extension() != ".jpeg")
{ {
@ -166,6 +179,8 @@ extern "C"
void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height) void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (filename == nullptr)
mlx::core::error::report(e_kind::fatal_error, "BMP loader : filename is NULL");
std::filesystem::path file(filename); std::filesystem::path file(filename);
if(file.extension() != ".bmp" && file.extension() != ".dib") if(file.extension() != ".bmp" && file.extension() != ".dib")
{ {
@ -202,6 +217,11 @@ extern "C"
void mlx_set_font(void* mlx, void* win, char* filepath) void mlx_set_font(void* mlx, void* win, char* filepath)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (filepath == nullptr)
{
mlx::core::error::report(e_kind::error, "Font loader : filepath is NULL");
return;
}
std::filesystem::path file(filepath); std::filesystem::path file(filepath);
if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte") if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte")
{ {
@ -214,6 +234,11 @@ extern "C"
void mlx_set_font_scale(void* mlx, void* win, char* filepath, float scale) void mlx_set_font_scale(void* mlx, void* win, char* filepath, float scale)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (filepath == nullptr)
{
mlx::core::error::report(e_kind::error, "Font loader : filepath is NULL");
return;
}
std::filesystem::path file(filepath); std::filesystem::path file(filepath);
if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte") if(std::strcmp(filepath, "default") != 0 && file.extension() != ".ttf" && file.extension() != ".tte")
{ {
@ -246,10 +271,27 @@ extern "C"
return 0; return 0;
} }
int mlx_get_screens_size(void* mlx, int* w, int* h) int mlx_get_screens_size(void* mlx, void* win, int* w, int* h)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::core::Application*>(mlx)->getScreenSize(w, h); static_cast<mlx::core::Application*>(mlx)->getScreenSize(win, w, h);
return 0;
}
int mlx_set_fps_goal(void* mlx, int fps)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if(fps < 0)
{
mlx::core::error::report(e_kind::error, "You cannot set a negative FPS cap (nice try)");
fps = -fps;
}
if(fps == 0)
{
mlx::core::error::report(e_kind::error, "You cannot set a FPS cap to 0 (nice try)");
return 0;
}
static_cast<mlx::core::Application*>(mlx)->setFPSCap(static_cast<uint32_t>(fps));
return 0; return 0;
} }
} }

View File

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fps.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 14:56:17 by maldavid #+# #+# */
/* Updated: 2024/01/18 15:20:03 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <core/fps.h>
#include <chrono>
#include <SDL2/SDL.h>
#include <thread>
namespace mlx
{
void FpsManager::init()
{
_timer = SDL_GetTicks64();
_fps_before = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
_fps_now = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
}
bool FpsManager::update()
{
using namespace std::chrono_literals;
_fps_now = static_cast<uint64_t>(std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count());
if(SDL_GetTicks64() - _timer > 1000)
_timer += 1000;
_fps_elapsed_time = _fps_now - _fps_before;
if(_fps_elapsed_time >= _ns)
{
_fps_before += _ns;
return true;
}
std::this_thread::sleep_for(std::chrono::duration<double, std::nano>(_ns));
return false;
}
}

41
MacroLibX/src/core/fps.h Normal file
View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fps.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 14:53:30 by maldavid #+# #+# */
/* Updated: 2024/01/18 15:16:06 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_FPS__
#define __MLX_FPS__
#include <cstdint>
namespace mlx
{
class FpsManager
{
public:
FpsManager() = default;
void init();
bool update();
inline void setMaxFPS(uint32_t fps) noexcept { _max_fps = fps; _ns = 1000000000.0 / fps; }
~FpsManager() = default;
private:
double _ns = 1000000000.0 / 1'337'000.0;
uint64_t _timer = 0;
uint64_t _fps_before = 0;
uint64_t _fps_now = 0;
uint32_t _max_fps = 1'337'000;
uint32_t _fps_elapsed_time = 0;
};
}
#endif

View File

@ -6,13 +6,12 @@
/* 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: 2024/01/10 18:31:13 by maldavid ### ########.fr */ /* Updated: 2024/01/16 07:59:15 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "inputs.h" #include "inputs.h"
#include <mlx.h> #include <mlx.h>
#include <cstring>
#include <core/profiler.h> #include <core/profiler.h>
namespace mlx namespace mlx
@ -35,7 +34,7 @@ namespace mlx
} }
uint32_t id = _event.window.windowID; uint32_t id = _event.window.windowID;
if(!_events_hooks.count(id)) if(_events_hooks.find(id) == _events_hooks.end())
continue; continue;
auto& hooks = _events_hooks[id]; auto& hooks = _events_hooks[id];

View File

@ -6,15 +6,14 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */ /* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */
/* Updated: 2023/12/11 19:47:20 by vavaas ### ########.fr */ /* Updated: 2024/01/16 07:59:08 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <array> #include <array>
#include <memory> #include <memory>
#include <vector>
#include <cstdint> #include <cstdint>
#include <functional> #include <function.h>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <unordered_map> #include <unordered_map>
@ -26,7 +25,7 @@ namespace mlx
{ {
struct Hook struct Hook
{ {
std::function<int(int, void*)> hook; func::function<int(int, void*)> hook;
void* param = nullptr; void* param = nullptr;
}; };

View File

@ -6,14 +6,13 @@
/* 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/12/27 16:57:28 by maldavid ### ########.fr */ /* Updated: 2024/01/16 07:59:21 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <platform/window.h> #include <platform/window.h>
#include <core/errors.h> #include <core/errors.h>
#include <utils/icon_mlx.h> #include <utils/icon_mlx.h>
#include <iostream>
namespace mlx namespace mlx
{ {

View File

@ -6,7 +6,7 @@
/* By: kbz_8 <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: kbz_8 <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/20 22:02:37 by kbz_8 #+# #+# */ /* Created: 2023/10/20 22:02:37 by kbz_8 #+# #+# */
/* Updated: 2024/01/10 21:54:35 by maldavid ### ########.fr */ /* Updated: 2024/01/18 10:11:02 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -98,7 +98,7 @@ namespace mlx
vmaSetAllocationName(_allocator, allocation, name); vmaSetAllocationName(_allocator, allocation, name);
} }
#ifdef DEBUG #ifdef DEBUG
core::error::report(e_kind::message, "Graphics Allocator : created new buffer"); core::error::report(e_kind::message, "Graphics Allocator : created new buffer '%s'", name);
#endif #endif
_active_buffers_allocations++; _active_buffers_allocations++;
return allocation; return allocation;
@ -128,7 +128,7 @@ namespace mlx
vmaSetAllocationName(_allocator, allocation, name); vmaSetAllocationName(_allocator, allocation, name);
} }
#ifdef DEBUG #ifdef DEBUG
core::error::report(e_kind::message, "Graphics Allocator : created new image"); core::error::report(e_kind::message, "Graphics Allocator : created new image '%s'", name);
#endif #endif
_active_images_allocations++; _active_images_allocations++;
return allocation; return allocation;

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/23 18:34:23 by maldavid #+# #+# */ /* Created: 2023/01/23 18:34:23 by maldavid #+# #+# */
/* Updated: 2024/01/03 13:13:54 by maldavid ### ########.fr */ /* Updated: 2024/01/18 10:19:55 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,6 +22,7 @@ namespace mlx
poolInfo.poolSizeCount = n; poolInfo.poolSizeCount = n;
poolInfo.pPoolSizes = size; poolInfo.pPoolSizes = size;
poolInfo.maxSets = 8192; poolInfo.maxSets = 8192;
poolInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
VkResult res = vkCreateDescriptorPool(Render_Core::get().getDevice().get(), &poolInfo, nullptr, &_pool); VkResult res = vkCreateDescriptorPool(Render_Core::get().getDevice().get(), &poolInfo, nullptr, &_pool);
if(res != VK_SUCCESS) if(res != VK_SUCCESS)

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/23 18:32:43 by maldavid #+# #+# */ /* Created: 2023/01/23 18:32:43 by maldavid #+# #+# */
/* Updated: 2024/01/03 15:27:45 by maldavid ### ########.fr */ /* Updated: 2024/01/18 10:22:20 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,6 +28,8 @@ namespace mlx
inline VkDescriptorPool& operator()() noexcept { return _pool; } inline VkDescriptorPool& operator()() noexcept { return _pool; }
inline VkDescriptorPool& get() noexcept { return _pool; } inline VkDescriptorPool& get() noexcept { return _pool; }
inline bool isInit() const noexcept { return _pool != VK_NULL_HANDLE; }
private: private:
VkDescriptorPool _pool = VK_NULL_HANDLE; VkDescriptorPool _pool = VK_NULL_HANDLE;
}; };

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/23 18:40:44 by maldavid #+# #+# */ /* Created: 2023/01/23 18:40:44 by maldavid #+# #+# */
/* Updated: 2024/01/10 18:28:34 by maldavid ### ########.fr */ /* Updated: 2024/01/18 10:22:10 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -106,8 +106,24 @@ namespace mlx
{ {
return _desc_set[_renderer->getActiveImageIndex()]; return _desc_set[_renderer->getActiveImageIndex()];
} }
VkDescriptorSet& DescriptorSet::get() noexcept VkDescriptorSet& DescriptorSet::get() noexcept
{ {
return _desc_set[_renderer->getActiveImageIndex()]; return _desc_set[_renderer->getActiveImageIndex()];
} }
void DescriptorSet::destroy() noexcept
{
MLX_PROFILE_FUNCTION();
if(_pool->isInit())
vkFreeDescriptorSets(Render_Core::get().getDevice().get(), _pool->get(), _desc_set.size(), _desc_set.data());
for(auto& set : _desc_set)
{
if(set != VK_NULL_HANDLE)
set = VK_NULL_HANDLE;
}
#ifdef DEBUG
core::error::report(e_kind::message, "Vulkan : destroyed descriptor set");
#endif
}
} }

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/23 18:39:36 by maldavid #+# #+# */ /* Created: 2023/01/23 18:39:36 by maldavid #+# #+# */
/* Updated: 2024/01/03 15:27:50 by maldavid ### ########.fr */ /* Updated: 2024/01/18 10:13:25 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,6 +35,8 @@ namespace mlx
VkDescriptorSet& operator()() noexcept; VkDescriptorSet& operator()() noexcept;
VkDescriptorSet& get() noexcept; VkDescriptorSet& get() noexcept;
void destroy() noexcept;
private: private:
std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT> _desc_set; std::array<VkDescriptorSet, MAX_FRAMES_IN_FLIGHT> _desc_set;
class DescriptorPool* _pool = nullptr; class DescriptorPool* _pool = nullptr;

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: 2024/01/11 01:20:29 by maldavid ### ########.fr */ /* Updated: 2024/01/18 10:18:22 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -155,6 +155,7 @@ namespace mlx
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
Image::destroy(); Image::destroy();
_set.destroy();
if(_buf_map.has_value()) if(_buf_map.has_value())
_buf_map->destroy(); _buf_map->destroy();
_vbo.destroy(); _vbo.destroy();

View File

@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/07 16:40:09 by maldavid #+# #+# */ /* Created: 2023/04/07 16:40:09 by maldavid #+# #+# */
/* Updated: 2023/12/31 00:52:01 by maldavid ### ########.fr */ /* Updated: 2024/01/18 10:18:08 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -51,5 +51,6 @@ namespace mlx
void TextureAtlas::destroy() noexcept void TextureAtlas::destroy() noexcept
{ {
Image::destroy(); Image::destroy();
_set.destroy();
} }
} }

View File

@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/07 16:36:33 by maldavid #+# #+# */ /* Created: 2023/04/07 16:36:33 by maldavid #+# #+# */
/* Updated: 2024/01/08 21:42:31 by maldavid ### ########.fr */ /* Updated: 2024/01/18 02:47:30 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,7 +30,8 @@ namespace mlx
void destroy() noexcept override; void destroy() noexcept override;
inline void setDescriptor(DescriptorSet&& set) noexcept { _set = set; } inline void setDescriptor(DescriptorSet&& set) noexcept { _set = set; }
inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; } inline VkDescriptorSet getVkSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; }
inline DescriptorSet getSet() noexcept { return _set; }
inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, *this); _has_been_updated = true; } inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, *this); _has_been_updated = true; }
inline bool hasBeenUpdated() const noexcept { return _has_been_updated; } inline bool hasBeenUpdated() const noexcept { return _has_been_updated; }
inline constexpr void resetUpdate() noexcept { _has_been_updated = false; } inline constexpr void resetUpdate() noexcept { _has_been_updated = false; }

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: 2024/01/07 01:17:54 by maldavid ### ########.fr */ /* Updated: 2024/01/18 09:47:26 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -252,16 +252,16 @@ namespace mlx
void Image::destroy() noexcept void Image::destroy() noexcept
{ {
// not creating destroyer in `create` as some image may be copied (and so `this` will be invalid) // not creating destroyer in `create` as some image may be copied (and so `this` will be invalid)
CmdResource::setDestroyer([this]() //CmdResource::setDestroyer([this]()
{ //{
destroySampler(); destroySampler();
destroyImageView(); destroyImageView();
if(_image != VK_NULL_HANDLE) if(_image != VK_NULL_HANDLE)
Render_Core::get().getAllocator().destroyImage(_allocation, _image); Render_Core::get().getAllocator().destroyImage(_allocation, _image);
_image = VK_NULL_HANDLE; _image = VK_NULL_HANDLE;
}); //});
CmdResource::requireDestroy(); //CmdResource::requireDestroy();
} }
uint32_t formatSize(VkFormat format) uint32_t formatSize(VkFormat format)

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 21:27:38 by maldavid #+# #+# */ /* Created: 2022/12/18 21:27:38 by maldavid #+# #+# */
/* Updated: 2024/01/10 21:53:38 by maldavid ### ########.fr */ /* Updated: 2024/01/16 07:45:15 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -49,7 +49,7 @@ namespace mlx
gl_Position = uProj.mat * vec4(pos.x, pos.y, 0.0, 1.0); gl_Position = uProj.mat * vec4(pos.x, pos.y, 0.0, 1.0);
} }
*/ */
const std::vector<uint32_t> vertex_shader = { const std::vector<uint32_t> vertex_shader = { // precompiled vertex shader
0x07230203,0x00010000,0x0008000b,0x0000003b,0x00000000,0x00020011,0x00000001,0x0006000b, 0x07230203,0x00010000,0x0008000b,0x0000003b,0x00000000,0x00020011,0x00000001,0x0006000b,
0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
0x000a000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000b,0x0000000f,0x00000015, 0x000a000f,0x00000000,0x00000004,0x6e69616d,0x00000000,0x0000000b,0x0000000f,0x00000015,
@ -124,7 +124,7 @@ namespace mlx
fColor = process_color; fColor = process_color;
} }
*/ */
const std::vector<uint32_t> fragment_shader = { const std::vector<uint32_t> fragment_shader = { // pre compiled fragment shader
0x07230203,0x00010000,0x0008000b,0x0000002c,0x00000000,0x00020011,0x00000001,0x0006000b, 0x07230203,0x00010000,0x0008000b,0x0000002c,0x00000000,0x00020011,0x00000001,0x0006000b,
0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001, 0x00000001,0x4c534c47,0x6474732e,0x3035342e,0x00000000,0x0003000e,0x00000000,0x00000001,
0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x0000000d,0x0000002a,0x00030010, 0x0007000f,0x00000004,0x00000004,0x6e69616d,0x00000000,0x0000000d,0x0000002a,0x00030010,

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: 2024/01/10 14:18:35 by maldavid ### ########.fr */ /* Updated: 2024/01/16 08:02:57 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -82,7 +82,7 @@ namespace mlx
if(result == VK_ERROR_OUT_OF_DATE_KHR) if(result == VK_ERROR_OUT_OF_DATE_KHR)
{ {
_swapchain.recreate(); recreateRenderData();
return false; return false;
} }
else if(result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR) else if(result != VK_SUCCESS && result != VK_SUBOPTIMAL_KHR)
@ -145,7 +145,7 @@ namespace mlx
if(result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || _framebufferResized) if(result == VK_ERROR_OUT_OF_DATE_KHR || result == VK_SUBOPTIMAL_KHR || _framebufferResized)
{ {
_framebufferResized = false; _framebufferResized = false;
_swapchain.recreate(); recreateRenderData();
} }
else if(result != VK_SUCCESS) else if(result != VK_SUCCESS)
core::error::report(e_kind::fatal_error, "Vulkan error : failed to present swap chain image"); core::error::report(e_kind::fatal_error, "Vulkan error : failed to present swap chain image");
@ -158,6 +158,18 @@ namespace mlx
} }
} }
void Renderer::recreateRenderData()
{
_swapchain.recreate();
_pass.destroy();
_pass.init(_swapchain.getImagesFormat(), VK_IMAGE_LAYOUT_PRESENT_SRC_KHR);
for(auto& fb : _framebuffers)
fb.destroy();
_framebuffers.clear();
for(std::size_t i = 0; i < _swapchain.getImagesNumber(); i++)
_framebuffers.emplace_back().init(_pass, _swapchain.getImage(i));
}
void Renderer::destroy() void Renderer::destroy()
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();

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/12/22 21:59:15 by kbz_8 ### ########.fr */ /* Updated: 2024/01/16 08:01:25 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -113,6 +113,9 @@ namespace mlx
~Renderer() = default; ~Renderer() = default;
private:
void recreateRenderData();
private: private:
GraphicPipeline _pipeline; GraphicPipeline _pipeline;
CmdManager _cmd; CmdManager _cmd;

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/06 18:21:36 by maldavid #+# #+# */ /* Created: 2022/10/06 18:21:36 by maldavid #+# #+# */
/* Updated: 2024/01/10 21:53:03 by maldavid ### ########.fr */ /* Updated: 2024/01/16 08:22:39 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,7 +18,7 @@
namespace mlx namespace mlx
{ {
static const VkClearValue clearColor = {{{ 0.0f, 0.0f, 0.0f, 1.0f }}}; // wtf, this mess to satisfy a warning static const VkClearValue clearColor = {{{ 0.f, 0.f, 0.f, 1.0f }}}; // wtf, this mess to satisfy a warning
void RenderPass::init(VkFormat attachement_format, VkImageLayout layout) void RenderPass::init(VkFormat attachement_format, VkImageLayout layout)
{ {

View File

@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 22:06:09 by kbz_8 #+# #+# */ /* Created: 2023/12/11 22:06:09 by kbz_8 #+# #+# */
/* Updated: 2024/01/11 01:23:20 by maldavid ### ########.fr */ /* Updated: 2024/01/18 13:16:18 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,51 +19,43 @@ constexpr const int RANGE = 1024;
namespace mlx namespace mlx
{ {
Font::Font(Renderer& renderer, const std::filesystem::path& path, float scale) : non_copyable(), _name(path.string()), _scale(scale) Font::Font(Renderer& renderer, const std::filesystem::path& path, float scale) : _name(path.string()), _renderer(renderer), _scale(scale)
{
_build_data = path;
}
Font::Font(class Renderer& renderer, const std::string& name, const std::vector<uint8_t>& ttf_data, float scale) : _name(name), _renderer(renderer), _scale(scale)
{
_build_data = ttf_data;
}
void Font::buildFont()
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
std::vector<uint8_t> tmp_bitmap(RANGE * RANGE); std::vector<uint8_t> file_bytes;
std::vector<uint8_t> vulkan_bitmap(RANGE * RANGE * 4); if(std::holds_alternative<std::filesystem::path>(_build_data))
{
std::ifstream file(path, std::ios::binary); std::ifstream file(std::get<std::filesystem::path>(_build_data), std::ios::binary);
if(!file.is_open()) if(!file.is_open())
{ {
core::error::report(e_kind::error, "Font load : cannot open font file, %s", _name.c_str()); core::error::report(e_kind::error, "Font load : cannot open font file, %s", _name.c_str());
return; return;
} }
std::ifstream::pos_type fileSize = std::filesystem::file_size(path); std::ifstream::pos_type fileSize = std::filesystem::file_size(std::get<std::filesystem::path>(_build_data));
file.seekg(0, std::ios::beg); file.seekg(0, std::ios::beg);
std::vector<uint8_t> bytes(fileSize); file_bytes.resize(fileSize);
file.read(reinterpret_cast<char*>(bytes.data()), fileSize); file.read(reinterpret_cast<char*>(file_bytes.data()), fileSize);
file.close(); file.close();
stbtt_pack_context pc;
stbtt_PackBegin(&pc, tmp_bitmap.data(), RANGE, RANGE, RANGE, 1, nullptr);
stbtt_PackFontRange(&pc, bytes.data(), 0, scale, 32, 96, _cdata.data());
stbtt_PackEnd(&pc);
for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4)
{
vulkan_bitmap[j + 0] = tmp_bitmap[i];
vulkan_bitmap[j + 1] = tmp_bitmap[i];
vulkan_bitmap[j + 2] = tmp_bitmap[i];
vulkan_bitmap[j + 3] = tmp_bitmap[i];
}
#ifdef DEBUG
_atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, std::string(_name + "_font_altas").c_str(), true);
#else
_atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, nullptr, true);
#endif
_atlas.setDescriptor(renderer.getFragDescriptorSet().duplicate());
} }
Font::Font(class Renderer& renderer, const std::string& name, const std::vector<uint8_t>& ttf_data, float scale) : non_copyable(), _name(name), _scale(scale)
{
MLX_PROFILE_FUNCTION();
std::vector<uint8_t> tmp_bitmap(RANGE * RANGE); std::vector<uint8_t> tmp_bitmap(RANGE * RANGE);
std::vector<uint8_t> vulkan_bitmap(RANGE * RANGE * 4); std::vector<uint8_t> vulkan_bitmap(RANGE * RANGE * 4);
stbtt_pack_context pc; stbtt_pack_context pc;
stbtt_PackBegin(&pc, tmp_bitmap.data(), RANGE, RANGE, RANGE, 1, nullptr); stbtt_PackBegin(&pc, tmp_bitmap.data(), RANGE, RANGE, RANGE, 1, nullptr);
stbtt_PackFontRange(&pc, ttf_data.data(), 0, scale, 32, 96, _cdata.data()); if(std::holds_alternative<std::filesystem::path>(_build_data))
stbtt_PackFontRange(&pc, file_bytes.data(), 0, _scale, 32, 96, _cdata.data());
else
stbtt_PackFontRange(&pc, std::get<std::vector<uint8_t>>(_build_data).data(), 0, _scale, 32, 96, _cdata.data());
stbtt_PackEnd(&pc); stbtt_PackEnd(&pc);
for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4) for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4)
{ {
@ -77,12 +69,20 @@ namespace mlx
#else #else
_atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, nullptr, true); _atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, nullptr, true);
#endif #endif
_atlas.setDescriptor(renderer.getFragDescriptorSet().duplicate()); _atlas.setDescriptor(_renderer.getFragDescriptorSet().duplicate());
_is_init = true;
}
void Font::destroy()
{
MLX_PROFILE_FUNCTION();
_atlas.destroy();
_is_init = false;
} }
Font::~Font() Font::~Font()
{ {
MLX_PROFILE_FUNCTION(); if(_is_init)
_atlas.destroy(); destroy();
} }
} }

View File

@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 21:17:04 by kbz_8 #+# #+# */ /* Created: 2023/12/11 21:17:04 by kbz_8 #+# #+# */
/* Updated: 2023/12/14 17:51:40 by maldavid ### ########.fr */ /* Updated: 2024/01/18 13:15:55 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,45 +15,41 @@
#include <array> #include <array>
#include <stb_truetype.h> #include <stb_truetype.h>
#include <utils/non_copyable.h>
#include <renderer/images/texture_atlas.h> #include <renderer/images/texture_atlas.h>
#include <utils/combine_hash.h> #include <utils/combine_hash.h>
#include <variant>
namespace mlx namespace mlx
{ {
class Font : public non_copyable class Font
{ {
friend class FontLibrary;
public: public:
Font() = delete; Font() = delete;
Font(class Renderer& renderer, const std::filesystem::path& path, float scale); Font(class Renderer& renderer, const std::filesystem::path& path, float scale);
Font(class Renderer& renderer, const std::string& name, const std::vector<uint8_t>& ttf_data, float scale); Font(class Renderer& renderer, const std::string& name, const std::vector<uint8_t>& ttf_data, float scale);
inline const std::string& getName() const { return _name; } inline const std::string& getName() const { return _name; }
inline float getScale() const noexcept { return _scale; } inline float getScale() const noexcept { return _scale; }
inline const std::array<stbtt_packedchar, 96>& getCharData() const { return _cdata; } inline const std::array<stbtt_packedchar, 96>& getCharData() const { return _cdata; }
inline const TextureAtlas& getAtlas() const noexcept { return _atlas; } inline const TextureAtlas& getAtlas() const noexcept { return _atlas; }
inline bool operator==(const Font& rhs) const { return rhs._name == _name; } inline bool operator==(const Font& rhs) const { return rhs._name == _name && rhs._scale == _scale; }
inline bool operator!=(const Font& rhs) const { return rhs._name != _name; } inline bool operator!=(const Font& rhs) const { return rhs._name != _name || rhs._scale != _scale; }
void destroy();
~Font(); ~Font();
private:
void buildFont();
private: private:
std::array<stbtt_packedchar, 96> _cdata; std::array<stbtt_packedchar, 96> _cdata;
TextureAtlas _atlas; TextureAtlas _atlas;
std::variant<std::filesystem::path, std::vector<uint8_t>> _build_data;
std::string _name; std::string _name;
class Renderer& _renderer;
float _scale = 0; float _scale = 0;
}; bool _is_init = false;
}
namespace std
{
template <>
struct hash<mlx::Font>
{
std::size_t operator()(const mlx::Font& f) const noexcept
{
std::size_t hash = 0;
mlx::hashCombine(hash, f.getName(), f.getScale());
return hash;
}
}; };
} }

View File

@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* font_library.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 09:28:14 by maldavid #+# #+# */
/* Updated: 2024/01/18 13:07:48 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <renderer/texts/font_library.h>
#include <renderer/texts/font.h>
#include <core/errors.h>
#include <renderer/renderer.h>
#include <algorithm>
#include <core/profiler.h>
namespace mlx
{
std::shared_ptr<Font> FontLibrary::getFontData(FontID id)
{
MLX_PROFILE_FUNCTION();
if(!_cache.count(id) || std::find(_invalid_ids.begin(), _invalid_ids.end(), id) != _invalid_ids.end())
core::error::report(e_kind::fatal_error, "Font Library : wrong font ID '%d'", id);
return _cache[id];
}
FontID FontLibrary::addFontToLibrary(std::shared_ptr<Font> font)
{
MLX_PROFILE_FUNCTION();
auto it = std::find_if(_cache.begin(), _cache.end(), [&](const std::pair<FontID, std::shared_ptr<Font>>& v)
{
return v.second->getScale() == font->getScale() &&
v.second->getName() == font->getName() &&
std::find(_invalid_ids.begin(), _invalid_ids.end(), v.first) == _invalid_ids.end();
});
if(it != _cache.end())
return it->first;
font->buildFont();
_cache[_current_id] = font;
_current_id++;
return _current_id - 1;
}
void FontLibrary::removeFontFromLibrary(FontID id)
{
MLX_PROFILE_FUNCTION();
if(!_cache.count(id) || std::find(_invalid_ids.begin(), _invalid_ids.end(), id) != _invalid_ids.end())
{
core::error::report(e_kind::warning, "Font Library : trying to remove a font with an unkown or invalid ID '%d'", id);
return;
}
_cache[id]->destroy();
_invalid_ids.push_back(id);
}
void FontLibrary::clearLibrary()
{
MLX_PROFILE_FUNCTION();
for(auto& [id, font] : _cache)
{
font->destroy();
_invalid_ids.push_back(id);
}
// do not `_cache.clear();` as it releases the fonts and may not destroy the texture atlas that is in use by command buffers
}
}

View File

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* font_library.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/18 09:26:03 by maldavid #+# #+# */
/* Updated: 2024/01/18 09:33:30 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef __MLX_FONT_LIBRARY__
#define __MLX_FONT_LIBRARY__
#include <unordered_map>
#include <memory>
#include <vector>
#include <cstdint>
#include <mlx_profile.h>
#include <renderer/texts/font.h>
#include <renderer/core/render_core.h>
#include <utils/singleton.h>
namespace mlx
{
using FontID = uint32_t;
constexpr FontID nullfont = 0;
class FontLibrary : public Singleton<FontLibrary>
{
friend class Singleton<FontLibrary>;
public:
std::shared_ptr<class Font> getFontData(FontID id);
FontID addFontToLibrary(std::shared_ptr<Font> font);
void removeFontFromLibrary(FontID id);
void clearLibrary();
private:
FontLibrary() = default;
~FontLibrary() = default;
private:
std::unordered_map<FontID, std::shared_ptr<class Font>> _cache;
std::vector<FontID> _invalid_ids;
FontID _current_id = 1;
};
}
#endif

View File

@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 00:11:56 by maldavid #+# #+# */ /* Created: 2024/01/11 00:11:56 by maldavid #+# #+# */
/* Updated: 2024/01/11 03:31:57 by maldavid ### ########.fr */ /* Updated: 2024/01/18 13:56:50 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,15 +16,21 @@
namespace mlx namespace mlx
{ {
void Text::init(std::string text, Font const* font, std::vector<Vertex> vbo_data, std::vector<uint16_t> ibo_data) void Text::init(std::string text, FontID font, std::vector<Vertex> vbo_data, std::vector<uint16_t> ibo_data)
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
_text = std::move(text); _text = std::move(text);
_font = font; _font = font;
#ifdef DEBUG #ifdef DEBUG
std::string debug_name = _text;
for(char& c : debug_name)
{
if(c == ' ' || c == '"' || c == '\'')
c = '_';
}
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
_vbo[i].create(sizeof(Vertex) * vbo_data.size(), static_cast<const void*>(vbo_data.data()), _text.c_str()); _vbo[i].create(sizeof(Vertex) * vbo_data.size(), static_cast<const void*>(vbo_data.data()), debug_name.c_str());
_ibo.create(sizeof(uint16_t) * ibo_data.size(), ibo_data.data(), _text.c_str()); _ibo.create(sizeof(uint16_t) * ibo_data.size(), ibo_data.data(), debug_name.c_str());
#else #else
for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) for(int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++)
_vbo[i].create(sizeof(Vertex) * vbo_data.size(), static_cast<const void*>(vbo_data.data()), nullptr); _vbo[i].create(sizeof(Vertex) * vbo_data.size(), static_cast<const void*>(vbo_data.data()), nullptr);

View File

@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 00:09:04 by maldavid #+# #+# */ /* Created: 2024/01/11 00:09:04 by maldavid #+# #+# */
/* Updated: 2024/01/11 00:13:25 by maldavid ### ########.fr */ /* Updated: 2024/01/18 09:37:42 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <renderer/texts/font.h> #include <renderer/texts/font.h>
#include <renderer/texts/font_library.h>
#include <renderer/buffers/vk_ibo.h> #include <renderer/buffers/vk_ibo.h>
#include <renderer/buffers/vk_vbo.h> #include <renderer/buffers/vk_vbo.h>
@ -26,9 +27,9 @@ namespace mlx
public: public:
Text() = default; Text() = default;
void init(std::string text, Font const* font, std::vector<Vertex> vbo_data, std::vector<uint16_t> ibo_data); void init(std::string text, FontID font, std::vector<Vertex> vbo_data, std::vector<uint16_t> ibo_data);
void bind(class Renderer& renderer) noexcept; void bind(class Renderer& renderer) noexcept;
inline const Font& getFontInUse() const noexcept { return *_font; } inline FontID getFontInUse() const noexcept { return _font; }
void updateVertexData(int frame, std::vector<Vertex> vbo_data); void updateVertexData(int frame, std::vector<Vertex> vbo_data);
inline uint32_t getIBOsize() noexcept { return _ibo.getSize(); } inline uint32_t getIBOsize() noexcept { return _ibo.getSize(); }
inline const std::string& getText() const { return _text; } inline const std::string& getText() const { return _text; }
@ -40,7 +41,7 @@ namespace mlx
std::array<D_VBO, MAX_FRAMES_IN_FLIGHT> _vbo; std::array<D_VBO, MAX_FRAMES_IN_FLIGHT> _vbo;
C_IBO _ibo; C_IBO _ibo;
std::string _text; std::string _text;
Font const* _font = nullptr; FontID _font = nullfont;
}; };
} }

View File

@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 00:23:11 by maldavid #+# #+# */ /* Created: 2024/01/11 00:23:11 by maldavid #+# #+# */
/* Updated: 2024/01/11 03:40:54 by maldavid ### ########.fr */ /* Updated: 2024/01/18 09:44:54 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,7 +33,7 @@ namespace mlx
TextDrawDescriptor::TextDrawDescriptor(std::string text, uint32_t _color, int _x, int _y) : color(_color), x(_x), y(_y), _text(std::move(text)) TextDrawDescriptor::TextDrawDescriptor(std::string text, uint32_t _color, int _x, int _y) : color(_color), x(_x), y(_y), _text(std::move(text))
{} {}
void TextDrawDescriptor::init(Font* const font) noexcept void TextDrawDescriptor::init(FontID font) noexcept
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
std::vector<Vertex> vertexData; std::vector<Vertex> vertexData;
@ -42,13 +42,16 @@ namespace mlx
float stb_x = 0.0f; float stb_x = 0.0f;
float stb_y = 0.0f; float stb_y = 0.0f;
{
std::shared_ptr<Font> font_data = FontLibrary::get().getFontData(font);
for(char c : _text) for(char c : _text)
{ {
if(c < 32) if(c < 32)
continue; continue;
stbtt_aligned_quad q; stbtt_aligned_quad q;
stbtt_GetPackedQuad(font->getCharData().data(), RANGE, RANGE, c - 32, &stb_x, &stb_y, &q, 1); stbtt_GetPackedQuad(font_data->getCharData().data(), RANGE, RANGE, c - 32, &stb_x, &stb_y, &q, 1);
std::size_t index = vertexData.size(); std::size_t index = vertexData.size();
@ -71,6 +74,7 @@ namespace mlx
indexData.emplace_back(index + 3); indexData.emplace_back(index + 3);
indexData.emplace_back(index + 0); indexData.emplace_back(index + 0);
} }
}
std::shared_ptr<Text> text_data = std::make_shared<Text>(); std::shared_ptr<Text> text_data = std::make_shared<Text>();
text_data->init(_text, font, std::move(vertexData), std::move(indexData)); text_data->init(_text, font, std::move(vertexData), std::move(indexData));
id = TextLibrary::get().addTextToLibrary(text_data); id = TextLibrary::get().addTextToLibrary(text_data);
@ -84,13 +88,14 @@ namespace mlx
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
std::shared_ptr<Text> draw_data = TextLibrary::get().getTextData(id); std::shared_ptr<Text> draw_data = TextLibrary::get().getTextData(id);
TextureAtlas& atlas = const_cast<TextureAtlas&>(draw_data->getFontInUse().getAtlas()); std::shared_ptr<Font> font_data = FontLibrary::get().getFontData(draw_data->getFontInUse());
TextureAtlas& atlas = const_cast<TextureAtlas&>(font_data->getAtlas());
draw_data->bind(renderer); draw_data->bind(renderer);
if(atlas.getSet() == VK_NULL_HANDLE) if(!atlas.getSet().isInit())
atlas.setDescriptor(renderer.getFragDescriptorSet().duplicate()); atlas.setDescriptor(renderer.getFragDescriptorSet().duplicate());
if(!atlas.hasBeenUpdated()) if(!atlas.hasBeenUpdated())
atlas.updateSet(0); atlas.updateSet(0);
sets[1] = const_cast<TextureAtlas&>(atlas).getSet(); sets[1] = const_cast<TextureAtlas&>(atlas).getVkSet();
vkCmdBindDescriptorSets(renderer.getActiveCmdBuffer().get(), VK_PIPELINE_BIND_POINT_GRAPHICS, renderer.getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr); vkCmdBindDescriptorSets(renderer.getActiveCmdBuffer().get(), VK_PIPELINE_BIND_POINT_GRAPHICS, renderer.getPipeline().getPipelineLayout(), 0, sets.size(), sets.data(), 0, nullptr);
atlas.render(renderer, x, y, draw_data->getIBOsize()); atlas.render(renderer, x, y, draw_data->getIBOsize());
} }
@ -98,7 +103,8 @@ namespace mlx
void TextDrawDescriptor::resetUpdate() void TextDrawDescriptor::resetUpdate()
{ {
std::shared_ptr<Text> draw_data = TextLibrary::get().getTextData(id); std::shared_ptr<Text> draw_data = TextLibrary::get().getTextData(id);
TextureAtlas& atlas = const_cast<TextureAtlas&>(draw_data->getFontInUse().getAtlas()); std::shared_ptr<Font> font_data = FontLibrary::get().getFontData(draw_data->getFontInUse());
TextureAtlas& atlas = const_cast<TextureAtlas&>(font_data->getAtlas());
atlas.resetUpdate(); atlas.resetUpdate();
} }
} }

View File

@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 00:13:34 by maldavid #+# #+# */ /* Created: 2024/01/11 00:13:34 by maldavid #+# #+# */
/* Updated: 2024/01/11 04:28:58 by maldavid ### ########.fr */ /* Updated: 2024/01/18 09:40:06 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,6 +19,7 @@
#include <utils/combine_hash.h> #include <utils/combine_hash.h>
#include <renderer/core/drawable_resource.h> #include <renderer/core/drawable_resource.h>
#include <renderer/texts/text_library.h> #include <renderer/texts/text_library.h>
#include <renderer/texts/font_library.h>
#include <array> #include <array>
namespace mlx namespace mlx
@ -36,7 +37,7 @@ namespace mlx
public: public:
TextDrawDescriptor(std::string text, uint32_t _color, int _x, int _y); TextDrawDescriptor(std::string text, uint32_t _color, int _x, int _y);
void init(Font* const font) noexcept; void init(FontID font) noexcept;
bool operator==(const TextDrawDescriptor& rhs) const { return _text == rhs._text && x == rhs.x && y == rhs.y && color == rhs.color; } bool operator==(const TextDrawDescriptor& rhs) const { return _text == rhs._text && x == rhs.x && y == rhs.y && color == rhs.color; }
void render(std::array<VkDescriptorSet, 2>& sets, Renderer& renderer) override; void render(std::array<VkDescriptorSet, 2>& sets, Renderer& renderer) override;
void resetUpdate() override; void resetUpdate() override;

View File

@ -6,11 +6,10 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/10 11:59:57 by maldavid #+# #+# */ /* Created: 2023/04/10 11:59:57 by maldavid #+# #+# */
/* Updated: 2024/01/11 05:19:24 by maldavid ### ########.fr */ /* Updated: 2024/01/18 08:02:31 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <iostream>
#include <renderer/texts/text_library.h> #include <renderer/texts/text_library.h>
#include <renderer/texts/text.h> #include <renderer/texts/text.h>
#include <core/errors.h> #include <core/errors.h>

View File

@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/10 11:52:30 by maldavid #+# #+# */ /* Created: 2023/04/10 11:52:30 by maldavid #+# #+# */
/* Updated: 2024/01/11 05:08:04 by maldavid ### ########.fr */ /* Updated: 2024/01/16 08:54:15 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,7 +15,6 @@
#include <renderer/buffers/vk_vbo.h> #include <renderer/buffers/vk_vbo.h>
#include <renderer/buffers/vk_ibo.h> #include <renderer/buffers/vk_ibo.h>
#include <string>
#include <unordered_map> #include <unordered_map>
#include <memory> #include <memory>
#include <vector> #include <vector>

View File

@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */ /* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */
/* Updated: 2024/01/11 04:54:16 by maldavid ### ########.fr */ /* Updated: 2024/01/18 09:45:24 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,28 +14,28 @@
#include <renderer/texts/text_library.h> #include <renderer/texts/text_library.h>
#include <renderer/texts/text.h> #include <renderer/texts/text.h>
#include <renderer/texts/text_manager.h> #include <renderer/texts/text_manager.h>
#include <array>
#include <core/profiler.h> #include <core/profiler.h>
#include <fstream>
#include <utils/dogica_ttf.h> #include <utils/dogica_ttf.h>
#include <cstdio>
namespace mlx namespace mlx
{ {
void TextManager::init(Renderer& renderer) noexcept void TextManager::init(Renderer& renderer) noexcept
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
_font_in_use = &const_cast<Font&>(*_font_set.emplace(renderer, "default", dogica_ttf, 6.0f).first); loadFont(renderer, "default", 6.f);
} }
void TextManager::loadFont(Renderer& renderer, const std::filesystem::path& filepath, float scale) void TextManager::loadFont(Renderer& renderer, const std::filesystem::path& filepath, float scale)
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
if(filepath.string() == "default") // we're sure it is already loaded std::shared_ptr<Font> font;
_font_in_use = &const_cast<Font&>(*_font_set.emplace(renderer, "default", dogica_ttf, scale).first); if(filepath.string() == "default")
font = std::make_shared<Font>(renderer, "default", dogica_ttf, scale);
else else
_font_in_use = &const_cast<Font&>(*_font_set.emplace(renderer, filepath, scale).first); font = std::make_shared<Font>(renderer, filepath, scale);
_font_in_use = FontLibrary::get().addFontToLibrary(font);
} }
std::pair<DrawableResource*, bool> TextManager::registerText(int x, int y, uint32_t color, std::string str) std::pair<DrawableResource*, bool> TextManager::registerText(int x, int y, uint32_t color, std::string str)
@ -49,8 +49,9 @@ namespace mlx
} }
auto text_ptr = TextLibrary::get().getTextData(res.first->id); auto text_ptr = TextLibrary::get().getTextData(res.first->id);
if(*_font_in_use != text_ptr->getFontInUse()) if(_font_in_use != text_ptr->getFontInUse())
{ {
// TODO : update text vertex buffers rather than destroying it and recreating it
TextLibrary::get().removeTextFromLibrary(res.first->id); TextLibrary::get().removeTextFromLibrary(res.first->id);
const_cast<TextDrawDescriptor&>(*res.first).init(_font_in_use); const_cast<TextDrawDescriptor&>(*res.first).init(_font_in_use);
} }
@ -61,6 +62,5 @@ namespace mlx
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
_text_descriptors.clear(); _text_descriptors.clear();
_font_set.clear();
} }
} }

View File

@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/06 16:24:11 by maldavid #+# #+# */ /* Created: 2023/04/06 16:24:11 by maldavid #+# #+# */
/* Updated: 2024/01/11 05:18:42 by maldavid ### ########.fr */ /* Updated: 2024/01/18 13:52:01 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,9 +19,12 @@
#include <stb_truetype.h> #include <stb_truetype.h>
#include <cstdint> #include <cstdint>
#include <unordered_set> #include <unordered_set>
#include <vector>
#include <mlx_profile.h> #include <mlx_profile.h>
#include <renderer/texts/text_descriptor.h> #include <renderer/texts/text_descriptor.h>
#include <renderer/texts/text_library.h> #include <renderer/texts/text_library.h>
#include <renderer/texts/font_library.h>
#include <memory>
namespace mlx namespace mlx
{ {
@ -40,8 +43,7 @@ namespace mlx
private: private:
std::unordered_set<TextDrawDescriptor> _text_descriptors; std::unordered_set<TextDrawDescriptor> _text_descriptors;
std::unordered_set<Font> _font_set; FontID _font_in_use = nullfont;
Font* _font_in_use = nullptr;
}; };
} }

0
MacroLibX/third_party/glm/common.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/_features.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/_fixes.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/_noise.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/_swizzle.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/_swizzle_func.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/_vectorize.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/compute_common.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/compute_vector_relational.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_common.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_common_simd.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_exponential.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_exponential_simd.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_geometric.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_geometric_simd.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_integer.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_integer_simd.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_matrix.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_matrix_simd.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_packing.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_packing_simd.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_trigonometric.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_trigonometric_simd.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_vector_relational.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/func_vector_relational_simd.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/glm.cpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/qualifier.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/setup.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_float.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_half.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_half.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat2x2.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat2x2.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat2x3.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat2x3.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat2x4.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat2x4.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat3x2.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat3x2.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat3x3.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat3x3.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat3x4.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat3x4.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat4x2.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat4x2.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat4x3.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat4x3.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat4x4.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat4x4.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_mat4x4_simd.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_quat.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_quat.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_quat_simd.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_vec1.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_vec1.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_vec2.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_vec2.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_vec3.hpp vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_vec3.inl vendored Normal file → Executable file
View File

0
MacroLibX/third_party/glm/detail/type_vec4.hpp vendored Normal file → Executable file
View File

Some files were not shown because too many files have changed in this diff Show More