add textures

This commit is contained in:
kinou-p 2022-05-18 19:15:19 +02:00
parent 83d429796c
commit 95d5361d7a
13 changed files with 13396 additions and 38 deletions

View File

@ -6,12 +6,13 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ # # By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2022/02/13 16:27:49 by apommier #+# #+# # # Created: 2022/02/13 16:27:49 by apommier #+# #+# #
# Updated: 2022/05/04 18:08:34 by apommier ### ########.fr # # Updated: 2022/05/06 18:52:57 by apommier ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
NAME = Cub3D NAME = Cub3D
SRCS = srcs/main.c\ SRCS = srcs/main.c\
srcs/get_texture_array.c\
srcs/cast_ray.c\ srcs/cast_ray.c\
srcs/utils.c srcs/utils.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 16:30:59 by apommier #+# #+# */ /* Created: 2022/02/13 16:30:59 by apommier #+# #+# */
/* Updated: 2022/05/05 02:58:26 by apommier ### ########.fr */ /* Updated: 2022/05/17 22:51:01 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,8 +25,28 @@
# define PI 3.1415926535 # define PI 3.1415926535
typedef struct ray_info{
float ty;
float tx;
float mp;
float dist;
int index;
int wall_type;
int pixel;
} ray;
typedef struct all_wall_texture{
int *north;
int *east;
int *west;
int *south;
} sprite;
typedef struct map_information{ typedef struct map_information{
char **map; char **map;
sprite texture;
int floor; int floor;
int sky; int sky;
char *simple_map; char *simple_map;
@ -52,9 +72,10 @@ typedef struct s_data {
player player; player player;
} t_data; } t_data;
int get_color(char one, char two, char three); int *get_texture(int type);
int reset_angle(int angle); int get_color(char one, char two, char three);
float deg_to_rad(int angle); int reset_angle(int angle);
float deg_to_rad(int angle);
void draw_ray(t_data *img); void draw_ray(t_data *img);
void print_ray(t_data *img); void print_ray(t_data *img);
int key_press(int code, t_data *img); int key_press(int code, t_data *img);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:09:17 by apommier #+# #+# */ /* Created: 2020/11/29 00:09:17 by apommier #+# #+# */
/* Updated: 2022/01/18 06:50:22 by apommier ### ########.fr */ /* Updated: 2022/05/06 20:01:08 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,6 +21,8 @@ long ft_atoi(const char *nptr)
minus = 1; minus = 1;
nbr = 0; nbr = 0;
i = 0; i = 0;
if (!nptr)
return (0);
while ((nptr[i] >= 9 && nptr[i] <= 13) || nptr[i] == 32) while ((nptr[i] >= 9 && nptr[i] <= 13) || nptr[i] == 32)
i++; i++;
if (nptr[i] == '+') if (nptr[i] == '+')

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/20 21:44:01 by apommier #+# #+# */ /* Created: 2022/01/20 21:44:01 by apommier #+# #+# */
/* Updated: 2022/01/21 08:07:04 by apommier ### ########.fr */ /* Updated: 2022/05/06 19:29:27 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -39,3 +39,27 @@ char *ft_strjoin(char *save, char *s2)
dest[j] = 0; dest[j] = 0;
return (dest); return (dest);
} }
char *ft_strjoin_delete(char *save, char *s2)
{
char *dest;
int i;
int j;
i = 0;
j = 0;
if (!save && !s2)
return (0);
dest = malloc(ft_strlen(save) + ft_strlen(s2) + 1);
while (save && save[i])
dest[j++] = save[i++];
i = 0;
while (s2 && s2[i])
dest[j++] = s2[i++];
dest[j] = 0;
if (save)
free(save);
if (s2)
free(s2);
return (dest);
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/11 03:21:27 by apommier #+# #+# */ /* Created: 2020/12/11 03:21:27 by apommier #+# #+# */
/* Updated: 2022/05/02 18:17:12 by apommier ### ########.fr */ /* Updated: 2022/05/06 19:29:58 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -50,6 +50,7 @@ int ft_strncmp(const char *s1, const char *s2, size_t n);
size_t ft_strlcpy(char *dst, const char *src, size_t size); size_t ft_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlcat(char *dst, const char *src, size_t size); size_t ft_strlcat(char *dst, const char *src, size_t size);
char *ft_strjoin(char *save, char *s2); char *ft_strjoin(char *save, char *s2);
char *ft_strjoin_delete(char *save, char *s2);
char *ft_strnstr(const char *big, const char *little, size_t len); char *ft_strnstr(const char *big, const char *little, size_t len);
long ft_atoi(const char *nptr); long ft_atoi(const char *nptr);
void *ft_calloc(size_t nmenb, size_t size); void *ft_calloc(size_t nmenb, size_t size);

View File

@ -3,6 +3,6 @@
10100001 10100001
10000001 10000001
10000001 10000001
10100101 11000101
10000101 11000101
11111111 11111111

12290
sprite/brick_wall.ppm Normal file

File diff suppressed because it is too large Load Diff

770
sprite/brick_wall_old.ppm Normal file
View File

@ -0,0 +1,770 @@
16 16
255
136
136
136
131
131
131
131
131
131
136
136
136
136
136
136
77
90
53
136
136
136
131
131
131
148
148
148
131
131
131
131
131
131
155
155
155
96
96
96
136
136
136
148
148
148
136
136
136
115
115
115
121
121
121
103
123
63
121
121
121
148
148
148
96
96
96
115
115
115
121
121
121
115
115
115
99
112
73
103
123
63
131
131
131
77
90
53
121
121
121
121
121
121
115
115
115
115
115
115
103
123
63
99
112
73
121
121
121
136
136
136
77
90
53
115
115
115
115
115
115
121
121
121
103
123
63
121
121
121
136
136
136
77
90
53
121
121
121
115
115
115
115
115
115
103
102
102
103
102
102
96
96
96
96
96
96
103
102
102
91
89
89
83
97
56
77
90
53
94
94
94
104
104
104
104
104
104
103
102
102
91
89
89
77
90
53
100
102
95
103
102
102
131
131
131
136
136
136
96
96
96
136
136
136
106
127
67
121
137
91
136
136
136
131
131
131
155
155
155
96
96
96
136
136
136
131
131
131
131
131
131
136
136
136
136
136
136
131
131
131
121
121
121
131
131
131
96
96
96
106
127
67
99
112
73
121
121
121
121
121
121
121
121
121
131
131
131
96
96
96
115
115
115
121
121
121
103
123
63
115
115
115
121
121
121
121
121
121
121
121
121
136
136
136
91
89
89
115
115
115
103
123
63
115
115
115
115
115
115
121
121
121
136
136
136
91
89
89
121
121
121
115
115
115
121
121
121
99
112
73
103
123
63
121
121
121
104
104
104
103
102
102
91
89
89
83
97
56
83
97
56
77
90
53
104
104
104
104
104
104
91
89
89
83
97
56
83
97
56
77
90
53
96
96
96
103
102
102
103
102
102
103
102
102
136
136
136
131
131
131
131
131
131
155
155
155
77
90
53
148
148
148
127
127
127
131
131
131
106
127
67
121
137
91
136
136
136
131
131
131
155
155
155
164
164
164
103
102
102
136
136
136
115
115
115
115
115
115
121
121
121
131
131
131
96
96
96
121
121
121
115
115
115
99
112
73
99
112
73
115
115
115
115
115
115
121
121
121
121
121
121
131
131
131
96
96
96
121
121
121
103
123
63
115
115
115
121
121
121
136
136
136
103
102
102
115
115
115
115
115
115
103
123
63
121
121
121
115
115
115
115
115
115
103
123
63
121
121
121
136
136
136
96
96
96
121
121
121
91
89
89
77
90
53
83
97
56
104
104
104
103
102
102
83
97
56
83
97
56
77
90
53
96
96
96
91
89
89
91
89
89
103
102
102
103
102
102
104
104
104
96
96
96
91
89
89
131
131
131
136
136
136
104
104
104
136
136
136
136
136
136
121
137
91
131
131
131
131
131
131
136
136
136
155
155
155
104
104
104
136
136
136
136
136
136
131
131
131
136
136
136
131
131
131
121
121
121
131
131
131
96
96
96
121
121
121
99
112
73
121
121
121
121
121
121
115
115
115
121
121
121
131
131
131
103
102
102
115
115
115
121
121
121
99
112
73
103
123
63
115
115
115
121
121
121
136
136
136
77
90
53
121
121
121
103
123
63
99
112
73
115
115
115
121
121
121
121
121
121
136
136
136
104
104
104
121
121
121
99
112
73
103
123
63
121
121
121
121
121
121
96
96
96
77
90
53
77
90
53
77
90
53
83
97
56
83
97
56
77
90
53
96
96
96
104
104
104
103
102
102
103
102
102
103
102
102
83
97
56
77
90
53
96
96
96
96
96
96

View File

@ -0,0 +1,5 @@
P6
# Created by GIMP version 2.10.30 PNM plug-in
16 16
255
屁娟LLャャャg{?cpIャ<49>屁屁媼YYyyyyyycpIg{?ャ<><EFBFBD><E5B181>YYャペyyyyycpIsssャャャ[YYャペyycpIyyyyyy<79><79>ャNLLg{?yyysssssssssssscpI[YYg{?cpIャペyyyyyyyyャクLLgffyyyyyyyyyssscpIMZ5NLLyyy<79><79>ャャャャyyyNLLNLLNLL[YYgff[YYMZ5NLLNLLNLL[YYMZ5Sa8MZ5[YYNLLャャャ屁娟LL<4C><4C>Cy閏<79><E9968F><EFBFBD><E5B181>屁屁娥Z5<5A><35><EFBFBD>ャヨ<E383A3>yyyャチ``jCcpIyyyyyyyyyyyysssャゴa8sssyyyyyyyyyyyyャチ``jCMZ5yyyyyyyyyyyysssャゴa8sssyyyyyyyyyyyy<79><79>YYMZ5g{?sssyyyyyyyyyyyysssNLLcpIg{?yyysssssssssNLLgffsssyyysssssssssyyyyyy[YYsssssssssNLLNLLNLLMZ5NLL[YY[YY[YY[YY[YYNLLNLLNLLNLLNLLNLL<4C><4C><EFBFBD><E383A3>屁ャツpINLL<4C><4C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>屁寃ャ屁媼YY<59><59>{?sssyyyssssssg{?NLLssssssyyysssg{?yyy<79><79>YYyyyg{?sssyyyyyysssg{?NLLsssyyyyyyssscpIyyy<79><79>YYyyyg{?sssyyyyyysssg{?NLLyyyyyyyyycpIMZ5yyy<79><79>YYyyyNLL[YYNLLNLLNLL[YYNLL[YY[YYgffMZ5g{?cpI[YYNLL[YY

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/04 18:08:14 by apommier #+# #+# */ /* Created: 2022/05/04 18:08:14 by apommier #+# #+# */
/* Updated: 2022/05/05 02:59:14 by apommier ### ########.fr */ /* Updated: 2022/05/18 19:13:39 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -64,31 +64,118 @@ int get_dark_red()
return (red); return (red);
} }
void draw_ray3d(t_data *img, float dist, int ray, int type) void draw_ray3d(t_data *img, ray ray)
{ {
float line_height; float line_height;
float line_offset; float line_offset;
int i; int x = 0;
int y; float y = 0;
int mx = 0;
int my = 0;
int pixel;
int color;
int texture_size = 64;
//char *wall;
i = 0; //wall = get_texture(1);
line_height = img->map.size * 320 / dist; /*for(y=0;y<lineH;y++)
{
int pixel=((int)ty*32+(int)tx)*3+(hmt*32*32*3);
int red =All_Textures[pixel+0]*shade;
int green =All_Textures[pixel+1]*shade;
int blue =All_Textures[pixel+2]*shade;
glPointSize(8); glColor3ub(red,green,blue); glBegin(GL_POINTS); glVertex2i(r*8,y+lineOff); glEnd();
ty+=ty_step;
}*/
// float ty=ty_off*ty_step;//+hmt*32;
// float tx;
// if(shade==1)
// {
// tx=(int)(rx/2.0)%32;
// if(ra>180)
// tx=31-tx;
// }
// else
// {
// tx=(int)(ry/2.0)%32;
// if(ra>90 && ra<270)
// tx=31-tx;
// }
//pixel = ((int)ty * 32 + (int)tx) * 3 + (hmt * 32 * 32 * 3);
int copy = ray.ty;
//i = 0;
line_height = img->map.size * 320 / ray.dist;
if (line_height > 320) if (line_height > 320)
line_height = 320; line_height = 320;
line_offset = 160 - line_height / 2; line_offset = 160 - line_height / 2;
while (i < 8) float gap = 1;
{ float old_y = 0;
float myy = 0;
printf("mp= %f modulo texture_size= %d\n", ray.mp, ((int)ray.mp / 4) % texture_size);
//while (x < 8)
//{
y = 0; y = 0;
my = 0;
myy = 0;
//double step = 1.0 * texHeight / lineHeight;
gap = (texture_size / line_height);
old_y = 0;
//ray.ty = ;
mx = ((int)ray.mp) % texture_size;
//int texX = int(wallX * double(texWidth));
//if(side == 0 && rayDirX > 0) texX = texWidth - texX - 1;
//if(side == 1 && rayDirY < 0) texX = texWidth - texX - 1;
while (y < line_height) while (y < line_height)
{ {
if (type) //if (y > old_y + (16 / line_height)/*(line_height / 16)*/)
mlx_pixel_put(img->mlx, img->mlx_win, ray * 8 + 530 + i, y + line_offset , get_red()); //{
else // gap++;
mlx_pixel_put(img->mlx, img->mlx_win, ray * 8 + 530 + i, y + line_offset , get_dark_red()); // old_y = y;
//}
//(x * 16 + y)* 3 + 1;
//mp= x du mur
//my = gap;
myy += gap;
my = (int)myy;//gap;
//mx = ((int)ray.mp) % 16;
//ray.pixel = ((((int)ray.mp) % 16) * 16 + gap /*+ x*/) * 3 + 1 ;
ray.pixel = ((my) * texture_size + mx)* 3 - 1;
x = -1;
//printf("my= %d mx= %d pixel= %d\n", my, mx, ray.pixel);
color = get_color(img->map.texture.north[ray.pixel], img->map.texture.north[ray.pixel + 1], img->map.texture.north[ray.pixel + 2]);
while (++x < 8)
{
if (ray.wall_type)
mlx_pixel_put(img->mlx, img->mlx_win, ray.index * 8 + 530 + x, y + line_offset , color);
else
mlx_pixel_put(img->mlx, img->mlx_win, ray.index * 8 + 530 + x, y + line_offset , (color >> 1) & 8355711);
}
//printf("pixel=%d ", ray.pixel);
//if (ray.wall_type)
int l = 0;
//while (l < 8)
//{
//x = -1;
//while (++x < 8)
//l++;
//}
//else
// mlx_pixel_put(img->mlx, img->mlx_win, ray.index * 8 + 530 + x, y + line_offset , color);
y++; y++;
} }
i++; x++;
} //}
printf("\n");
//ray.tx++;
} }
void draw_ray(t_data *img) void draw_ray(t_data *img)
@ -111,6 +198,7 @@ void draw_ray(t_data *img)
int mp = 0; int mp = 0;
(void)dist_f; (void)dist_f;
printf("---NEW RAY----\n\n");
//printf("\nENTER DRAW RAY\n"); //printf("\nENTER DRAW RAY\n");
//while (++k < ft_strlen(img->map.simple_map)) //while (++k < ft_strlen(img->map.simple_map))
// printf("%d--- %c\n", k, img->map.simple_map[k]); // printf("%d--- %c\n", k, img->map.simple_map[k]);
@ -120,6 +208,7 @@ void draw_ray(t_data *img)
//ray_angle = reset_angle(img->player.angle); //ray_angle = reset_angle(img->player.angle);
while (++nb_ray < 60) while (++nb_ray < 60)
{ {
//if (nb_ray) //if (nb_ray)
// ray_angle -= 30; // ray_angle -= 30;
count = 0; count = 0;
@ -203,20 +292,20 @@ void draw_ray(t_data *img)
count = 8; count = 8;
}//looking straight left or right }//looking straight left or right
while (count<8) while (count < 8)
{ {
//printf("ray_y= %f ray_x= %f\n", ray_y, ray_x); //printf("ray_y= %f ray_x= %f\n", ray_y, ray_x);
mx = (int)(ray_x)>>6; mx = (int)(ray_x)>>6;
my = (int)(ray_y)>>6; my = (int)(ray_y)>>6;
mp = my * img->map.x + mx; mp = my * img->map.x + mx;
//printf("mx=%d my=%d mp= %d\n", mx, my, mp); //printf("mx=%d my=%d mp= %d\n", mx, my, mp);
if (mp > 0 && mp < img->map.x * img->map.y && img->map.simple_map[mp] == '1') if (mp > 0 && mp < img->map.x * img->map.y && img->map.simple_map[mp] == '1')//hit
{ {
count = 8; count = 8;
//printf ("horizontal wall\n"); //printf ("horizontal wall\n");
//printf("case: x= %d, y= %d mp= %d\n", mx, my, mp); //printf("case: x= %d, y= %d mp= %d\n", mx, my, mp);
dist_h = cos(deg_to_rad(ray_angle)) * (ray_x - img->player.x) - sin(deg_to_rad(ray_angle)) * (ray_y - img->player.y); dist_h = cos(deg_to_rad(ray_angle)) * (ray_x - img->player.x) - sin(deg_to_rad(ray_angle)) * (ray_y - img->player.y);
}//hit }
else else
{ {
ray_x += next_x; ray_x += next_x;
@ -225,27 +314,59 @@ void draw_ray(t_data *img)
} //check next horizontal } //check next horizontal
} }
//printf("dist_h= %f dist_v= %f\n", dist_h, dist_v); //printf("dist_h= %f dist_v= %f\n", dist_h, dist_v);
vx = cos(deg_to_rad(ray_angle)); //vx = cos(deg_to_rad(ray_angle));
vy = -sin(deg_to_rad(ray_angle)); //vy = -sin(deg_to_rad(ray_angle));
//printf("player.vx= %f vx= %f player.vy= %f vy= %f\n", img->player.vx, vx, img->player.vy, vy); //printf("vx= %f vy= %f\n rx= %f ry= %f", vx, vy, ray_x, ray_y);
int wall_type; int wall_type;
ray ray_info;
if (dist_h != -1 && (dist_h < dist_v || dist_v == -1)) if (dist_h != -1 && (dist_h < dist_v || dist_v == -1))
{ {
print_ray2(img, vx, vy, fabs(dist_h)); print_ray2(img, cos(deg_to_rad(ray_angle)), -sin(deg_to_rad(ray_angle)), fabs(dist_h));
dist_f = dist_h; dist_f = dist_h;
printf("rx= %f ry= %f\n", ray_x, ray_y);
ray_info.mp = ray_x;
wall_type = 0; wall_type = 0;
} }
else if (dist_v != -1) else if (dist_v != -1)
{ {
dist_f = dist_v; dist_f = dist_v;
print_ray2(img, vx, vy, fabs(dist_v)); printf("vx= %f vy= %f\n", vx, vy);
ray_x = vx;
ray_y = vy;
ray_info.mp = vy;
print_ray2(img, cos(deg_to_rad(ray_angle)), -sin(deg_to_rad(ray_angle)), fabs(dist_v));
wall_type = 1; wall_type = 1;
} }
else else
dist_f = 0; dist_f = 0;
float tx;
float ty_off = 0;
float ty_step = 32.0/dist_f;
//float ty = ;
/*if(dist_f > 640)
{
ty_off = (dist_f - 320) / 2.0;
dist_f = 320;
} */
//ty = ty_off * 16.0 / dist_f; ;//+hmt*32;
//tx = (int) (ray_y / 2.0) % 16;
tx=(int)(ray_x/2.0) % 32; if(ray_angle>180){ tx=31-tx;}
//if(ray_angle > 180)
// tx = 15 - tx;
//ray_info.pixel = ((int)ty * 16 + (int)tx) * 3 + 3;
printf("nb_ray= %d\n", nb_ray);
ray_info.ty = ray_y;
ray_info.tx = ray_x;
ray_info.index = nb_ray;
ray_info.wall_type = wall_type;
int ca = reset_angle(img->player.angle - ray_angle); //fisheye int ca = reset_angle(img->player.angle - ray_angle); //fisheye
dist_f = dist_f * cos(deg_to_rad(ca)); //fisheye dist_f = dist_f * cos(deg_to_rad(ca));
draw_ray3d(img, dist_f, nb_ray, wall_type); ray_info.dist = dist_f; //fisheye
draw_ray3d(img, ray_info);
ray_angle = reset_angle(ray_angle - 1); ray_angle = reset_angle(ray_angle - 1);
} }
} }

