Skip to content

Commit

Permalink
Fixing mirrored texture in directions SOUTH and WEST. To make this fi…
Browse files Browse the repository at this point in the history
…x, I need to : define exactly what was the side hit(N,S,W,E); change enum to fit this; create a function to set this; then I create two functions to load the textures from end to beggin in S and W directions, and load from beggin to end in N and E directions.
  • Loading branch information
pedromelocf committed Dec 14, 2024
1 parent db5fa67 commit 84aa904
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 36 deletions.
8 changes: 5 additions & 3 deletions includes/cub3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,14 @@
# define LEFT MLX_KEY_LEFT
# define MOVE_SPEED_MULTI 4.0
# define ROTATE_SPEED_MULTI 1.5
# define COLISION_DISTANCE_MULTI 8.0
# define COLISION_DISTANCE_MULTI 10.0

enum e_side
{
WEST_EAST,
NORTH_SOUTH
NORTH,
SOUTH,
WEST,
EAST
};

typedef struct s_loaded_textures
Expand Down
28 changes: 25 additions & 3 deletions sources/dda.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,60 @@
#include "../includes/cub3d.h"

static void set_perp_wall_distance(t_cub3d *s_cub3d);
static void set_side_hit(t_cub3d *s_cub3d, int side);

void dda(t_cub3d *s_cub3d)
{
int hit;
int side;

hit = 0;
side = 0;
while (hit == 0)
{
if (s_cub3d->dda.side_dist_x < s_cub3d->dda.side_dist_y)
{
s_cub3d->dda.side_dist_x += s_cub3d->dda.delta_dist_x;
s_cub3d->dda.map_x += s_cub3d->dda.step_x;
s_cub3d->rays.side_hit = WEST_EAST;
side = 1;
}
else
{
s_cub3d->dda.side_dist_y += s_cub3d->dda.delta_dist_y;
s_cub3d->dda.map_y += s_cub3d->dda.step_y;
s_cub3d->rays.side_hit = NORTH_SOUTH;
side = 2;
}
if (s_cub3d->map[s_cub3d->dda.map_y][s_cub3d->dda.map_x] == '1')
hit = 1;
}
set_side_hit(s_cub3d, side);
set_perp_wall_distance(s_cub3d);
}

static void set_perp_wall_distance(t_cub3d *s_cub3d)
{
if (s_cub3d->rays.side_hit == WEST_EAST)
if (s_cub3d->rays.side_hit == WEST || s_cub3d->rays.side_hit == EAST)
s_cub3d->rays.perp_wall_dist = (s_cub3d->dda.side_dist_x
- s_cub3d->dda.delta_dist_x);
else
s_cub3d->rays.perp_wall_dist = (s_cub3d->dda.side_dist_y
- s_cub3d->dda.delta_dist_y);
}

