part: implement mlx_xpm_file_to_image()

This commit is contained in:
Zy 2024-10-12 19:42:46 +02:00
parent 8649e86eaa
commit 9264c47494
4 changed files with 318 additions and 20 deletions

View file

@ -47,7 +47,7 @@ int main(void) {
// MENU
{
switch (uc_menu_quick("pixels", "images", "xpm", "files",
"navigate", "assets", "quit", NULL))
"navigate", "assets", "xpm files", "quit", NULL))
{
case 0:
goto pixels;
@ -67,6 +67,9 @@ int main(void) {
case 5:
goto assets;
break;
case 6:
goto xpm_files;
break;
}
goto end;
}
@ -551,6 +554,34 @@ assets:
}
goto end;
xpm_files:
{
cout
<< "Display snas.xpm from" << endl
<< "mlx_xpm_file_to_image()..." << endl;
uc_pause();
{
win = mlx_new_window(mlx, 400, 240, NULL);
if (!win) {
cout << "mlx_new_window() error :/" << endl;
goto xpm_files_end;
}
int w, h;
t_image img = mlx_xpm_file_to_image(mlx, "snas.xpm", &w, &h);
if (!img) {
cout << "Image not found :/" << endl;
goto xpm_files_end;
}
cout << "width: " << w << ", height: " << h << endl;
mlx_put_image_to_window(mlx, win, img, 100, 100);
}
xpm_files_end:
cout << "DONE! (i hope)" << endl;
uc_pause();
}
goto end;
end:
cout << "Exit..." << endl;
uc_pause();

View file

@ -18,6 +18,7 @@
#include "mlx_int.h"
#include "mlx.h"
#include "mlx_internal.h"
#include "mlx3ds.h"
extern struct s_col_name mlx_col_name[];
@ -271,34 +272,34 @@ int mlx_int_file_get_rid_comment(char *ptr, int size)
return (0);
}
/* [USED]
t_image mlx_xpm_file_to_image(t_mlx xvar,char *file,int *width,int *height)
// [USED]
t_image mlx_xpm_file_to_image(t_mlx xvar, const char *file,int *width,int *height)
{
int fd;
int size;
const t_embeddedasset *asset;
char *ptr;
t_internal_image *img;
fd = -1;
if ((fd = open(file,O_RDONLY))==-1 || (size = lseek(fd,0,SEEK_END))==-1 ||
(ptr = mmap(0,size,PROT_WRITE|PROT_READ,MAP_PRIVATE,fd,0))==
(void *)MAP_FAILED)
{
if (fd>=0)
close(fd);
return ((void *)0);
}
mlx_int_file_get_rid_comment(ptr, size);
if (img = mlx_int_parse_xpm(xvar,ptr,size,mlx_int_get_line))
// fd = -1;
// if ((fd = open(file,O_RDONLY))==-1 || (size = lseek(fd,0,SEEK_END))==-1 ||
// (ptr = mmap(0,size,PROT_WRITE|PROT_READ,MAP_PRIVATE,fd,0))==
// (void *)MAP_FAILED)
// {
// if (fd>=0)
// close(fd);
// return ((void *)0);
// }
asset = mlx3ds_assets_get(file);
if (!asset)
return (NULL);
ptr = strdup(asset->data);
mlx_int_file_get_rid_comment(ptr, asset->size);
if (img = mlx_int_parse_xpm(xvar,ptr,asset->size,mlx_int_get_line))
{
*width = img->width;
*height = img->height;
}
munmap(ptr,size);
close(fd);
return (img);
}
//*/
// [USED] [FULL]
t_image mlx_xpm_to_image(t_mlx mlx_ptr, const char **xpm_data,int *width,int *height)