86
srcs/get_texture_array.c Normal file
View File

@ -0,0 +1,86 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_texture_array.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/06 16:10:11 by apommier #+# #+# */
/* Updated: 2022/05/17 23:49:38 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/Cub3D.h"
int *get_texture(int type)
{
int fd;
int *ret;
//int start = 1;
int count;
int len;
char *swap = 0;
//line = 0;
count = 0;
fd = open("./sprite/brick_wall.ppm", O_RDONLY);
while (swap || !count)
{
if (swap)
free(swap);
count++;
swap = get_next_line(fd);
}
close(fd);
ret = ft_calloc(sizeof(int), count + 1);
//char **ret2 = ft_calloc(sizeof(ret), count + 1);
fd = open("./sprite/brick_wall.ppm", O_RDONLY);
if (!ret)
return (0);
ret[count] = -1;
len = count;
count = 0;
while (swap || !count)
{
if (swap)
free(swap);
swap = get_next_line(fd);
//printf("swap= %s\n", swap);
ret[count] = (int)ft_atoi(swap);
count++;
}
int k = 0;
/*while (len)
{
printf("nbr= %d", ret[len--]);
}*/
printf("nrb3= %d\n", ret[1]);
//print_double_fd(ret2, 1);
//free_double(ret2);
close(fd);
/*int v = 0;
int b = 0;
int pixel = 0;
//printf("pixel=%d ", ray.pixel);
while (v < 16)
{
while (b < 16)
{
pixel = (v * 3) + b;
int color = get_color(ret[pixel], ret[pixel + 1], ret[pixel + 2]);
mlx_pixel_put(img->mlx, img->mlx_win, v, b, color);
b++;
}
v++;
}*/
return (ret);
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 16:42:55 by apommier #+# #+# */ /* Created: 2022/02/13 16:42:55 by apommier #+# #+# */
/* Updated: 2022/05/05 02:55:40 by apommier ### ########.fr */ /* Updated: 2022/05/17 23:52:27 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -189,16 +189,20 @@ void print_player(player player, t_data *img)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
t_data img; t_data img;
sprite texture;
texture.north = get_texture(1);
if (argc != 2) if (argc != 2)
ft_error("Error: bad number of arguments, only need a map"); ft_error("Error: bad number of arguments, only need a map");
img.mlx = mlx_init(); img.mlx = mlx_init();
if (!img.mlx) if (!img.mlx)
ft_error("Error: mlx_init fail"); ft_error("Error: mlx_init fail");
img.map = set_map(argv); img.map = set_map(argv);
img.mlx_win = mlx_new_window(img.mlx, 1024, 512, "Cub3D"); img.mlx_win = mlx_new_window(img.mlx, 1024, 512, "Cub3D");
//img->player.x=150; img->player.y=400; pa=90; //img->player.x=150; img->player.y=400; pa=90;
//pdx=cos(deg_to_rad(pa)); pdy=-sin(deg_to_rad(pa)); //pdx=cos(deg_to_rad(pa)); pdy=-sin(deg_to_rad(pa));
img.map.texture = texture;
img.player.x = 150; img.player.x = 150;
img.player.y = 400; img.player.y = 400;
img.player.angle = 90; img.player.angle = 90;
@ -206,6 +210,12 @@ int main(int argc, char **argv)
img.player.vy = sin(deg_to_rad(img.player.angle)); img.player.vy = sin(deg_to_rad(img.player.angle));
print_map(img.map, &img); print_map(img.map, &img);
print_player(img.player, &img); print_player(img.player, &img);
//print_ray(&img); //print_ray(&img);
mlx_hook(img.mlx_win, 2, 1L << 0, &key_press, &img); mlx_hook(img.mlx_win, 2, 1L << 0, &key_press, &img);
mlx_hook(img.mlx_win, 17, 0L, &quit_game, &img); mlx_hook(img.mlx_win, 17, 0L, &quit_game, &img);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/13 23:37:02 by apommier #+# #+# */ /* Created: 2022/02/13 23:37:02 by apommier #+# #+# */
/* Updated: 2022/05/05 03:05:12 by apommier ### ########.fr */ /* Updated: 2022/05/18 00:08:25 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -130,8 +130,35 @@ int key_press(int code, t_data *img)
print_player(img->player, img); print_player(img->player, img);
print_back(img); print_back(img);
draw_ray(img); draw_ray(img);
print_ray(img); print_ray(img);
} }
} }
int y = 0;
int x = 0;
int pixel = 0;
int color;
//printf("pixel=%d ", ray.pixel);
while (x < 16)
{
y = 0;
while (y < 16)
{
//printf("test\n");
pixel = (x * 16 + y)* 3 + 1;
color = get_color(img->map.texture.north[pixel], img->map.texture.north[pixel + 1], img->map.texture.north[pixel + 2]);
mlx_pixel_put(img->mlx, img->mlx_win, x, y, color);
y++;
}
x++;
}
return (1); return (1);
} }