changed rgb deal, and improve memorybook with 2d

This commit is contained in:
hugogogo
2022-05-01 22:55:00 +02:00
parent 43852938c4
commit eb5f2db7fa
9 changed files with 60 additions and 54 deletions

View File

@@ -23,6 +23,7 @@ D_SRCS = srcs \
SRCS = cube3d.c
# mem/
SRCS += memorybook.c \
memorybook_2d.c \
memorybook_utils.c
# init/
SRCS += init_struct.c

View File

@@ -6,7 +6,7 @@
*/
/* nbr pixel player move */
# define PLR_MV 1
# define PLR_MV 2
/* degree of rotation of the player per press */
# define PLR_ROT 2
/* nbr key you can press at the same time */

View File

@@ -89,8 +89,10 @@ typedef struct s_txt
char *txt_south;
char *txt_east;
char *txt_west;
int rgb_floor[3];
int rgb_ceiling[3];
int rgb_floor;
int rgb_ceiling;
// int rgb_floor[3];
// int rgb_ceiling[3];
} t_txt;
/*

View File

@@ -1,10 +1,15 @@
#ifndef MEMORYBOOK_H
# define MEMORYBOOK_H
// memorybook.c
void mb_init(void(*f)(void*), void *param);
void *mb_alloc(size_t size);
void mb_add(void *addr);
void mb_free(void *addr);
void mb_exit(char *str);
void mb_exit(char *str, int status);
// memorybook_2d.c
void mb_add_2d(void **addr, int nb);
void mb_free_2d(void **addr, int nb);
#endif

View File

@@ -19,7 +19,7 @@ void destroy_mlx(void *param)
int shut_down(void)
{
mb_exit(B_RED"close windows"RESET"\n");
mb_exit(B_RED"close windows"RESET"\n", EXIT_SUCCESS);
return (0);
}

View File

@@ -1,6 +1,6 @@
#include "cube3d.h"
void draw_floor_ceiling(t_game *game, t_rcast *rcast)
void draw_floor_ceiling(t_game *game, t_rcast *rcast, t_txt *txt)
{
t_vec plan;
@@ -10,20 +10,20 @@ void draw_floor_ceiling(t_game *game, t_rcast *rcast)
{
plan.start.y = rcast->screen_height;
plan.end.y = rcast->wall.start.y;
draw_line(&game->img, &plan, 0x00FF0000);
draw_line(&game->img, &plan, txt->rgb_floor);
}
if (rcast->wall.start.y < rcast->screen_height)
{
plan.start.y = rcast->wall.end.y;
plan.end.y = 0;
draw_line(&game->img, &plan, 0x000000FF);
draw_line(&game->img, &plan, txt->rgb_ceiling);
}
}
void draw_column(t_game *game, t_rcast *rcast)
{
int color;
draw_floor_ceiling(game, rcast);
draw_floor_ceiling(game, rcast, &game->txt);
color = 0x00FF00FF;
if (rcast->is_x)
color = 0x00EE00EE;

View File

@@ -18,9 +18,9 @@ void *mb_alloc(size_t size)
lst = mb_get_lst();
tmp = ft_memalloc(size);
if (!tmp)
mb_exit(B_RED"failed create new allocation"RESET"\n");
mb_exit(B_RED"failed create new allocation"RESET"\n", EXIT_FAILURE);
if (!ft_lstpush_back(lst, ft_lstcreate(tmp)))
mb_exit(B_RED"failed add new element to list"RESET"\n");
mb_exit(B_RED"failed add new element to list"RESET"\n", EXIT_FAILURE);
return (tmp);
}
@@ -30,7 +30,7 @@ void mb_add(void *addr)
lst = mb_get_lst();
if (!ft_lstpush_back(lst, ft_lstcreate(addr)))
mb_exit(B_RED"failed add new element to list"RESET"\n");
mb_exit(B_RED"failed add new element to list"RESET"\n", EXIT_FAILURE);
}
void mb_free(void *addr)
@@ -45,7 +45,7 @@ void mb_free(void *addr)
ft_lsterase(tmp, free);
}
void mb_exit(char *str)
void mb_exit(char *str, int status)
{
t_list **lst;
@@ -53,5 +53,5 @@ void mb_exit(char *str)
mb_exec_exit_func();
ft_putstr_fd(str, 2);
ft_lstfree((*lst), free);
exit(0);
exit(status);
}

19
srcs/mem/memorybook_2d.c Normal file
View File

@@ -0,0 +1,19 @@
#include "cube3d.h"
void mb_add_2d(void **addr, int nb)
{
while (nb)
{
mb_add(addr[nb]);
nb--;
}
}
void mb_free_2d(void **addr, int nb)
{
while (nb)
{
mb_free(addr[nb]);
nb--;
}
}

View File

@@ -6,13 +6,13 @@
/* By: pblagoje <pblagoje@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/15 16:11:24 by pblagoje #+# #+# */
/* Updated: 2022/04/23 18:40:13 by pblagoje ### ########.fr */
/* Updated: 2022/05/01 22:53:25 by simplonco ### ########.fr */
/* */
/* ************************************************************************** */
#include "cube3d.h"
int check_colors(char **str)
static int check_colors(char **str)
{
int i;
int num;
@@ -29,7 +29,7 @@ int check_colors(char **str)
return (EXIT_SUCCESS);
}
int ft_strlen_2d(char **str)
static int ft_strlen_2d(char **str)
{
int i;
@@ -39,28 +39,16 @@ int ft_strlen_2d(char **str)
return (i);
}
void set_floor_rgb(t_txt *txt, char **rgb)
static void set_rgb(int *plan, char **rgb)
{
int i;
int r;
int g;
int b;
i = 0;
while (i < 3)
{
txt->rgb_floor[i] = ft_atoi(rgb[i]);
i++;
}
}
void set_ceiling_rgb(t_txt *txt, char **rgb)
{
int i;
i = 0;
while (i < 3)
{
txt->rgb_ceiling[i] = ft_atoi(rgb[i]);
i++;
}
r = ft_atoi(rgb[0]);
g = ft_atoi(rgb[1]);
b = ft_atoi(rgb[2]);
*plan = 0 << 24 | r << 16 | g << 8 | b;
}
void ft_free_2d(char **str)
@@ -76,28 +64,19 @@ void ft_free_2d(char **str)
free(str);
}
int check_rgb(t_txt *txt, char *element, char identifier)
int check_rgb(t_txt *txt, char *elem, char identifier)
{
char **rgb;
rgb = ft_split(element, ',');
if (!rgb || ft_strlen_2d(rgb) != 3 || \
element[ft_strlen(element) - 1] == ',')
{
ft_free_2d(rgb);
ft_putstr_fd("Error\nInvalid RGB code.\n", 2);
return (EXIT_FAILURE);
}
rgb = ft_split(elem, ',');
mb_add_2d((void**)rgb, ft_strlen_2d(rgb));
if (!rgb || ft_strlen_2d(rgb) != 3 || elem[ft_strlen(elem) - 1] == ',')
mb_exit("Error\nInvalid RGB code.\n", EXIT_FAILURE);
if ((identifier != 'F' && identifier != 'C') || check_colors(rgb))
{
ft_free_2d(rgb);
ft_putstr_fd("Error\nInvalid RGB code.\n", 2);
return (EXIT_FAILURE);
}
mb_exit("Error\nInvalid RGB code.\n", EXIT_FAILURE);
if (identifier == 'F')
set_floor_rgb(txt, rgb);
set_rgb(&txt->rgb_floor, rgb);
else
set_ceiling_rgb(txt, rgb);
ft_free_2d(rgb);
set_rgb(&txt->rgb_ceiling, rgb);
return (EXIT_SUCCESS);
}