Browse Source

fix: no more crash

master
narnaud 3 years ago
parent
commit
5b86ddcaea
  1. 7
      Makefile
  2. 11
      includes/cub3d.h
  3. 37
      sources/render.c

7
Makefile

@ -1,5 +1,6 @@
NAME = cub3d
LIBFT = libft.a
MLX = includes/mlx.h
SRCS = sources/main.c sources/parsing.c sources/getline.c sources/utils.c
SRCS += sources/render.c
@ -15,7 +16,7 @@ endif
ifeq ($(UNAME_S), Darwin)
LFLAGS = -lmlx -framework OpenGL -framework AppKit -L. -lft
endif
CFLAGS = -Werror -Wall -Wextra -O3 -ffast-math -funsafe-math-optimizations
CFLAGS = -Werror -Wall -Wextra #-O3 -ffast-math -funsafe-math-optimizations
%.o:%.c
${CC} ${CFLAGS} -c $< -o ${<:.c=.o}
@ -29,8 +30,10 @@ $(LIBFT):
cp libft/libft.a .
cp libft/libft.h includes/
$(MLX):
cp mlx/mlx.h includes/
$(NAME): $(LIBFT) $(OBJS)
$(NAME): $(LIBFT) $(MLX) $(OBJS)
echo Making Cub3D...
${CC} ${OBJS} -o ${NAME} ${LFLAGS}
echo

11
includes/cub3d.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/21 19:24:12 by narnaud #+# #+# */
/* Updated: 2022/05/31 16:17:03 by narnaud ### ########.fr */
/* Updated: 2022/05/31 18:59:46 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -52,6 +52,15 @@ typedef struct s_vector_d
double y;
} t_vec_d;
typedef struct s_rectangle
{
int startX;
int startY;
int sizeX;
int sizeY;
int color;
} t_rectangle;
typedef struct s_env
{
void *mlx;

37
sources/render.c

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/31 09:33:02 by narnaud #+# #+# */
/* Updated: 2022/05/31 18:11:15 by narnaud ### ########.fr */
/* Updated: 2022/06/01 12:22:39 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,21 +35,23 @@ void draw_vert(t_env *env, int x, int *startEnd, int color)
}
}
void draw_square(t_env *env, t_vec_d vec, int size, int color)
void draw_rectangle(t_env *env, t_rectangle rect)
{
int step_x;
int step_y;
step_y = 0;
while (step_y < size)
while (step_y < rect.sizeY)
{
step_x = 0;
while (step_x < size)
while (step_x < rect.sizeX)
{
if (step_x == size - 1 || step_y == size - 1)
env->buffer[(env->line_bytes * (int)(vec.y * size + step_y)) + (int)(vec.x * size + step_x)] = 0;
if (step_x == rect.sizeX - 1 || step_y == rect.sizeY - 1)
env->buffer[(env->line_bytes * (int)(rect.startY + step_y))
+ (int)(rect.startX + step_x)] = 0;
else
env->buffer[(env->line_bytes * (int)(vec.y * size + step_y)) + (int)(vec.x * size + step_x)] = color;
env->buffer[(env->line_bytes * (int)(rect.startY + step_y))
+ (int)(rect.startX+ step_x)] = rect.color;
step_x++;
}
step_y++;
@ -60,7 +62,6 @@ void render_minimap(t_env *env)
{
char **map;
t_vec vec;
t_vec_d vecd;
vec.y = 0;
map = env->map;
@ -69,17 +70,15 @@ void render_minimap(t_env *env)
vec.x = 0;
while (map[vec.y][vec.x])
{
vecd = (t_vec_d){vec.x, vec.y};
if (map[vec.y][vec.x] == '0')
draw_square(env, vecd, 6, 39424);
draw_rectangle(env, (t_rectangle){vec.x * 6, vec.y * 6, 6, 6, 39424});
else if (map[vec.y][vec.x] == '1')
draw_square(env, vecd, 6, 11885067);
draw_rectangle(env, (t_rectangle){vec.x * 6, vec.y * 6, 6, 6, 11885067});
vec.x++;
}
vec.y++;
}
vecd = (t_vec_d){env->playerPos.x, env->playerPos.y};
draw_square(env, vecd, 6, 255);
draw_rectangle(env, (t_rectangle){env->playerPos.x * 6 - 2,env->playerPos.y * 6 - 2, 4, 4, 255});
}
double get_wall_dist(t_env *env, int x)
@ -93,10 +92,12 @@ double get_wall_dist(t_env *env, int x)
int hit;
int side;
if (env->map[(int)env->playerPos.y][(int)env->playerPos.x] > '0')
return (1e-30);
camX = 2 * x / (double)WIN_X_SZ - 1;
set_vec(&ray, env->playerDir.x + env->camPlan.x * camX, env->playerDir.y + env->camPlan.y * camX);
cell.x = round(env->playerPos.x);
cell.y = round(env->playerPos.y);
cell.x = (int)(env->playerPos.x);
cell.y = (int)(env->playerPos.y);
if (ray.x == 0)
dDist.x = 1e30;
else
@ -143,6 +144,10 @@ double get_wall_dist(t_env *env, int x)
if (env->map[cell.y][cell.x] > '0')
hit = 1;
}
if (DEBUG)
{
//printf("X:%d - sideDist: %f, %f. deltaDist: %f, %f.\n", x, sDist.x, sDist.y, dDist.x, dDist.y);
}
if (side == 0)
return (sDist.x - dDist.x);
else
@ -161,6 +166,8 @@ void render_view(t_env *env)
{
perpWallDist = get_wall_dist(env, x);
lineHeight = (int)(WIN_Y_SZ / perpWallDist);
if (lineHeight < 0)
lineHeight = 0; // = WIN_Y_SZ
startEnd[0] = -lineHeight / 2 + WIN_Y_SZ / 2;
if (startEnd[0] < 0)
startEnd[0] = 0;

Loading…
Cancel
Save