working on xpm support

This commit is contained in:
2023-04-06 12:56:36 +02:00
parent 27000b6163
commit fbe97415cf
6 changed files with 83 additions and 74 deletions

View File

@@ -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
```

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <renderer/core/render_core.h>
#include <X11/X.h> // for LSBFirst
#include <array>
#include <cstring>
#ifndef MLX_NO_XPM
#include <X11/xpm.h>
#endif
#include <iostream>
#include <utils/xpm_reader.h>
namespace mlx::core
{
@@ -69,60 +63,11 @@ namespace mlx::core
return map;
}
#ifndef MLX_NO_XPM
struct SafeXpmImage: public XpmImage // included in <X11/xpm.h>
{
~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<uint8_t> 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<uint8_t> pixels = parseXpmData(data, w, h);
std::shared_ptr<Texture> texture = std::make_shared<Texture>();
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> texture = std::make_shared<Texture>();
texture->create(reinterpret_cast<uint8_t*>(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)
{

46
src/utils/xpm_reader.cpp git.filemode.normal_file
View File

@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* xpm_reader.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/05 13:37:21 by maldavid #+# #+# */
/* Updated: 2023/04/05 13:53:30 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <utils/xpm_reader.h>
#include <core/errors.h>
#include <cstdio>
namespace mlx
{
std::vector<uint8_t> parseXpmFile(std::filesystem::path file, int* w, int* h)
{
return {};
}
std::vector<uint8_t> 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;
}
}

26
src/utils/xpm_reader.h git.filemode.normal_file
View File

@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* xpm_reader.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <filesystem>
#include <cstdint>
#include <vector>
namespace mlx
{
std::vector<uint8_t> parseXpmData(char** data, int* w, int* h);
std::vector<uint8_t> parseXpmFile(std::filesystem::path file, int* w, int* h);
}
#endif

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@@ -1 +1 @@
clang main.c ../libmlx.so -lSDL2 -lXpm -g && ./a.out
clang main.c ../libmlx.so -lSDL2 -g && ./a.out