working on xpm implementation

This commit is contained in:
2023-04-04 15:41:45 +02:00
parent d02f04c294
commit c85c12bfaf
8 changed files with 145 additions and 28 deletions

View File

@@ -6,7 +6,7 @@
# By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2022/10/04 16:43:41 by maldavid #+# #+# #
# Updated: 2023/03/31 12:19:47 by maldavid ### ########.fr #
# Updated: 2023/04/04 13:09:06 by maldavid ### ########.fr #
# #
# **************************************************************************** #
@@ -29,7 +29,7 @@ ifeq ($(TOOLCHAIN), gcc)
endif
CXXFLAGS = -std=c++17 -O3 -fPIC
INCLUDES = -I./includes -I./src -I./third_party
INCLUDES = -I./includes -I./src -I./third_party -I/nfs/homes/maldavid/.xmake/packages/l/libsdl/2.26.4/8dfbcb8049e744a597cd5333e1b399cd/include
ifeq ($(DEBUG), true)
CXXFLAGS += -g

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 16:56:35 by maldavid #+# #+# */
/* Updated: 2023/04/03 10:49:01 by maldavid ### ########.fr */
/* Updated: 2023/04/04 13:43:42 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -42,6 +42,8 @@ int mlx_destroy_image(void* mlx, void* img);
void* mlx_png_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);
void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height);
void* mlx_xpm_file_to_image(void* mlx, char* filename, int* width, int* height);
void* mlx_xpm_to_image(void* mlx, char** xpm_data, int* width, int* height);
int mlx_clear_window(void* mlx, void* win);

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/02 23:49:03 by maldavid ### ########.fr */
/* Updated: 2023/04/04 14:54:12 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -14,6 +14,7 @@
#include <renderer/images/texture.h>
#include <renderer/core/render_core.h>
#include <X11/X.h> // for LSBFirst
#include <cstdio>
namespace mlx::core
{
@@ -61,6 +62,15 @@ namespace mlx::core
return map;
}
void* Application::newXpmTexture(char** data, int* w, int* h)
{
std::shared_ptr<Texture> texture = std::make_shared<Texture>();
texture->create(pixels.get(), width, height, VK_FORMAT_R8G8B8A8_UNORM);
TextureID id = _texture_lib.addTextureToLibrary(texture);
_texture_ids.push_back(id);
return &_texture_ids.back();
}
void Application::destroyTexture(void* ptr)
{
vkDeviceWaitIdle(Render_Core::get().getDevice().get());

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 21:49:46 by maldavid #+# #+# */
/* Updated: 2023/04/03 11:18:41 by maldavid ### ########.fr */
/* Updated: 2023/04/04 13:43:04 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -47,6 +47,7 @@ namespace mlx::core
inline void pixelPut(void* win_ptr, int x, int y, int color) const noexcept;
void* newTexture(int w, int h);
void* newXpmTexture(char** data, int* w, int* h);
void* newStbTexture(char* file, int* w, int* h); // stb textures are format managed by stb image (png, jpg, bpm, ...)
char* mapTexture(void* img_ptr, int* bits_per_pixel, int* size_line, int* endian);
inline void texturePut(void* win_ptr, void* img, int x, int y);

View File

@@ -6,7 +6,7 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
/* Updated: 2023/04/03 14:18:50 by maldavid ### ########.fr */
/* Updated: 2023/04/04 13:44:12 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
@@ -57,9 +57,9 @@ extern "C"
return SDL_ShowCursor(SDL_DISABLE);
}
int mlx_mouse_move(void* mlx, void* win_ptr, int x, int y)
int mlx_mouse_move(void* mlx, void* win, int x, int y)
{
static_cast<mlx::core::Application*>(mlx)->mouseMove(win_ptr, x, y);
static_cast<mlx::core::Application*>(mlx)->mouseMove(win, x, y);
return 0;
}
@@ -86,24 +86,24 @@ extern "C"
return static_cast<mlx::core::Application*>(mlx)->newTexture(width, height);
}
char* mlx_get_data_addr(void* mlx, void* img_ptr, int* bits_per_pixel, int* size_line, int* endian)
char* mlx_get_data_addr(void* mlx, void* img, int* bits_per_pixel, int* size_line, int* endian)
{
return static_cast<mlx::core::Application*>(mlx)->mapTexture(img_ptr, bits_per_pixel, size_line, endian);
return static_cast<mlx::core::Application*>(mlx)->mapTexture(img, bits_per_pixel, size_line, endian);
}
int mlx_put_image_to_window(void* mlx_ptr, void* win_ptr, void* img_ptr, int x, int y)
int mlx_put_image_to_window(void* mlx, void* win, void* img, int x, int y)
{
static_cast<mlx::core::Application*>(mlx_ptr)->texturePut(win_ptr, img_ptr, x, y);
static_cast<mlx::core::Application*>(mlx)->texturePut(win, img, x, y);
return 0;
}
int mlx_destroy_image(void* mlx_ptr, void* img_ptr)
int mlx_destroy_image(void* mlx, void* img)
{
static_cast<mlx::core::Application*>(mlx_ptr)->destroyTexture(img_ptr);
static_cast<mlx::core::Application*>(mlx)->destroyTexture(img);
return 0;
}
void* mlx_png_file_to_image(void* mlx_ptr, char* filename, int* width, int* height)
void* mlx_png_file_to_image(void* mlx, char* filename, int* width, int* height)
{
std::filesystem::path file(filename);
if(file.extension() != ".png")
@@ -111,10 +111,10 @@ extern "C"
mlx::core::error::report(e_kind::error, "PNG loader : not a png file '%s'", filename);
return nullptr;
}
return static_cast<mlx::core::Application*>(mlx_ptr)->newStbTexture(filename, width, height);
return static_cast<mlx::core::Application*>(mlx)->newStbTexture(filename, width, height);
}
void* mlx_jpg_file_to_image(void* mlx_ptr, char* filename, int* width, int* height)
void* mlx_jpg_file_to_image(void* mlx, char* filename, int* width, int* height)
{
std::filesystem::path file(filename);
if(file.extension() != ".jpg" && file.extension() != ".jpeg")
@@ -122,10 +122,10 @@ extern "C"
mlx::core::error::report(e_kind::error, "PNG loader : not a jpg file '%s'", filename);
return nullptr;
}
return static_cast<mlx::core::Application*>(mlx_ptr)->newStbTexture(filename, width, height);
return static_cast<mlx::core::Application*>(mlx)->newStbTexture(filename, width, height);
}
void* mlx_bmp_file_to_image(void* mlx_ptr, char* filename, int* width, int* height)
void* mlx_bmp_file_to_image(void* mlx, char* filename, int* width, int* height)
{
std::filesystem::path file(filename);
if(file.extension() != ".bmp" && file.extension() != ".dib")
@@ -133,24 +133,34 @@ extern "C"
mlx::core::error::report(e_kind::error, "PNG loader : not a jpg file '%s'", filename);
return nullptr;
}
return static_cast<mlx::core::Application*>(mlx_ptr)->newStbTexture(filename, width, height);
return static_cast<mlx::core::Application*>(mlx)->newStbTexture(filename, width, height);
}
int mlx_pixel_put(void* mlx, void* win_ptr, int x, int y, int color)
void* mlx_xpm_file_to_image(void* mlx, char* filename, int* width, int* height)
{
static_cast<mlx::core::Application*>(mlx)->pixelPut(win_ptr, x, y, color);
//return static_cast<mlx::core::Application*>(mlx)->newXpmTexture(filename, width, height);
}
void* mlx_xpm_to_image(void* mlx, char** xpm_data, int* width, int* height)
{
return static_cast<mlx::core::Application*>(mlx)->newXpmTexture(xpm_data, width, height);
}
int mlx_pixel_put(void* mlx, void* win, int x, int y, int color)
{
static_cast<mlx::core::Application*>(mlx)->pixelPut(win, x, y, color);
return 0;
}
int mlx_clear_window(void* mlx_ptr, void* win_ptr)
int mlx_clear_window(void* mlx, void* win)
{
static_cast<mlx::core::Application*>(mlx_ptr)->clearGraphicsSupport(win_ptr);
static_cast<mlx::core::Application*>(mlx)->clearGraphicsSupport(win);
return 0;
}
int mlx_destroy_window(void* mlx, void* win_ptr)
int mlx_destroy_window(void* mlx, void* win)
{
static_cast<mlx::core::Application*>(mlx)->destroyGraphicsSupport(win_ptr);
static_cast<mlx::core::Application*>(mlx)->destroyGraphicsSupport(win);
return 0;
}

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

@@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* xpm_reader.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/04 14:55:09 by maldavid #+# #+# */
/* Updated: 2023/04/04 15:39:16 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
/**
* https://github.com/clckwrkbdgr/libchthon
* This xpm reader is a modified version of libchthon one
* because xmp readers are as rare as the sun in Scotland.
*/
#ifndef __MLX_XPM_READER__
#define __MLX_XPM_READER__
#include <string>
#include <vector>
#include <cstddef>
#include <map>
namespace mlx
{
struct XPMData
{
XPMData();
};
typedef uint32_t Color;
typedef uint8_t ColorComponent;
Color from_rgb(ColorComponent r, ColorComponent g, ColorComponent b);
uint8_t get_red(Color color);
uint8_t get_green(Color color);
uint8_t get_blue(Color color);
bool is_transparent(Color color);
Color rgb_to_argb(Color color);
class Pixmap
{
public:
Pixmap(uint32_t w = 1, uint32_t h = 1, uint32_t palette_size = 1);
void load(const std::vector<std::string>& xpm_lines);
Map<uint32_t> pixels;
std::vector<Color> palette;
private:
uint32_t _color_count;
uint32_t _row_count;
std::vector<std::string> _interspaces;
std::vector<std::string> _colors;
std::vector<std::string> _values_interspaces;
std::vector<std::pair<std::string, std::pair<std::string, std::string>>> _colors_interspaces;
};
}
#endif

