reworking application and bridge

This commit is contained in:
2024-12-15 03:35:17 +01:00
parent e365c8a48c
commit 5a36b90a72
22 changed files with 464 additions and 505 deletions

View File

@@ -3,12 +3,12 @@
typedef struct typedef struct
{ {
void* mlx; mlx_context mlx;
void* win; mlx_window win;
void* logo_png; mlx_image logo_png;
void* logo_jpg; mlx_image logo_jpg;
void* logo_bmp; mlx_image logo_bmp;
void* img; mlx_image img;
} mlx_t; } mlx_t;
int update(void* param) int update(void* param)
@@ -17,17 +17,17 @@ int update(void* param)
mlx_t* mlx = (mlx_t*)param; mlx_t* mlx = (mlx_t*)param;
if(i == 200) if(i == 200)
mlx_clear_window(mlx->mlx, mlx->win, 0xFF334D4D); mlx_clear_window(mlx->mlx, mlx->win, 0x334D4DFF);
if(i >= 250) if(i >= 250)
mlx_set_font_scale(mlx->mlx, "default", 16.f); mlx_set_font_scale(mlx->mlx, "default", 16.f);
else else
mlx_set_font_scale(mlx->mlx, "default", 6.f); mlx_set_font_scale(mlx->mlx, "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, 0xFF2066FF, "this text should be hidden");
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_png, 100, 100);
mlx_transform_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_bmp, 220, 40, 0.5f, 75.0f); //mlx_transform_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_bmp, 220, 40, 0.5f, 75.0f);
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);
mlx_set_font(mlx->mlx, "default"); mlx_set_font(mlx->mlx, "default");
@@ -35,29 +35,29 @@ int update(void* param)
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, 0x0000FFFF + (color << 24));
mlx_pixel_put(mlx->mlx, mlx->win, 399 - j, j, 0xFF0000FF); mlx_pixel_put(mlx->mlx, mlx->win, 399 - j, j, 0x0000FFFF);
color += (color < 255); color += (color < 255);
} }
mlx_transform_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_jpg, 210, 150, 2.0f, 0.0f); //mlx_transform_put_image_to_window(mlx->mlx, mlx->win, mlx->logo_jpg, 210, 150, 2.0f, 0.0f);
mlx_set_font_scale(mlx->mlx, "default", 8.f); mlx_set_font_scale(mlx->mlx, "default", 8.f);
mlx_string_put(mlx->mlx, mlx->win, 210, 175, 0xFFAF2BFF, "hidden"); mlx_string_put(mlx->mlx, mlx->win, 210, 175, 0xFFAF2BFF, "hidden");
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, 0xFF0000FF);
} }
i++; i++;
return 0; return 0;
} }
void* create_image(mlx_t* mlx) mlx_image create_image(mlx_t* mlx)
{ {
unsigned char pixel[4]; unsigned char pixel[4];
void* img = mlx_new_image(mlx->mlx, 100, 100); mlx_image img = mlx_new_image(mlx->mlx, 100, 100);
for(int i = 0, j = 0, k = 0; i < (100 * 100) * 4; i += 4, j++) for(int i = 0, j = 0, k = 0; i < (100 * 100) * 4; i += 4, j++)
{ {
if(j >= 100) if(j >= 100)
@@ -67,10 +67,10 @@ void* create_image(mlx_t* mlx)
} }
if(i < 10000 || i > 20000) if(i < 10000 || i > 20000)
{ {
pixel[0] = i; pixel[0] = 0x99;
pixel[1] = j; pixel[1] = i;
pixel[2] = k; pixel[2] = j;
pixel[3] = 0x99; pixel[3] = k;
mlx_set_image_pixel(mlx->mlx, img, j, k, *((int*)pixel)); mlx_set_image_pixel(mlx->mlx, img, j, k, *((int*)pixel));
} }
} }
@@ -90,13 +90,13 @@ int key_hook(int key, void* param)
mlx_loop_end(mlx->mlx); mlx_loop_end(mlx->mlx);
break; break;
case 22 : // (S)how case 22 : // (S)how
mlx_mouse_show(); mlx_mouse_show(mlx->mlx);
break; break;
case 11 : // (H)ide case 11 : // (H)ide
mlx_mouse_hide(); mlx_mouse_hide(mlx->mlx);
break; break;
case 6 : // (C)lear case 6 : // (C)lear
mlx_clear_window(mlx->mlx, mlx->win, 0xFF334D4D); mlx_clear_window(mlx->mlx, mlx->win, 0x334D4DFF);
break; break;
case 79 : // RIGHT KEY case 79 : // RIGHT KEY
mlx_mouse_move(mlx->mlx, mlx->win, x + 10, y); mlx_mouse_move(mlx->mlx, mlx->win, x + 10, y);
@@ -131,9 +131,15 @@ int main(void)
int dummy; int dummy;
mlx.mlx = mlx_init(); mlx.mlx = mlx_init();
mlx.win = mlx_new_resizable_window(mlx.mlx, 400, 400, "My window");
mlx_get_screens_size(mlx.mlx, mlx.win, &w, &h); 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_get_screen_size(mlx.mlx, mlx.win, &w, &h);
printf("screen size : %dx%d\n", w, h); printf("screen size : %dx%d\n", w, h);
mlx_set_fps_goal(mlx.mlx, 60); mlx_set_fps_goal(mlx.mlx, 60);
@@ -141,17 +147,17 @@ int main(void)
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_png = mlx_png_file_to_image(mlx.mlx, "42_logo.png", &dummy, &dummy); mlx.logo_png = mlx_new_image_from_file(mlx.mlx, "42_logo.png", &dummy, &dummy);
mlx.logo_bmp = mlx_bmp_file_to_image(mlx.mlx, "42_logo.bmp", &dummy, &dummy); mlx.logo_bmp = mlx_new_image_from_file(mlx.mlx, "42_logo.bmp", &dummy, &dummy);
mlx.logo_jpg = mlx_jpg_file_to_image(mlx.mlx, "42_logo.jpg", &dummy, &dummy); mlx.logo_jpg = mlx_new_image_from_file(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, 0xFF00FFFF);
mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo_png, 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_set_font_scale(mlx.mlx, "font.ttf", 16.f); mlx_set_font_scale(mlx.mlx, "font.ttf", 16.f);
mlx_string_put(mlx.mlx, mlx.win, 20, 20, 0xFF0020FF, "that text will disappear"); mlx_string_put(mlx.mlx, mlx.win, 20, 20, 0x0020FFFF, "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);
@@ -162,7 +168,7 @@ int main(void)
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_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/14 16:33:17 by maldavid ### ########.fr */ /* Updated: 2024/12/15 01:58:12 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -16,21 +16,62 @@
#ifndef MACROLIB_X_H #ifndef MACROLIB_X_H
#define MACROLIB_X_H #define MACROLIB_X_H
#include <stdbool.h>
#include "mlx_profile.h" #include "mlx_profile.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
typedef enum mlx_event_type
{ /* MLX types */
MLX_KEYDOWN = 0,
MLX_KEYUP = 1, /**
MLX_MOUSEDOWN = 2, * @brief Opaque handle that represents the MLX context
MLX_MOUSEUP = 3, */
MLX_MOUSEWHEEL = 4, MLX_DEFINE_HANDLE(mlx_context);
MLX_WINDOW_EVENT = 5
} mlx_event_type; /**
* @brief Opaque handle that represents a MLX window
*/
MLX_DEFINE_HANDLE(mlx_window);
/**
* @brief Opaque handle that represents a MLX image
*/
MLX_DEFINE_HANDLE(mlx_image);
/* MLX backend related functions */
/**
* @brief Initializes the MLX internal application
*
* @return (mlx_context) An opaque handler to the internal MLX application or NULL (0x0) in case of error
*/
MLX_API mlx_context mlx_init();
/**
* @brief Caps the FPS
*
* @param mlx Internal MLX application
* @param fps The FPS cap
*/
MLX_API void mlx_set_fps_goal(mlx_context mlx, int fps);
/**
* @brief Destroy internal MLX application
*
* @param mlx Internal MLX application
*/
MLX_API void mlx_destroy_context(mlx_context mlx);
/* Window related functions */
/** /**
* @brief Descriptor structure for window creation * @brief Descriptor structure for window creation
@@ -45,46 +86,15 @@ typedef struct mlx_window_create_info
bool is_resizable; bool is_resizable;
} mlx_window_create_info; } mlx_window_create_info;
/* MLX backend related functions */
/**
* @brief Initializes the MLX internal application
*
* @return (void*) An opaque pointer to the internal MLX application or NULL (0x0) in case of error
*/
MLX_API void* mlx_init();
/**
* @brief Caps the FPS
*
* @param mlx Internal MLX application
* @param fps The FPS cap
*/
MLX_API void mlx_set_fps_goal(void* mlx, int fps);
/**
* @brief Destroy internal MLX application
*
* @param mlx Internal MLX application
*/
MLX_API void mlx_destroy_display(void* mlx);
/* Window related functions */
/** /**
* @brief Creates a new window * @brief Creates a new window
* *
* @param mlx Internal MLX application * @param mlx Internal MLX application
* @param info Pointer to a descriptor structure * @param info Pointer to a descriptor structure
* *
* @return (void*) An opaque pointer to the internal MLX window or NULL (0x0) in case of error * @return (mlx_widnow) An opaque handler to the internal MLX window or NULL (0x0) in case of error
*/ */
MLX_API void* mlx_new_window(void* mlx, const mlx_window_create_info* info); MLX_API mlx_window mlx_new_window(mlx_context mlx, const mlx_window_create_info* info);
/** /**
* @brief Destroys internal window * @brief Destroys internal window
@@ -92,7 +102,7 @@ MLX_API void* mlx_new_window(void* mlx, const mlx_window_create_info* info);
* @param mlx Internal MLX application * @param mlx Internal MLX application
* @param win Internal window * @param win Internal window
*/ */
MLX_API void mlx_destroy_window(void* mlx, void* win); MLX_API void mlx_destroy_window(mlx_context mlx, mlx_window win);
/** /**
* @brief Sets window position * @brief Sets window position
@@ -102,7 +112,7 @@ MLX_API void mlx_destroy_window(void* mlx, void* win);
* @param x New x position * @param x New x position
* @param y New y position * @param y New y position
*/ */
MLX_API void mlx_set_window_position(void *mlx, void *win, int x, int y); MLX_API void mlx_set_window_position(mlx_context mlx, mlx_window win, int x, int y);
/** /**
* @brief Sets window size * @brief Sets window size
@@ -112,7 +122,7 @@ MLX_API void mlx_set_window_position(void *mlx, void *win, int x, int y);
* @param width New width * @param width New width
* @param height New height * @param height New height
*/ */
MLX_API void mlx_set_window_size(void *mlx, void *win, int width, int height); MLX_API void mlx_set_window_size(mlx_context mlx, mlx_window win, int width, int height);
/** /**
* @brief Sets window title * @brief Sets window title
@@ -121,7 +131,7 @@ MLX_API void mlx_set_window_size(void *mlx, void *win, int width, int height);
* @param win Internal window to move * @param win Internal window to move
* @param title New title * @param title New title
*/ */
MLX_API void mlx_set_window_title(void *mlx, void *win, const char* title); MLX_API void mlx_set_window_title(mlx_context mlx, mlx_window win, const char* title);
/** /**
* @brief Enables/Disables window fullscreen mode * @brief Enables/Disables window fullscreen mode
@@ -130,7 +140,7 @@ MLX_API void mlx_set_window_title(void *mlx, void *win, const char* title);
* @param win Internal window to move * @param win Internal window to move
* @param enable Switch or not to fullscreen * @param enable Switch or not to fullscreen
*/ */
MLX_API void mlx_set_window_fullscreen(void *mlx, void *win, bool enable); MLX_API void mlx_set_window_fullscreen(mlx_context mlx, mlx_window win, bool enable);
/** /**
* @brief Gets window position * @brief Gets window position
@@ -140,7 +150,7 @@ MLX_API void mlx_set_window_fullscreen(void *mlx, void *win, bool enable);
* @param x Pointers to get position of the window * @param x Pointers to get position of the window
* @param y Pointers to get position of the window * @param y Pointers to get position of the window
*/ */
MLX_API void mlx_get_window_position(void *mlx, void *win, int* x, int* y); MLX_API void mlx_get_window_position(mlx_context mlx, mlx_window win, int* x, int* y);
/** /**
* @brief Gets window size * @brief Gets window size
@@ -150,7 +160,7 @@ MLX_API void mlx_get_window_position(void *mlx, void *win, int* x, int* y);
* @param x Pointers to get size of the window * @param x Pointers to get size of the window
* @param y Pointers to get size of the window * @param y Pointers to get size of the window
*/ */
MLX_API void mlx_get_window_size(void *mlx, void *win, int* x, int* y); MLX_API void mlx_get_window_size(mlx_context mlx, mlx_window win, int* x, int* y);
/** /**
* @brief Clears the given window (resets all rendered data) * @brief Clears the given window (resets all rendered data)
@@ -158,7 +168,7 @@ MLX_API void mlx_get_window_size(void *mlx, void *win, int* x, int* y);
* @param mlx Internal MLX application * @param mlx Internal MLX application
* @param win Internal window * @param win Internal window
*/ */
MLX_API void mlx_clear_window(void* mlx, void* win, int color); MLX_API void mlx_clear_window(mlx_context mlx, mlx_window win, int color);
/** /**
* @brief Get the size of the screen the given window is on * @brief Get the size of the screen the given window is on
@@ -168,7 +178,7 @@ MLX_API void mlx_clear_window(void* mlx, void* win, int color);
* @param w Get width size * @param w Get width size
* @param h Get height size * @param h Get height size
*/ */
MLX_API void mlx_get_screens_size(void* mlx, void* win, int* w, int* h); MLX_API void mlx_get_screen_size(mlx_context mlx, mlx_window win, int* w, int* h);
@@ -182,21 +192,21 @@ MLX_API void mlx_get_screens_size(void* mlx, void* win, int* w, int* h);
* @param f The function * @param f The function
* @param param Param to give to the function passed * @param param Param to give to the function passed
*/ */
MLX_API void mlx_loop_hook(void* mlx, int (*f)(void*), void* param); MLX_API void mlx_loop_hook(mlx_context mlx, int (*f)(void*), void* param);
/** /**
* @brief Starts the internal main loop * @brief Starts the internal main loop
* *
* @param mlx Internal MLX application * @param mlx Internal MLX application
*/ */
MLX_API void mlx_loop(void* mlx); MLX_API void mlx_loop(mlx_context mlx);
/** /**
* @brief Ends the internal run loop * @brief Ends the internal run loop
* *
* @param mlx Internal MLX application * @param mlx Internal MLX application
*/ */
MLX_API void mlx_loop_end(void* mlx); MLX_API void mlx_loop_end(mlx_context mlx);
@@ -205,13 +215,17 @@ MLX_API void mlx_loop_end(void* mlx);
/** /**
* @brief Shows mouse cursor * @brief Shows mouse cursor
*
* @param mlx Internal MLX application
*/ */
MLX_API void mlx_mouse_show(); MLX_API void mlx_mouse_show(mlx_context mlx);
/** /**
* @brief Hides mouse cursor * @brief Hides mouse cursor
*
* @param mlx Internal MLX application
*/ */
MLX_API void mlx_mouse_hide(); MLX_API void mlx_mouse_hide(mlx_context mlx);
/** /**
* @brief Moves cursor to givent position * @brief Moves cursor to givent position
@@ -221,7 +235,7 @@ MLX_API void mlx_mouse_hide();
* @param x X coordinate * @param x X coordinate
* @param y Y coordinate * @param y Y coordinate
*/ */
MLX_API void mlx_mouse_move(void* mlx, void* win, int x, int y); MLX_API void mlx_mouse_move(mlx_context mlx, mlx_window win, int x, int y);
/** /**
* @brief Get cursor's position * @brief Get cursor's position
@@ -230,7 +244,20 @@ MLX_API void mlx_mouse_move(void* mlx, void* win, int x, int y);
* @param x Get x coordinate * @param x Get x coordinate
* @param y Get y coordinate * @param y Get y coordinate
*/ */
MLX_API void mlx_mouse_get_pos(void* mlx, int* x, int* y); MLX_API void mlx_mouse_get_pos(mlx_context mlx, int* x, int* y);
/**
* @brief Type of event
*/
typedef enum mlx_event_type
{
MLX_KEYDOWN = 0,
MLX_KEYUP = 1,
MLX_MOUSEDOWN = 2,
MLX_MOUSEUP = 3,
MLX_MOUSEWHEEL = 4,
MLX_WINDOW_EVENT = 5
} mlx_event_type;
/** /**
* @brief Gives a function to be executed on event type * @brief Gives a function to be executed on event type
@@ -241,7 +268,7 @@ MLX_API void mlx_mouse_get_pos(void* mlx, int* x, int* y);
* @param f Function to be executed * @param f Function to be executed
* @param param Parameter given to the function * @param param Parameter given to the function
*/ */
MLX_API void mlx_on_event(void* mlx, void* win, mlx_event_type event, int (*f)(int, void*), void* param); MLX_API void mlx_on_event(mlx_context mlx, mlx_window win, mlx_event_type event, int (*f)(int, void*), void* param);
@@ -257,7 +284,7 @@ MLX_API void mlx_on_event(void* mlx, void* win, mlx_event_type event, int (*f)(i
* @param y Y coordinate * @param y Y coordinate
* @param color Color of the pixel (coded on 4 bytes in an int, 0xRRGGBBAA) * @param color Color of the pixel (coded on 4 bytes in an int, 0xRRGGBBAA)
*/ */
MLX_API void mlx_pixel_put(void* mlx, void* win, int x, int y, int color); MLX_API void mlx_pixel_put(mlx_context mlx, mlx_window win, int x, int y, int color);
@@ -271,9 +298,9 @@ MLX_API void mlx_pixel_put(void* mlx, void* win, int x, int y, int color);
* @param width Width of the image * @param width Width of the image
* @param height Height of the image * @param height Height of the image
* *
* @return (void*) An opaque pointer to the internal image or NULL (0x0) in case of error * @return (mlx_image) An opaque handler to the internal image or NULL (0x0) in case of error
*/ */
MLX_API void* mlx_new_image(void* mlx, int width, int height); MLX_API mlx_image mlx_new_image(mlx_context mlx, int width, int height);
/** /**
* @brief Create a new image from a png/jpg/bmp file * @brief Create a new image from a png/jpg/bmp file
@@ -283,9 +310,9 @@ MLX_API void* mlx_new_image(void* mlx, int width, int height);
* @param width Get the width of the image * @param width Get the width of the image
* @param heigth Get the height of the image * @param heigth Get the height of the image
* *
* @return (void*) An opaque pointer to the internal image or NULL (0x0) in case of error * @return (mlx_image) An opaque handler to the internal image or NULL (0x0) in case of error
*/ */
MLX_API void* mlx_new_image_from_file(void* mlx, char* filename, int* width, int* height); MLX_API mlx_image mlx_new_image_from_file(mlx_context mlx, char* filename, int* width, int* height);
/** /**
* @brief Destroys internal image * @brief Destroys internal image
@@ -293,7 +320,7 @@ MLX_API void* mlx_new_image_from_file(void* mlx, char* filename, int* width, int
* @param mlx Internal MLX application * @param mlx Internal MLX application
* @param img Internal image * @param img Internal image
*/ */
MLX_API void mlx_destroy_image(void* mlx, void* img); MLX_API void mlx_destroy_image(mlx_context mlx, mlx_image image);
/** /**
* @brief Get image pixel data * @brief Get image pixel data
@@ -313,7 +340,7 @@ MLX_API void mlx_destroy_image(void* mlx, void* img);
* ~ make IMAGES_OPTIMIZED=false * ~ make IMAGES_OPTIMIZED=false
* ``` * ```
*/ */
MLX_API int mlx_get_image_pixel(void* mlx, void* img, int x, int y); MLX_API int mlx_get_image_pixel(mlx_context mlx, mlx_image image, int x, int y);
/** /**
* @brief Set image pixel data * @brief Set image pixel data
@@ -332,7 +359,7 @@ MLX_API int mlx_get_image_pixel(void* mlx, void* img, int x, int y);
* ~ make IMAGES_OPTIMIZED=false * ~ make IMAGES_OPTIMIZED=false
* ``` * ```
*/ */
MLX_API void mlx_set_image_pixel(void* mlx, void* img, int x, int y, int color); MLX_API void mlx_set_image_pixel(mlx_context mlx, mlx_image image, int x, int y, int color);
/** /**
* @brief Put image to the given window * @brief Put image to the given window
@@ -343,7 +370,7 @@ MLX_API void mlx_set_image_pixel(void* mlx, void* img, int x, int y, int color);
* @param x X coordinate * @param x X coordinate
* @param y Y coordinate * @param y Y coordinate
*/ */
MLX_API void mlx_put_image_to_window(void* mlx, void* win, void* img, int x, int y); MLX_API void mlx_put_image_to_window(mlx_context mlx, mlx_window win, mlx_image image, int x, int y);
@@ -360,7 +387,7 @@ MLX_API void mlx_put_image_to_window(void* mlx, void* win, void* img, int x, int
* @param color Color of the pixel (coded on 4 bytes in an int, 0xAARRGGBB) * @param color Color of the pixel (coded on 4 bytes in an int, 0xAARRGGBB)
* @param str Text to put * @param str Text to put
*/ */
MLX_API void mlx_string_put(void* mlx, void* win, int x, int y, int color, char* str); MLX_API void mlx_string_put(mlx_context mlx, mlx_window win, int x, int y, int color, char* str);
/** /**
* @brief Loads a font to be used by `mlx_string_put` * @brief Loads a font to be used by `mlx_string_put`
@@ -369,7 +396,7 @@ MLX_API void mlx_string_put(void* mlx, void* win, int x, int y, int color, char*
* @param win Internal window * @param win Internal window
* @param filepath Filepath to the font or "default" to reset to the embedded font * @param filepath Filepath to the font or "default" to reset to the embedded font
*/ */
MLX_API void mlx_set_font(void* mlx, char* filepath); MLX_API void mlx_set_font(mlx_context mlx, char* filepath);
/** /**
* @brief Loads a font to be used by `mlx_string_put` and scales it * @brief Loads a font to be used by `mlx_string_put` and scales it
@@ -379,7 +406,7 @@ MLX_API void mlx_set_font(void* mlx, char* filepath);
* @param filepath Filepath to the font or "default" to reset to the embedded font * @param filepath Filepath to the font or "default" to reset to the embedded font
* @param scale Scale to apply to the font * @param scale Scale to apply to the font
*/ */
MLX_API void mlx_set_font_scale(void* mlx, char* filepath, float scale); MLX_API void mlx_set_font_scale(mlx_context mlx, char* filepath, float scale);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <contact@kbz8.me> +#+ +:+ +#+ */ /* By: maldavid <contact@kbz8.me> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/14 16:17:10 by maldavid #+# #+# */ /* Created: 2024/12/14 16:17:10 by maldavid #+# #+# */
/* Updated: 2024/12/14 16:37:49 by maldavid ### ########.fr */ /* Updated: 2024/12/14 17:42:06 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -16,12 +16,16 @@
#ifndef MACROLIB_X_EXTENDED_H #ifndef MACROLIB_X_EXTENDED_H
#define MACROLIB_X_EXTENDED_H #define MACROLIB_X_EXTENDED_H
#include "mlx_profile.h" #include "mlx.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
/* Window related functions */
typedef struct mlx_window_create_info_extension typedef struct mlx_window_create_info_extension
{ {
int position_x; int position_x;
@@ -32,11 +36,6 @@ typedef struct mlx_window_create_info_extension
int min_height; int min_height;
} mlx_window_create_info_extension; } mlx_window_create_info_extension;
/* Window related functions */
/** /**
* @brief Sets maximum window size * @brief Sets maximum window size
* *
@@ -45,7 +44,7 @@ typedef struct mlx_window_create_info_extension
* @param x New x maximum size * @param x New x maximum size
* @param y New y maximum size * @param y New y maximum size
*/ */
MLX_API void mlx_set_window_max_size(void *mlx, void *win, int x, int y); MLX_API void mlx_set_window_max_size(mlx_context mlx, mlx_window win, int x, int y);
/** /**
* @brief Sets minimum window size * @brief Sets minimum window size
@@ -55,7 +54,7 @@ MLX_API void mlx_set_window_max_size(void *mlx, void *win, int x, int y);
* @param x New x minimum size * @param x New x minimum size
* @param y New y minimum size * @param y New y minimum size
*/ */
MLX_API void mlx_set_window_min_size(void *mlx, void *win, int x, int y); MLX_API void mlx_set_window_min_size(mlx_context mlx, mlx_window win, int x, int y);
/** /**
* @brief Maximizes a window * @brief Maximizes a window
@@ -63,7 +62,7 @@ MLX_API void mlx_set_window_min_size(void *mlx, void *win, int x, int y);
* @param mlx Internal MLX application * @param mlx Internal MLX application
* @param win Internal window to move * @param win Internal window to move
*/ */
MLX_API void mlx_maximise_window(void *mlx, void *win); MLX_API void mlx_maximise_window(mlx_context mlx, mlx_window win);
/** /**
* @brief Minimizes a window * @brief Minimizes a window
@@ -71,7 +70,7 @@ MLX_API void mlx_maximise_window(void *mlx, void *win);
* @param mlx Internal MLX application * @param mlx Internal MLX application
* @param win Internal window to move * @param win Internal window to move
*/ */
MLX_API void mlx_minimize_window(void *mlx, void *win); MLX_API void mlx_minimize_window(mlx_context mlx, mlx_window win);
/** /**
* @brief Restore window to formal size * @brief Restore window to formal size
@@ -79,7 +78,7 @@ MLX_API void mlx_minimize_window(void *mlx, void *win);
* @param mlx Internal MLX application * @param mlx Internal MLX application
* @param win Internal window to move * @param win Internal window to move
*/ */
MLX_API void mlx_restore_window(void *mlx, void *win); MLX_API void mlx_restore_window(mlx_context mlx, mlx_window win);
@@ -95,7 +94,7 @@ MLX_API void mlx_restore_window(void *mlx, void *win);
* @param y Y coordinate * @param y Y coordinate
* @param pixels Array of pixels (coded on 4 bytes in an int, 0xRRGGBBAA) * @param pixels Array of pixels (coded on 4 bytes in an int, 0xRRGGBBAA)
*/ */
MLX_API void mlx_pixel_put_array(void* mlx, void* win, int x, int y, int* pixels); MLX_API void mlx_pixel_put_array(mlx_context mlx, mlx_window win, int x, int y, int* pixels);
@@ -124,7 +123,7 @@ MLX_API void mlx_pixel_put_array(void* mlx, void* win, int x, int y, int* pixels
* ~ make IMAGES_OPTIMIZED=false * ~ make IMAGES_OPTIMIZED=false
* ``` * ```
*/ */
MLX_API void mlx_get_image_region(void* mlx, void* img, int x, int y, int w, int h, int* dst); MLX_API void mlx_get_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, int* dst);
/** /**
* @brief Set image region * @brief Set image region
@@ -148,7 +147,7 @@ MLX_API void mlx_get_image_region(void* mlx, void* img, int x, int y, int w, int
* ~ make IMAGES_OPTIMIZED=false * ~ make IMAGES_OPTIMIZED=false
* ``` * ```
*/ */
MLX_API void mlx_set_image_region(void* mlx, void* img, int x, int y, int w, int h, int* pixels); MLX_API void mlx_set_image_region(mlx_context mlx, mlx_image image, int x, int y, int w, int h, int* pixels);
/** /**
* @brief Transform and put image to the given window * @brief Transform and put image to the given window
@@ -162,7 +161,7 @@ MLX_API void mlx_set_image_region(void* mlx, void* img, int x, int y, int w, int
* @param scale_y Scale y of the image * @param scale_y Scale y of the image
* @param angle Rotation angle of the image (clockwise) * @param angle Rotation angle of the image (clockwise)
*/ */
MLX_API void mlx_put_transformed_image_to_window(void* mlx, void* win, void* img, int x, int y, float scale_x, float scale_y, float angle); MLX_API void mlx_put_transformed_image_to_window(mlx_context mlx, mlx_window win, mlx_image image, int x, int y, float scale_x, float scale_y, float angle);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */ /* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/10 08:49:17 by maldavid #+# #+# */ /* Created: 2023/11/10 08:49:17 by maldavid #+# #+# */
/* Updated: 2024/04/23 18:28:12 by maldavid ### ########.fr */ /* Updated: 2024/12/14 17:58:37 by maldavid ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -156,8 +156,10 @@
#define MLX_VERSION_MINOR(version) (((uint32_t)(version) >> 12U) & 0x3FFU) #define MLX_VERSION_MINOR(version) (((uint32_t)(version) >> 12U) & 0x3FFU)
#define MLX_VERSION_PATCH(version) ((uint32_t)(version) & 0xFFFU) #define MLX_VERSION_PATCH(version) ((uint32_t)(version) & 0xFFFU)
#define MLX_DEFINE_HANDLE(object) typedef struct object##_handler* object
#define MLX_VERSION MLX_MAKE_VERSION(2, 0, 0) #define MLX_VERSION MLX_MAKE_VERSION(2, 0, 0)
#define MLX_TARGET_VULKAN_API_VERSION MLX_MAKE_VERSION(1, 2, 0) #define MLX_TARGET_VULKAN_API_VERSION MLX_MAKE_VERSION(1, 0, 0)
// Checking common assumptions // Checking common assumptions
#ifdef __cplusplus #ifdef __cplusplus

View File

@@ -11,35 +11,25 @@
namespace mlx namespace mlx
{ {
// TODO : FIX THIS DAMN GOD CLASS !!!!!!!!!!!!!!!!
class Application class Application
{ {
public: public:
Application(); Application();
inline void GetMousePos(int* x, int* y) noexcept; inline void GetMousePos(int* x, int* y) noexcept;
inline void MouseMove(Handle win, int x, int y) noexcept; inline void GetScreenSize(mlx_window win, int* w, int* h) noexcept;
inline void OnEvent(Handle win, int event, int (*funct_ptr)(int, void*), void* param) noexcept;
inline void GetScreenSize(Handle win, int* w, int* h) noexcept;
inline void SetFPSCap(std::uint32_t fps) noexcept; inline void SetFPSCap(std::uint32_t fps) noexcept;
inline Handle NewGraphicsSuport(std::size_t w, std::size_t h, const char* title, bool is_resizable); inline void OnEvent(mlx_window win, int event, int (*funct_ptr)(int, void*), void* param) noexcept;
inline void ClearGraphicsSupport(Handle win, int color);
inline void DestroyGraphicsSupport(Handle win);
inline void SetGraphicsSupportPosition(Handle win, int x, int y);
inline void PixelPut(Handle win, int x, int y, std::uint32_t color) const noexcept; inline mlx_window NewGraphicsSuport(const mlx_window_create_info* info);
inline void StringPut(Handle win, int x, int y, std::uint32_t color, char* str); inline NonOwningPtr<GraphicsSupport> GetGraphicsSupport(mlx_window win);
inline void DestroyGraphicsSupport(mlx_window win);
Handle NewTexture(int w, int h); mlx_image NewTexture(int w, int h);
Handle NewStbTexture(char* file, int* w, int* h); // stb textures are image files (png, jpg, bpm, ...) mlx_image NewStbTexture(char* file, int* w, int* h); // stb textures are image files (png, jpg, bpm, ...)
inline void TexturePut(Handle win, Handle img, int x, int y, float scale, float angle); inline NonOwningPtr<Texture> GetTexture(mlx_image image);
inline int GetTexturePixel(Handle img, int x, int y); void DestroyTexture(mlx_image img);
inline void SetTexturePixel(Handle img, int x, int y, std::uint32_t color);
void DestroyTexture(Handle ptr);
inline void LoopHook(int (*f)(void*), void* param); inline void LoopHook(int (*f)(void*), void* param);
inline void LoopEnd() noexcept; inline void LoopEnd() noexcept;

View File

@@ -1,33 +1,34 @@
#pragma once #pragma once
#include <Core/Application.h> #include <Core/Application.h>
#include <Core/Handles.h>
#include <Embedded/DogicaTTF.h> #include <Embedded/DogicaTTF.h>
#ifndef DISABLE_ALL_SAFETIES #ifndef DISABLE_ALL_SAFETIES
#define CHECK_WINDOW_PTR(win) \ #define CHECK_WINDOW_PTR(win, retval) \
if(win == nullptr) \ if(win == nullptr) \
{ \ { \
Error("invalid window ptr (NULL)"); \ Error("invalid window handle (NULL)"); \
return; \ return retval; \
} \ } \
else if(std::find_if(m_graphics.begin(), m_graphics.end(), [win](const std::unique_ptr<GraphicsSupport>& gs){ return gs && *static_cast<int*>(win) == gs->GetID(); }) == m_graphics.end()) \ else if(std::find_if(m_graphics.begin(), m_graphics.end(), [win](const std::unique_ptr<GraphicsSupport>& gs){ return gs && win->id == gs->GetID(); }) == m_graphics.end()) \
{ \ { \
Error("invalid window ptr"); \ Error("invalid window handle"); \
return; \ return retval; \
} else {} } else {}
#define CHECK_IMAGE_PTR(img, retval) \ #define CHECK_IMAGE_PTR(img, retval) \
if(img == nullptr) \ if(img == nullptr) \
{ \ { \
Error("invalid image ptr (NULL)"); \ Error("invalid image handle (NULL)"); \
retval; \ return retval; \
} \ } \
else if(!m_image_registry.IsTextureKnown(static_cast<Texture*>(img))) \ else if(!m_image_registry.IsTextureKnown(image->texture)) \
{ \ { \
Error("invalid image ptr"); \ Error("invalid image handle"); \
retval; \ return retval; \
} else {} } else {}
#else #else
#define CHECK_WINDOW_PTR(win) #define CHECK_WINDOW_PTR(win, retval)
#define CHECK_IMAGE_PTR(img, retval) #define CHECK_IMAGE_PTR(img, retval)
#endif #endif
@@ -39,32 +40,12 @@ namespace mlx
*y = m_in.GetY(); *y = m_in.GetY();
} }
void Application::MouseMove(Handle win, int x, int y) noexcept void Application::OnEvent(mlx_window win, int event, int (*funct_ptr)(int, void*), void* param) noexcept
{ {
CHECK_WINDOW_PTR(win); CHECK_WINDOW_PTR(win, );
if(!m_graphics[*static_cast<int*>(win)]->HasWindow()) if(!m_graphics[win->id]->HasWindow())
{
Warning("trying to move the mouse relative to a window that is targeting an image and not a real window, this is not allowed (move ignored)");
return; return;
} m_in.OnEvent(m_graphics[win->id]->GetWindow()->GetID(), event, funct_ptr, param);
m_graphics[*static_cast<int*>(win)]->GetWindow()->MoveMouse(x, y);
}
void Application::OnEvent(Handle win, int event, int (*funct_ptr)(int, void*), void* param) noexcept
{
CHECK_WINDOW_PTR(win);
if(!m_graphics[*static_cast<int*>(win)]->HasWindow())
{
Warning("trying to add event hook for a window that is targeting an image and not a real window, this is not allowed (hook ignored)");
return;
}
m_in.OnEvent(m_graphics[*static_cast<int*>(win)]->GetWindow()->GetID(), event, funct_ptr, param);
}
void Application::GetScreenSize(Handle win, int* w, int* h) noexcept
{
CHECK_WINDOW_PTR(win);
m_graphics[*static_cast<int*>(win)]->GetWindow()->GetScreenSizeWindowIsOn(w, h);
} }
void Application::SetFPSCap(std::uint32_t fps) noexcept void Application::SetFPSCap(std::uint32_t fps) noexcept
@@ -72,81 +53,38 @@ namespace mlx
m_fps.SetMaxFPS(fps); m_fps.SetMaxFPS(fps);
} }
void* Application::NewGraphicsSuport(std::size_t w, std::size_t h, const char* title, bool is_resizable) mlx_window Application::NewGraphicsSuport(const mlx_window_create_info* info)
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
if(m_image_registry.IsTextureKnown(reinterpret_cast<Texture*>(const_cast<char*>(title)))) if(!info)
m_graphics.emplace_back(std::make_unique<GraphicsSupport>(w, h, reinterpret_cast<Texture*>(const_cast<char*>(title)), m_graphics.size()));
else
{ {
if(title == NULL) Error("invalid window create info (NULL)");
{
FatalError("invalid window title (NULL)");
return nullptr; return nullptr;
} }
if(static_cast<void*>(const_cast<char*>(title)) == static_cast<void*>(this))
{ mlx_window window;
for(std::size_t i = 0; i < 8; i++) try { window = new mlx_window_handler; }
{ catch(...) { return nullptr; }
m_graphics.emplace_back(std::make_unique<GraphicsSupport>(std::rand() % 1920, std::rand() % 1080, "让我们在月光下做爱吧", m_graphics.size(), is_resizable));
m_graphics.back()->GetWindow()->SetPosition(std::rand() % 1920, std::rand() % 1080); m_graphics.emplace_back(std::make_unique<GraphicsSupport>(info, m_graphics.size()));
}
}
else
{
m_graphics.emplace_back(std::make_unique<GraphicsSupport>(w, h, title, m_graphics.size(), is_resizable));
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);
return static_cast<void*>(&m_graphics.back()->GetID()); window->id = m_graphics.back()->GetID();
return window;
} }
void Application::ClearGraphicsSupport(Handle win, int color) NonOwningPtr<GraphicsSupport> Application::GetGraphicsSupport(mlx_window win)
{
CHECK_WINDOW_PTR(win, nullptr);
return m_graphics[win->id].get();
}
void Application::DestroyGraphicsSupport(mlx_window win)
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
CHECK_WINDOW_PTR(win); CHECK_WINDOW_PTR(win, );
m_graphics[*static_cast<int*>(win)]->ResetRenderData(color); m_graphics[win->id].reset();
} delete win;
void Application::DestroyGraphicsSupport(Handle win)
{
MLX_PROFILE_FUNCTION();
CHECK_WINDOW_PTR(win);
m_graphics[*static_cast<int*>(win)].reset();
}
void Application::SetGraphicsSupportPosition(Handle win, int x, int y)
{
CHECK_WINDOW_PTR(win);
if(!m_graphics[*static_cast<int*>(win)]->HasWindow())
Warning("trying to move a window that is targeting an image and not a real window, this is not allowed");
else
m_graphics[*static_cast<int*>(win)]->GetWindow()->SetPosition(x, y);
}
void Application::PixelPut(Handle win, int x, int y, std::uint32_t color) const noexcept
{
MLX_PROFILE_FUNCTION();
CHECK_WINDOW_PTR(win);
m_graphics[*static_cast<int*>(win)]->PixelPut(x, y, color);
}
void Application::StringPut(Handle win, int x, int y, std::uint32_t color, char* str)
{
MLX_PROFILE_FUNCTION();
CHECK_WINDOW_PTR(win);
if(str == nullptr)
{
Error("invalid text (NULL)");
return;
}
if(std::strlen(str) == 0)
{
Warning("trying to put an empty text");
return;
}
m_graphics[*static_cast<int*>(win)]->StringPut(x, y, color, str);
} }
void Application::LoadFont(const std::filesystem::path& filepath, float scale) void Application::LoadFont(const std::filesystem::path& filepath, float scale)
@@ -172,40 +110,16 @@ namespace mlx
} }
} }
void Application::TexturePut(Handle win, Handle img, int x, int y, float scale, float angle) NonOwningPtr<Texture> Application::GetTexture(mlx_image image)
{ {
MLX_PROFILE_FUNCTION(); CHECK_IMAGE_PTR(image, nullptr);
CHECK_WINDOW_PTR(win); NonOwningPtr<Texture> texture = image->texture;
CHECK_IMAGE_PTR(img, return);
NonOwningPtr<Texture> texture = static_cast<Texture*>(img);
if(!texture->IsInit())
Error("trying to put a texture that has been destroyed");
else
m_graphics[*static_cast<int*>(win)]->TexturePut(texture, x, y, scale, angle);
}
int Application::GetTexturePixel(Handle img, int x, int y)
{
MLX_PROFILE_FUNCTION();
CHECK_IMAGE_PTR(img, return 0);
NonOwningPtr<Texture> texture = static_cast<Texture*>(img);
if(!texture->IsInit()) if(!texture->IsInit())
{ {
Error("trying to get a pixel from texture that has been destroyed"); Error("trying to use a texture that has been destroyed");
return 0; return nullptr;
} }
return texture->GetPixel(x, y); return texture;
}
void Application::SetTexturePixel(Handle img, int x, int y, std::uint32_t color)
{
MLX_PROFILE_FUNCTION();
CHECK_IMAGE_PTR(img, return);
NonOwningPtr<Texture> texture = static_cast<Texture*>(img);
if(!texture->IsInit())
Error("trying to set a pixel on texture that has been destroyed");
else
texture->SetPixel(x, y, color);
} }
void Application::LoopHook(int (*f)(void*), void* param) void Application::LoopHook(int (*f)(void*), void* param)

View File

@@ -1,6 +1,7 @@
#ifndef __MLX_GRAPHICS__ #ifndef __MLX_GRAPHICS__
#define __MLX_GRAPHICS__ #define __MLX_GRAPHICS__
#include <mlx.h>
#include <Platform/Window.h> #include <Platform/Window.h>
#include <Renderer/Renderer.h> #include <Renderer/Renderer.h>
#include <Graphics/Scene.h> #include <Graphics/Scene.h>
@@ -14,8 +15,7 @@ namespace mlx
class GraphicsSupport : public NonCopyable class GraphicsSupport : public NonCopyable
{ {
public: public:
GraphicsSupport(std::size_t w, std::size_t h, NonOwningPtr<Texture> render_target, int id); GraphicsSupport(const mlx_window_create_info* info, int id);
GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id, bool is_resizable);
[[nodiscard]] MLX_FORCEINLINE int& GetID() noexcept { return m_id; } [[nodiscard]] MLX_FORCEINLINE int& GetID() noexcept { return m_id; }
[[nodiscard]] inline std::shared_ptr<Window> GetWindow() { return p_window; } [[nodiscard]] inline std::shared_ptr<Window> GetWindow() { return p_window; }
@@ -24,8 +24,8 @@ namespace mlx
inline void ResetRenderData(int color) noexcept; inline void ResetRenderData(int color) noexcept;
inline void PixelPut(int x, int y, std::uint32_t color) noexcept; inline void PixelPut(int x, int y, int color) noexcept;
inline void StringPut(int x, int y, std::uint32_t color, std::string str); inline void StringPut(int x, int y, int, std::string str);
inline void TexturePut(NonOwningPtr<class Texture> texture, int x, int y, float scale, float angle); inline void TexturePut(NonOwningPtr<class Texture> texture, int x, int y, float scale, float angle);
inline void TryEraseSpritesInScene(NonOwningPtr<Texture> texture) noexcept; inline void TryEraseSpritesInScene(NonOwningPtr<Texture> texture) noexcept;

View File

@@ -7,10 +7,10 @@ namespace mlx
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
Vec4f vec_color = { Vec4f vec_color = {
static_cast<float>((color & 0x000000FF)) / 255.0f, static_cast<float>((color & 0xFF000000) >> 24) / 255.0f,
static_cast<float>((color & 0x0000FF00) >> 8) / 255.0f,
static_cast<float>((color & 0x00FF0000) >> 16) / 255.0f, static_cast<float>((color & 0x00FF0000) >> 16) / 255.0f,
static_cast<float>((color & 0xFF000000) >> 24) / 255.0f static_cast<float>((color & 0x0000FF00) >> 8) / 255.0f,
static_cast<float>((color & 0x000000FF)) / 255.0f,
}; };
p_scene->ResetScene(std::move(vec_color)); p_scene->ResetScene(std::move(vec_color));
m_put_pixel_manager.ResetRenderData(); m_put_pixel_manager.ResetRenderData();
@@ -18,7 +18,7 @@ namespace mlx
m_pixelput_called = false; m_pixelput_called = false;
} }
void GraphicsSupport::PixelPut(int x, int y, std::uint32_t color) noexcept void GraphicsSupport::PixelPut(int x, int y, int color) noexcept
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixel(x, y, m_draw_layer, color); NonOwningPtr<Texture> texture = m_put_pixel_manager.DrawPixel(x, y, m_draw_layer, color);
@@ -30,17 +30,17 @@ namespace mlx
} }
} }
void GraphicsSupport::StringPut(int x, int y, std::uint32_t color, std::string str) void GraphicsSupport::StringPut(int x, int y, int color, std::string str)
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
if(str.empty()) if(str.empty())
return; return;
Vec4f vec_color = { Vec4f vec_color = {
static_cast<float>((color & 0x000000FF)) / 255.0f, static_cast<float>((color & 0xFF000000) >> 24) / 255.0f,
static_cast<float>((color & 0x0000FF00) >> 8) / 255.0f,
static_cast<float>((color & 0x00FF0000) >> 16) / 255.0f, static_cast<float>((color & 0x00FF0000) >> 16) / 255.0f,
static_cast<float>((color & 0xFF000000) >> 24) / 255.0f static_cast<float>((color & 0x0000FF00) >> 8) / 255.0f,
static_cast<float>((color & 0x000000FF)) / 255.0f,
}; };
NonOwningPtr<Text> text = p_scene->GetTextFromPositionAndColor(str, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, vec_color); NonOwningPtr<Text> text = p_scene->GetTextFromPositionAndColor(str, Vec2f{ static_cast<float>(x), static_cast<float>(y) }, vec_color);

25
runtime/Includes/Core/Handles.h git.filemode.normal_file
View File

@@ -0,0 +1,25 @@
#ifndef __MLX_HANDLES__
#define __MLX_HANDLES__
#include <Core/Application.h>
#include <Renderer/Image.h>
extern "C"
{
struct mlx_context_handler
{
mlx::NonOwningPtr<mlx::Application> app;
};
struct mlx_window_handler
{
int id;
};
struct mlx_image_handler
{
mlx::NonOwningPtr<mlx::Texture> texture;
};
}
#endif

View File

@@ -1,6 +1,7 @@
#ifndef __MLX_SDL_MANAGER__ #ifndef __MLX_SDL_MANAGER__
#define __MLX_SDL_MANAGER__ #define __MLX_SDL_MANAGER__
#include <mlx.h>
#include <Maths/Vec2.h> #include <Maths/Vec2.h>
namespace mlx namespace mlx
@@ -10,7 +11,7 @@ namespace mlx
public: public:
SDLManager(); SDLManager();
Handle CreateWindow(const std::string& title, std::size_t w, std::size_t h, bool hidden, std::int32_t& id, bool is_resizable); Handle CreateWindow(const mlx_window_create_info* info, std::int32_t& id, bool hidden);
void DestroyWindow(Handle window) noexcept; void DestroyWindow(Handle window) noexcept;
void InputsFetcher(func::function<void(mlx_event_type, int, int)> functor); void InputsFetcher(func::function<void(mlx_event_type, int, int)> functor);

View File

@@ -11,7 +11,7 @@ namespace mlx
PutPixelManager(NonOwningPtr<class Renderer> renderer) : p_renderer(renderer) {} PutPixelManager(NonOwningPtr<class Renderer> renderer) : p_renderer(renderer) {}
// Return a valid pointer when a new texture has been created // Return a valid pointer when a new texture has been created
NonOwningPtr<Texture> DrawPixel(int x, int y, std::uint64_t draw_layer, std::uint32_t color); NonOwningPtr<Texture> DrawPixel(int x, int y, std::uint64_t draw_layer, int color);
void ResetRenderData(); void ResetRenderData();
~PutPixelManager(); ~PutPixelManager();

View File

@@ -1,6 +1,7 @@
#ifndef __MLX_WINDOW__ #ifndef __MLX_WINDOW__
#define __MLX_WINDOW__ #define __MLX_WINDOW__
#include <mlx.h>
#include <Maths/Vec2.h> #include <Maths/Vec2.h>
#include <Core/SDLManager.h> #include <Core/SDLManager.h>
@@ -9,7 +10,7 @@ namespace mlx
class Window class Window
{ {
public: public:
Window(std::size_t w, std::size_t h, const std::string& title, bool is_resizable, bool hidden = false); Window(const mlx_window_create_info* info, bool hidden = false);
inline Handle GetWindowHandle() const noexcept { return p_window; } inline Handle GetWindowHandle() const noexcept { return p_window; }
inline int GetWidth() const noexcept { return m_width; } inline int GetWidth() const noexcept { return m_width; }

View File

@@ -44,6 +44,7 @@
#include <iterator> #include <iterator>
#include <stb_truetype.h> #include <stb_truetype.h>
#include <variant> #include <variant>
#include <bit>
#include <type_traits> #include <type_traits>
#include <string_view> #include <string_view>
#include <sstream> #include <sstream>

View File

@@ -83,7 +83,7 @@ namespace mlx
void Init(CPUBuffer pixels, std::uint32_t width, std::uint32_t height, VkFormat format, bool is_multisampled, [[maybe_unused]] std::string_view debug_name); void Init(CPUBuffer pixels, std::uint32_t width, std::uint32_t height, VkFormat format, bool is_multisampled, [[maybe_unused]] std::string_view debug_name);
void Destroy() noexcept override; void Destroy() noexcept override;
void SetPixel(int x, int y, std::uint32_t color) noexcept; void SetPixel(int x, int y, int color) noexcept;
int GetPixel(int x, int y) noexcept; int GetPixel(int x, int y) noexcept;
void Update(VkCommandBuffer cmd); void Update(VkCommandBuffer cmd);
@@ -94,7 +94,7 @@ namespace mlx
void OpenCPUBuffer(); void OpenCPUBuffer();
private: private:
std::vector<std::uint32_t> m_cpu_buffer; std::vector<int> m_cpu_buffer;
std::optional<GPUBuffer> m_staging_buffer; std::optional<GPUBuffer> m_staging_buffer;
bool m_has_been_modified = false; bool m_has_been_modified = false;
}; };

View File

@@ -50,37 +50,49 @@ namespace mlx
RenderCore::Get().WaitDeviceIdle(); RenderCore::Get().WaitDeviceIdle();
} }
void* Application::NewTexture(int w, int h) mlx_image Application::NewTexture(int w, int h)
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
mlx_image image;
try { image = new mlx_image_handler; }
catch(...) { return nullptr; }
Texture* texture; Texture* texture;
try { texture = new Texture({}, w, h, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_user_image"); } try { texture = new Texture({}, w, h, VK_FORMAT_R8G8B8A8_SRGB, false, "mlx_user_image"); }
catch(...) { return nullptr; } catch(...) { return nullptr; }
m_image_registry.RegisterTexture(texture); m_image_registry.RegisterTexture(texture);
return texture; image->texture = texture;
return image;
} }
void* Application::NewStbTexture(char* file, int* w, int* h) mlx_image Application::NewStbTexture(char* file, int* w, int* h)
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
mlx_image image;
try { image = new mlx_image_handler; }
catch(...) { return nullptr; }
Texture* texture = StbTextureLoad(file, w, h); Texture* texture = StbTextureLoad(file, w, h);
if(texture == nullptr) if(texture == nullptr)
return nullptr; return nullptr;
m_image_registry.RegisterTexture(texture); m_image_registry.RegisterTexture(texture);
return texture; image->texture = texture;
return image;
} }
void Application::DestroyTexture(void* ptr) void Application::DestroyTexture(mlx_image image)
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
RenderCore::Get().WaitDeviceIdle(); RenderCore::Get().WaitDeviceIdle();
if(!m_image_registry.IsTextureKnown(static_cast<Texture*>(ptr))) if(!m_image_registry.IsTextureKnown(image->texture))
{ {
Error("invalid image ptr"); Error("invalid image handle");
return; return;
} }
Texture* texture = static_cast<Texture*>(ptr); Texture* texture = image->texture.Get();
if(!texture->IsInit()) if(!texture->IsInit())
Error("trying to destroy a texture that has already been destroyed"); Error("trying to destroy a texture that has already been destroyed");
else else
@@ -92,6 +104,7 @@ namespace mlx
gs->TryEraseSpritesInScene(texture); gs->TryEraseSpritesInScene(texture);
} }
delete texture; delete texture;
delete image;
} }
Application::~Application() Application::~Application()

View File

@@ -5,13 +5,14 @@
#include <Renderer/RenderCore.h> #include <Renderer/RenderCore.h>
#include <mlx.h> #include <mlx.h>
#include <Core/Memory.h> #include <Core/Memory.h>
#include <Core/Handles.h>
static void* __mlx_ptr = nullptr; static mlx::Application* __internal_application_ptr = nullptr;
#ifndef DISABLE_ALL_SAFETIES #ifndef DISABLE_ALL_SAFETIES
#define MLX_CHECK_APPLICATION_POINTER(ptr) \ #define MLX_CHECK_APPLICATION_POINTER(ptr) \
if(ptr != __mlx_ptr || ptr == NULL) \ if(ptr == NULL || ptr->app.Get() != __internal_application_ptr) \
mlx::FatalError("invalid mlx pointer passed to '%'", MLX_FUNC_SIG); \ mlx::FatalError("invalid mlx handle passed to '%'", MLX_FUNC_SIG); \
else {} // just to avoid issues with possible if-else statements outside this macro else {} // just to avoid issues with possible if-else statements outside this macro
#else #else
#define MLX_CHECK_APPLICATION_POINTER(ptr) #define MLX_CHECK_APPLICATION_POINTER(ptr)
@@ -19,96 +20,151 @@ static void* __mlx_ptr = nullptr;
extern "C" extern "C"
{ {
void* mlx_init() mlx_context mlx_init()
{ {
if(__mlx_ptr != nullptr) if(__internal_application_ptr != nullptr)
{ {
mlx::Error("MLX cannot be initialized multiple times"); mlx::Error("MLX cannot be initialized multiple times");
return nullptr; return nullptr;
} }
mlx::MemManager::Get(); // just to initialize the C garbage collector mlx::MemManager::Get(); // just to initialize the C garbage collector
mlx::Application* app = new mlx::Application;
if(app == nullptr) try { __internal_application_ptr = new mlx::Application; }
mlx::FatalError("Tout a pété"); catch(...) { mlx::FatalError("internal application memory allocation failed"); }
__mlx_ptr = static_cast<void*>(app);
return __mlx_ptr; mlx_context_handler* context;
try { context = new mlx_context_handler; }
catch(...) { mlx::FatalError("mlx_context memory allocation failed"); }
context->app = __internal_application_ptr;
return context;
} }
void* mlx_new_window(void* mlx, int w, int h, const char* title) void mlx_set_fps_goal(mlx_context mlx, int fps)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if(w <= 0 || h <= 0) if(fps < 0)
{ mlx::Error("You cannot set a negative FPS cap (nice try)");
mlx::FatalError("invalid window size (%d x %d)", w, h); else if(fps == 0)
return NULL; // not nullptr for the C compatibility mlx::Error("You cannot set a FPS cap to 0 (nice try)");
} else
return static_cast<mlx::Application*>(mlx)->NewGraphicsSuport(w, h, title, false); mlx->app->SetFPSCap(static_cast<std::uint32_t>(fps));
} }
void* mlx_new_resizable_window(void* mlx, int w, int h, const char* title) void mlx_destroy_context(mlx_context mlx)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if(w <= 0 || h <= 0) delete mlx;
{ delete __internal_application_ptr;
mlx::FatalError("invalid window size (%d x %d)", w, h); __internal_application_ptr = nullptr;
return NULL; // not nullptr for the C compatibility
}
return static_cast<mlx::Application*>(mlx)->NewGraphicsSuport(w, h, title, true);
} }
void mlx_set_window_position(void *mlx, void *win, int x, int y) mlx_window mlx_new_window(mlx_context mlx, const mlx_window_create_info* info)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->SetGraphicsSupportPosition(win, x, y); return mlx->app->NewGraphicsSuport(info);
} }
void mlx_loop_hook(void* mlx, int (*f)(void*), void* param) void mlx_destroy_window(mlx_context mlx, mlx_window win)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->LoopHook(f, param); mlx->app->DestroyGraphicsSupport(win);
} }
void mlx_loop(void* mlx) void mlx_set_window_position(mlx_context mlx, mlx_window win, int x, int y)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->Run(); mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs && !gs->HasWindow())
return;
gs->GetWindow()->SetPosition(x, y);
} }
void mlx_loop_end(void* mlx) void mlx_clear_window(mlx_context mlx, mlx_window win, int color)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->LoopEnd(); mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
gs->ResetRenderData(color);
} }
void mlx_mouse_show() void mlx_get_screen_size(mlx_context mlx, mlx_window win, int* w, int* h)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
if(!gs->HasWindow())
{
*w = 0;
*h = 0;
}
else
gs->GetWindow()->GetScreenSizeWindowIsOn(w, h);
}
void mlx_loop_hook(mlx_context mlx, int (*f)(void*), void* param)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx->app->LoopHook(f, param);
}
void mlx_loop(mlx_context mlx)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx->app->Run();
}
void mlx_loop_end(mlx_context mlx)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx->app->LoopEnd();
}
void mlx_mouse_show(mlx_context mlx)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::SDLManager::ShowCursor(); mlx::SDLManager::ShowCursor();
} }
void mlx_mouse_hide() void mlx_mouse_hide(mlx_context mlx)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::SDLManager::HideCursor(); mlx::SDLManager::HideCursor();
} }
void mlx_mouse_move(void* mlx, void* win, int x, int y) void mlx_mouse_move(mlx_context mlx, mlx_window win, int x, int y)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->MouseMove(win, x, y); mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs && !gs->HasWindow())
return;
gs->GetWindow()->MoveMouse(x, y);
} }
void mlx_mouse_get_pos(void* mlx, int* x, int* y) void mlx_mouse_get_pos(mlx_context mlx, int* x, int* y)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->GetMousePos(x, y); mlx->app->GetMousePos(x, y);
} }
void mlx_on_event(void* mlx, void* win, mlx_event_type event, int (*funct_ptr)(int, void*), void* param) void mlx_on_event(mlx_context mlx, mlx_window win, mlx_event_type event, int (*funct_ptr)(int, void*), void* param)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->OnEvent(win, static_cast<int>(event), funct_ptr, param); mlx->app->OnEvent(win, static_cast<int>(event), funct_ptr, param);
} }
void* mlx_new_image(void* mlx, int width, int height) void mlx_pixel_put(mlx_context mlx, mlx_window win, int x, int y, int color)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
gs->PixelPut(x, y, color);
}
mlx_image mlx_new_image(mlx_context mlx, int width, int height)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if(width <= 0 || height <= 0) if(width <= 0 || height <= 0)
@@ -116,51 +172,10 @@ extern "C"
mlx::Error("invalid image size (% x %)", width, height); mlx::Error("invalid image size (% x %)", width, height);
return nullptr; return nullptr;
} }
return static_cast<mlx::Application*>(mlx)->NewTexture(width, height); return mlx->app->NewTexture(width, height);
} }
int mlx_get_image_pixel(void* mlx, void* img, int x, int y) mlx_image mlx_new_image_from_file(mlx_context mlx, char* filename, int* width, int* height)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
int color = static_cast<mlx::Application*>(mlx)->GetTexturePixel(img, x, y);
unsigned char color_bits[4];
color_bits[0] = (color & 0x000000FF);
color_bits[1] = (color & 0x0000FF00) >> 8;
color_bits[2] = (color & 0x00FF0000) >> 16;
color_bits[3] = (color & 0xFF000000) >> 24;
return *reinterpret_cast<int*>(color_bits);
}
void mlx_set_image_pixel(void* mlx, void* img, int x, int y, int color)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
unsigned char color_bits[4];
color_bits[0] = (color & 0x00FF0000) >> 16;
color_bits[1] = (color & 0x0000FF00) >> 8;
color_bits[2] = (color & 0x000000FF);
color_bits[3] = (color & 0xFF000000) >> 24;
static_cast<mlx::Application*>(mlx)->SetTexturePixel(img, x, y, *reinterpret_cast<unsigned int*>(color_bits));
}
void mlx_put_image_to_window(void* mlx, void* win, void* img, int x, int y)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->TexturePut(win, img, x, y, 1.0f, 0.0f);
}
void mlx_transform_put_image_to_window(void* mlx, void* win, void* img, int x, int y, float scale, float angle)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->TexturePut(win, img, x, y, scale, angle);
}
void mlx_destroy_image(void* mlx, void* img)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->DestroyTexture(img);
}
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) if (filename == nullptr)
@@ -169,71 +184,74 @@ extern "C"
return nullptr; return nullptr;
} }
std::filesystem::path file(filename); std::filesystem::path file(filename);
if(file.extension() != ".png") if( file.extension() != ".png" &&
file.extension() != ".jpg" &&
file.extension() != ".jpeg" &&
file.extension() != ".bmp" &&
file.extension() != ".dib")
{ {
mlx::Error("PNG loader: not a png file '%'", filename); mlx::Error("PNG loader: not a valid file format '%'", filename);
return nullptr; return nullptr;
} }
return static_cast<mlx::Application*>(mlx)->NewStbTexture(filename, width, height); return mlx->app->NewStbTexture(filename, width, height);
} }
void* mlx_jpg_file_to_image(void* mlx, char* filename, int* width, int* height) void mlx_destroy_image(mlx_context mlx, mlx_image image)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (filename == nullptr) mlx->app->DestroyTexture(image);
{
mlx::Error("JPG loader: filename is NULL");
return nullptr;
}
std::filesystem::path file(filename);
if(file.extension() != ".jpg" && file.extension() != ".jpeg")
{
mlx::Error("JPG loader: not a jpg file '%'", filename);
return nullptr;
}
return static_cast<mlx::Application*>(mlx)->NewStbTexture(filename, width, height);
} }
void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height) int mlx_get_image_pixel(mlx_context mlx, mlx_image image, int x, int y)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (filename == nullptr) mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
{ if(!texture)
mlx::Error("BMP loader: filename is NULL"); return 0;
return nullptr; return texture->GetPixel(x, y);
}
std::filesystem::path file(filename);
if(file.extension() != ".bmp" && file.extension() != ".dib")
{
mlx::Error("BMP loader: not a bmp file '%'", filename);
return nullptr;
}
return static_cast<mlx::Application*>(mlx)->NewStbTexture(filename, width, height);
} }
void mlx_pixel_put(void* mlx, void* win, int x, int y, int color) void mlx_set_image_pixel(mlx_context mlx, mlx_image image, int x, int y, int color)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
unsigned char color_bits[4]; mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
color_bits[0] = (color & 0x00FF0000) >> 16; if(!texture)
color_bits[1] = (color & 0x0000FF00) >> 8; return;
color_bits[2] = (color & 0x000000FF); texture->SetPixel(x, y, color);
color_bits[3] = (color & 0xFF000000) >> 24;
static_cast<mlx::Application*>(mlx)->PixelPut(win, x, y, *reinterpret_cast<unsigned int*>(color_bits));
} }
void mlx_string_put(void* mlx, void* win, int x, int y, int color, char* str) void mlx_put_image_to_window(mlx_context mlx, mlx_window win, mlx_image image, int x, int y)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
unsigned char color_bits[4]; mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
color_bits[0] = (color & 0x00FF0000) >> 16; if(!gs)
color_bits[1] = (color & 0x0000FF00) >> 8; return;
color_bits[2] = (color & 0x000000FF); mlx::NonOwningPtr<mlx::Texture> texture = mlx->app->GetTexture(image);
color_bits[3] = (color & 0xFF000000) >> 24; if(!texture)
static_cast<mlx::Application*>(mlx)->StringPut(win, x, y, *reinterpret_cast<unsigned int*>(color_bits), str); return;
gs->TexturePut(texture, x, y, 1.0f, 0.0f);
} }
void mlx_set_font(void* mlx, char* filepath) void mlx_string_put(mlx_context mlx, mlx_window win, int x, int y, int color, char* str)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
mlx::NonOwningPtr<mlx::GraphicsSupport> gs = mlx->app->GetGraphicsSupport(win);
if(!gs)
return;
if(str == nullptr)
{
mlx::Error("invalid text (NULL)");
return;
}
if(std::strlen(str) == 0)
{
mlx::Warning("trying to put an empty text");
return;
}
gs->StringPut(x, y, color, str);
}
void mlx_set_font(mlx_context mlx, char* filepath)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (filepath == nullptr) if (filepath == nullptr)
@@ -248,12 +266,12 @@ extern "C"
return; return;
} }
if(std::strcmp(filepath, "default") == 0) if(std::strcmp(filepath, "default") == 0)
static_cast<mlx::Application*>(mlx)->LoadFont(file, 6.f); mlx->app->LoadFont(file, 6.f);
else else
static_cast<mlx::Application*>(mlx)->LoadFont(file, 16.f); mlx->app->LoadFont(file, 16.f);
} }
void mlx_set_font_scale(void* mlx, char* filepath, float scale) void mlx_set_font_scale(mlx_context mlx, char* filepath, float scale)
{ {
MLX_CHECK_APPLICATION_POINTER(mlx); MLX_CHECK_APPLICATION_POINTER(mlx);
if (filepath == nullptr) if (filepath == nullptr)
@@ -267,47 +285,6 @@ extern "C"
mlx::Error("TTF loader: not a truetype font file '%'", filepath); mlx::Error("TTF loader: not a truetype font file '%'", filepath);
return; return;
} }
static_cast<mlx::Application*>(mlx)->LoadFont(file, scale); mlx->app->LoadFont(file, scale);
}
void mlx_clear_window(void* mlx, void* win, int color)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
unsigned char color_bits[4];
color_bits[0] = (color & 0x00FF0000) >> 16;
color_bits[1] = (color & 0x0000FF00) >> 8;
color_bits[2] = (color & 0x000000FF);
color_bits[3] = (color & 0xFF000000) >> 24;
static_cast<mlx::Application*>(mlx)->ClearGraphicsSupport(win, *reinterpret_cast<unsigned int*>(color_bits));
}
void mlx_destroy_window(void* mlx, void* win)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->DestroyGraphicsSupport(win);
}
void mlx_destroy_display(void* mlx)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
delete static_cast<mlx::Application*>(mlx);
__mlx_ptr = nullptr;
}
void mlx_get_screens_size(void* mlx, void* win, int* w, int* h)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
static_cast<mlx::Application*>(mlx)->GetScreenSize(win, w, h);
}
void mlx_set_fps_goal(void* mlx, int fps)
{
MLX_CHECK_APPLICATION_POINTER(mlx);
if(fps < 0)
mlx::Error("You cannot set a negative FPS cap (nice try)");
else if(fps == 0)
mlx::Error("You cannot set a FPS cap to 0 (nice try)");
else
static_cast<mlx::Application*>(mlx)->SetFPSCap(static_cast<std::uint32_t>(fps));
} }
} }

View File

@@ -3,25 +3,15 @@
namespace mlx namespace mlx
{ {
GraphicsSupport::GraphicsSupport([[maybe_unused]] std::size_t w, [[maybe_unused]] std::size_t h, NonOwningPtr<Texture> render_target, int id) : GraphicsSupport::GraphicsSupport(const mlx_window_create_info* info, int id) :
m_put_pixel_manager(&m_renderer), m_put_pixel_manager(&m_renderer),
p_window(nullptr), m_id(id)
m_id(id),
m_has_window(false)
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
m_renderer.Init(render_target);
m_scene_renderer.Init(render_target);
p_scene = std::make_unique<Scene>();
}
GraphicsSupport::GraphicsSupport(std::size_t w, std::size_t h, std::string title, int id, bool is_resizable) : p_window = std::make_shared<Window>(info);
m_put_pixel_manager(&m_renderer), m_has_window = true;
p_window(std::make_shared<Window>(w, h, title, is_resizable)),
m_id(id),
m_has_window(true)
{
MLX_PROFILE_FUNCTION();
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>();

View File

@@ -47,7 +47,7 @@ namespace mlx
DebugLog("SDL Manager initialized"); DebugLog("SDL Manager initialized");
} }
Handle SDLManager::CreateWindow(const std::string& title, std::size_t w, std::size_t h, bool hidden, std::int32_t& id, bool is_resizable) Handle SDLManager::CreateWindow(const mlx_window_create_info* info, std::int32_t& id, bool hidden)
{ {
Internal::WindowInfos* infos = new Internal::WindowInfos; Internal::WindowInfos* infos = new Internal::WindowInfos;
Verify(infos != nullptr, "SDL: window allocation failed"); Verify(infos != nullptr, "SDL: window allocation failed");
@@ -57,10 +57,10 @@ namespace mlx
flags |= SDL_WINDOW_HIDDEN; flags |= SDL_WINDOW_HIDDEN;
else else
flags |= SDL_WINDOW_SHOWN; flags |= SDL_WINDOW_SHOWN;
if(is_resizable) if(info->is_resizable)
flags |= SDL_WINDOW_RESIZABLE; flags |= SDL_WINDOW_RESIZABLE;
infos->window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags); infos->window = SDL_CreateWindow(info->title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, info->width, info->height, flags);
if(!infos->window) if(!infos->window)
FatalError("SDL: unable to open a new window; %", SDL_GetError()); FatalError("SDL: unable to open a new window; %", SDL_GetError());
infos->icon = SDL_CreateRGBSurfaceFrom(static_cast<void*>(logo_mlx), logo_mlx_width, logo_mlx_height, 32, 4 * logo_mlx_width, rmask, gmask, bmask, amask); infos->icon = SDL_CreateRGBSurfaceFrom(static_cast<void*>(logo_mlx), logo_mlx_width, logo_mlx_height, 32, 4 * logo_mlx_width, rmask, gmask, bmask, amask);

View File

@@ -5,7 +5,7 @@
namespace mlx namespace mlx
{ {
NonOwningPtr<Texture> PutPixelManager::DrawPixel(int x, int y, std::uint64_t draw_layer, std::uint32_t color) NonOwningPtr<Texture> PutPixelManager::DrawPixel(int x, int y, std::uint64_t draw_layer, int color)
{ {
Verify((bool)p_renderer, "invalid renderer pointer"); Verify((bool)p_renderer, "invalid renderer pointer");

View File

@@ -5,9 +5,9 @@
namespace mlx namespace mlx
{ {
Window::Window(std::size_t w, std::size_t h, const std::string& title, bool is_resizable, bool hidden) : m_name(title), m_width(w), m_height(h) Window::Window(const mlx_window_create_info* info, bool hidden) : m_name(info->title), m_width(info->width), m_height(info->height)
{ {
p_window = SDLManager::Get().CreateWindow(title, w, h, hidden, m_id, is_resizable); p_window = SDLManager::Get().CreateWindow(info, m_id, hidden);
} }
void Window::Destroy() noexcept void Window::Destroy() noexcept

View File

@@ -30,6 +30,14 @@
namespace mlx namespace mlx
{ {
unsigned char reverse(unsigned char b)
{
b = (b & 0xF0) >> 4 | (b & 0x0F) << 4;
b = (b & 0xCC) >> 2 | (b & 0x33) << 2;
b = (b & 0xAA) >> 1 | (b & 0x55) << 1;
return b;
}
void Image::Init(ImageType type, std::uint32_t width, std::uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, bool is_multisampled, [[maybe_unused]] std::string_view debug_name) void Image::Init(ImageType type, std::uint32_t width, std::uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, bool is_multisampled, [[maybe_unused]] std::string_view debug_name)
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
@@ -199,14 +207,19 @@ namespace mlx
Image::Destroy(); Image::Destroy();
} }
void Texture::SetPixel(int x, int y, std::uint32_t color) noexcept void Texture::SetPixel(int x, int y, int color) noexcept
{ {
MLX_PROFILE_FUNCTION(); MLX_PROFILE_FUNCTION();
if(x < 0 || y < 0 || static_cast<std::uint32_t>(x) > m_width || static_cast<std::uint32_t>(y) > m_height) if(x < 0 || y < 0 || static_cast<std::uint32_t>(x) > m_width || static_cast<std::uint32_t>(y) > m_height)
return; return;
if(!m_staging_buffer.has_value()) if(!m_staging_buffer.has_value())
OpenCPUBuffer(); OpenCPUBuffer();
m_cpu_buffer[(y * m_width) + x] = color; unsigned char bytes[4];
bytes[0] = (color >> 24) & 0xFF;
bytes[1] = (color >> 16) & 0xFF;
bytes[2] = (color >> 8) & 0xFF;
bytes[3] = color & 0xFF;
m_cpu_buffer[(y * m_width) + x] = *reinterpret_cast<int*>(bytes);
m_has_been_modified = true; m_has_been_modified = true;
} }
@@ -217,12 +230,7 @@ namespace mlx
return 0; return 0;
if(!m_staging_buffer.has_value()) if(!m_staging_buffer.has_value())
OpenCPUBuffer(); OpenCPUBuffer();
std::uint32_t color = m_cpu_buffer[(y * m_width) + x]; return m_cpu_buffer[(y * m_width) + x];
std::uint8_t* bytes = reinterpret_cast<std::uint8_t*>(&color);
std::uint8_t tmp = bytes[0];
bytes[0] = bytes[2];
bytes[2] = tmp;
return *reinterpret_cast<int*>(bytes);
} }
void Texture::Update(VkCommandBuffer cmd) void Texture::Update(VkCommandBuffer cmd)

View File

@@ -26,6 +26,7 @@
#include <kvf.h> #include <kvf.h>
#endif #endif
#include <mlx.h>
#include <Renderer/Vulkan/VulkanLoader.h> #include <Renderer/Vulkan/VulkanLoader.h>
#include <Core/SDLManager.h> #include <Core/SDLManager.h>
#include <Platform/Window.h> #include <Platform/Window.h>
@@ -86,7 +87,11 @@ namespace mlx
kvfSetValidationErrorCallback(&ValidationErrorCallback); kvfSetValidationErrorCallback(&ValidationErrorCallback);
kvfSetValidationWarningCallback(&WarningCallback); kvfSetValidationWarningCallback(&WarningCallback);
Window window(1, 1, "", true); mlx_window_create_info info{};
info.title = "";
info.width = 1;
info.height = 1;
Window window(&info, true);
std::vector<const char*> instance_extensions = window.GetRequiredVulkanInstanceExtentions(); std::vector<const char*> instance_extensions = window.GetRequiredVulkanInstanceExtentions();
#ifdef MLX_PLAT_MACOS #ifdef MLX_PLAT_MACOS
instance_extensions.push_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME); instance_extensions.push_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);