Skip to content

Commit 0751857

Browse files
author
redone
committed
refactoring: naming
1 parent 2a52287 commit 0751857

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

Diff for: includes/cray.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,13 @@ void init(t_seer *pSeer);
160160
void position_direction(t_seer *pSeer, t_camera *pCamera,
161161
int xPixel);
162162
void calculate_initial_distance(t_seer *pSeer, t_camera *pCamera);
163-
void dda(t_seer *pSeer, t_camera *pCamera, int *side);
163+
void dda_loop(t_seer *pSeer, t_camera *pCamera, int *side);
164164
void vertline(t_seer *pSeer, t_camera *pCamera,
165165
int side);
166166
unsigned int get_image_color(mlx_image_t *image, t_point p);
167167
void set_texture_params(t_seer *pSeer, int xPixel);
168168
void set_env(t_seer *pSeer);
169169
void draw_3d_scene(t_seer *pSeer);
170-
void fill_texture_buffer(t_seer *pSeer, int x,
170+
void fill_texture_buffer(t_seer *pSeer, int xPixel,
171171
int drawStart, int drawEnd);
172172
#endif // !CRAY_H

Diff for: srcs/hooks.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void draw_hook(void *args)
3838
{
3939
position_direction(seer, &seer->camera, x);
4040
calculate_initial_distance(seer, &seer->camera);
41-
dda(seer, &seer->camera, &seer->texture.side);
41+
dda_loop(seer, &seer->camera, &seer->texture.side);
4242
vertline(seer, &seer->camera, seer->texture.side);
4343
set_texture_params(seer, x);
4444
}

Diff for: srcs/raycasting.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ void position_direction(t_seer *pSeer, t_camera *pCamera, int xPixel)
1313
pSeer->dda.scaling_factor.y = sqrt(1 + (pCamera->direction.x * pCamera->direction.x) / (pCamera->direction.y * pCamera->direction.y));
1414
}
1515

16+
/*
17+
adding 1.0 to the player position
18+
is to avoid the case where the player is on the grid.
19+
*/
1620
void calculate_initial_distance(t_seer *pSeer, t_camera *pCamera)
1721
{
1822
if (pCamera->direction.x < 0)
@@ -41,7 +45,7 @@ void calculate_initial_distance(t_seer *pSeer, t_camera *pCamera)
4145
}
4246
}
4347

44-
void dda(t_seer *pSeer, t_camera *pCamera, int *side)
48+
void dda_loop(t_seer *pSeer, t_camera *pCamera, int *side)
4549
{
4650
while (true)
4751
{
@@ -62,6 +66,13 @@ void dda(t_seer *pSeer, t_camera *pCamera, int *side)
6266
}
6367
}
6468

69+
/*
70+
* ( 1 - step_x ) / 2 = 1 : if step_x = -1 and 0 : if step_x = 1
71+
why? because if step_x = -1, then the player is on the right side of the wall,
72+
1 is already added in the calculation of the initial distance.
73+
* mapX - playerX + (1 - stepX) / 2 is the number of square the player has to crossed in the X axis.
74+
pCamera.direction is the ray direction vector so we can get the perpendicular wall distance.
75+
*/
6576
void vertline(t_seer *pSeer, t_camera *pCamera, int side)
6677
{
6778
if (side == HORIZONTAL)
@@ -70,7 +81,7 @@ void vertline(t_seer *pSeer, t_camera *pCamera, int side)
7081
pCamera->wall_distance = fabs((pCamera->map_y - pSeer->player.position.y + (1 - pSeer->dda.step_y) / 2) / pCamera->direction.y);
7182
if (pCamera->wall_distance == 0)
7283
pCamera->wall_distance = 0.0001;
73-
pSeer->vertline.height = SCREEN_HEIGHT / pCamera->wall_distance;
84+
pSeer->vertline.height = fabs(SCREEN_HEIGHT / pCamera->wall_distance);
7485
pSeer->vertline.start = -pSeer->vertline.height / 2 + SCREEN_HEIGHT / 2;
7586
if (pSeer->vertline.start < 0)
7687
pSeer->vertline.start = 0;

Diff for: srcs/textures.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "../includes/cray.h"
22

3+
// the floor is used to get the value between 0 and 1
34
void set_texture_params(t_seer *pSeer, int xPixel)
45
{
56
double wall_x;
@@ -16,7 +17,7 @@ void set_texture_params(t_seer *pSeer, int xPixel)
1617
pSeer->texture.tex_x = CUBE_SIZE - pSeer->texture.tex_x - 1;
1718
if (pSeer->texture.side == VERTICAL && pSeer->camera.direction.y < 0)
1819
pSeer->texture.tex_x = CUBE_SIZE - pSeer->texture.tex_x - 1;
19-
pSeer->texture.step = 1.0 * CUBE_SIZE / pSeer->vertline.height;
20+
pSeer->texture.step = CUBE_SIZE / (double)pSeer->vertline.height;
2021
pSeer->texture.pos = (pSeer->vertline.start - SCREEN_HEIGHT / 2
2122
+ pSeer->vertline.height / 2) * pSeer->texture.step;
2223
fill_texture_buffer(pSeer, xPixel, pSeer->vertline.start,
@@ -66,7 +67,7 @@ void draw_3d_scene(t_seer *pSeer)
6667
}
6768
}
6869

69-
void fill_texture_buffer(t_seer *pSeer, int x, int drawStart, int drawEnd)
70+
void fill_texture_buffer(t_seer *pSeer, int xPixel, int drawStart, int drawEnd)
7071
{
7172
int tex_y;
7273
int y;
@@ -79,16 +80,16 @@ void fill_texture_buffer(t_seer *pSeer, int x, int drawStart, int drawEnd)
7980
pSeer->texture.pos += pSeer->texture.step;
8081
cord = (t_point){pSeer->texture.tex_x, tex_y};
8182
if (pSeer->texture.side == HORIZONTAL && pSeer->camera.direction.x > 0)
82-
pSeer->texture.buffer[y][x] = \
83+
pSeer->texture.buffer[y][xPixel] = \
8384
get_image_color(pSeer->map_info.south_image, cord);
8485
else if (pSeer->texture.side == HORIZONTAL)
85-
pSeer->texture.buffer[y][x] = \
86+
pSeer->texture.buffer[y][xPixel] = \
8687
get_image_color (pSeer->map_info.north_image, cord);
8788
else if (pSeer->camera.direction.y > 0)
88-
pSeer->texture.buffer[y][x] = \
89+
pSeer->texture.buffer[y][xPixel] = \
8990
get_image_color (pSeer->map_info.east_image, cord);
9091
else
91-
pSeer->texture.buffer[y][x] = \
92+
pSeer->texture.buffer[y][xPixel] = \
9293
get_image_color (pSeer->map_info.west_image, cord);
9394
}
9495
}

Diff for: srcs/utils.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ unsigned int get_image_color(mlx_image_t *image, t_point p)
55
unsigned int color;
66
unsigned int index;
77

8-
index = (p.y % image->height * image->width + p.x % image->width)
8+
index = ((p.y % image->height) * image->width + (p.x % image->width))
99
* sizeof(unsigned int);
1010
color = image->pixels[index] << 24;
1111
color |= image->pixels[index + 1] << 16;

0 commit comments

Comments
 (0)