View File

@@ -6,13 +6,39 @@
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:55:21 by maldavid #+# #+# */
/* Updated: 2023/04/03 00:05:08 by maldavid ### ########.fr */
/* Updated: 2023/04/04 14:54:04 by maldavid ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include "../includes/mlx.h"
static char * exemple_xpm[] = {
"24 20 3 1",
" c None",
". c #3A32E4",
"+ c #E43232",
" ",
" .. ",
" .... ",
" ......++++++++ ",
" .........+++++++ ",
" ..........+++++++ ",
" ............++++++ ",
" .............++++++ ",
" ..............++++ ",
" +.............+++ ",
" ++.............++ ",
" +++.............+ ",
" +++++............. ",
" ++++++.............. ",
" ++++++++............ ",
" +++++++++........... ",
" +++++++++......... ",
" ++++++++++....... ",
" ++++++++++..... ",
" +++++++++ ... "};
typedef struct s_mlx
{
void *mlx;
@@ -71,10 +97,13 @@ int main(void)
int w;
int h;
void *img;
void *pic;
mlx.mlx = mlx_init();
mlx.win = mlx_new_window(mlx.mlx, 400, 400, "My window");
mlx.logo = mlx_png_file_to_image(mlx.mlx, "42_logo.png", &w, &h);
pic = mlx_xpm_to_image(mlx.mlx, exemple_xpm, &w, &h);
mlx_put_image_to_window(mlx.mlx, mlx.win, pic, 20, 20);
mlx_pixel_put(mlx.mlx, mlx.win, 200, 10, 0xFFFF00FF);
mlx_put_image_to_window(mlx.mlx, mlx.win, mlx.logo, 200, 200);
img = create_image(&mlx);
@@ -82,6 +111,7 @@ int main(void)
mlx_loop_hook(mlx.mlx, update, &mlx);
mlx_loop(mlx.mlx);
mlx_destroy_image(mlx.mlx, img);
mlx_destroy_image(mlx.mlx, pic);
mlx_destroy_image(mlx.mlx, mlx.logo);
mlx_destroy_window(mlx.mlx, mlx.win);
mlx_destroy_display(mlx.mlx);

View File

@@ -1 +1 @@
clang main.c ../libmlx.so -lSDL2 -g && ./a.out
clang main.c ../libmlx.so `/nfs/homes/maldavid/.xmake/packages/l/libsdl/2.26.4/8dfbcb8049e744a597cd5333e1b399cd/bin/sdl2-config --cflags --libs` -g && ./a.out