From 0466c40242f97c78bc612360f8b925aa29eb7194 Mon Sep 17 00:00:00 2001 From: kbz_8 Date: Thu, 6 Apr 2023 12:56:36 +0200 Subject: [PATCH] working on xpm support --- README.md | 3 +- src/core/application.cpp | 76 ++++------------------------------------ src/utils/xpm_reader.cpp | 46 ++++++++++++++++++++++++ src/utils/xpm_reader.h | 26 ++++++++++++++ test/main.c | 4 ++- test/run.sh | 2 +- 6 files changed, 83 insertions(+), 74 deletions(-) create mode 100644 src/utils/xpm_reader.cpp create mode 100644 src/utils/xpm_reader.h diff --git a/README.md b/README.md index 771cf72..d1ad42e 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,6 @@ For Arch based distros 3. Compile your project -If you didn't disable Xpm support you'll have to link `libXpm`. You can remove it if you used `#define MLX_NO_XPM` before including `mlx.h` ```bash -clang myApp.c MacroLibX/libmlx.so -lSDL2 -lXpm +clang myApp.c MacroLibX/libmlx.so -lSDL2 ``` diff --git a/src/core/application.cpp b/src/core/application.cpp index 8bb3798..bd5440f 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */ -/* Updated: 2023/04/05 01:11:36 by maldavid ### ########.fr */ +/* Updated: 2023/04/05 13:42:39 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,13 +15,7 @@ #include #include // for LSBFirst #include -#include - -#ifndef MLX_NO_XPM - #include -#endif - -#include +#include namespace mlx::core { @@ -69,60 +63,11 @@ namespace mlx::core return map; } -#ifndef MLX_NO_XPM - struct SafeXpmImage: public XpmImage // included in - { - ~SafeXpmImage() { XpmFreeXpmImage(this); } - }; - void* Application::newXpmTexture(char** data, int* w, int* h) { - SafeXpmImage xpm_image; - if(XpmCreateXpmImageFromData(data, &xpm_image, nullptr) != XpmSuccess) - { - error::report(e_kind::error, "XPM reader : invalid xpm file"); - return nullptr; - } - - *w = xpm_image.width; - *h = xpm_image.height; - - std::vector formatted_data; - formatted_data.reserve((*w) * (*h) * 4); - uint32_t pixel_color; - for(int i = 0; i < *h; i++) - { - for(int j = 0, k = 0; k < *w; j += 4, k++) - { - pixel_color = -1; - for(int l = 0; l < xpm_image.ncolors; l++) - { - if(std::memcmp(xpm_image.colorTable[l].c_color, &xpm_image.data[(i * (*w)) + k], 1) == 0) - { - pixel_color = l; - break; - } - } - - if(pixel_color < xpm_image.ncolors) - { - formatted_data[(i * (*w)) + j + 0] = xpm_image.colorTable[pixel_color].m_color[0]; - formatted_data[(i * (*w)) + j + 1] = xpm_image.colorTable[pixel_color].m_color[1]; - formatted_data[(i * (*w)) + j + 2] = xpm_image.colorTable[pixel_color].m_color[1]; - formatted_data[(i * (*w)) + j + 3] = 0xFF; - } - else - { - formatted_data[(i * (*w)) + j + 0] = 0xFF; - formatted_data[(i * (*w)) + j + 1] = 0xFF; - formatted_data[(i * (*w)) + j + 2] = 0xFF; - formatted_data[(i * (*w)) + j + 3] = 0xFF; - } - } - } - + std::vector pixels = parseXpmData(data, w, h); std::shared_ptr texture = std::make_shared(); - texture->create(formatted_data.data(), *w, *h, VK_FORMAT_R8G8B8A8_UNORM); + texture->create(pixels.data(), *w, *h, VK_FORMAT_R8G8B8A8_UNORM); TextureID id = _texture_lib.addTextureToLibrary(texture); _texture_ids.push_back(id); return &_texture_ids.back(); @@ -130,23 +75,14 @@ namespace mlx::core void* Application::newXpmTexture(std::string filename, int* w, int* h) { - SafeXpmImage xpm_image; - if(XpmReadFileToXpmImage(filename.c_str(), &xpm_image, nullptr) != XpmSuccess) - { - error::report(e_kind::error, "XPM reader : invalid xpm file"); - return nullptr; - } - - *w = xpm_image.width; - *h = xpm_image.height; - + /* std::shared_ptr texture = std::make_shared(); texture->create(reinterpret_cast(xpm_image.data), *w, *h, VK_FORMAT_R8G8B8A8_UNORM); TextureID id = _texture_lib.addTextureToLibrary(texture); _texture_ids.push_back(id); return &_texture_ids.back(); + */ } -#endif void Application::destroyTexture(void* ptr) { diff --git a/src/utils/xpm_reader.cpp b/src/utils/xpm_reader.cpp new file mode 100644 index 0000000..0041945 --- /dev/null +++ b/src/utils/xpm_reader.cpp @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* xpm_reader.cpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/05 13:37:21 by maldavid #+# #+# */ +/* Updated: 2023/04/05 13:53:30 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include + +namespace mlx +{ + std::vector parseXpmFile(std::filesystem::path file, int* w, int* h) + { + return {}; + } + + std::vector parseXpmData(char** data, int* w, int* h) + { + if(data == nullptr) + { + core::error::report(e_kind::error, "Xpm reader : null data"); + return {}; + } + + uint32_t width; + uint32_t height; + uint32_t ncolors; + uint32_t cpp; // chars per pixels + + if(std::sscanf(data[0], "%d %d %d %d", &width, &height, &ncolors, &cpp) != 4) + { + core::error::report(e_kind::error, "Xpm reader : invalid pixmap description"); + return {}; + } + + *w = width; + *h = height; + } +} diff --git a/src/utils/xpm_reader.h b/src/utils/xpm_reader.h new file mode 100644 index 0000000..ec1e33c --- /dev/null +++ b/src/utils/xpm_reader.h @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* xpm_reader.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maldavid +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/04/05 13:25:55 by maldavid #+# #+# */ +/* Updated: 2023/04/05 13:35:48 by maldavid ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef __MLX_XPM_READER__ +#define __MLX_XPM_READER__ + +#include +#include +#include + +namespace mlx +{ + std::vector parseXpmData(char** data, int* w, int* h); + std::vector parseXpmFile(std::filesystem::path file, int* w, int* h); +} + +#endif diff --git a/test/main.c b/test/main.c index 932e357..4c59a8b 100644 --- a/test/main.c +++ b/test/main.c @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */ -/* Updated: 2023/04/04 21:41:32 by maldavid ### ########.fr */ +/* Updated: 2023/04/05 13:03:09 by maldavid ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,11 +59,13 @@ int update(t_mlx *mlx) mlx_pixel_put(mlx->mlx, mlx->win, 399 - j, j, 0xFF0000FF); j++; } + /* i++; if (i == 5000) mlx_clear_window(mlx->mlx, mlx->win); if (i > 10000) mlx_loop_end(mlx->mlx); + */ return (0); } diff --git a/test/run.sh b/test/run.sh index 4975129..f3c908d 100755 --- a/test/run.sh +++ b/test/run.sh @@ -1 +1 @@ -clang main.c ../libmlx.so -lSDL2 -lXpm -g && ./a.out +clang main.c ../libmlx.so -lSDL2 -g && ./a.out