v1.0 - normed texture system, multiple keypress, fixes on parsing, removed trailing maps
This commit is contained in:
parent
e581c72b02
commit
3f43074d05
35 changed files with 474 additions and 438 deletions
|
|
@ -6,63 +6,66 @@
|
|||
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/12/01 17:47:37 by bgoulard #+# #+# */
|
||||
/* Updated: 2024/12/01 17:52:28 by bgoulard ### ########.fr */
|
||||
/* Updated: 2024/12/16 09:36:28 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_addons.h"
|
||||
#include "cub3d_struct.h"
|
||||
#include "cub3d_parsing.h"
|
||||
#include "ft_string.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// col [ 3 ] is alpha channel (not used in the project)
|
||||
bool color_from_str(const char *str, t_color *color)
|
||||
{
|
||||
int col[4];
|
||||
char **split;
|
||||
int col[4];
|
||||
const char *arr[3];
|
||||
|
||||
split = ft_split(str, ',');
|
||||
if (split == NULL)
|
||||
if (ft_strcnb(str, ',') != 2)
|
||||
return (false);
|
||||
col[0] = ft_atoi(split[0]);
|
||||
col[1] = ft_atoi(split[1]);
|
||||
col[2] = ft_atoi(split[2]);
|
||||
if (split[3])
|
||||
col[3] = ft_atoi(split[3]);
|
||||
else
|
||||
col[3] = 255;
|
||||
if (col[0] < 0 || col[0] > 255 || col[1] < 0 || col[1] > 255 || col[2] < 0
|
||||
|| col[2] > 255 || col[3] < 0 || col[3] > 255)
|
||||
arr[0] = ft_strtok((char *)str, ",");
|
||||
arr[1] = ft_strtok(NULL, ",");
|
||||
arr[2] = ft_strtok(NULL, ",");
|
||||
if (!arr[0] || !arr[1] || !arr[2] || !ft_str_isnum(arr[0]) || \
|
||||
!ft_str_isnum(arr[1]) || !ft_str_isnum(arr[2]))
|
||||
return (false);
|
||||
col[0] = ft_atoi(arr[0]);
|
||||
col[1] = ft_atoi(arr[1]);
|
||||
col[2] = ft_atoi(arr[2]);
|
||||
col[3] = 255;
|
||||
if (ft_inrange(col[0], 0, 255) == false || \
|
||||
ft_inrange(col[1], 0, 255) == false || \
|
||||
ft_inrange(col[2], 0, 255) == false)
|
||||
return (false);
|
||||
*color = (t_color){.r = col[0], .g = col[1], .b = col[2], .a = col[3]};
|
||||
ft_free_2d((void **)split);
|
||||
return (true);
|
||||
}
|
||||
|
||||
bool load_bg(t_info *info, const char *line, const char **id_str)
|
||||
{
|
||||
t_color color;
|
||||
size_t i;
|
||||
size_t id_idx;
|
||||
|
||||
i = 0;
|
||||
id_idx = 0;
|
||||
ft_bzero(&color, sizeof(t_color));
|
||||
while (id_str[i])
|
||||
{
|
||||
if (ft_strstart_with(line, id_str[i]) && (color_from_str(line
|
||||
+ ft_strlen(id_str[i]), &color) == false || color.a < 255))
|
||||
return (info->last_error = ERROR_PARSE, false);
|
||||
if (ft_strstart_with(id_str[i], "F "))
|
||||
info->map.bg_colors[BG_FLR] = color;
|
||||
else
|
||||
info->map.bg_colors[BG_CLG] = color;
|
||||
i++;
|
||||
}
|
||||
if (color.color == 0)
|
||||
return (info->last_error = ERROR_PARSE, false);
|
||||
while (id_str[id_idx] && ft_strstart_with(line, id_str[id_idx]) == false)
|
||||
id_idx++;
|
||||
if (color_from_str(line + ft_strlen(id_str[id_idx]), &color) == false)
|
||||
return (info->last_error = ERROR_PARSE_BG_COLOR_FORMAT, false);
|
||||
if (id_idx == BG_FLR && info->map.bg_colors[BG_FLR].color == 0)
|
||||
info->map.bg_colors[BG_FLR] = color;
|
||||
else if (id_idx == BG_SKY && info->map.bg_colors[BG_SKY].color == 0)
|
||||
info->map.bg_colors[BG_SKY] = color;
|
||||
else
|
||||
return (info->last_error = ERROR_PARSE_ALREADY_SET, false);
|
||||
return (true);
|
||||
}
|
||||
|
||||
void *load_bgs(void *data)
|
||||
{
|
||||
const char *id_str[] = {"F ", "C ", NULL};
|
||||
const char *id_str[] = {[BG_FLR] = "F ", [BG_SKY] = "C ", [2] = NULL};
|
||||
t_info *info;
|
||||
size_t i;
|
||||
|
||||
|
|
@ -70,8 +73,8 @@ void *load_bgs(void *data)
|
|||
info = (t_info *)data;
|
||||
while (info->map.fraw[i])
|
||||
{
|
||||
if (is_identifier(info->map.fraw[i], id_str) && load_bg(info,
|
||||
info->map.fraw[i], id_str) == false)
|
||||
if (is_identifier(info->map.fraw[i], id_str) && \
|
||||
load_bg(info, info->map.fraw[i], id_str) == false)
|
||||
return (NULL);
|
||||
i++;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/12/01 17:46:52 by bgoulard #+# #+# */
|
||||
/* Updated: 2024/12/01 17:53:44 by bgoulard ### ########.fr */
|
||||
/* Updated: 2024/12/16 14:25:52 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,6 +17,13 @@
|
|||
|
||||
#include "mlx_functions.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static void sv_errno(t_info *inf, int err_code)
|
||||
{
|
||||
inf->errno_state = errno;
|
||||
inf->last_error = err_code;
|
||||
}
|
||||
|
||||
bool load_texture(t_info *info, const char *str, const char **id_str)
|
||||
{
|
||||
|
|
@ -28,17 +35,17 @@ bool load_texture(t_info *info, const char *str, const char **id_str)
|
|||
{
|
||||
if (ft_strstart_with(str, id_str[i]))
|
||||
{
|
||||
texture.path = ft_strtrim(str + ft_strlen(id_str[i]), " ");
|
||||
if (info->map.texture_[i].img != NULL)
|
||||
return (sv_errno(info, ERROR_PARSE_ALREADY_SET), false);
|
||||
texture.path = ft_strtrim(str + ft_strlen(id_str[i]), " \t");
|
||||
if (texture.path == NULL)
|
||||
return (info->errno_state = errno,
|
||||
info->last_error = ERROR_MALLOC, false);
|
||||
return (sv_errno(info, ERROR_MALLOC), false);
|
||||
if (ft_strend_with(texture.path, ".xpm") == false)
|
||||
return (info->last_error = ERROR_TEXTURE_FORMAT, false);
|
||||
return (sv_errno(info, ERROR_TEXTURE_FORMAT), false);
|
||||
texture.img = mlx_xpm_file_to_image(info->mlx_ptr, texture.path,
|
||||
&texture.width, &texture.height);
|
||||
if (texture.img == NULL)
|
||||
return (info->errno_state = errno, info->last_error = ERROR_MLX,
|
||||
false);
|
||||
return (sv_errno(info, ERROR_MLX), false);
|
||||
return (info->map.texture_[i] = texture, true);
|
||||
}
|
||||
i++;
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/12/01 17:47:15 by bgoulard #+# #+# */
|
||||
/* Updated: 2024/12/01 17:53:53 by bgoulard ### ########.fr */
|
||||
/* Updated: 2024/12/16 09:33:53 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -17,6 +17,8 @@
|
|||
#include "ft_vector.h"
|
||||
#include "ft_math.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
void *ft_strchrs(const char *str, const char *chrs)
|
||||
{
|
||||
while (*str)
|
||||
|
|
@ -68,16 +70,28 @@ t_vector *load_vector(t_map *map)
|
|||
int set_player(t_info *info, int i, t_vector *str_map)
|
||||
{
|
||||
t_dpoint pos;
|
||||
char *str;
|
||||
|
||||
ft_bzero(&pos, sizeof(t_dpoint));
|
||||
if (info->player.pos.x != 0 || info->player.pos.y != 0 || i == 0)
|
||||
return (ft_vec_destroy(&str_map), info->last_error = ERROR_PARSE, \
|
||||
EXIT_FAILURE);
|
||||
str = ft_strchrs(ft_vec_at(str_map, i), "SNWE");
|
||||
pos.y = i + .5;
|
||||
pos.x = ft_strchrs(ft_vec_at(str_map, i), "SNWE")
|
||||
- ft_vec_at(str_map, i) + .5;
|
||||
pos.x = str - (char *)ft_vec_at(str_map, i) + .5;
|
||||
info->player.dir = (t_dpoint){.x = 0, 0};
|
||||
info->player.pos = pos;
|
||||
info->player.pos_i = (t_ipoint){.x = (int)pos.x, .y = (int)pos.y};
|
||||
if (*str == 'N')
|
||||
info->player.dir.y = -1;
|
||||
else if (*str == 'S')
|
||||
info->player.dir.y = 1;
|
||||
else if (*str == 'W')
|
||||
info->player.dir.x = -1;
|
||||
else if (*str == 'E')
|
||||
info->player.dir.x = 1;
|
||||
info->player.plane = (t_dpoint){.x = info->player.dir.y, \
|
||||
.y = -info->player.dir.x};
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/12 08:31:06 by bgoulard #+# #+# */
|
||||
/* Updated: 2024/12/01 17:51:13 by bgoulard ### ########.fr */
|
||||
/* Updated: 2024/12/16 08:49:53 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -37,8 +37,8 @@ void parse_map(t_info *info)
|
|||
|
||||
opt = (t_optional){.val = info, .pres = OPT_SOME};
|
||||
info->map.path = info->cli_ctx.file;
|
||||
if (ft_optional_chain(&opt, function_list) == false)
|
||||
return (c3_perror(info), (void)0);
|
||||
info->player.plane = (t_dpoint){.x = 0, .y = 2 * atan(deg2rad(FOV / 2))};
|
||||
info->player.dir = (t_dpoint){.x = -1, .y = 0};
|
||||
if (ft_optional_chain(&opt, function_list) == false)
|
||||
return (c3_perror(info), (void)0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/12/01 17:49:12 by bgoulard #+# #+# */
|
||||
/* Updated: 2024/12/01 17:54:03 by bgoulard ### ########.fr */
|
||||
/* Updated: 2024/12/16 09:45:25 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -49,6 +49,6 @@ void *traverse_map(void *data)
|
|||
info = (t_info *)data;
|
||||
pos_start = (t_ipoint){.x = info->player.pos.x, .y = info->player.pos.y};
|
||||
if (flood_fill(info->map.map, pos_start, info->map.size) == false)
|
||||
return (info->last_error = ERROR_PARSE, NULL);
|
||||
return (info->last_error = ERROR_MAP_OPEN, NULL);
|
||||
return (info);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/12/01 17:43:17 by bgoulard #+# #+# */
|
||||
/* Updated: 2024/12/01 17:45:58 by bgoulard ### ########.fr */
|
||||
/* Updated: 2024/12/16 09:39:50 by bgoulard ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -21,9 +21,7 @@ t_tile *c3_get_cell(t_tile *map, t_ipoint dimensions, t_ipoint pos)
|
|||
{
|
||||
if (pos.x < 0 || pos.y < 0 || pos.x >= dimensions.x
|
||||
|| pos.y >= dimensions.y)
|
||||
return (printf("runtime error: %s:%d (pos:%d,%d on dims:%d,%d)\n",
|
||||
__func__, __LINE__, pos.x, pos.y, dimensions.x, dimensions.y),
|
||||
NULL);
|
||||
return (NULL);
|
||||
return (map + (pos.y * dimensions.x + pos.x));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue