From 6ff81b1c4dcdf870cab622b7ff7ebde1a18b1a64 Mon Sep 17 00:00:00 2001 From: kinou-p Date: Wed, 15 Jun 2022 18:21:20 +0200 Subject: [PATCH] norm --- libft/double_utils.c | 4 +- srcs/cast_ray.c | 145 +++++++++++------------------ srcs/get_texture_array.c | 20 ++-- srcs/main.c | 16 +--- srcs/parsing.c | 94 ------------------- srcs/parsing/check_color_texture.c | 29 +++--- srcs/test.c | 44 --------- srcs/utils.c | 35 ++++--- 8 files changed, 106 insertions(+), 281 deletions(-) delete mode 100644 srcs/parsing.c delete mode 100644 srcs/test.c diff --git a/libft/double_utils.c b/libft/double_utils.c index c43bc85..8eb08dc 100644 --- a/libft/double_utils.c +++ b/libft/double_utils.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 18:16:18 by apommier #+# #+# */ -/* Updated: 2022/05/02 18:16:28 by apommier ### ########.fr */ +/* Updated: 2022/06/15 17:49:23 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,4 +51,4 @@ void print_double_fd(char **tab, int fd) i++; } } -} \ No newline at end of file +} diff --git a/srcs/cast_ray.c b/srcs/cast_ray.c index 4ceb0a3..8d5ad6e 100644 --- a/srcs/cast_ray.c +++ b/srcs/cast_ray.c @@ -12,77 +12,35 @@ #include "../includes/Cub3D.h" -void print_ray2(t_data *img, double vx, double vy, double dist) +int get_color(char R, char G, char B) { - int i = -1; - int red = 0; + int color; - red = red << 8; - red +=255; - red = red << 8; - red = red << 8; - while (++i < dist) - { - mlx_pixel_put(img->mlx_test, img->mlx_win_test, (img->player.x + (vx) * i) , (img->player.y + (vy) * i) , red); - mlx_pixel_put(img->mlx_test, img->mlx_win_test, (img->player.x + (vx) * i) + 1, (img->player.y + (vy) * i) , red); - mlx_pixel_put(img->mlx_test, img->mlx_win_test, (img->player.x + (vx) * i) , (img->player.y + (vy) * i) + 1, red); - } -} - -int get_color(char R, char G, char B) -{ - int color = 0; - - //printf("R= %d G= %d B= %d\n", R, G, B); + color = 0; color = color << 8; color += R; - //printf("color= %d\n", color); color = color << 8; color += G; - //printf("color= %d\n", color); color = color << 8; color += B; - //printf("color= %d\n", color); return (color); } -int get_red() -{ - int red = 0; - - red = red << 8; - red +=255; - red = red << 8; - red = red << 8; - return (red); -} - -int get_dark_red() -{ - int red = 0; - - red = red << 8; - red +=139; - red = red << 8; - red = red << 8; - return (red); -} - void set_pixel(t_data *img, int color, int x, int y) { - int pixel; + int pixel; if (y < 0 || y > 520 || x < 0 || x > 960) return ; pixel = (y * img->size_line) + (x * 4); - if (img->endian == 1) // Most significant (Alpha) byte first + if (img->endian == 1) { img->buffer[pixel + 0] = 0; img->buffer[pixel + 1] = (color >> 16) & 0xFF; img->buffer[pixel + 2] = (color >> 8) & 0xFF; img->buffer[pixel + 3] = (color) & 0xFF; } - else if (img->endian == 0) // Least significant (Blue) byte first + else if (img->endian == 0) { img->buffer[pixel + 0] = (color) & 0xFF; img->buffer[pixel + 1] = (color >> 8) & 0xFF; @@ -95,55 +53,60 @@ void draw_ray3d(t_data *img, t_ray ray) { double line_height; double line_offset; - int x = 0; - double y = 0; - int mx = 0; - int my = 0; - int color; - int texture_size = 64; + int x; + double y; + int mx; + int my; + int color; + double gap; + double myy; - line_height = /*img->map.size*/64 * 960 / ray.dist; + x = 0; + y = 0; + mx = 0; + my = 0; + line_height = 64 * 960 / ray.dist; line_offset = 256 - ((int)line_height) / 2; - double gap = 1; - double myy = 0; - y = 0; - my = 0; - myy = 0; - gap = (texture_size / line_height); - mx = ((int)ray.mp) % texture_size; - if (line_height > 512) + gap = 1; + myy = 0; + y = 0; + my = 0; + myy = 0; + gap = (64 / line_height); + mx = ((int)ray.mp) % 64; + if (line_height > 512) + { + line_offset = 0; + myy = gap * ((line_height - 512) / 2); + } + while (y < line_height - 8 && y < 512) + { + myy += gap; + my = (int)myy; + ray.pixel = ((my) * 64 + mx) * 3 + 1; + x = -1; + if (ray.pixel >= 12290 || ray.pixel < 0) + return ; + else if (ray.pixel > 0) { - line_offset = 0; - myy = gap * ((line_height - 512) / 2); + if (ray.texture_type == 'N' && img->map.texture.north) + color = get_color(img->map.texture.north[ray.pixel], img->map.texture.north[ray.pixel + 1], img->map.texture.north[ray.pixel + 2]); + else if (ray.texture_type == 'S' && img->map.texture.south) + color = get_color(img->map.texture.south[ray.pixel], img->map.texture.south[ray.pixel + 1], img->map.texture.south[ray.pixel + 2]); + else if (ray.texture_type == 'W' && img->map.texture.west) + color = get_color(img->map.texture.west[ray.pixel], img->map.texture.west[ray.pixel + 1], img->map.texture.west[ray.pixel + 2]); + else if (ray.texture_type == 'E' && img->map.texture.east) + color = get_color(img->map.texture.east[ray.pixel], img->map.texture.east[ray.pixel + 1], img->map.texture.east[ray.pixel + 2]); } - while (y < line_height - 8 && y < 512) + else + color = 0; + while (++x < /*img->map.x / 2*/4) { - myy += gap; - my = (int)myy;//gap; - ray.pixel = ((my) * 64 + mx)* 3 + 1; - x = -1; - if (ray.pixel >= 12290 || ray.pixel < 0)//here read - return ; - else if (ray.pixel > 0) - { - if (ray.texture_type == 'N' && img->map.texture.north) - color = get_color(img->map.texture.north[ray.pixel], img->map.texture.north[ray.pixel + 1], img->map.texture.north[ray.pixel + 2]); - else if (ray.texture_type == 'S' && img->map.texture.south) - color = get_color(img->map.texture.south[ray.pixel], img->map.texture.south[ray.pixel + 1], img->map.texture.south[ray.pixel + 2]); - else if (ray.texture_type == 'W' && img->map.texture.west) - color = get_color(img->map.texture.west[ray.pixel], img->map.texture.west[ray.pixel + 1], img->map.texture.west[ray.pixel + 2]); - else if (ray.texture_type == 'E' && img->map.texture.east) - color = get_color(img->map.texture.east[ray.pixel], img->map.texture.east[ray.pixel + 1], img->map.texture.east[ray.pixel + 2]); - } - else - color = 0; - while (++x < /*img->map.x / 2*/4) - { - set_pixel(img, color, ray.index * /*(img->map.x / 2)*/4 + x, y + line_offset); - } - y++; + set_pixel(img, color, ray.index * /*(img->map.x / 2)*/4 + x, y + line_offset); } - x++; + y++; + } + x++; } void draw_ray(t_data *img) diff --git a/srcs/get_texture_array.c b/srcs/get_texture_array.c index 5b8eb1b..0e4f8e2 100644 --- a/srcs/get_texture_array.c +++ b/srcs/get_texture_array.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/06 16:10:11 by apommier #+# #+# */ -/* Updated: 2022/06/15 17:25:21 by apommier ### ########.fr */ +/* Updated: 2022/06/15 18:10:05 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,7 +42,7 @@ void put_texture_in_struct(char type, unsigned char *texture, t_data *img) int is_nbr(char *str) { - int i; + int i; i = 0; while (str[i]) @@ -54,17 +54,18 @@ int is_nbr(char *str) return (1); } -unsigned char *get_texture(char type, char *path, t_data *img)//change in list +unsigned char *get_texture(char type, char *path, t_data *img) { - int fd; - unsigned char *ret; - int count; - char *swap = 0; + int fd; + unsigned char *ret; + int count; + char *swap; + swap = 0; (void)type; count = 0; if (!path) - return(0); + return (0); fd = open(path, O_DIRECTORY); if (fd >= 0) { @@ -107,7 +108,8 @@ unsigned char *get_texture(char type, char *path, t_data *img)//change in list } if (swap) swap[ft_strlen(swap) - 1] = 0; - if ((swap && is_nbr(swap) && ft_strlen(swap) <= 3 && ft_atoi(swap) <= 255 && ft_atoi(swap) >= 0) || !count) + if ((swap && is_nbr(swap) && ft_strlen(swap) <= 3 + && ft_atoi(swap) <= 255 && ft_atoi(swap) >= 0) || !count) ret[count] = (unsigned char)ft_atoi(swap); else if (swap) { diff --git a/srcs/main.c b/srcs/main.c index 42c5549..eca5011 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/02/13 16:42:55 by apommier #+# #+# */ -/* Updated: 2022/06/15 17:16:33 by apommier ### ########.fr */ +/* Updated: 2022/06/15 18:11:33 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,9 +48,9 @@ int key_pressed(int type, t_data *img) img->player.side = -1; else if (type == 'd') img->player.side = 1; - else if (type == 65361)//fleche gauche + else if (type == 65361) img->player.angle_side = -1; - else if (type == 65363)//fleche droite + else if (type == 65363) img->player.angle_side = 1; else if (type == 65505) img->player.shift = 1; @@ -67,9 +67,9 @@ int key_released(int type, t_data *img) img->player.side = 0; else if (type == 'd') img->player.side = 0; - else if (type == 65361)//fleche gauche + else if (type == 65361) img->player.angle_side = 0; - else if (type == 65363)//fleche droite + else if (type == 65363) img->player.angle_side = 0; else if (type == 65505) img->player.shift = 0; @@ -90,8 +90,6 @@ int main(int argc, char **argv) if (argc != 2) ft_error("Error\nBad number of arguments, only need a map\n"); set_map_player(&img); - img.player.x = 0; - img.player.y = 0; if (check_map(argv, &img)) return (0); img.mlx = mlx_init(); @@ -100,10 +98,6 @@ int main(int argc, char **argv) img.mlx_win = mlx_new_window(img.mlx, 960, 512, "Cub3D"); img.player.vx = cos(deg_to_rad(img.player.angle)); img.player.vy = sin(deg_to_rad(img.player.angle)); - img.player.front = 0; - img.player.side = 0; - img.player.angle_side = 0; - img.player.shift = 0; mlx_hook(img.mlx_win, 2, 1L << 0, key_pressed, &img); mlx_hook(img.mlx_win, 3, 1L << 1, key_released, &img); mlx_loop_hook(img.mlx, loop, &img); diff --git a/srcs/parsing.c b/srcs/parsing.c deleted file mode 100644 index 3df01a5..0000000 --- a/srcs/parsing.c +++ /dev/null @@ -1,94 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* parsing.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: sadjigui +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2022/05/24 14:17:25 by sadjigui #+# #+# */ -/* Updated: 2022/05/24 14:49:21 by sadjigui ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "../includes/Cub3D.h" - -int parsing(char **av); -{ - char *line; - int fd; - int i; - - i = 0; - fd = open(argv[1], O_RDONLY); - if (fd == -1) - ft_error("Error: Open call fail"); - line = get_next_line(fd); - while (line != NULL) - i++; - close(fd); - if (set_map_test) - return (1); - return (ret_map); -} - -int set_map_test(char **av, int size) -{ - char *line; - char **map; - int fd; - int i; - - i = 0; - fd = open(argv[1], O_RDONLY); - if (fd == -1) - ft_error("Error: Open call fail"); - map = malloc(sizeof(char *) * (size + 1)); - if (!map) - return (0); - line = get_next_line(fd); - while (line != NULL) - { - map[i] = line; - free(line); - line = get_next_line(fd); - i++; - } - map[i] = 0; - test_map(map, size); - // free map - close(fd); -} - -char *out_of_map(int size) -{ - int i; - char *str; - - i = 0; - str = malloc(sizeof(char) * (size + 1)); - if (!str) - return (NULL); - while (i < size) - { - str[i] = '3'; - i++; - } - return (str); -} - -int test_map(char **map, int height) -{ - char **tmp; - int width; - int i; - - i = 0; - width = find_max(map); - tmp = malloc(sizeof(char *) * (height + 3)); - if (!tmp) - return (0); - while (tmp[i]) - { - tmp[i] = out_of_map(width); - } -} diff --git a/srcs/parsing/check_color_texture.c b/srcs/parsing/check_color_texture.c index 241e1e3..f812977 100644 --- a/srcs/parsing/check_color_texture.c +++ b/srcs/parsing/check_color_texture.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/06/11 01:00:17 by apommier #+# #+# */ -/* Updated: 2022/06/15 17:42:02 by apommier ### ########.fr */ +/* Updated: 2022/06/15 18:16:40 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ int next_space_index(char *str, int i) return (i); } -char *transform_map(char **double_map, t_data *img) +char *transform_map(char **double_map, t_data *img) { char *map; int i; @@ -82,8 +82,8 @@ void set_texture_file(char *str, t_data *img, char c) void check_value(char **tab, t_data *img) { - int i; - int j; + int i; + int j; i = 0; while (tab[i]) @@ -101,11 +101,14 @@ void check_value(char **tab, t_data *img) void set_color_utils(char **tab, t_color *rgb, t_data *img) { - if (ft_strlen(tab[0]) > 3 || !(ft_atoi(tab[0]) >= 0 && ft_atoi(tab[0]) <= 255)) + if (ft_strlen(tab[0]) > 3 || !(ft_atoi(tab[0]) >= 0 + && ft_atoi(tab[0]) <= 255)) ft_exit("Error\nBad parameter in map file identifier(RGB)\n", img); - if (ft_strlen(tab[0]) > 3 || !(ft_atoi(tab[1]) >= 0 && ft_atoi(tab[1]) <= 255)) + if (ft_strlen(tab[0]) > 3 || !(ft_atoi(tab[1]) >= 0 + && ft_atoi(tab[1]) <= 255)) ft_exit("Error\nBad parameter in map file identifier(RGB)\n", img); - if (ft_strlen(tab[0]) > 3 || !(ft_atoi(tab[2]) >= 0 && ft_atoi(tab[2]) <= 255)) + if (ft_strlen(tab[0]) > 3 || !(ft_atoi(tab[2]) >= 0 + && ft_atoi(tab[2]) <= 255)) ft_exit("Error\nBad parameter in map file identifier(RGB)\n", img); check_value(tab, img); rgb->r = (unsigned char)ft_atoi(tab[0]); @@ -118,7 +121,7 @@ void set_color(char *str, t_data *img) { char c; int index; - char **tab; + char **tab; index = -1; while (str[++index]) @@ -140,7 +143,7 @@ void set_color(char *str, t_data *img) img->to_be_free.tab_two = 0; } -int check_texture_color(char **tab, t_data *img) +int check_texture_color(char **tab, t_data *img) { int next; int index; @@ -153,9 +156,13 @@ int check_texture_color(char **tab, t_data *img) ft_exit("Error\nBad syntax in map file (identifier)\n", img); while (index < next) { - if (next_space(tab[index], 0) == 'N' || next_space(tab[index], 0) == 'S' || next_space(tab[index], 0) == 'W' || next_space(tab[index], 0) == 'E') + if (next_space(tab[index], 0) == 'N' + || next_space(tab[index], 0) == 'S' + || next_space(tab[index], 0) == 'W' + || next_space(tab[index], 0) == 'E') set_texture_file(tab[index], img, next_space(tab[index], 0)); - else if (next_space(tab[index], 0) == 'F' || next_space(tab[index], 0) == 'C') + else if (next_space(tab[index], 0) == 'F' + || next_space(tab[index], 0) == 'C') set_color(tab[index], img); else if (next_space(tab[index], 0)) ft_exit("Error\nBad syntax in map file (identifier)\n", img); diff --git a/srcs/test.c b/srcs/test.c deleted file mode 100644 index 2c04a59..0000000 --- a/srcs/test.c +++ /dev/null @@ -1,44 +0,0 @@ -#include "../includes/Cub3D.h" - -int main() -{ - void *mlx = mlx_init(); - void *win = mlx_new_window(mlx, 640, 360, "Tutorial Window - Create Image"); - - void *image = mlx_new_image(mlx, 640, 360); - - // The following code goes here. - int pixel_bits; - int line_bytes; - int endian; - - char *buffer = mlx_get_data_addr(image, &pixel_bits, &line_bytes, &endian); - - int color = 0xABCDEF; - -if (pixel_bits != 32) - color = mlx_get_color_value(mlx, color); - -for(int y = 0; y < 360; ++y) -for(int x = 0; x < 640; ++x) -{ - int pixel = (y * line_bytes) + (x * 4); - - if (endian == 1) // Most significant (Alpha) byte first - { - buffer[pixel + 0] = (color >> 24); - buffer[pixel + 1] = (color >> 16) & 0xFF; - buffer[pixel + 2] = (color >> 8) & 0xFF; - buffer[pixel + 3] = (color) & 0xFF; - } - else if (endian == 0) // Least significant (Blue) byte first - { - buffer[pixel + 0] = (color) & 0xFF; - buffer[pixel + 1] = (color >> 8) & 0xFF; - buffer[pixel + 2] = (color >> 16) & 0xFF; - buffer[pixel + 3] = (color >> 24); - } -} -mlx_put_image_to_window(mlx, win, image, 0, 0); - mlx_loop(mlx); -} \ No newline at end of file diff --git a/srcs/utils.c b/srcs/utils.c index 3fb05a7..08d651a 100644 --- a/srcs/utils.c +++ b/srcs/utils.c @@ -6,7 +6,7 @@ /* By: apommier +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/02/13 23:37:02 by apommier #+# #+# */ -/* Updated: 2022/06/15 15:53:35 by apommier ### ########.fr */ +/* Updated: 2022/06/15 18:13:51 by apommier ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,18 +44,15 @@ int quit_game(t_data *img) free(img->to_be_free.str); if (img->to_be_free.fd != -1) close(img->to_be_free.fd); - - //free(img->map.texture.north); - //free_double(img->map); - exit(0); + exit(1); } -double deg_to_rad(double angle) +double deg_to_rad(double angle) { return (angle * 3.14159265358979323846 / 180.0); } -double reset_angle(double angle) +double reset_angle(double angle) { if (angle > 359) angle -= 360.0; @@ -72,11 +69,11 @@ void ft_error(char *error_msg) int update_pos(t_data *img) { - int multiplicator; + int multiplicator; if (img->player.shift == 1) multiplicator = 4; - else + else multiplicator = 2; if (img->player.front == 1) { @@ -90,22 +87,22 @@ int update_pos(t_data *img) } if (img->player.side == 1) { - img->player.x -= img->player.vy ; - img->player.y += img->player.vx ; + img->player.x -= img->player.vy; + img->player.y += img->player.vx; } else if (img->player.side == -1) { - img->player.x += img->player.vy; //* 5; - img->player.y -= img->player.vx; //* 5; + img->player.x += img->player.vy; + img->player.y -= img->player.vx; } - if (img->player.angle_side == -1)//fleche gauche + if (img->player.angle_side == -1) { img->player.angle += 1; img->player.angle = reset_angle(img->player.angle); img->player.vx = cos(deg_to_rad(img->player.angle)); img->player.vy = -sin(deg_to_rad(img->player.angle)); } - else if (img->player.angle_side == 1)//fleche droite + else if (img->player.angle_side == 1) { img->player.angle -= 1; img->player.angle = reset_angle(img->player.angle); @@ -115,9 +112,9 @@ int update_pos(t_data *img) return (0); } -void set_back(t_data *img) +void set_back(t_data *img) { - int x; + int x; x = 0; while (x < 512 * 960 * 4) @@ -128,7 +125,7 @@ void set_back(t_data *img) { img->buffer[x + 0] = img->map.floor.b; img->buffer[x + 1] = img->map.floor.g; - img->buffer[x + 2] =img->map.floor.r; + img->buffer[x + 2] = img->map.floor.r; img->buffer[x + 3] = 0; } } @@ -136,7 +133,7 @@ void set_back(t_data *img) { img->buffer[x + 0] = img->map.sky.b; img->buffer[x + 1] = img->map.sky.g; - img->buffer[x + 2] =img->map.sky.r; + img->buffer[x + 2] = img->map.sky.r; img->buffer[x + 3] = 0; } x += 4;