static void set_side_hit(t_cub3d *s_cub3d, int side)
{
if (side == 1)
{
if (s_cub3d->rays.ray_dir_x > 0)
s_cub3d->rays.side_hit = EAST;
else if (s_cub3d->rays.ray_dir_x < 0)
s_cub3d->rays.side_hit = WEST;
}
else if (side == 2)
{
if (s_cub3d->rays.ray_dir_y < 0)
s_cub3d->rays.side_hit = NORTH;
else if (s_cub3d->rays.ray_dir_y > 0)
s_cub3d->rays.side_hit = SOUTH;
}
}
4 changes: 2 additions & 2 deletions sources/draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void draw_texturized_line(int x, t_cub3d *s_cub3d)
while (i < s_cub3d->wall.draw_end)
{
s_cub3d->textures.texture_y = (int)s_cub3d->textures.texture_pos;
s_cub3d->textures.texture_pos += s_cub3d->textures.step;
s_cub3d->textures.texture_pos += s_cub3d->textures.step;
mlx_put_pixel(s_cub3d->image, x, i, get_color(s_cub3d));
i++;
}
Expand All @@ -37,7 +37,7 @@ static uint32_t get_color(t_cub3d *s_cub3d)
texture_y = s_cub3d->textures.texture_y;
texture_x = s_cub3d->textures.texture_x;
pixel = &s_cub3d->textures.wall_texture->pixels[(texture_y
* s_cub3d->textures.wall_texture->width + texture_x)
* s_cub3d->textures.wall_texture->width + texture_x)
* s_cub3d->textures.wall_texture->bytes_per_pixel];
return (pixel[0] << 24 | pixel[1] << 16 | pixel[2] << 8 | pixel[3]);
}
Expand Down
44 changes: 25 additions & 19 deletions sources/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,37 @@ int main()
t_cub3d s_cub3d = {
{"220,100,0", "225,30,0"},

{"1111111111111111111111",
"1000000000N00000000001",
"1000000000000000000001",
"1000000000000000000001",
"1000000000000000000001",
"1000000000000000000001",
"1000000111111111000001",
"1000000000000000000001",
"1000000000000000000001",
"1000000000000000000001",
"1000000000000000000001",
"1000000000000000000001",
"1111111111111111111111",
},
// {"1111111111111111111111",
// "1000000000N00000000001",
// "1000000000000000000001",
// "1000000000000000000001",
// "1000000000000000000001",
// "1000000000000000000001",
// "1000000111111111000001",
// "1000000000000000000001",
// "1000000000000000000001",
// "1000000000000000000001",
// "1000000000000000000001",
// "1000000000000000000001",
// "1111111111111111111111",
// },

{4, 5},
{"11111",
"100001",
"100N01",
"111111",
},

{1.5, 1.5},
{1, 0},
{0, 0.66},
NULL,
NULL,

{{mlx_load_png("./textures/rgb.png"),
mlx_load_png("./textures/fire.png"),
mlx_load_png("./textures/fire.png"),
mlx_load_png("/nfs/homes/pmelo-ca/Downloads/fractal.png"),
{{mlx_load_png("./textures/directions/N.png"),
mlx_load_png("./textures/directions/S.png"),
mlx_load_png("./textures/directions/W.png"),
mlx_load_png("./textures/directions/E.png"),
},
NULL,
0, 0, 0, 0, 0,
Expand Down
35 changes: 27 additions & 8 deletions sources/textures.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

static void set_texture_info(t_cub3d *s_cub3d);
static void set_wall_texture(t_cub3d *s_cub3d);
static void set_texture_north_east(t_cub3d *s_cub3d);
static void set_texture_south_west(t_cub3d *s_cub3d);

void load_texture_info(t_cub3d *s_cub3d)
{
Expand All @@ -23,25 +25,42 @@ void load_texture_info(t_cub3d *s_cub3d)

static void set_wall_texture(t_cub3d *s_cub3d)
{
if (s_cub3d->rays.side_hit == NORTH_SOUTH && s_cub3d->rays.ray_dir_y < 0)
if (s_cub3d->rays.side_hit == NORTH)
s_cub3d->textures.wall_texture = s_cub3d->textures.loaded_textures.no;
else if (s_cub3d->rays.side_hit == NORTH_SOUTH
&& s_cub3d->rays.ray_dir_y > 0)
s_cub3d->textures.wall_texture = s_cub3d->textures.loaded_textures.so;
else if (s_cub3d->rays.side_hit == WEST_EAST && s_cub3d->rays.ray_dir_x > 0)
else if (s_cub3d->rays.side_hit == SOUTH)
s_cub3d->textures.wall_texture = s_cub3d->textures.loaded_textures.so;
else if (s_cub3d->rays.side_hit == EAST)
s_cub3d->textures.wall_texture = s_cub3d->textures.loaded_textures.ea;
else if (s_cub3d->rays.side_hit == WEST_EAST && s_cub3d->rays.ray_dir_x < 0)
else if (s_cub3d->rays.side_hit == WEST)
s_cub3d->textures.wall_texture = s_cub3d->textures.loaded_textures.we;
//invert texture from south and west
}

static void set_texture_info(t_cub3d *s_cub3d)
{
if (s_cub3d->rays.side_hit == NORTH || s_cub3d->rays.side_hit == EAST)
set_texture_north_east(s_cub3d);
else if (s_cub3d->rays.side_hit == SOUTH || s_cub3d->rays.side_hit == WEST)
set_texture_south_west(s_cub3d);
}

static void set_texture_north_east(t_cub3d *s_cub3d)
{
s_cub3d->textures.step = 1.0 * s_cub3d->textures.wall_texture->height
/ s_cub3d->wall.line_height;
/ s_cub3d->wall.line_height;
s_cub3d->textures.texture_x = ((int)(s_cub3d->textures.wall_hit_x
* (double)s_cub3d->textures.wall_texture->width));
s_cub3d->textures.texture_pos = (s_cub3d->wall.draw_start - SCREEN_HEIGHT
/ 2 + s_cub3d->wall.line_height / 2) * s_cub3d->textures.step;
s_cub3d->textures.texture_y = ((int)s_cub3d->textures.texture_pos);
}

static void set_texture_south_west(t_cub3d *s_cub3d)
{
s_cub3d->textures.step = 1.0 * s_cub3d->textures.wall_texture->height
/ s_cub3d->wall.line_height;
s_cub3d->textures.texture_x = s_cub3d->textures.wall_texture->width - 1
- ((int)(s_cub3d->textures.wall_hit_x * (double)s_cub3d->textures.wall_texture->width));
s_cub3d->textures.texture_pos = (s_cub3d->wall.draw_start - SCREEN_HEIGHT
/ 2 + s_cub3d->wall.line_height / 2) * s_cub3d->textures.step;
s_cub3d->textures.texture_y = ((int)s_cub3d->textures.texture_pos);
}
2 changes: 1 addition & 1 deletion sources/wall_calculations.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static void draw_end_calculation(t_cub3d *s_cub3d)

void set_wall_hit_x(t_cub3d *s_cub3d)
{
if (s_cub3d->rays.side_hit == WEST_EAST)
if (s_cub3d->rays.side_hit == WEST || s_cub3d->rays.side_hit == EAST)
s_cub3d->textures.wall_hit_x = s_cub3d->player_pos.y
+ s_cub3d->rays.perp_wall_dist * s_cub3d->rays.ray_dir_y;
else
Expand Down

0 comments on commit 84aa904

Please sign in to comment.