diff options
Diffstat (limited to 'src')
49 files changed, 1673 insertions, 533 deletions
diff --git a/src/ft_basic_keys.c b/src/ft_basic_keys.c index ff74262..29044d9 100644 --- a/src/ft_basic_keys.c +++ b/src/ft_basic_keys.c @@ -19,7 +19,7 @@ int t_player *pl; const float move_speed = FT_MOVE_SPEED; - pl = clist->plist; + pl = &clist->plist; pl->pos_y += move_speed * pl->dir_x; pl->pos_x += move_speed * pl->dir_y; return (0); @@ -31,7 +31,7 @@ int t_player *pl; const float move_speed = FT_STRAFE_SPEED; - pl = clist->plist; + pl = &clist->plist; pl->pos_y += move_speed * -pl->dir_y; pl->pos_x += move_speed * pl->dir_x; return (0); @@ -43,7 +43,7 @@ int t_player *pl; const float move_speed = FT_MOVE_SPEED; - pl = clist->plist; + pl = &clist->plist; pl->pos_y += move_speed * -pl->dir_x; pl->pos_x += move_speed * -pl->dir_y; return (0); @@ -55,7 +55,7 @@ int t_player *pl; const float move_speed = FT_STRAFE_SPEED; - pl = clist->plist; + pl = &clist->plist; pl->pos_y += move_speed * pl->dir_y; pl->pos_x += move_speed * -pl->dir_x; return (0); diff --git a/src/ft_check_map_line.c b/src/ft_check_map_line.c index 1009aa3..7fa68b7 100644 --- a/src/ft_check_map_line.c +++ b/src/ft_check_map_line.c @@ -18,18 +18,23 @@ static int8_t ft_first_checks(char *line, size_t i, t_cub *clist) { - if (!ft_ischarset("012NSEW ", line[i])) + if (!ft_ischarset(FT_CHRST_MAP_ENTRY, line[i])) { - ft_strlcpy(clist->errmsg, FT_ERR_ILL_MAP, - ft_strlen(FT_ERR_ILL_MAP) + 1); + ft_sprintf(clist->errmsg, FT_ERR_ILL_MAP); return (-1); } - if (ft_ischarset("NSEW", line[i])) - clist->mlist->isspawn += 1; - if (clist->mlist->isspawn > 1) + if (ft_ischarset(FT_CHRST_SPAWN, line[i])) + clist->mlist.isspawn += 1; + if (clist->mlist.isspawn > 1) { - ft_strlcpy(clist->errmsg, FT_ERR_MULT_SPAWN, - ft_strlen(FT_ERR_MULT_SPAWN) + 1); + ft_sprintf(clist->errmsg, FT_ERR_MULT_SPAWN); + return (-1); + } + if (line[i] == 'L') + clist->mlist.isnlvl += 1; + if (clist->mlist.isnlvl > 1) + { + ft_sprintf(clist->errmsg, FT_ERR_MULT_NLVL); return (-1); } return (0); @@ -40,8 +45,7 @@ static int8_t { if (!ft_ischarset("1 ", line[i])) { - ft_strlcpy(clist->errmsg, FT_ERR_ILL_MAP, - ft_strlen(FT_ERR_ILL_MAP) + 1); + ft_sprintf(clist->errmsg, FT_ERR_MAP_WALLS); return (-1); } return (0); @@ -67,10 +71,9 @@ size_t static int8_t ft_check_side_walls(char *line, size_t i, t_cub *clist) { - if (line[0] != '1' || line[i - 1] != '1') + if ((line[0] != ' ' && line[0] != '1') || line[i - 1] != '1') { - ft_strlcpy(clist->errmsg, FT_ERR_ILL_ENTRY, - ft_strlen(FT_ERR_ILL_ENTRY) + 1); + ft_sprintf(clist->errmsg, FT_ERR_MAP_WALLS); return (-1); } return (0); @@ -97,11 +100,5 @@ int8_t } if (ft_check_side_walls(line, i, clist) < 0) return (-1); - if (ft_get_line_len(line) != clist->mlist->map_w) - { - ft_strlcpy(clist->errmsg, FT_ERR_MAP_LEN, - ft_strlen(FT_ERR_MAP_LEN) + 1); - return (-1); - } return (0); } diff --git a/src/ft_check_map_surrounds.c b/src/ft_check_map_surrounds.c new file mode 100644 index 0000000..8122cc3 --- /dev/null +++ b/src/ft_check_map_surrounds.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_check_map_surrounds.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:28:34 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:28:37 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> +#include <stddef.h> +#include <stdint.h> + +static int8_t + ft_wall_check(size_t y, size_t x, char **map) +{ + if (map[y + 1][x] == ' ' || + map[y - 1][x] == ' ' || + map[y][x + 1] == ' ' || + map[y][x - 1] == ' ' || + map[y + 1][x] == '\0' || + map[y - 1][x] == '\0' || + map[y][x + 1] == '\0' || + map[y][x - 1] == '\0') + return (-1); + return (0); +} + +void + ft_check_map_surrounds(t_map *ml, t_cub *cl) +{ + size_t y; + size_t x; + + y = 0; + x = 0; + while (ml->map[y]) + { + while (ml->map[y][x]) + { + if (ft_ischarset(FT_CHRST_MAP_NON_WALL, ml->map[y][x])) + { + if (ft_wall_check(y, x, ml->map) < 0) + ft_map_error(FT_ERR_MAP_WALLS, cl); + } + x++; + } + x = 0; + y++; + } +} diff --git a/src/ft_check_missing.c b/src/ft_check_missing.c index 7353a0e..80edf83 100644 --- a/src/ft_check_missing.c +++ b/src/ft_check_missing.c @@ -23,28 +23,44 @@ int return (ft_exit(1, clist)); } +static int + ft_check_missing_norme(t_cub *clist) +{ + if (!clist->mlist.isftex) + { + if (clist->f_rgb.r == -1 || clist->f_rgb.g == -1 + || clist->f_rgb.b == -1) + return (ft_missing_error(FT_ERR_MISS_FLOOR_C, clist)); + } + if (!clist->mlist.isctex) + { + if (clist->c_rgb.r == -1 || clist->c_rgb.g == -1 + || clist->c_rgb.b == -1) + return (ft_missing_error(FT_ERR_MISS_CEIL_C, clist)); + } + if (clist->plist.pos_x == 0 || clist->plist.pos_y == 0) + return (ft_missing_error(FT_ERR_MISS_PLAYER_SPAWN, clist)); + else if (clist->mlist.isnlvl && !clist->mlist.nl_tex_path[0]) + return (ft_missing_error(FT_ERR_MISS_NLVL, clist)); + else if (clist->mlist.isnlvl && !clist->mlist.nlevel_path[0]) + return (ft_missing_error(FT_ERR_MISS_NLVL_PATH, clist)); + return (0); +} + int ft_check_missing(t_cub *clist) { - if (!clist->mlist->no_tex_path[0]) + if (!clist->mlist.no_tex_path[0]) return (ft_missing_error(FT_ERR_MISS_NORTH, clist)); - else if (!clist->mlist->so_tex_path[0]) + else if (!clist->mlist.so_tex_path[0]) return (ft_missing_error(FT_ERR_MISS_SOUTH, clist)); - else if (!clist->mlist->ea_tex_path[0]) + else if (!clist->mlist.ea_tex_path[0]) return (ft_missing_error(FT_ERR_MISS_EAST, clist)); - else if (!clist->mlist->we_tex_path[0]) + else if (!clist->mlist.we_tex_path[0]) return (ft_missing_error(FT_ERR_MISS_WEST, clist)); - else if (!clist->mlist->sprite_path[0]) + else if (!clist->mlist.sprite_path[0]) return (ft_missing_error(FT_ERR_MISS_SPRITE, clist)); - else if (clist->wlist->x_size == 0 || clist->wlist->y_size == 0) + else if (clist->wlist.x_size == 0 || clist->wlist.y_size == 0) return (ft_missing_error(FT_ERR_MISS_RESOLUTION, clist)); - else if (clist->f_rgb.r == -1 || clist->f_rgb.g == -1 - || clist->f_rgb.b == -1) - return (ft_missing_error(FT_ERR_MISS_FLOOR_C, clist)); - else if (clist->c_rgb.r == -1 || clist->c_rgb.g == -1 - || clist->c_rgb.b == -1) - return (ft_missing_error(FT_ERR_MISS_CEIL_C, clist)); - else if (clist->plist->pos_x == 0 || clist->plist->pos_y == 0) - return (ft_missing_error(FT_ERR_MISS_PLAYER_SPAWN, clist)); - return (0); + return (ft_check_missing_norme(clist)); } diff --git a/src/ft_darken_rgb.c b/src/ft_darken_rgb.c new file mode 100644 index 0000000..223a540 --- /dev/null +++ b/src/ft_darken_rgb.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_darken_rgb.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/03/05 16:04:28 by rbousset #+# #+# */ +/* Updated: 2020/03/05 16:04:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <cub3d.h> +#include <stdint.h> + +uint32_t + ft_darken(t_rgb rgb, t_cub *cl) +{ + t_rgb darker; + float calc; + float dist; + + if ((dist = cl->rlist.row_dist) <= 0) + dist = 0.0001; + calc = (dist * 0.1 * cl->mlist.darklvl); + calc = (calc >= 255) ? (255) : (calc); + calc = (calc < 1) ? (1) : (calc); + darker = rgb; + darker.r /= calc; + darker.g /= calc; + darker.b /= calc; + return (ft_rgb_to_hex(darker)); +} diff --git a/src/ft_detect.c b/src/ft_detect.c index 10d7147..7ec0473 100644 --- a/src/ft_detect.c +++ b/src/ft_detect.c @@ -22,13 +22,13 @@ static void cl->rlist.y_ray_dir)); if (cl->rlist.y_ray_dir < 0) { - cl->mlist->y_step = -1; + cl->mlist.y_step = -1; cl->rlist.y_side_dist = (cl->rlist.y_ray_pos - cl->rlist.sqy) * cl->rlist.y_delta_dist; } else { - cl->mlist->y_step = 1; + cl->mlist.y_step = 1; cl->rlist.y_side_dist = (cl->rlist.sqy + 1.0 - cl->rlist.y_ray_pos) * cl->rlist.y_delta_dist; } @@ -42,13 +42,13 @@ static void cl->rlist.x_ray_dir)); if (cl->rlist.x_ray_dir < 0) { - cl->mlist->x_step = -1; + cl->mlist.x_step = -1; cl->rlist.x_side_dist = (cl->rlist.x_ray_pos - cl->rlist.sqx) * cl->rlist.x_delta_dist; } else { - cl->mlist->x_step = 1; + cl->mlist.x_step = 1; cl->rlist.x_side_dist = (cl->rlist.sqx + 1.0 - cl->rlist.x_ray_pos) * cl->rlist.x_delta_dist; } @@ -65,16 +65,17 @@ void if (cl->rlist.x_side_dist < cl->rlist.y_side_dist) { cl->rlist.x_side_dist += cl->rlist.x_delta_dist; - cl->rlist.sqx += cl->mlist->x_step; + cl->rlist.sqx += cl->mlist.x_step; cl->rlist.side = 0; } else { cl->rlist.y_side_dist += cl->rlist.y_delta_dist; - cl->rlist.sqy += cl->mlist->y_step; + cl->rlist.sqy += cl->mlist.y_step; cl->rlist.side = 1; } - if (cl->mlist->map[cl->rlist.sqx][cl->rlist.sqy] == '1') + if (ft_ischarset(FT_CHRST_DETECT, + cl->mlist.map[cl->rlist.sqx][cl->rlist.sqy])) cl->rlist.hit = 1; } } diff --git a/src/ft_draw_circle.c b/src/ft_draw_circle.c index 570543a..249b2e9 100644 --- a/src/ft_draw_circle.c +++ b/src/ft_draw_circle.c @@ -25,7 +25,7 @@ void float y1; i = 0; - scale = cl->mlist->scale / 2.5; + scale = cl->mlist.scale / 2.5; while (scale > 0) { while (i < 360) diff --git a/src/ft_draw_hud.c b/src/ft_draw_hud.c index 9cb5b1c..4ae497a 100644 --- a/src/ft_draw_hud.c +++ b/src/ft_draw_hud.c @@ -15,48 +15,19 @@ #include <stdint.h> static void - ft_draw_hud_back(t_win *wl, t_cub *cl) + ft_draw_minimap_back(size_t map_h, size_t map_w, t_win *wl, t_cub *cl) { - const uint16_t scl = cl->mlist->scale; + const uint16_t scl = cl->mlist.scale; uint32_t x; uint32_t y; int32_t col; - col = 0x00404040; - x = 0; - y = wl->y_size - ((cl->mlist->map_h * scl)); - while (x < wl->x_size) - { - while (y < wl->y_size) - { - *(int*)(cl->img.ptr + (x * 4 + (y * cl->img.sizeline))) = col; - if (!(y % 3)) - { - if (col < 0x00909090) - col += 0x00010101; - } - y++; - } - col = 0x00404040; - y = wl->y_size - ((cl->mlist->map_h * scl)); - x++; - } -} - -static void - ft_draw_hud_back_top_l(size_t map_h, size_t map_w, t_win *wl, t_cub *cl) -{ - const uint16_t scl = cl->mlist->scale; - uint32_t x; - uint32_t y; - int32_t col; - - col = 0x00373737; + col = 0x00353535; x = 0; y = wl->y_size - (map_h * scl) - 20; while (x < (map_w * scl) + 20) { - while (y < wl->y_size - (map_h * scl)) + while (y < wl->y_size) { *(int*)(cl->img.ptr + (x * 4 + (y * cl->img.sizeline))) = col; if (!(y % 3)) @@ -73,41 +44,44 @@ static void } static void - ft_draw_hud_back_top_r(size_t map_h, size_t map_w, t_win *wl, t_cub *cl) + ft_draw_stage_back(t_cub *clist) { - const uint16_t scl = cl->mlist->scale; - uint32_t x; + float x; uint32_t y; int32_t col; + uint16_t x_dest; + const uint16_t scl = clist->mlist.scale; - col = 0x00373737; - x = wl->x_size - (map_w * scl) + 20; - y = wl->y_size - (map_h * scl) - 20; - while (x < wl->x_size) + col = 0x00353535; + x = 0; + y = clist->wlist.y_size - (clist->mlist.map_h * scl) - 45; + x_dest = 1.5 * clist->mlist.scale + 70; + while (x_dest > (clist->mlist.map_w * scl) + 20) + { + x_dest--; + } + while (x < x_dest) { - while (y < wl->y_size - (map_h * scl)) + while (y < clist->wlist.y_size - (clist->mlist.map_h * scl) - 20) { - *(int*)(cl->img.ptr + (x * 4 + (y * cl->img.sizeline))) = col; - if (!(y % 2)) - { - if (col < 0x00aaaaaa) - col += 0x00010101; - } + *(int*)(clist->img.ptr + + ((uint8_t)x * 4 + (y * clist->img.sizeline))) = col; y++; } - col = 0x00353535; - y = wl->y_size - (map_h * scl) - 20; - x++; + y = clist->wlist.y_size - (clist->mlist.map_h * scl) - 45; + x += 1.0; } } -void +int8_t ft_draw_hud(t_cub *clist) { - ft_draw_hud_back(clist->wlist, clist); - ft_draw_hud_back_top_l(clist->mlist->map_h, - clist->mlist->map_w, clist->wlist, clist); - ft_draw_hud_back_top_r(clist->mlist->map_h, - clist->mlist->map_w, clist->wlist, clist); - ft_draw_map(clist->mlist->map, clist); + ft_draw_minimap_back(clist->mlist.map_h, + clist->mlist.map_w, &clist->wlist, clist); + ft_draw_map(clist->mlist.map, clist); + if (clist->mlist.isnlvl) + { + ft_draw_stage_back(clist); + } + return (0); } diff --git a/src/ft_draw_map.c b/src/ft_draw_map.c index a334fc1..232891c 100644 --- a/src/ft_draw_map.c +++ b/src/ft_draw_map.c @@ -10,21 +10,22 @@ /* */ /* ************************************************************************** */ +#include <libft.h> #include <cub3d.h> #include <stdint.h> static uint16_t ft_y_offset(t_cub *clist) { - return (clist->wlist->y_size - - (clist->mlist->map_h * clist->mlist->scale) - + clist->mlist->scale - 1); + return (clist->wlist.y_size + - (clist->mlist.map_h * clist->mlist.scale) + + clist->mlist.scale - 1); } static void ft_draw_player(t_player *plist, t_cub *clist) { - const uint16_t scale = clist->mlist->scale; + const uint16_t scale = clist->mlist.scale; const float x = plist->pos_x; const float y = plist->pos_y; @@ -38,7 +39,7 @@ static void void ft_draw_map(char **map, t_cub *clist) { - const uint8_t scale = clist->mlist->scale; + const uint8_t scale = clist->mlist.scale; size_t x; size_t y; @@ -48,13 +49,13 @@ void { while (map[y][x]) { - if (map[y][x] == '1') + if (ft_ischarset("1D", map[y][x])) ft_draw_square(scale + 9 + (x * (scale)), ft_y_offset(clist) - 9 + (y * (scale)), 0x00ca5422, clist); else if (map[y][x] == '2') ft_draw_square(scale + 9 + (x * (scale)), ft_y_offset(clist) - 9 + (y * (scale)), 0x0033ccff, clist); - else + else if (ft_ischarset("0LNSEW", map[y][x])) ft_draw_square(scale + 9 + (x * (scale)), ft_y_offset(clist) - 9 + (y * (scale)), 0x006afa6a, clist); x++; @@ -62,5 +63,5 @@ void x = 0; y++; } - ft_draw_player(clist->plist, clist); + ft_draw_player(&clist->plist, clist); } diff --git a/src/ft_draw_scene.c b/src/ft_draw_scene.c index a1a4a20..68a72e9 100644 --- a/src/ft_draw_scene.c +++ b/src/ft_draw_scene.c @@ -15,18 +15,64 @@ #include <mlx.h> #include <stdint.h> +static int8_t + ft_put_stage(t_cub *clist) +{ + uint8_t len; + char *str; + float x; + uint32_t y; + + x = 0.5 * clist->mlist.scale; + y = clist->wlist.y_size - (clist->mlist.map_h * clist->mlist.scale) - 25; + len = 6 + ft_uintlen(clist->currlvl); + if (!(str = (char*)malloc((len + 1) * sizeof(char)))) + return (-1); + ft_sprintf(str, "Stage %hd", clist->currlvl); + mlx_string_put(clist->wlist.wlx, + clist->wlist.winptr, + (uint32_t)x, y, + 0x002288da, + str); + ft_memdel((void**)&str); + return (0); +} + void ft_draw_scene(t_cub *clist) { - clist->img.img = mlx_new_image(clist->wlist->wlx, - clist->wlist->x_size, clist->wlist->y_size); + clist->img.img = mlx_new_image(clist->wlist.wlx, + clist->wlist.x_size, clist->wlist.y_size); clist->img.ptr = mlx_get_data_addr(clist->img.img, &clist->img.bpp, &clist->img.sizeline, &clist->img.endian); ft_castray(clist); if (clist->ishud) + { ft_draw_hud(clist); - ft_calc_sprite(clist); - mlx_put_image_to_window(clist->wlist->wlx, - clist->wlist->winptr, clist->img.img, 0, 0); - mlx_destroy_image(clist->wlist->wlx, clist->img.img); + } + ft_calc_sprite(clist); + mlx_put_image_to_window(clist->wlist.wlx, + clist->wlist.winptr, clist->img.img, 0, 0); + if (clist->ishud && clist->mlist.isnlvl) + { + if (ft_put_stage(clist) < 0) + { + ft_error(FT_RET_ALLOC_ERR, FT_ERR_ALLOCATE, clist); + } + } + mlx_destroy_image(clist->wlist.wlx, clist->img.img); +} + +void + ft_draw_scene_bmp(t_cub *clist) +{ + clist->img.img = mlx_new_image(clist->wlist.wlx, + clist->wlist.x_size, clist->wlist.y_size); + clist->img.ptr = mlx_get_data_addr(clist->img.img, &clist->img.bpp, + &clist->img.sizeline, &clist->img.endian); + ft_castray(clist); + ft_calc_sprite(clist); + if (ft_save_to_bmp(clist) < 0) + ft_error(FT_RET_BMP_ERR, FT_ERR_WR_BMP, clist); + mlx_destroy_image(clist->wlist.wlx, clist->img.img); } diff --git a/src/ft_draw_sprite.c b/src/ft_draw_sprite.c index ffc2d50..1393d38 100644 --- a/src/ft_draw_sprite.c +++ b/src/ft_draw_sprite.c @@ -25,26 +25,26 @@ void int32_t it; it = 0; - while (it < cl->mlist->sprite_nbr) + while (it < cl->mlist.sprite_nbr) { - dist_tab[it] = ((cl->plist->pos_x - cl->sprites[it].s_pos_x) * - (cl->plist->pos_x - cl->sprites[it].s_pos_x) + - (cl->plist->pos_y - cl->sprites[it].s_pos_y) * - (cl->plist->pos_y - cl->sprites[it].s_pos_y)); - cl->mlist->sprite_order[it] = it; + dist_tab[it] = ((cl->plist.pos_x - cl->sprites[it].s_pos_x) * + (cl->plist.pos_x - cl->sprites[it].s_pos_x) + + (cl->plist.pos_y - cl->sprites[it].s_pos_y) * + (cl->plist.pos_y - cl->sprites[it].s_pos_y)); + cl->mlist.sprite_order[it] = it; it++; } it = 0; - while (it < cl->mlist->sprite_nbr) + while (it < cl->mlist.sprite_nbr) { if (dist_tab[it] > dist_tab[it + 1]) { tmp = dist_tab[it]; dist_tab[it] = dist_tab[it + 1]; dist_tab[it + 1] = tmp; - tmp = cl->mlist->sprite_order[it]; - cl->mlist->sprite_order[it] = cl->mlist->sprite_order[it + 1]; - cl->mlist->sprite_order[it + 1] = tmp; + tmp = cl->mlist.sprite_order[it]; + cl->mlist.sprite_order[it] = cl->mlist.sprite_order[it + 1]; + cl->mlist.sprite_order[it + 1] = tmp; it = 0; } } @@ -53,24 +53,24 @@ void void ft_sprite_height(t_cub *cl, t_sprite *sprite) { - sprite->spriteheight = abs((int)(cl->wlist->y_size / (sprite->transformy))); - sprite->drawstarty = -sprite->spriteheight / 2 + cl->wlist->y_size / 2; + sprite->spriteheight = abs((int)(cl->wlist.y_size / (sprite->transformy))); + sprite->drawstarty = -sprite->spriteheight / 2 + cl->wlist.y_size / 2; if(sprite->drawstarty < 0) sprite->drawstarty = 0; - sprite->drawendy = sprite->spriteheight / 2 + cl->wlist->y_size / 2; - if(sprite->drawendy >= (int)cl->wlist->y_size) - sprite->drawendy = cl->wlist->y_size - 1; + sprite->drawendy = sprite->spriteheight / 2 + cl->wlist.y_size / 2; + if(sprite->drawendy >= (int)cl->wlist.y_size) + sprite->drawendy = cl->wlist.y_size - 1; } void ft_sprite_width(t_cub *cl, t_sprite *sprite) { - sprite->spritewidth = abs((int)(cl->wlist->x_size / (sprite->transformy))); + sprite->spritewidth = abs((int)(cl->wlist.x_size / (sprite->transformy))); sprite->drawstartx = -sprite->spritewidth / 2 + sprite->spritescreenx; if(sprite->drawstartx < 0) sprite->drawstartx = 0; sprite->drawendx = sprite->spritewidth / 2 + sprite->spritescreenx; - if(sprite->drawendx >= (int)cl->wlist->x_size) sprite->drawendx = cl->wlist->x_size - 1; + if(sprite->drawendx >= (int)cl->wlist.x_size) sprite->drawendx = cl->wlist.x_size - 1; } void @@ -79,15 +79,15 @@ void t_sprite sprite; int i = 0; - while (i < cl->mlist->sprite_nbr) + while (i < cl->mlist.sprite_nbr) { sprite = cl->sprites[i]; - sprite.spritey = sprite.s_pos_x - (cl->plist->pos_x - 0.5); - sprite.spritex = sprite.s_pos_y - (cl->plist->pos_y - 0.5); - sprite.invdet = 1.0 / (cl->plist->plane_x * cl->plist->dir_y - cl->plist->dir_x * cl->plist->plane_y); - sprite.transformx = sprite.invdet * (cl->plist->dir_y * sprite.spritex- cl->plist->dir_x* sprite.spritey); - sprite.transformy = sprite.invdet * (-cl->plist->plane_y* sprite.spritex + cl->plist->plane_x * sprite.spritey); - sprite.spritescreenx = (int)(cl->wlist->x_size / 2) * (1 + sprite.transformx / sprite.transformy); + sprite.spritey = sprite.s_pos_x - (cl->plist.pos_x - 0.5); + sprite.spritex = sprite.s_pos_y - (cl->plist.pos_y - 0.5); + sprite.invdet = 1.0 / (cl->plist.plane_x * cl->plist.dir_y - cl->plist.dir_x * cl->plist.plane_y); + sprite.transformx = sprite.invdet * (cl->plist.dir_y * sprite.spritex- cl->plist.dir_x* sprite.spritey); + sprite.transformy = sprite.invdet * (-cl->plist.plane_y* sprite.spritex + cl->plist.plane_x * sprite.spritey); + sprite.spritescreenx = (int)(cl->wlist.x_size / 2) * (1 + sprite.transformx / sprite.transformy); ft_sprite_height(cl, &sprite); ft_sprite_width(cl, &sprite); ft_draw_sprite(cl, &sprite); @@ -108,7 +108,7 @@ void sprite->y = sprite->drawstarty; while (sprite->y < sprite->drawendy) { - d = sprite->y * 256 - cl->wlist->y_size * 128 + + d = sprite->y * 256 - cl->wlist.y_size * 128 + sprite->spriteheight * 128; sprite->tex_y = ((d * cl->tlist[4].img_h / 2) / sprite->spriteheight) / 128; diff --git a/src/ft_draw_square.c b/src/ft_draw_square.c index 355969e..4223f26 100644 --- a/src/ft_draw_square.c +++ b/src/ft_draw_square.c @@ -17,7 +17,7 @@ void ft_draw_square(int a, int b, int rgb, t_cub *clist) { - const uint16_t scale = clist->mlist->scale; + const uint16_t scale = clist->mlist.scale; int x; int y; diff --git a/src/ft_draw_textures.c b/src/ft_draw_textures.c index 598fd91..5fc3bdd 100644 --- a/src/ft_draw_textures.c +++ b/src/ft_draw_textures.c @@ -24,26 +24,42 @@ void ft_draw_texture(t_cub *cl, int x, int y, int tex_y) { + float dist; + float calc; + + if ((dist = cl->rlist.wall_dist) <= 0) + dist = 0.0001; + calc = (dist * 0.1 * cl->mlist.darklvl); + calc = (calc >= 255) ? (255) : (calc); + calc = (calc < 1) ? (1) : (calc); cl->img.ptr[x * 4 + (cl->img.sizeline * y)] = - (char)cl->tlist[cl->w_side].ptr[cl->tlist[cl->w_side].tex_x * 4 + 4 * - cl->tlist[cl->w_side].img_h * tex_y]; + (uint8_t)cl->tlist[cl->w_side].ptr[cl->tlist[cl->w_side].tex_x * 4 + 4 * + cl->tlist[cl->w_side].img_h * tex_y] / calc; cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 1] = - (char)cl->tlist[cl->w_side].ptr[cl->tlist[cl->w_side].tex_x * 4 + 4 * - cl->tlist[cl->w_side].img_h * tex_y + 1]; + (uint8_t)cl->tlist[cl->w_side].ptr[cl->tlist[cl->w_side].tex_x * 4 + 4 * + cl->tlist[cl->w_side].img_h * tex_y + 1] / calc; cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 2] = - (char)cl->tlist[cl->w_side].ptr[cl->tlist[cl->w_side].tex_x * 4 + 4 * - cl->tlist[cl->w_side].img_h * tex_y + 2]; - cl->img.ptr[x * 4 + cl->wlist->x_size * y + 3] = (char)0; + (uint8_t)cl->tlist[cl->w_side].ptr[cl->tlist[cl->w_side].tex_x * 4 + 4 * + cl->tlist[cl->w_side].img_h * tex_y + 2] / calc; + cl->img.ptr[x * 4 + cl->wlist.x_size * y + 3] = (char)0; } void ft_choose_tex(t_cub *clist) { - if (clist->rlist.side == 0 && clist->rlist.x_ray_dir < 0) - clist->w_side = 1; - else if (clist->rlist.side == 0 && clist->rlist.x_ray_dir > 0) - clist->w_side = 0; - else if (clist->rlist.side == 1 && clist->rlist.y_ray_dir > 0) - clist->w_side = 3; + if (clist->rlist.sqy == clist->mlist.nlx + && clist->rlist.sqx == clist->mlist.nly) + { + clist->w_side = 5; + } else - clist->w_side = 2; + { + if (clist->rlist.side == 0 && clist->rlist.x_ray_dir < 0) + clist->w_side = 1; + else if (clist->rlist.side == 0 && clist->rlist.x_ray_dir > 0) + clist->w_side = 0; + else if (clist->rlist.side == 1 && clist->rlist.y_ray_dir > 0) + clist->w_side = 3; + else + clist->w_side = 2; + } } diff --git a/src/ft_draw_verline.c b/src/ft_draw_verline.c index 99fff3c..16dc63c 100644 --- a/src/ft_draw_verline.c +++ b/src/ft_draw_verline.c @@ -12,31 +12,7 @@ #include <stdio.h> #include <cub3d.h> - -static void - ft_draw_floor(t_cub *cl, int32_t y, int32_t x) -{ - while ((uint32_t)y < cl->wlist->y_size) - { - *(int*)(cl->img.ptr + - (x * 4 + (y * cl->img.sizeline))) = ft_rgb_to_hex(cl->f_rgb); - y++; - } -} - -static void - ft_draw_ceil(t_cub *cl, int32_t y, int32_t x) -{ - int16_t i; - - i = 0; - while (i <= y) - { - *(int*)(cl->img.ptr + - (x * 4 + (i * cl->img.sizeline))) = ft_rgb_to_hex(cl->c_rgb); - i++; - } -} +#include <stdint.h> int8_t ft_draw_verline(t_cub *cl, int32_t x, int32_t y, int32_t y2) @@ -46,17 +22,15 @@ int8_t (y < 0) ? (y = 0) : 0; (y2 < 0) ? (y2 = 0) : 0; - ft_draw_ceil(cl, y, x); (cl->rlist.line_h <= 0) ? (cl->rlist.line_h = 1) : 0; while (y < y2) { - d = y * 256 - cl->wlist->y_size * 128 + cl->rlist.line_h * 128; + d = y * 256 - cl->wlist.y_size * 128 + cl->rlist.line_h * 128; d = (d <= 0) ? (-d) : (d); tex_y = ((d * cl->tlist[cl->w_side].img_h) / cl->rlist.line_h) / 256; (tex_y <= 0) ? (tex_y = 1) : 0; ft_draw_texture(cl, x, y, tex_y); y++; } - ft_draw_floor(cl, y, x); return (0); } diff --git a/src/ft_map_error.c b/src/ft_error.c index a36507a..87be01e 100644 --- a/src/ft_map_error.c +++ b/src/ft_error.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* ft_map_error.c :+: :+: :+: */ +/* ft_error.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -15,12 +15,22 @@ #include <unistd.h> int + ft_error(uint8_t retval, const char *errmsg, t_cub *clist) +{ + ft_dprintf(STDERR_FILENO, "Error\n"); + ft_dprintf(STDERR_FILENO, + "\033[1;31m%s\033[0m\n", + errmsg); + return (ft_exit(retval, clist)); +} + +int ft_map_error(const char *errmsg, t_cub *clist) { ft_dprintf(STDERR_FILENO, "Error\n"); ft_dprintf(STDERR_FILENO, "\033[1;31mMap error: line %lu: %s\033[0m\n", - clist->mlist->line_chk, + clist->mlist.line_chk, errmsg); - return (ft_exit(4, clist)); + return (ft_exit(FT_RET_MAP_ERR, clist)); } diff --git a/src/ft_exit.c b/src/ft_exit.c index b9e061f..659d0ec 100644 --- a/src/ft_exit.c +++ b/src/ft_exit.c @@ -15,38 +15,63 @@ #include <mlx.h> #include <stddef.h> #include <stdlib.h> -#include <inttypes.h> +#include <signal.h> +#include <sys/wait.h> +#include <stdint.h> static void ft_free_lists(t_cub *clist) { - ft_memdel((void**)&clist->mlist->no_tex_path); - ft_memdel((void**)&clist->mlist->so_tex_path); - ft_memdel((void**)&clist->mlist->ea_tex_path); - ft_memdel((void**)&clist->mlist->we_tex_path); - ft_memdel((void**)&clist->mlist->sprite_path); - ft_memdel((void**)&clist->mlist->mapl); - ft_free_words(clist->mlist->map); - ft_memdel((void**)&clist->mlist); - ft_memdel((void**)&clist->plist); - if (!clist->wlist->inited) - ft_memdel((void**)&clist->wlist->winptr); - ft_memdel((void**)&clist->wlist->wlx); - ft_memdel((void**)&clist->wlist); - ft_memdel((void**)&clist); + ft_memdel((void**)&clist->mlist.filename); + ft_memdel((void**)&clist->mlist.no_tex_path); + ft_memdel((void**)&clist->mlist.so_tex_path); + ft_memdel((void**)&clist->mlist.ea_tex_path); + ft_memdel((void**)&clist->mlist.we_tex_path); + ft_memdel((void**)&clist->mlist.sprite_path); + ft_memdel((void**)&clist->mlist.nl_tex_path); + ft_memdel((void**)&clist->mlist.fl_tex_path); + ft_memdel((void**)&clist->mlist.ce_tex_path); + ft_memdel((void**)&clist->mlist.nlevel_path); + ft_memdel((void**)&clist->mlist.music_path); + ft_memdel((void**)&clist->mlist.music_cmd); + ft_memdel((void**)&clist->mlist.mapl); + ft_free_words(clist->mlist.map); + ft_free_words(clist->mlist.mcmd_words); + if (!clist->wlist.inited) + ft_memdel((void**)&clist->wlist.winptr); +} + +static void + ft_del_tex(t_cub *clist) +{ + if (clist->tlist[0].img) + mlx_destroy_image(clist->wlist.wlx, clist->tlist[0].img); + if (clist->tlist[1].img) + mlx_destroy_image(clist->wlist.wlx, clist->tlist[1].img); + if (clist->tlist[2].img) + mlx_destroy_image(clist->wlist.wlx, clist->tlist[2].img); + if (clist->tlist[3].img) + mlx_destroy_image(clist->wlist.wlx, clist->tlist[3].img); + if (clist->tlist[4].img) + mlx_destroy_image(clist->wlist.wlx, clist->tlist[4].img); + if (clist->mlist.isnlvl && clist->tlist[5].img) + mlx_destroy_image(clist->wlist.wlx, clist->tlist[5].img); } int ft_exit(uint8_t exit_code, t_cub *clist) { - if (clist->wlist->inited) + if (clist->walltexgood) + ft_del_tex(clist); + if (clist->wlist.inited) + { + mlx_destroy_window(clist->wlist.wlx, clist->wlist.winptr); + clist->wlist.winptr = NULL; + } + if (clist->isoldmus && clist->wlist.inited) { - mlx_destroy_image(clist->wlist->wlx, clist->tlist[0].img); - mlx_destroy_image(clist->wlist->wlx, clist->tlist[1].img); - mlx_destroy_image(clist->wlist->wlx, clist->tlist[2].img); - mlx_destroy_image(clist->wlist->wlx, clist->tlist[3].img); - mlx_destroy_image(clist->wlist->wlx, clist->tlist[4].img); - mlx_destroy_window(clist->wlist->wlx, clist->wlist->winptr); + kill(clist->mpid, SIGTERM); + wait(&clist->mpid); } ft_free_lists(clist); ft_printf("Exiting program\n"); diff --git a/src/ft_extra_keys.c b/src/ft_extra_keys.c index 507f808..e694667 100644 --- a/src/ft_extra_keys.c +++ b/src/ft_extra_keys.c @@ -22,7 +22,7 @@ int float sav_plane_x; const float rot_speed = FT_ROT_SPEED; - pl = clist->plist; + pl = &clist->plist; sav_dir_x = pl->dir_x; pl->dir_x = pl->dir_x * cos(rot_speed) - pl->dir_y * sin(rot_speed); pl->dir_y = sav_dir_x * sin(rot_speed) + pl->dir_y * cos(rot_speed); @@ -40,7 +40,7 @@ int float sav_plane_x; const float rot_speed = FT_ROT_SPEED; - pl = clist->plist; + pl = &clist->plist; sav_dir_x = pl->dir_x; pl->dir_x = pl->dir_x * cos(-rot_speed) - pl->dir_y * sin(-rot_speed); pl->dir_y = sav_dir_x * sin(-rot_speed) + pl->dir_y * cos(-rot_speed); diff --git a/src/ft_floor_cast.c b/src/ft_floor_cast.c new file mode 100644 index 0000000..6111eca --- /dev/null +++ b/src/ft_floor_cast.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_raycasting.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:22:57 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:23:42 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <cub3d.h> +#include <stdint.h> + +static void + ft_draw_plain_horizontal(t_rgb rgb, t_cub *cl, int32_t y, int32_t x) +{ + *(int*)(cl->img.ptr + + (x * 4 + (y * cl->img.sizeline))) = ft_darken(rgb, cl); +} + +static void + ft_draw_extra_tex(uint8_t tid, uint16_t y, uint16_t x, t_cub *cl) +{ + float dist; + float calc; + + if ((dist = cl->rlist.row_dist) <= 0) + dist = 0.0001; + calc = (dist * 0.1 * cl->mlist.darklvl); + calc = (calc >= 255) ? (255) : (calc); + calc = (calc < 1) ? (1) : (calc); + cl->img.ptr[x * 4 + (cl->img.sizeline * y)] = + (uint8_t)cl->tlist[tid].ptr[cl->tlist[tid].tex_x * 4 + 4 * + cl->tlist[tid].img_h * cl->tlist[tid].tex_y] / calc; + cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 1] = + (uint8_t)cl->tlist[tid].ptr[cl->tlist[tid].tex_x * 4 + 4 * + cl->tlist[tid].img_h * cl->tlist[tid].tex_y + 1] / calc; + cl->img.ptr[x * 4 + (cl->img.sizeline * y) + 2] = + (uint8_t)cl->tlist[tid].ptr[cl->tlist[tid].tex_x * 4 + 4 * + cl->tlist[tid].img_h * cl->tlist[tid].tex_y + 2] / calc; + cl->img.ptr[x * 4 + cl->wlist.x_size * y + 3] = (char)0; +} + +static void + ft_set_tex_xy(uint8_t tid, t_ray *rl, t_cub *cl) +{ + cl->tlist[tid].tex_y = (int32_t)(cl->tlist[tid].img_w + * (rl->x_floor - rl->x_cell)) % (cl->tlist[tid].img_w); + cl->tlist[tid].tex_x = (int32_t)(cl->tlist[tid].img_h + * (rl->y_floor - rl->y_cell)) % (cl->tlist[tid].img_h); + cl->tlist[tid].tex_x = (cl->tlist[tid].tex_x > 0) + ? (cl->tlist[tid].tex_x) : (-cl->tlist[tid].tex_x); + cl->tlist[tid].tex_y = (cl->tlist[tid].tex_y > 0) + ? (cl->tlist[tid].tex_y) : (-cl->tlist[tid].tex_y); +} + +static void + ft_floor_cast_loop(uint16_t y, uint16_t x, t_ray *rl, t_cub *cl) +{ + rl->x_cell = (int32_t)(rl->x_floor); + rl->y_cell = (int32_t)(rl->y_floor); + if (cl->mlist.isftex) + { + ft_set_tex_xy(6, rl, cl); + } + if (cl->mlist.isctex) + { + ft_set_tex_xy(7, rl, cl); + } + rl->x_floor += cl->mlist.x_floor_step; + rl->y_floor += cl->mlist.y_floor_step; + if (cl->mlist.isftex) + ft_draw_extra_tex(6, y, x, cl); + else + ft_draw_plain_horizontal(cl->f_rgb, cl, y, x); + if (cl->mlist.isctex) + ft_draw_extra_tex(7, cl->wlist.y_size - y - 1, x, cl); + else + ft_draw_plain_horizontal(cl->c_rgb, cl, cl->wlist.y_size - y - 1, x); +} + +void + ft_floor_cast(uint16_t y, t_cub *cl) +{ + t_ray *rl; + uint16_t x; + + rl = &cl->rlist; + rl->x_f_ray_dir = cl->plist.dir_x - cl->plist.plane_x; + rl->y_f_ray_dir = cl->plist.dir_y - cl->plist.plane_y; + rl->x_f_ray_dir_bis = cl->plist.dir_x + cl->plist.plane_x; + rl->y_f_ray_dir_bis = cl->plist.dir_y + cl->plist.plane_y; + rl->p = y - cl->wlist.y_size / 2; + cl->plist.pos_z = 0.5 * cl->wlist.y_size; + rl->row_dist = cl->plist.pos_z / rl->p; + cl->mlist.x_floor_step = rl->row_dist * + (rl->x_f_ray_dir_bis - rl->x_f_ray_dir) / cl->wlist.x_size; + cl->mlist.y_floor_step = rl->row_dist * + (rl->y_f_ray_dir_bis - rl->y_f_ray_dir) / cl->wlist.x_size; + rl->x_floor = cl->plist.pos_y + rl->row_dist * rl->x_f_ray_dir; + rl->y_floor = cl->plist.pos_x + rl->row_dist * rl->y_f_ray_dir; + x = 0; + while (++x < cl->wlist.x_size) + { + ft_floor_cast_loop(y, x, rl, cl); + } +} diff --git a/src/ft_get_colors.c b/src/ft_get_colors.c index f54fabc..0f79a7b 100644 --- a/src/ft_get_colors.c +++ b/src/ft_get_colors.c @@ -30,8 +30,7 @@ static int8_t if (i != ft_strlen(num[j])) { ft_free_words(num); - ft_strlcpy(clist->errmsg, FT_ERR_COLOR_ALPHA, - ft_strlen(FT_ERR_COLOR_ALPHA) + 1); + ft_sprintf(clist->errmsg, FT_ERR_COLOR_ALPHA); return (-1); } i = 0; @@ -46,8 +45,7 @@ static int8_t if (ft_atoi(num[0]) > 255 || ft_atoi(num[1]) > 255 || ft_atoi(num[2]) > 255) { - ft_strlcpy(clist->errmsg, FT_ERR_COLOR_MAX, - ft_strlen(FT_ERR_COLOR_MAX) + 1); + ft_sprintf(clist->errmsg, FT_ERR_COLOR_MAX); ft_free_words(num); return (-1); } @@ -59,8 +57,7 @@ static int8_t { if (!num[0] || !num[1] || !num[2] || num[3]) { - ft_strlcpy(clist->errmsg, FT_ERR_COLOR_ARGS, - ft_strlen(FT_ERR_COLOR_ARGS) + 1); + ft_sprintf(clist->errmsg, FT_ERR_COLOR_ARGS); ft_free_words(num); return (-1); } @@ -74,13 +71,14 @@ int8_t if (!(*words) || !words[1] || words[2]) { - ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); + ft_sprintf(clist->errmsg, FT_ERR_ARGS); return (-1); } + if (!ft_check_ext(words[1], ".xpm")) + return (ft_get_f_tex(words, clist)); if (!(num = ft_split(words[1], ','))) { - ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, - ft_strlen(FT_ERR_ALLOCATE) + 1); + ft_sprintf(clist->errmsg, FT_ERR_ALLOCATE); return (-1); } if (ft_check_nums_amount(num, clist) < 0) @@ -103,13 +101,14 @@ int8_t if (!(*words) || !words[1] || words[2]) { - ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); + ft_sprintf(clist->errmsg, FT_ERR_ARGS); return (-1); } + if (!ft_check_ext(words[1], ".xpm")) + return (ft_get_c_tex(words, clist)); if (!(num = ft_split(words[1], ','))) { - ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, - ft_strlen(FT_ERR_ALLOCATE) + 1); + ft_sprintf(clist->errmsg, FT_ERR_ALLOCATE); return (-1); } if (ft_check_nums_amount(num, clist) < 0) diff --git a/src/ft_get_darkness.c b/src/ft_get_darkness.c new file mode 100644 index 0000000..14a9f00 --- /dev/null +++ b/src/ft_get_darkness.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_get_darkness.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:28:53 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:28:53 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> + +static int8_t + ft_checkdigit(const char *word, t_cub *clist) +{ + size_t i; + + i = 0; + while (ft_isdigit(word[i])) + i++; + if (i != ft_strlen(word)) + { + ft_sprintf(clist->errmsg, FT_ERR_SH_ALPHA); + return (-1); + } + return (0); +} + +int8_t + ft_get_darkness(char **words, t_cub *clist) +{ + if (!(*words) || !words[1] || words[2]) + { + ft_sprintf(clist->errmsg, FT_ERR_ARGS); + return (-1); + } + if (ft_checkdigit(words[1], clist)) + return (-1); + if (ft_atoi(words[1]) < 0 || ft_atoi(words[1]) > 20) + { + ft_sprintf(clist->errmsg, FT_ERR_SH_RANGE); + return (-1); + } + clist->mlist.darklvl = ft_atoi(words[1]); + return (0); +} diff --git a/src/ft_get_map.c b/src/ft_get_map.c index cb677da..08c9734 100644 --- a/src/ft_get_map.c +++ b/src/ft_get_map.c @@ -12,20 +12,10 @@ #include <libft.h> #include <cub3d.h> +#include <stdlib.h> #include <stddef.h> #include <stdint.h> -static size_t - ft_get_map_h(char **map) -{ - size_t i; - - i = 0; - while (map[i]) - i++; - return (i); -} - static void ft_linecpy(char *line, char *mapl, size_t start) { @@ -38,8 +28,6 @@ static void slen = ft_strlen(line); while (i < slen && line[i]) { - while (line[i] == ' ') - i++; mapl[j] = line[i]; i++; j++; @@ -48,39 +36,39 @@ static void } static int8_t - ft_cat_mapl(char *line, size_t i, t_cub *clist) + ft_cat_mapl(char *line, t_cub *clist) { - if (!(clist->mlist->mapl = (char *)ft_nrealloc(clist->mlist->mapl, - ((clist->mlist->map_w + 1) * i) * sizeof(char), - ((clist->mlist->map_w + 1) * (i + 1)) * sizeof(char)))) + if (!line[0] || + !(clist->mlist.mapl = (char *)ft_nrealloc(clist->mlist.mapl, + clist->mlist.mapl_len * sizeof(char), + (clist->mlist.mapl_len + ft_strlen(line) + 1) * sizeof(char)))) { - ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, - ft_strlen(FT_ERR_ALLOCATE) + 1); + ft_sprintf(clist->errmsg, FT_ERR_ALLOCATE); return (-1); } - ft_linecpy(line, clist->mlist->mapl, (clist->mlist->map_w + 1) * i); + ft_linecpy(line, clist->mlist.mapl, + clist->mlist.mapl_len); + clist->mlist.mapl_len += ft_strlen(line) + 1; return (0); } int ft_get_map_first_line(char *line, t_cub *clist) { - clist->mlist->map_start = clist->mlist->line_chk; + clist->mlist.map_start = clist->mlist.line_chk; if (!line[0]) { ft_memdel((void**)&line); - ft_strlcpy(clist->errmsg, FT_ERR_READ, ft_strlen(FT_ERR_READ) + 1); + ft_sprintf(clist->errmsg, FT_ERR_READ); return (-1); } - clist->mlist->map_w = ft_get_line_len(line); if (ft_check_map_line(line, 1, clist) < 0) { ft_memdel((void**)&line); - ft_strlcpy(clist->errmsg, FT_ERR_READ, ft_strlen(FT_ERR_READ) + 1); return (-1); } - ft_memdel((void**)&clist->mlist->mapl); - if (ft_cat_mapl(line, 0, clist) < 0) + ft_memdel((void**)&clist->mlist.mapl); + if (ft_cat_mapl(line, clist) < 0) { ft_memdel((void**)&line); return (-1); @@ -93,28 +81,28 @@ int ft_get_map_core(int fd, t_cub *clist) { int ret; - size_t i; char *line; - i = 1; ret = 1; while ((ret = get_next_line(fd, &line)) > 0) { - clist->mlist->line_chk += 1; + clist->mlist.line_chk += 1; if (!line[0] || ft_check_map_line(line, 0, clist) < 0 - || ft_cat_mapl(line, i, clist) < 0) + || ft_cat_mapl(line, clist) < 0) { + if (!line[0]) + ft_sprintf(clist->errmsg, FT_ERR_MAP_EMPL); ft_memdel((void**)&line); return (-1); } ft_memdel((void**)&line); - i++; } ft_memdel((void**)&line); - clist->mlist->mapl[((clist->mlist->map_w + 1) * i) - 1] = '\0'; - ft_free_words(clist->mlist->map); - clist->mlist->map = ft_split(clist->mlist->mapl, '\n'); - clist->mlist->map_h = ft_get_map_h(clist->mlist->map); - ft_memdel((void**)&clist->mlist->mapl); + clist->mlist.mapl[clist->mlist.mapl_len - 1] = '\0'; + ft_free_words(clist->mlist.map); + clist->mlist.map = ft_split(clist->mlist.mapl, '\n'); + clist->mlist.map_h = ft_get_map_h(clist->mlist.map); + clist->mlist.map_w = ft_get_map_w(clist->mlist.map); + ft_memdel((void**)&clist->mlist.mapl); return (0); } diff --git a/src/ft_get_map_dims.c b/src/ft_get_map_dims.c new file mode 100644 index 0000000..281f2fb --- /dev/null +++ b/src/ft_get_map_dims.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_get_map_dims.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:28:47 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:28:47 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <stdint.h> + +size_t + ft_get_map_h(char **map) +{ + size_t i; + + i = 0; + while (map[i]) + i++; + return (i); +} + +size_t + ft_get_map_w(char **map) +{ + size_t i; + size_t big; + size_t tmp; + + i = 0; + big = 0; + tmp = 0; + while (map[i]) + { + tmp = ft_strlen(map[i]); + if (tmp > big) + big = tmp; + i++; + } + return (big); +} diff --git a/src/ft_get_music.c b/src/ft_get_music.c new file mode 100644 index 0000000..2383e0a --- /dev/null +++ b/src/ft_get_music.c @@ -0,0 +1,62 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_get_music.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/28 18:30:18 by rbousset #+# #+# */ +/* Updated: 2020/02/28 18:30:19 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> +#include <stdlib.h> +#include <stdint.h> + +static int8_t + ft_set_music_cmd(t_map *mlist) +{ + uint8_t len; + + len = ft_strlen(mlist->music_path); + len += ft_strlen(FT_MUS_CMD) - 2; + ft_memdel((void**)&mlist->music_cmd); + if (!(mlist->music_cmd = (char *)malloc((len + 1) * sizeof(char)))) + return (-1); + ft_sprintf(mlist->music_cmd, FT_MUS_CMD, + mlist->music_path); + ft_free_words(mlist->mcmd_words); + if (!(mlist->mcmd_words = ft_split(mlist->music_cmd, ' '))) + return (-1); + return (0); +} + +int8_t + ft_get_music(char **words, t_cub *clist) +{ + if (!(*words) || !(*(words + 1)) || (*(words + 2))) + { + ft_sprintf(clist->errmsg, FT_ERR_ARGS); + return (-1); + } + if (ft_check_ext(*(words + 1), ".wav") < 0) + { + ft_sprintf(clist->errmsg, FT_ERR_NOT_A_WAV); + return (-1); + } + ft_memdel((void**)&clist->mlist.music_path); + if (!(clist->mlist.music_path = ft_strdup(*(words + 1)))) + { + ft_sprintf(clist->errmsg, FT_ERR_ALLOCATE); + return (-1); + } + if (ft_check_not_found(clist->mlist.music_path) < 0) + { + ft_sprintf(clist->errmsg, FT_ERR_RD_MUSIC); + return (-1); + } + clist->mlist.ismusic = 1; + return ((ft_set_music_cmd(&clist->mlist) == 0) ? (0) : (-1)); +} diff --git a/src/ft_get_nlvl_pos.c b/src/ft_get_nlvl_pos.c new file mode 100644 index 0000000..db726e1 --- /dev/null +++ b/src/ft_get_nlvl_pos.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_get_nlvl_pos.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/28 20:56:25 by rbousset #+# #+# */ +/* Updated: 2020/02/28 20:56:26 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <cub3d.h> + +void + ft_get_nlvl_pos(t_map *ml) +{ + size_t x; + size_t y; + + x = 1; + y = 1; + while (ml->map[y]) + { + while (ml->map[y][x]) + { + if (ml->map[y][x] == 'L') + { + ml->nlx = x; + ml->nly = y; + return ; + } + x++; + } + x = 1; + y++; + } +} diff --git a/src/ft_get_path_nl.c b/src/ft_get_path_nl.c new file mode 100644 index 0000000..68de5c6 --- /dev/null +++ b/src/ft_get_path_nl.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_get_path_nl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/28 18:30:18 by rbousset #+# #+# */ +/* Updated: 2020/02/28 18:30:19 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> +#include <stdint.h> + +int8_t + ft_get_path_nl(char **words, t_cub *clist) +{ + if (!(*words) || !(*(words + 1)) || (*(words + 2))) + { + ft_sprintf(clist->errmsg, FT_ERR_ARGS); + return (-1); + } + if (ft_check_ext(*(words + 1), ".cub") < 0) + { + ft_sprintf(clist->errmsg, FT_ERR_NOT_A_CUB); + return (-1); + } + ft_memdel((void**)&clist->mlist.nlevel_path); + if (!(clist->mlist.nlevel_path = ft_strdup(*(words + 1)))) + { + ft_sprintf(clist->errmsg, FT_ERR_ALLOCATE); + return (-1); + } + if (ft_check_not_found(clist->mlist.nlevel_path) < 0) + { + ft_sprintf(clist->errmsg, FT_ERR_RD_NL_MAP); + return (-1); + } + return (0); +} diff --git a/src/ft_get_player_spawn.c b/src/ft_get_player_spawn.c index 774a80e..6853313 100644 --- a/src/ft_get_player_spawn.c +++ b/src/ft_get_player_spawn.c @@ -71,17 +71,17 @@ void x = 1; y = 1; - while (clist->mlist->map[y]) + while (clist->mlist.map[y]) { - while (clist->mlist->map[y][x]) + while (clist->mlist.map[y][x]) { - if (ft_ischarset("NSEW", clist->mlist->map[y][x])) + if (ft_ischarset(FT_CHRST_SPAWN, clist->mlist.map[y][x])) { plist->pos_x = x + 0.5; plist->pos_y = y + 0.5; plist->start_x = plist->pos_x; plist->start_y = plist->pos_y; - ft_get_start_side(clist->mlist->map[y][x], clist->plist); + ft_get_start_side(clist->mlist.map[y][x], plist); ft_get_sprite_spawn(clist); return ; } diff --git a/src/ft_get_res.c b/src/ft_get_res.c index 26853be..4447c1d 100644 --- a/src/ft_get_res.c +++ b/src/ft_get_res.c @@ -24,8 +24,7 @@ static int8_t i++; if (i != ft_strlen(word)) { - ft_strlcpy(clist->errmsg, FT_ERR_RES_ALPHA, - ft_strlen(FT_ERR_RES_ALPHA) + 1); + ft_sprintf(clist->errmsg, FT_ERR_RES_ALPHA); return (-1); } return (0); @@ -40,28 +39,37 @@ static void wl->y_size -= 1; } -int8_t - ft_get_res(char **words, t_cub *clist) +static int8_t + ft_check_res_args(char **words, t_cub *clist) { - t_win *wlist; - - wlist = clist->wlist; if (!(*words + 0) || !(*(words + 1)) || !(*(words + 2)) || (*(words + 3))) { - ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); + ft_sprintf(clist->errmsg, FT_ERR_ARGS); return (-1); } + return (0); +} + +int8_t + ft_get_res(char **words, t_cub *clist) +{ + t_win *wlist; + + wlist = &clist->wlist; + if (clist->currlvl > 0) + return (0); + if (ft_check_res_args(words, clist) < 0) + return (-1); if ((ft_checkdigit(words[1], clist) < 0) || (ft_checkdigit(words[2], clist) < 0)) return (-1); wlist->x_size = ft_atoi(words[1]); wlist->y_size = ft_atoi(words[2]); - if (wlist->x_size <= 10 - || wlist->y_size <= 10) + if (wlist->x_size <= 50 || + wlist->y_size <= 50) { - ft_strlcpy(clist->errmsg, FT_ERR_RES_SMALL, - ft_strlen(FT_ERR_RES_SMALL) + 1); + ft_sprintf(clist->errmsg, FT_ERR_RES_SMALL); return (-1); } if (ft_get_screen_size(wlist) < 0) diff --git a/src/ft_get_sprite.c b/src/ft_get_sprite.c index 84feeff..10c6bdd 100644 --- a/src/ft_get_sprite.c +++ b/src/ft_get_sprite.c @@ -19,21 +19,21 @@ void { size_t x; size_t y; - uint8_t i; + uint8_t i; x = 1; y = 1; - i = 0; - while (clist->mlist->map[y]) + i = 0; + while (clist->mlist.map[y]) { - while (clist->mlist->map[y][x]) + while (clist->mlist.map[y][x]) { - if (ft_ischarset("2", clist->mlist->map[y][x])) + if (ft_ischarset("2", clist->mlist.map[y][x])) { - clist->mlist->sprite_nbr++; + clist->mlist.sprite_nbr++; clist->sprites[i].s_pos_x = x; clist->sprites[i].s_pos_y = y; - i++; + i++; } x++; } @@ -47,25 +47,23 @@ int8_t { if (!(*words) || !words[1] || words[2]) { - ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_ARGS); return (-1); } if (ft_check_ext(*(words + 1), ".xpm") < 0) { - ft_strlcpy(clist->errmsg, FT_ERR_NOT_A_XPM, - ft_strlen(FT_ERR_NOT_A_XPM) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); return (-1); } - ft_memdel((void**)&clist->mlist->sprite_path); - if (!(clist->mlist->sprite_path = ft_strdup(*(words + 1)))) + ft_memdel((void**)&clist->mlist.sprite_path); + if (!(clist->mlist.sprite_path = ft_strdup(*(words + 1)))) { - ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, - ft_strlen(FT_ERR_ALLOCATE) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); return (-1); } - if (ft_check_not_found(clist->mlist->sprite_path) < 0) + if (ft_check_not_found(clist->mlist.sprite_path) < 0) { - ft_strlcpy(clist->errmsg, FT_ERR_RD_SP, ft_strlen(FT_ERR_RD_SP) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_RD_SP); return (-1); } return (0); diff --git a/src/ft_get_tex.c b/src/ft_get_tex.c index 58a0358..f8a817c 100644 --- a/src/ft_get_tex.c +++ b/src/ft_get_tex.c @@ -12,32 +12,30 @@ #include <libft.h> #include <cub3d.h> -#include <stdlib.h> +#include <stdint.h> int8_t ft_get_tex_no(char **words, t_cub *clist) { if (!(*words) || !(*(words + 1)) || (*(words + 2))) { - ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_ARGS); return (-1); } if (ft_check_ext(*(words + 1), ".xpm") < 0) { - ft_strlcpy(clist->errmsg, FT_ERR_NOT_A_XPM, - ft_strlen(FT_ERR_NOT_A_XPM) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); return (-1); } - ft_memdel((void**)&clist->mlist->no_tex_path); - if (!(clist->mlist->no_tex_path = ft_strdup(*(words + 1)))) + ft_memdel((void**)&clist->mlist.no_tex_path); + if (!(clist->mlist.no_tex_path = ft_strdup(*(words + 1)))) { - ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, - ft_strlen(FT_ERR_ALLOCATE) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); return (-1); } - if (ft_check_not_found(clist->mlist->no_tex_path) < 0) + if (ft_check_not_found(clist->mlist.no_tex_path) < 0) { - ft_strlcpy(clist->errmsg, FT_ERR_RD_NO, ft_strlen(FT_ERR_RD_NO) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_RD_NO); return (-1); } return (0); @@ -48,25 +46,23 @@ int8_t { if (!(*words) || !(*(words + 1)) || (*(words + 2))) { - ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_ARGS); return (-1); } if (ft_check_ext(*(words + 1), ".xpm") < 0) { - ft_strlcpy(clist->errmsg, FT_ERR_NOT_A_XPM, - ft_strlen(FT_ERR_NOT_A_XPM) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); return (-1); } - ft_memdel((void**)&clist->mlist->so_tex_path); - if (!(clist->mlist->so_tex_path = ft_strdup(*(words + 1)))) + ft_memdel((void**)&clist->mlist.so_tex_path); + if (!(clist->mlist.so_tex_path = ft_strdup(*(words + 1)))) { - ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, - ft_strlen(FT_ERR_ALLOCATE) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); return (-1); } - if (ft_check_not_found(clist->mlist->so_tex_path) < 0) + if (ft_check_not_found(clist->mlist.so_tex_path) < 0) { - ft_strlcpy(clist->errmsg, FT_ERR_RD_SO, ft_strlen(FT_ERR_RD_SO) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_RD_SO); return (-1); } return (0); @@ -77,25 +73,23 @@ int8_t { if (!(*words) || !(*(words + 1)) || (*(words + 2))) { - ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_ARGS); return (-1); } if (ft_check_ext(*(words + 1), ".xpm") < 0) { - ft_strlcpy(clist->errmsg, FT_ERR_NOT_A_XPM, - ft_strlen(FT_ERR_NOT_A_XPM) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); return (-1); } - ft_memdel((void**)&clist->mlist->ea_tex_path); - if (!(clist->mlist->ea_tex_path = ft_strdup(*(words + 1)))) + ft_memdel((void**)&clist->mlist.ea_tex_path); + if (!(clist->mlist.ea_tex_path = ft_strdup(*(words + 1)))) { - ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, - ft_strlen(FT_ERR_ALLOCATE) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); return (-1); } - if (ft_check_not_found(clist->mlist->ea_tex_path) < 0) + if (ft_check_not_found(clist->mlist.ea_tex_path) < 0) { - ft_strlcpy(clist->errmsg, FT_ERR_RD_EA, ft_strlen(FT_ERR_RD_EA) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_RD_EA); return (-1); } return (0); @@ -106,25 +100,23 @@ int8_t { if (!(*words) || !(*(words + 1)) || (*(words + 2))) { - ft_strlcpy(clist->errmsg, FT_ERR_ARGS, ft_strlen(FT_ERR_ARGS) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_ARGS); return (-1); } if (ft_check_ext(*(words + 1), ".xpm") < 0) { - ft_strlcpy(clist->errmsg, FT_ERR_NOT_A_XPM, - ft_strlen(FT_ERR_NOT_A_XPM) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); return (-1); } - ft_memdel((void**)&clist->mlist->we_tex_path); - if (!(clist->mlist->we_tex_path = ft_strdup(*(words + 1)))) + ft_memdel((void**)&clist->mlist.we_tex_path); + if (!(clist->mlist.we_tex_path = ft_strdup(*(words + 1)))) { - ft_strlcpy(clist->errmsg, FT_ERR_ALLOCATE, - ft_strlen(FT_ERR_ALLOCATE) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); return (-1); } - if (ft_check_not_found(clist->mlist->we_tex_path) < 0) + if (ft_check_not_found(clist->mlist.we_tex_path) < 0) { - ft_strlcpy(clist->errmsg, FT_ERR_RD_WE, ft_strlen(FT_ERR_RD_WE) + 1); + ft_sprintf(clist->errmsg, "%s", FT_ERR_RD_WE); return (-1); } return (0); diff --git a/src/ft_get_tex_extra.c b/src/ft_get_tex_extra.c new file mode 100644 index 0000000..25f9c23 --- /dev/null +++ b/src/ft_get_tex_extra.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_get_tex_extra.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/28 18:24:52 by rbousset #+# #+# */ +/* Updated: 2020/02/28 18:24:56 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> + +int8_t + ft_get_f_tex(char **words, t_cub *clist) +{ + if (!(*words) || !(*(words + 1)) || (*(words + 2))) + { + ft_sprintf(clist->errmsg, "%s", FT_ERR_ARGS); + return (-1); + } + if (ft_check_ext(*(words + 1), ".xpm") < 0) + { + ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); + return (-1); + } + ft_memdel((void**)&clist->mlist.fl_tex_path); + if (!(clist->mlist.fl_tex_path = ft_strdup(*(words + 1)))) + { + ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); + return (-1); + } + if (ft_check_not_found(clist->mlist.fl_tex_path) < 0) + { + ft_sprintf(clist->errmsg, FT_ERR_RD_NL_TEX); + return (-1); + } + clist->mlist.isftex = 1; + return (0); +} + +int8_t + ft_get_c_tex(char **words, t_cub *clist) +{ + if (!(*words) || !(*(words + 1)) || (*(words + 2))) + { + ft_sprintf(clist->errmsg, "%s", FT_ERR_ARGS); + return (-1); + } + if (ft_check_ext(*(words + 1), ".xpm") < 0) + { + ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); + return (-1); + } + ft_memdel((void**)&clist->mlist.ce_tex_path); + if (!(clist->mlist.ce_tex_path = ft_strdup(*(words + 1)))) + { + ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); + return (-1); + } + if (ft_check_not_found(clist->mlist.ce_tex_path) < 0) + { + ft_sprintf(clist->errmsg, FT_ERR_RD_NL_TEX); + return (-1); + } + clist->mlist.isctex = 1; + return (0); +} diff --git a/src/ft_get_tex_nl.c b/src/ft_get_tex_nl.c new file mode 100644 index 0000000..bb071f7 --- /dev/null +++ b/src/ft_get_tex_nl.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_get_tex_nl.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/28 18:24:52 by rbousset #+# #+# */ +/* Updated: 2020/02/28 18:24:56 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> +#include <stdint.h> + +int8_t + ft_get_tex_nl(char **words, t_cub *clist) +{ + if (!(*words) || !(*(words + 1)) || (*(words + 2))) + { + ft_sprintf(clist->errmsg, "%s", FT_ERR_ARGS); + return (-1); + } + if (ft_check_ext(*(words + 1), ".xpm") < 0) + { + ft_sprintf(clist->errmsg, "%s", FT_ERR_NOT_A_XPM); + return (-1); + } + ft_memdel((void**)&clist->mlist.nl_tex_path); + if (!(clist->mlist.nl_tex_path = ft_strdup(*(words + 1)))) + { + ft_sprintf(clist->errmsg, "%s", FT_ERR_ALLOCATE); + return (-1); + } + if (ft_check_not_found(clist->mlist.nl_tex_path) < 0) + { + ft_sprintf(clist->errmsg, FT_ERR_RD_NL_TEX); + return (-1); + } + return (0); +} diff --git a/src/ft_hex_to_rgb.c b/src/ft_hex_to_rgb.c new file mode 100644 index 0000000..30332cc --- /dev/null +++ b/src/ft_hex_to_rgb.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_hex_to_rgb.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:28:58 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:28:58 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <cub3d.h> +#include <stdint.h> + +t_bmp_rgb + ft_hex_to_rgb(uint32_t color) +{ + t_bmp_rgb rgb; + + rgb.r = (color >> 16) & 255; + rgb.g = (color >> 8) & 255; + rgb.b = color & 255; + return (rgb); +} diff --git a/src/ft_init_bmp.c b/src/ft_init_bmp.c new file mode 100644 index 0000000..18c6002 --- /dev/null +++ b/src/ft_init_bmp.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_init_bmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:28:46 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:28:46 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> +#include <stdint.h> + +t_bmp_info + ft_init_bmp_info(void) +{ + t_bmp_info bmp_info; + + bmp_info.size = 0; + bmp_info.width = 0; + bmp_info.height = 0; + bmp_info.planes = 1; + bmp_info.bit_count = 0; + bmp_info.compression = 0; + bmp_info.size_image = 0; + bmp_info.x_pixels_per_meter = 0; + bmp_info.y_pixels_per_meter = 0; + bmp_info.colors_used = 0; + bmp_info.colors_important = 0; + return (bmp_info); +} + +t_bmp_file + ft_init_bmp(void) +{ + t_bmp_file bmp_file; + + bmp_file.file_type = 0x4d42; + bmp_file.file_size = 0; + bmp_file.reserv_one = 0; + bmp_file.reserv_two = 0; + bmp_file.offset_data = 0; + return (bmp_file); +} diff --git a/src/ft_init_funptr.c b/src/ft_init_funptr.c new file mode 100644 index 0000000..f95e020 --- /dev/null +++ b/src/ft_init_funptr.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_init_funptr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:28:53 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:28:53 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> + +void + ft_init_ref(t_cub *clist) +{ + ft_sprintf(clist->ref[0], "R"); + ft_sprintf(clist->ref[1], "NO"); + ft_sprintf(clist->ref[2], "SO"); + ft_sprintf(clist->ref[3], "EA"); + ft_sprintf(clist->ref[4], "WE"); + ft_sprintf(clist->ref[5], "S"); + ft_sprintf(clist->ref[6], "F"); + ft_sprintf(clist->ref[7], "C"); + ft_sprintf(clist->ref[8], "L"); + ft_sprintf(clist->ref[9], "LT"); + ft_sprintf(clist->ref[10], "MU"); + ft_sprintf(clist->ref[11], "SH"); + ft_bzero(clist->ref[12], 3); +} + +void + ft_init_funptr(t_cub *clist) +{ + clist->key_ptr[0] = ft_w_key; + clist->key_ptr[1] = ft_a_key; + clist->key_ptr[2] = ft_s_key; + clist->key_ptr[3] = ft_d_key; + clist->key_ptr[4] = ft_left_key; + clist->key_ptr[5] = ft_right_key; + clist->get_ptr[0] = ft_get_res; + clist->get_ptr[1] = ft_get_tex_no; + clist->get_ptr[2] = ft_get_tex_so; + clist->get_ptr[3] = ft_get_tex_ea; + clist->get_ptr[4] = ft_get_tex_we; + clist->get_ptr[5] = ft_get_sprite; + clist->get_ptr[6] = ft_get_f_color; + clist->get_ptr[7] = ft_get_c_color; + clist->get_ptr[8] = ft_get_path_nl; + clist->get_ptr[9] = ft_get_tex_nl; + clist->get_ptr[10] = ft_get_music; + clist->get_ptr[11] = ft_get_darkness; +} diff --git a/src/ft_init_lists.c b/src/ft_init_lists.c index 5592358..d27df52 100644 --- a/src/ft_init_lists.c +++ b/src/ft_init_lists.c @@ -15,10 +15,9 @@ #include <cub3d.h> #include <stddef.h> #include <stdlib.h> -#include <limits.h> -#include <math.h> +#include <stdint.h> -static t_rgb +t_rgb ft_init_rgb(void) { t_rgb rgb; @@ -30,86 +29,66 @@ static t_rgb } static t_player - *ft_init_player(void) + ft_init_player(void) { - t_player *plist; + t_player plist; - if (!(plist = (t_player*)malloc(sizeof(t_player)))) - return (NULL); - plist->pos_x = 0; - plist->pos_y = 0; - plist->start_x = 0; - plist->start_y = 0; - plist->cam_x = 0; - plist->dir_x = -1; - plist->dir_y = 0; - plist->plane_x = 0; - plist->plane_y = 0.66666666; + plist.pos_x = 0; + plist.pos_y = 0; + plist.pos_z = 0; + plist.start_x = 0; + plist.start_y = 0; + plist.cam_x = 0; + plist.dir_x = -1; + plist.dir_y = 0; + plist.plane_x = 0; + plist.plane_y = 0.80; return (plist); } -static t_win - *ft_init_win(void) +static int8_t + ft_init_win(t_win *wl) { - t_win *wlist; - - if (!(wlist = (t_win*)malloc(sizeof(t_win)))) - return (NULL); - if (!(wlist->wlx = malloc(1)) || - !(wlist->winptr = malloc(1))) - return (NULL); - wlist->inited = 0; - wlist->x_size = 0; - wlist->y_size = 0; - wlist->x_max_size = 0; - wlist->y_max_size = 0; - return (wlist); + if (!(wl->winptr = malloc(1))) + return (-1); + wl->inited = 0; + wl->x_size = 0; + wl->y_size = 0; + wl->x_max_size = 0; + wl->y_max_size = 0; + return (0); } -static t_cub - *ft_init_cub(void) +static int8_t + ft_init_cub(t_cub *cl) { - t_cub *clist; - uint8_t i; + int8_t i; - if (!(clist = (t_cub*)malloc(sizeof(t_cub)))) - return (NULL); - if (!(clist->plist = ft_init_player()) || - !(clist->mlist = ft_init_map())) - return (NULL); - ft_bzero(clist->errmsg, 40); + if (ft_init_map(&cl->mlist) < 0) + return (-1); + cl->plist = ft_init_player(); + ft_bzero(cl->errmsg, 40); i = -1; while (++i < 5) - clist->key_input[i] = -1; - clist->ishud = 0; - clist->f_rgb = ft_init_rgb(); - clist->c_rgb = ft_init_rgb(); - clist->rlist = ft_init_s_ray(); - clist->key_ptr[0] = ft_w_key; - clist->key_ptr[1] = ft_a_key; - clist->key_ptr[2] = ft_s_key; - clist->key_ptr[3] = ft_d_key; - clist->key_ptr[4] = ft_left_key; - clist->key_ptr[5] = ft_right_key; - return (clist); + cl->key_input[i] = -1; + cl->ishud = 0; + cl->isoldmus = 0; + cl->f_rgb = ft_init_rgb(); + cl->c_rgb = ft_init_rgb(); + cl->rlist = ft_init_s_ray(); + cl->currlvl = 0; + cl->walltexgood = 0; + ft_init_funptr(cl); + ft_init_ref(cl); + return (0); } int8_t - ft_init_cub3d(t_cub **clist) + ft_init_cub3d(t_cub *clist) { - t_cub *cl; - - if (!(cl = ft_init_cub())) - { - ft_memdel((void**)&cl); + if (ft_init_cub(clist) < 0) return (-1); - } - if (!(cl->wlist = ft_init_win())) - { - ft_memdel((void**)&cl->wlist); - ft_memdel((void**)&cl); + if (ft_init_win(&clist->wlist) < 0) return (-1); - } - *clist = cl; return (0); } diff --git a/src/ft_init_map.c b/src/ft_init_map.c index cc767f6..68a3b6d 100644 --- a/src/ft_init_map.c +++ b/src/ft_init_map.c @@ -13,31 +13,54 @@ #include <libft.h> #include <cub3d.h> #include <stdlib.h> +#include <stdint.h> -t_map - *ft_init_map(void) +static int8_t + ft_init_map_callocs(t_map *mlist) { - t_map *mlist; - - if (!(mlist = (t_map*)malloc(sizeof(t_map)))) - return (NULL); - if (!(mlist->no_tex_path = (char*)ft_calloc(1, sizeof(char))) || + if (!(mlist->filename = (char*)ft_calloc(1, sizeof(char))) || + !(mlist->no_tex_path = (char*)ft_calloc(1, sizeof(char))) || !(mlist->so_tex_path = (char*)ft_calloc(1, sizeof(char))) || !(mlist->ea_tex_path = (char*)ft_calloc(1, sizeof(char))) || !(mlist->we_tex_path = (char*)ft_calloc(1, sizeof(char))) || !(mlist->sprite_path = (char*)ft_calloc(1, sizeof(char))) || + !(mlist->nl_tex_path = (char*)ft_calloc(1, sizeof(char))) || + !(mlist->fl_tex_path = (char*)ft_calloc(1, sizeof(char))) || + !(mlist->ce_tex_path = (char*)ft_calloc(1, sizeof(char))) || + !(mlist->nlevel_path = (char*)ft_calloc(1, sizeof(char))) || + !(mlist->music_path = (char*)ft_calloc(1, sizeof(char))) || + !(mlist->music_cmd = (char*)ft_calloc(1, sizeof(char))) || !(mlist->mapl = (char*)ft_calloc(1, sizeof(char))) || !(mlist->map = (char**)ft_calloc(2, sizeof(char*))) || - !(mlist->map[0] = (char*)ft_calloc(1, sizeof(char)))) - return (NULL); + !(mlist->map[0] = (char*)ft_calloc(1, sizeof(char))) || + !(mlist->mcmd_words = (char**)ft_calloc(2, sizeof(char*))) || + !(mlist->mcmd_words[0] = (char*)ft_calloc(1, sizeof(char)))) + return (-1); + return (0); +} + +int8_t + ft_init_map(t_map *mlist) +{ + if (ft_init_map_callocs(mlist) < 0) + return (-1); mlist->map[1] = 0; + mlist->mcmd_words[1] = 0; mlist->map_w = 0; mlist->map_h = 0; + mlist->mapl_len = 0; mlist->x_step = 0; mlist->y_step = 0; mlist->line_chk = 0; mlist->map_start = 0; mlist->isspawn = 0; + mlist->isftex = 0; + mlist->isctex = 0; + mlist->isnlvl = 0; + mlist->ismusic = 0; + mlist->darklvl = 0; mlist->scale = 0; - return (mlist); + mlist->nlx = 0; + mlist->nly = 0; + return (0); } diff --git a/src/ft_init_winlx.c b/src/ft_init_winlx.c index b41dc4a..dee4009 100644 --- a/src/ft_init_winlx.c +++ b/src/ft_init_winlx.c @@ -18,16 +18,18 @@ int ft_init_winlx(t_cub *clist) { - ft_memdel((void**)&clist->wlist->wlx); - if (!(clist->wlist->wlx = mlx_init())) + if (!(clist->wlist.wlx = mlx_init())) return (-1); - ft_memdel((void**)&clist->wlist->winptr); - if (!(clist->wlist->winptr = mlx_new_window(clist->wlist->wlx, - clist->wlist->x_size, clist->wlist->y_size, "Cub3D"))) + return (0); +} + +int + ft_init_winptr(t_cub *clist) +{ + ft_memdel((void**)&clist->wlist.winptr); + if (!(clist->wlist.winptr = mlx_new_window(clist->wlist.wlx, + clist->wlist.x_size, clist->wlist.y_size, "Cub3D"))) return (-1); - clist->wlist->inited = 1; - ft_printf("Created window of size %ux%u\nHost OS: %s\n", - clist->wlist->x_size, clist->wlist->y_size, FT_OS); - /* ft_music(clist); */ + clist->wlist.inited = 1; return (0); } diff --git a/src/ft_key_loop.c b/src/ft_key_loop.c index d913c49..d40de34 100644 --- a/src/ft_key_loop.c +++ b/src/ft_key_loop.c @@ -15,13 +15,46 @@ #include <stdint.h> #include <stddef.h> +static uint64_t + ft_find_x(int32_t key, const t_player *pl) +{ + if (key == 0) + return (pl->pos_x + (pl->dir_y * FT_COLL_MULT)); + else if (key == 1) + return (pl->pos_x + (pl->dir_x * (FT_COLL_MULT / 2))); + else if (key == 2) + return (pl->pos_x - (pl->dir_y * FT_COLL_MULT)); + else if (key == 3) + return (pl->pos_x - (pl->dir_x * (FT_COLL_MULT / 2))); + return (1); +} + +static uint64_t + ft_find_y(int32_t key, const t_player *pl) +{ + if (key == 0) + return (pl->pos_y + (pl->dir_x * FT_COLL_MULT)); + else if (key == 1) + return (pl->pos_y - (pl->dir_y * (FT_COLL_MULT / 2))); + else if (key == 2) + return (pl->pos_y - (pl->dir_x * FT_COLL_MULT)); + else if (key == 3) + return (pl->pos_y + (pl->dir_y * (FT_COLL_MULT / 2))); + return (1); +} + static void - ft_collision(float old_y, float old_x, t_player *pl, t_map *ml) + ft_collision(float old_y, float old_x, int32_t key, t_cub *cl) { - const size_t x = pl->pos_x; - const size_t y = pl->pos_y; + uint64_t x; + uint64_t y; + t_player *pl; - if (ml->map[y][x] == '1') + pl = &cl->plist; + x = ft_find_x(key, pl); + y = ft_find_y(key, pl); + if (ft_ischarset(FT_CHRST_COLLISION, cl->mlist.map[y][x]) || + cl->mlist.map[y][x] == '\0') { pl->pos_y = old_y; pl->pos_x = old_x; @@ -32,19 +65,24 @@ int ft_key_loop(t_cub *cl) { uint8_t i; - const float old_y = cl->plist->pos_y; - const float old_x = cl->plist->pos_x; + const float old_y = cl->plist.pos_y; + const float old_x = cl->plist.pos_x; i = 0; while (i < 5 && cl->key_input[i] != -1 && cl->key_input[i] <= 5) { cl->key_ptr[cl->key_input[i]](cl); - ft_collision(old_y, old_x, cl->plist, cl->mlist); + ft_collision(old_y, old_x, cl->key_input[i], cl); + if (cl->mlist.isnlvl) + { + if (ft_warp_level(cl) < 0) + return (ft_exit(FT_RET_FAILED_STRUCTS, cl)); + } i++; } - if (cl->key_input[0] != -1) - { - ft_draw_scene(cl); - } + if (cl->key_input[0] != -1) + { + ft_draw_scene(cl); + } return (0); } diff --git a/src/ft_music.c b/src/ft_music.c index 1be430b..234f86e 100644 --- a/src/ft_music.c +++ b/src/ft_music.c @@ -11,9 +11,21 @@ /* ************************************************************************** */ #include <cub3d.h> +#include <unistd.h> void - ft_music(t_cub *cl) + ft_music_fork(char **mcmd_words, char *const envp[]) { - (void)cl; + execve(*(mcmd_words + 0), mcmd_words, envp); +} + +void + ft_enable_music(t_cub *cl) +{ + cl->isoldmus = 1; + cl->mpid = fork(); + if (cl->mpid == 0) + { + ft_music_fork(cl->mlist.mcmd_words, cl->envp); + } } diff --git a/src/ft_parse_map.c b/src/ft_parse_map.c index 0dfb779..f1df73d 100644 --- a/src/ft_parse_map.c +++ b/src/ft_parse_map.c @@ -6,7 +6,7 @@ /* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 17:28:56 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:28:56 by rbousset ### ########lyon.fr */ +/* Updated: 2020/03/09 17:27:29 by rbousset ### ########lyon.fr */ /* */ /* ************************************************************************** */ @@ -25,13 +25,15 @@ static void size_t j; i = 0; - ml = clist->mlist; + ml = &clist->mlist; while (ml->map[i]) i++; j = 0; - while (ml->map[i - 1][j]) + i -= 1; + while (ml->map[i][j]) { - if (ml->map[i - 1][j] != '1' && ml->map[i - 1][j] != '\0') + if (ml->map[i][j] != '1' && ml->map[i][j] != ' ' && + ml->map[i][j] != '\0') ft_map_error(FT_ERR_MAP_L_L, clist); j++; } @@ -51,7 +53,7 @@ static int8_t char **words; int ret; - clist->mlist->line_chk += 1; + clist->mlist.line_chk += 1; if ((ret = get_next_line(fd, &line)) < 0) return (ft_map_error(FT_ERR_READ, clist)); if (ret == 0) @@ -61,21 +63,25 @@ static int8_t ft_memdel((void**)&line); return (ft_parse_it(fd, clist)); } - if (!ft_ischarset("RNSEWFC1\0", line[0]) - || !(words = ft_split(line, ' '))) + if (ft_ischarset("1 ", line[0])) + return ((ft_get_map_first_line(line, clist) < 0) ? (-1) : (25)); + if (!ft_ischarset(FT_CHRST_VALID_PARSE, line[0]) || + !(words = ft_split(line, ' '))) return (ft_error_here(FT_ERR_ILL_ENTRY, line, clist)); - if ((ret = ft_select_get(words, clist)) == 12) - return ((ft_get_map_first_line(line, clist) < 0) ? (-1) : (12)); + if ((ret = ft_select_get(words, clist)) == FT_PARSE_END_RET) + return ((ft_get_map_first_line(line, clist) < 0) ? (-1) : (25)); ft_memdel((void**)&line); return (ret); } -static void - ft_no_map_error(t_cub *clist) +void + ft_save_name(const char *map_path, t_cub *clist) { - ft_dprintf(STDERR_FILENO, "Error\n"); - ft_dprintf(STDERR_FILENO, "\033[31;1mNo map\033[0m\n"); - ft_exit(5, clist); + ft_memdel((void**)&clist->mlist.filename); + if (!(clist->mlist.filename = + (char*)malloc((ft_strlen(map_path) + 1) * sizeof(char)))) + ft_error(FT_RET_ALLOC_ERR, FT_ERR_ALLOCATE, clist); + ft_sprintf(clist->mlist.filename, "%s", map_path); } void @@ -88,17 +94,21 @@ void ft_map_error(FT_ERR_NOT_A_CUB, clist); fd = open(map_path, O_RDONLY); if (fd < 0) - ft_no_map_error(clist); + ft_error(FT_RET_NO_MAP, FT_ERR_NO_MAP, clist); + ft_save_name(map_path, clist); ret = 1; - while (ret != 12 && ret >= 0) + while (ret != FT_PARSE_END_RET && ret >= 0) ret = ft_parse_it(fd, clist); (ret == -2) ? (ft_map_error(FT_ERR_ALR_SET, clist)) : 0; (ret == -1) ? (ft_map_error(clist->errmsg, clist)) : 0; if (ft_get_map_core(fd, clist) < 0) ft_map_error(clist->errmsg, clist); ft_check_map_last_line(clist); - ft_get_player_spawn(clist->plist, clist); + ft_check_map_surrounds(&clist->mlist, clist); + ft_get_player_spawn(&clist->plist, clist); + ft_get_nlvl_pos(&clist->mlist); ft_check_missing(clist); ft_set_minimap_scale(clist); + clist->currlvl += 1; close(fd); } diff --git a/src/ft_raycasting.c b/src/ft_raycasting.c index 464eb95..9657b18 100644 --- a/src/ft_raycasting.c +++ b/src/ft_raycasting.c @@ -17,15 +17,14 @@ #include <stdlib.h> #include <math.h> - -void +static void ft_calc_tex(t_cub *clist) { if (clist->rlist.side == 0) - clist->rlist.wall_hit_x = (clist->plist->pos_x) + + clist->rlist.wall_hit_x = (clist->plist.pos_x) + clist->rlist.wall_dist * clist->rlist.y_ray_dir; else - clist->rlist.wall_hit_x = (clist->plist->pos_y) + + clist->rlist.wall_hit_x = (clist->plist.pos_y) + clist->rlist.wall_dist * clist->rlist.x_ray_dir; clist->rlist.wall_hit_x -= floor(clist->rlist.wall_hit_x); clist->tlist[clist->w_side].tex_x = (int)(clist->rlist.wall_hit_x * @@ -38,14 +37,14 @@ void - clist->tlist[clist->w_side].tex_x - 1; } - static void -ft_initray(t_cub *cl, uint16_t i) +static void + ft_initray(uint16_t i, t_cub *cl) { t_win *wl; t_player *pl; - wl = cl->wlist; - pl = cl->plist; + wl = &cl->wlist; + pl = &cl->plist; pl->cam_x = 2 * i / (float)(wl->x_size) - 1; cl->rlist.x_ray_pos = pl->pos_y; cl->rlist.y_ray_pos = pl->pos_x; @@ -59,29 +58,34 @@ ft_initray(t_cub *cl, uint16_t i) if (cl->rlist.side == 0) { cl->rlist.wall_dist = (cl->rlist.sqx - cl->rlist.x_ray_pos + - (1 - cl->mlist->x_step) / 2) / cl->rlist.x_ray_dir; + (1 - cl->mlist.x_step) / 2) / cl->rlist.x_ray_dir; } else + { cl->rlist.wall_dist = (cl->rlist.sqy - cl->rlist.y_ray_pos + - (1 - cl->mlist->y_step) / 2) / cl->rlist.y_ray_dir; + (1 - cl->mlist.y_step) / 2) / cl->rlist.y_ray_dir; + } } - void -ft_castray(t_cub *cl) +void + ft_castray(t_cub *cl) { uint16_t i; t_win *wl; - float *dist_tab; + float *dist_tab; - if (!(dist_tab = malloc(sizeof(float) * cl->wlist->x_size))) - return ; - if (!(cl->rlist.wall_dist_tab = malloc(sizeof(float) * cl->wlist->x_size))) - return ; + wl = &cl->wlist; + i = (wl->y_size / 2) + 1; + while (++i < wl->y_size) + ft_floor_cast(i, cl); + if (!(dist_tab = malloc(sizeof(float) * cl->wlist.x_size))) + return ; + if (!(cl->rlist.wall_dist_tab = malloc(sizeof(float) * cl->wlist.x_size))) + return ; i = 0; - wl = cl->wlist; while (i < wl->x_size) { - ft_initray(cl, i); + ft_initray(i, cl); cl->rlist.line_h = (int16_t)(wl->y_size / cl->rlist.wall_dist); cl->rlist.wall_t = -cl->rlist.line_h / 2 + wl->y_size / 2; if (cl->rlist.wall_t < 0) @@ -92,8 +96,8 @@ ft_castray(t_cub *cl) ft_choose_tex(cl); ft_calc_tex(cl); ft_draw_verline(cl, i, cl->rlist.wall_t, cl->rlist.wall_b); - dist_tab[i] = cl->rlist.wall_dist; - cl->rlist.wall_dist_tab = dist_tab; + dist_tab[i] = cl->rlist.wall_dist; + cl->rlist.wall_dist_tab = dist_tab; i++; } } diff --git a/src/ft_rgb_to_hex.c b/src/ft_rgb_to_hex.c index a25d3e0..e101dcf 100644 --- a/src/ft_rgb_to_hex.c +++ b/src/ft_rgb_to_hex.c @@ -18,6 +18,12 @@ uint32_t { uint32_t res; + rgb.r = (rgb.r > 255) ? (255) : (rgb.r); + rgb.g = (rgb.g > 255) ? (255) : (rgb.g); + rgb.b = (rgb.b > 255) ? (255) : (rgb.b); + rgb.r = (rgb.r < 0) ? (0) : (rgb.r); + rgb.g = (rgb.g < 0) ? (0) : (rgb.g); + rgb.b = (rgb.b < 0) ? (0) : (rgb.b); res = 0; res += ((rgb.r << 16) + (rgb.g << 8) + rgb.b); return (res); diff --git a/src/ft_save_to_bmp.c b/src/ft_save_to_bmp.c new file mode 100644 index 0000000..90bbb1f --- /dev/null +++ b/src/ft_save_to_bmp.c @@ -0,0 +1,125 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_save_to_bmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/29 20:50:35 by rbousset #+# #+# */ +/* Updated: 2020/02/29 20:50:37 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> +#include <stddef.h> +#include <stdlib.h> +#include <fcntl.h> +#include <unistd.h> +#include <stdint.h> + +static char + *ft_set_file_name(t_map *ml) +{ + char *str; + + if (ft_strrchr(ml->filename, '/')) + { + if (!(str = (char*)malloc((ft_strlen(FT_BMP_SAVE_DIR) + + ft_strlen(ft_strrchr(ml->filename, '/') + 1) + + 1) * sizeof(char)))) + return (NULL); + ft_sprintf(str, "%s%s", FT_BMP_SAVE_DIR, + ft_strrchr(ml->filename, '/') + 1); + } + else + { + if (!(str = (char*)malloc((ft_strlen(FT_BMP_SAVE_DIR) + + ft_strlen(ml->filename) + 1) * sizeof(char)))) + return (NULL); + ft_sprintf(str, "%s%s", FT_BMP_SAVE_DIR, ml->filename); + } + ft_memcpy(ft_strrchr(str, '.') + 1, "bmp", 3); + return (str); +} + +static uint8_t + *ft_convert_image_to_bmp(t_img img, t_cub *cl) +{ + uint8_t *bmp; + t_bmp_rgb px; + uint32_t y; + uint32_t x; + uint32_t i; + + (void)img; + if (!(bmp = (uint8_t*)malloc((3 * cl->wlist.x_size + * cl->wlist.y_size) * sizeof(uint8_t)))) + return (NULL); + y = cl->wlist.y_size; + i = 0; + while (--y > 0) + { + x = -1; + while (++x < cl->wlist.x_size) + { + px = ft_hex_to_rgb(*(int*)(cl->img.ptr + + (x * 4 + (y * cl->img.sizeline)))); + *(bmp + i++) = (uint8_t)px.b; + *(bmp + i++) = (uint8_t)px.g; + *(bmp + i++) = (uint8_t)px.r; + } + } + return (bmp); +} + +static int8_t + ft_write_bmp(const char *fname, uint32_t h, uint32_t w, uint8_t *bmp) +{ + t_bmp_file bmp_file; + t_bmp_info bmp_info; + uint32_t size; + int fd; + + size = w * h * 3; + bmp_file = ft_init_bmp(); + bmp_info = ft_init_bmp_info(); + bmp_file.file_size = size + sizeof(t_bmp_file) + sizeof(t_bmp_info); + bmp_file.offset_data = bmp_file.file_size - size; + bmp_info.size = 40; + bmp_info.width = w; + bmp_info.height = h; + bmp_info.bit_count = 24; + bmp_info.size_image = size; + if (!(fd = open(fname, O_RDWR | O_CREAT, 0644))) + return (-1); + write(fd, &bmp_file, sizeof(t_bmp_file)); + write(fd, &bmp_info, sizeof(t_bmp_info)); + write(fd, bmp, size); + close(fd); + return (0); +} + +int8_t + ft_save_to_bmp(t_cub *cl) +{ + uint8_t *bmp; + char *fname; + + if (!(fname = ft_set_file_name(&cl->mlist))) + return (-1); + if (!(bmp = ft_convert_image_to_bmp(cl->img, cl))) + { + ft_memdel((void**)&fname); + return (-1); + } + if (ft_write_bmp(fname, cl->wlist.y_size, cl->wlist.x_size, bmp) < 0) + { + ft_memdel((void**)&bmp); + ft_memdel((void**)&fname); + return (-1); + } + ft_memdel((void**)&bmp); + ft_memdel((void**)&fname); + return (0); +} diff --git a/src/ft_select_get.c b/src/ft_select_get.c index 4b82430..c246d25 100644 --- a/src/ft_select_get.c +++ b/src/ft_select_get.c @@ -18,19 +18,20 @@ static int8_t ft_check_exists(const int8_t ret, t_cub *clist) { - if (ret == 12) - return (12); - if (ret == 0 && (clist->wlist->x_size != 0 || clist->wlist->y_size != 0)) + if (ret == FT_PARSE_END_RET) + return (FT_PARSE_END_RET); + if (ret == 0 && clist->currlvl == 0 && + (clist->wlist.x_size != 0 || clist->wlist.y_size != 0)) return (-1); - else if (ret == 1 && (clist->mlist->no_tex_path[0])) + else if (ret == 1 && (clist->mlist.no_tex_path[0])) return (-1); - else if (ret == 2 && (clist->mlist->so_tex_path[0])) + else if (ret == 2 && (clist->mlist.so_tex_path[0])) return (-1); - else if (ret == 3 && (clist->mlist->ea_tex_path[0])) + else if (ret == 3 && (clist->mlist.ea_tex_path[0])) return (-1); - else if (ret == 4 && (clist->mlist->we_tex_path[0])) + else if (ret == 4 && (clist->mlist.we_tex_path[0])) return (-1); - else if (ret == 5 && (clist->mlist->sprite_path[0])) + else if (ret == 5 && (clist->mlist.sprite_path[0])) return (-1); else if (ret == 6 && ((clist->f_rgb.r != -1) || (clist->f_rgb.g != -1) || (clist->f_rgb.b != -1))) @@ -42,48 +43,47 @@ static int8_t } static int8_t + ft_check_exists_two(const int8_t ret, t_cub *clist) +{ + if (ret == FT_PARSE_END_RET) + return (FT_PARSE_END_RET); + if (ret == 8 && (clist->mlist.nlevel_path[0])) + return (-1); + if (ret == 9 && (clist->mlist.nl_tex_path[0])) + return (-1); + if (ret == 10 && (clist->mlist.music_path[0])) + return (-1); + return (ret); +} + +static int8_t ft_get_id(char **words, t_cub *clist) { int8_t ret; - char ref[9][3]; - ft_memcpy(ref[0], "R\0", 3); - ft_memcpy(ref[1], "NO", 3); - ft_memcpy(ref[2], "SO", 3); - ft_memcpy(ref[3], "EA", 3); - ft_memcpy(ref[4], "WE", 3); - ft_memcpy(ref[5], "S\0", 3); - ft_memcpy(ref[6], "F\0", 3); - ft_memcpy(ref[7], "C\0", 3); - ft_memcpy(ref[8], "", 1); + if (words[0][0] == ' ' || words[0][0] == '1') + return (FT_PARSE_END_RET); ret = 0; - while (ft_strncmp(words[0], ref[ret], 3) && ref[ret][0]) + while (ft_strncmp(words[0], clist->ref[ret], 3) && clist->ref[ret][0]) ret++; - if (ret == 8) - ret = 12; - return (ret = ft_check_exists(ret, clist)); + if (ret == 12) + ret = FT_PARSE_END_RET; + ret = ft_check_exists(ret, clist); + ret = ft_check_exists_two(ret, clist); + return (ret); } int8_t ft_select_get(char **words, t_cub *clist) { - int8_t (*fun_ptr[8])(char**, t_cub*); int8_t id; - fun_ptr[0] = ft_get_res; - fun_ptr[1] = ft_get_tex_no; - fun_ptr[2] = ft_get_tex_so; - fun_ptr[3] = ft_get_tex_ea; - fun_ptr[4] = ft_get_tex_we; - fun_ptr[5] = ft_get_sprite; - fun_ptr[6] = ft_get_f_color; - fun_ptr[7] = ft_get_c_color; - if ((id = ft_get_id(words, clist)) == 12) + if ((id = ft_get_id(words, clist)) == FT_PARSE_END_RET) { ft_free_words(words); - return (12); + return (FT_PARSE_END_RET); } - if (id < 0 || (*fun_ptr[id])(words, clist) < 0) + if (id < 0 || clist->get_ptr[id](words, clist) < 0) { ft_free_words(words); return ((id < 0) ? (-2) : (-1)); diff --git a/src/ft_set_minimap_scale.c b/src/ft_set_minimap_scale.c index dc4bd12..14a6f56 100644 --- a/src/ft_set_minimap_scale.c +++ b/src/ft_set_minimap_scale.c @@ -17,10 +17,20 @@ void { t_map *ml; - ml = clist->mlist; - ((ml->scale = (clist->wlist->x_size / (uint16_t)ml->map_w) - 1) < 1) - ? (ml->scale = 1) : 0; - (clist->wlist->y_size < (ml->map_h * ml->scale)) ? (ml->scale = - (clist->wlist->y_size / (uint16_t)ml->map_h) - 1) : 0; - (ml->scale >= 10) ? (ml->scale /= 4) : 0; + ml = &clist->mlist; + if (ml->map_w > ml->map_h) + { + ml->scale = (clist->wlist.x_size / (uint16_t)ml->map_w); + ml->scale = ((ml->scale - 1) < 1) ? (1) : (ml->scale); + ml->scale = (clist->wlist.y_size < (ml->map_h * ml->scale)) + ? ((clist->wlist.y_size / (uint16_t)ml->map_h) - 1) : (ml->scale); + } + else + { + ml->scale = (clist->wlist.y_size / (uint16_t)ml->map_h); + ml->scale = ((ml->scale - 1) < 1) ? (1) : (ml->scale); + ml->scale = (clist->wlist.y_size < (ml->map_h * ml->scale)) + ? ((clist->wlist.y_size / (uint16_t)ml->map_h) - 1) : (ml->scale); + } + ml->scale = (ml->scale >= 10) ? (ml->scale / 4) : (ml->scale); } diff --git a/src/ft_tex_init.c b/src/ft_tex_init.c index 40e39e5..adc7fd0 100644 --- a/src/ft_tex_init.c +++ b/src/ft_tex_init.c @@ -21,28 +21,70 @@ ** 2 : ea ** 3 : we ** 4 : sprite +** 5 : new level door */ -void ft_wall_tex_init(t_cub *cl) +static void + ft_get_nlvl_img(t_cub *cl) { - cl->tlist[0].img = mlx_xpm_file_to_image(cl->wlist->wlx, - cl->mlist->no_tex_path, &cl->tlist[0].img_w, &cl->tlist[0].img_h); + cl->tlist[5].img = mlx_xpm_file_to_image(cl->wlist.wlx, + cl->mlist.nl_tex_path, &cl->tlist[5].img_w, &cl->tlist[5].img_h); + cl->tlist[5].ptr = mlx_get_data_addr(cl->tlist[5].img, + &cl->tlist[5].bpp, &cl->tlist[5].sizeline, &cl->tlist[5].endian); +} + +static void + ft_get_floor_tex(t_cub *cl) +{ + cl->tlist[6].img = mlx_xpm_file_to_image(cl->wlist.wlx, + cl->mlist.fl_tex_path, &cl->tlist[6].img_w, &cl->tlist[6].img_h); + cl->tlist[6].ptr = mlx_get_data_addr(cl->tlist[6].img, + &cl->tlist[6].bpp, &cl->tlist[6].sizeline, &cl->tlist[6].endian); +} + +static void + ft_get_ceil_tex(t_cub *cl) +{ + cl->tlist[7].img = mlx_xpm_file_to_image(cl->wlist.wlx, + cl->mlist.ce_tex_path, &cl->tlist[7].img_w, &cl->tlist[7].img_h); + cl->tlist[7].ptr = mlx_get_data_addr(cl->tlist[7].img, + &cl->tlist[7].bpp, &cl->tlist[7].sizeline, &cl->tlist[7].endian); +} + +static void + ft_wall_tex_init_norme(t_cub *cl) +{ + if (cl->mlist.isnlvl) + ft_get_nlvl_img(cl); + if (cl->mlist.isftex) + ft_get_floor_tex(cl); + if (cl->mlist.isctex) + ft_get_ceil_tex(cl); + cl->walltexgood = 1; +} + +void + ft_wall_tex_init(t_cub *cl) +{ + cl->tlist[0].img = mlx_xpm_file_to_image(cl->wlist.wlx, + cl->mlist.no_tex_path, &cl->tlist[0].img_w, &cl->tlist[0].img_h); cl->tlist[0].ptr = mlx_get_data_addr(cl->tlist[0].img, &cl->tlist[0].bpp, &cl->tlist[0].sizeline, &cl->tlist[0].endian); - cl->tlist[1].img = mlx_xpm_file_to_image(cl->wlist->wlx, - cl->mlist->so_tex_path, &cl->tlist[1].img_w, &cl->tlist[1].img_h); + cl->tlist[1].img = mlx_xpm_file_to_image(cl->wlist.wlx, + cl->mlist.so_tex_path, &cl->tlist[1].img_w, &cl->tlist[1].img_h); cl->tlist[1].ptr = mlx_get_data_addr(cl->tlist[1].img, &cl->tlist[1].bpp, &cl->tlist[1].sizeline, &cl->tlist[1].endian); - cl->tlist[2].img = mlx_xpm_file_to_image(cl->wlist->wlx, - cl->mlist->ea_tex_path, &cl->tlist[2].img_w, &cl->tlist[2].img_h); + cl->tlist[2].img = mlx_xpm_file_to_image(cl->wlist.wlx, + cl->mlist.ea_tex_path, &cl->tlist[2].img_w, &cl->tlist[2].img_h); cl->tlist[2].ptr = mlx_get_data_addr(cl->tlist[2].img, &cl->tlist[2].bpp, &cl->tlist[2].sizeline, &cl->tlist[2].endian); - cl->tlist[3].img = mlx_xpm_file_to_image(cl->wlist->wlx, - cl->mlist->we_tex_path, &cl->tlist[3].img_w, &cl->tlist[3].img_h); + cl->tlist[3].img = mlx_xpm_file_to_image(cl->wlist.wlx, + cl->mlist.we_tex_path, &cl->tlist[3].img_w, &cl->tlist[3].img_h); cl->tlist[3].ptr = mlx_get_data_addr(cl->tlist[3].img, &cl->tlist[3].bpp, &cl->tlist[3].sizeline, &cl->tlist[3].endian); - cl->tlist[4].img = mlx_xpm_file_to_image(cl->wlist->wlx, - cl->mlist->sprite_path, &cl->tlist[4].img_w, &cl->tlist[4].img_h); + cl->tlist[4].img = mlx_xpm_file_to_image(cl->wlist.wlx, + cl->mlist.sprite_path, &cl->tlist[4].img_w, &cl->tlist[4].img_h); cl->tlist[4].ptr = mlx_get_data_addr(cl->tlist[4].img, &cl->tlist[4].bpp, &cl->tlist[4].sizeline, &cl->tlist[4].endian); + ft_wall_tex_init_norme(cl); } diff --git a/src/ft_treat_args.c b/src/ft_treat_args.c new file mode 100644 index 0000000..a47f9f7 --- /dev/null +++ b/src/ft_treat_args.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_treat_args.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:29:00 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:29:08 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> +#include <stddef.h> +#include <stdint.h> +#include <unistd.h> + +uint8_t + ft_check_map_arg(int argc, const char *argv[]) +{ + (void)argv; + if (argc < 2) + { + ft_dprintf(STDERR_FILENO, "Error\n\033[1;31mNo map selected\n\033[0m"); + return (FT_RET_BAD_ARGV); + } + return (0); +} + +uint8_t + ft_use_args(int argc, const char *argv[], char *const envp[], t_cub *clist) +{ + clist->envp = envp; + if (argc < 3) + { + if (ft_init_winptr(clist) < 0) + { + return (ft_exit(FT_RET_FAILED_MLX, clist)); + } + ft_draw_scene(clist); + if (clist->mlist.ismusic) + { + ft_enable_music(clist); + } + ft_hooks_and_loops(&clist->wlist, clist); + } + else if (argc == 3 && !ft_strncmp("--save", argv[2], 7)) + { + ft_draw_scene_bmp(clist); + return (ft_exit(FT_RET_FINE, clist)); + } + else + { + ft_dprintf(STDERR_FILENO, "Error\n\033[1;31mBad arguments\n\033[0m"); + return (ft_exit(FT_RET_BAD_ARGV, clist)); + } + return (FT_RET_FINE); +} diff --git a/src/ft_warp_level.c b/src/ft_warp_level.c new file mode 100644 index 0000000..de2103d --- /dev/null +++ b/src/ft_warp_level.c @@ -0,0 +1,119 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_warp_level.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/28 20:54:28 by rbousset #+# #+# */ +/* Updated: 2020/02/28 20:54:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include <libft.h> +#include <cub3d.h> +#include <stdlib.h> +#include <stddef.h> +#include <stdint.h> +#include <unistd.h> +#include <signal.h> +#include <sys/wait.h> +#include <mlx.h> + +static void + ft_del_map(t_map *ml) +{ + ft_memdel((void**)&ml->filename); + ft_memdel((void**)&ml->no_tex_path); + ft_memdel((void**)&ml->so_tex_path); + ft_memdel((void**)&ml->ea_tex_path); + ft_memdel((void**)&ml->we_tex_path); + ft_memdel((void**)&ml->sprite_path); + ft_memdel((void**)&ml->nl_tex_path); + ft_memdel((void**)&ml->fl_tex_path); + ft_memdel((void**)&ml->ce_tex_path); + ft_memdel((void**)&ml->nlevel_path); + ft_memdel((void**)&ml->music_path); + ft_memdel((void**)&ml->music_cmd); + ft_memdel((void**)&ml->mapl); + ft_free_words(ml->map); + ft_free_words(ml->mcmd_words); +} + +static void + ft_del_some(t_cub *cl) +{ + uint8_t i; + + cl->plist.pos_x = 0; + cl->plist.pos_y = 0; + cl->plist.pos_z = 0; + cl->plist.start_x = 0; + cl->plist.start_y = 0; + cl->plist.cam_x = 0; + cl->plist.dir_x = -1; + cl->plist.dir_y = 0; + cl->plist.plane_x = 0; + cl->plist.plane_y = 0.66666666; + cl->f_rgb = ft_init_rgb(); + cl->c_rgb = ft_init_rgb(); + cl->rlist = ft_init_s_ray(); + i = 0; + while (i <= 5) + { + mlx_destroy_image(cl->wlist.wlx, cl->tlist[i].img); + cl->tlist[i].img = NULL; + i++; + } +} + +static void + ft_treat_music(uint8_t isoldmus, char *tmp_mup, t_cub *cl) +{ + if (isoldmus && !cl->mlist.ismusic) + { + kill(cl->mpid, SIGTERM); + wait(&cl->mpid); + cl->isoldmus = 0; + } + else if (isoldmus && cl->mlist.ismusic + && ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1)) + { + kill(cl->mpid, SIGTERM); + wait(&cl->mpid); + ft_enable_music(cl); + } + else if (isoldmus && cl->mlist.ismusic + && !ft_strncmp(tmp_mup, cl->mlist.music_path, ft_strlen(tmp_mup) + 1)) + return ; +} + +int8_t + ft_warp_level(t_cub *cl) +{ + char *next_path; + char *tmp_mup; + uint8_t isoldmus; + + if ((uint32_t)cl->plist.pos_x == cl->mlist.nlx && + (uint32_t)cl->plist.pos_y == cl->mlist.nly) + { + if (!(next_path = (char *)malloc((ft_strlen( + cl->mlist.nlevel_path) + 1) * sizeof(char)))) + return (-1); + ft_sprintf(next_path, "%s", cl->mlist.nlevel_path); + if ((isoldmus = cl->mlist.ismusic)) + tmp_mup = ft_strdup(cl->mlist.music_path); + ft_del_some(cl); + ft_del_map(&cl->mlist); + if (ft_init_map(&cl->mlist) < 0) + return (-1); + ft_parse_map(next_path, cl); + ft_treat_music(isoldmus, tmp_mup, cl); + if (isoldmus) + ft_memdel((void**)&tmp_mup); + ft_wall_tex_init(cl); + ft_memdel((void**)&next_path); + } + return (0); +} @@ -13,26 +13,20 @@ #include <libft.h> #include <mlx.h> #include <cub3d.h> -#include <stddef.h> -#include <unistd.h> int - main(int argc, const char *argv[]) + main(int argc, const char *argv[], char *const envp[]) { - t_cub *clist; + t_cub clist; - if (argc < 2) - { - ft_dprintf(STDERR_FILENO, "Error\n\033[1;31mNo map selected\n\033[0m"); - return (1); - } + if (ft_check_map_arg(argc, argv) == FT_RET_BAD_ARGV) + return (FT_RET_BAD_ARGV); if (ft_init_cub3d(&clist) < 0) - return (2); - ft_parse_map(argv[1], clist); - if (ft_init_winlx(clist) < 0) - return (ft_exit(3, clist)); - ft_wall_tex_init(clist); - ft_draw_scene(clist); - ft_hooks_and_loops(clist->wlist, clist); - return (0); + return (FT_RET_FAILED_STRUCTS); + ft_parse_map(argv[1], &clist); + if (ft_init_winlx(&clist) < 0) + return (ft_exit(FT_RET_FAILED_MLX, &clist)); + ft_wall_tex_init(&clist); + ft_use_args(argc, argv, envp, &clist); + return (FT_RET_FINE); } |