mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 01:48:08 +01:00
fix: prevent crash due to out-of-bounds when game lags
This commit is contained in:
parent
1d6d97ef25
commit
a18c115ef1
7 changed files with 90 additions and 26 deletions
1
Makefile
1
Makefile
|
|
@ -15,6 +15,7 @@ SOURCEFILES = \
|
||||||
src/draw/drawutils.c \
|
src/draw/drawutils.c \
|
||||||
src/main.c \
|
src/main.c \
|
||||||
src/map/checkers.c \
|
src/map/checkers.c \
|
||||||
|
src/map/collision.c \
|
||||||
src/map/forbidden_characters.c \
|
src/map/forbidden_characters.c \
|
||||||
src/map/populate_map.c \
|
src/map/populate_map.c \
|
||||||
src/map/setters.c \
|
src/map/setters.c \
|
||||||
|
|
|
||||||
|
|
@ -6,16 +6,19 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/17 14:28:56 by kcolin #+# #+# */
|
/* Created: 2025/07/17 14:28:56 by kcolin #+# #+# */
|
||||||
/* Updated: 2025/07/17 15:48:56 by kcolin ### ########.fr */
|
/* Updated: 2025/08/06 11:43:12 by kcolin ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "img_data.h"
|
#include "img_data.h"
|
||||||
|
#include "../consts.h"
|
||||||
|
|
||||||
void my_mlx_pixel_put(t_img_data *data, int x, int y, int color)
|
void my_mlx_pixel_put(t_img_data *data, int x, int y, int color)
|
||||||
{
|
{
|
||||||
char *dst;
|
char *dst;
|
||||||
|
|
||||||
|
if (x < 0 || y < 0 || x >= HEIGHT || y >= WIDTH)
|
||||||
|
return ;
|
||||||
dst = data->addr + (y * data->line_length + x * (data->bits_per_pixel / 8));
|
dst = data->addr + (y * data->line_length + x * (data->bits_per_pixel / 8));
|
||||||
*(unsigned int *)dst = color;
|
*(unsigned int *)dst = color;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
38
src/map/collision.c
Normal file
38
src/map/collision.c
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* collision.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/08/06 11:25:05 by kcolin #+# #+# */
|
||||||
|
/* Updated: 2025/08/06 11:49:00 by kcolin ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "collision.h"
|
||||||
|
#include "../../libft/includes/libft.h"
|
||||||
|
|
||||||
|
static bool out_of_bounds(t_mapdata *data, int x, int y)
|
||||||
|
{
|
||||||
|
if (x < 0
|
||||||
|
|| y < 0
|
||||||
|
|| x >= data->mapheight
|
||||||
|
|| y > (int)ft_strlen(data->map[x]))
|
||||||
|
return (true);
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool blocks_movement(t_mapdata *data, int x, int y)
|
||||||
|
{
|
||||||
|
if (out_of_bounds(data, x, y) || data->map[x][y] == '1')
|
||||||
|
return (true);
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool blocks_view(t_mapdata *data, int x, int y)
|
||||||
|
{
|
||||||
|
if (out_of_bounds(data, x, y) || data->map[x][y] == '1')
|
||||||
|
return (true);
|
||||||
|
return (false);
|
||||||
|
}
|
||||||
21
src/map/collision.h
Normal file
21
src/map/collision.h
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* collision.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/08/06 11:24:10 by kcolin #+# #+# */
|
||||||
|
/* Updated: 2025/08/06 11:28:57 by kcolin ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef COLLISION_H
|
||||||
|
# define COLLISION_H
|
||||||
|
|
||||||
|
# include "mapdata.h"
|
||||||
|
|
||||||
|
bool blocks_movement(t_mapdata *data, int x, int y);
|
||||||
|
bool blocks_view(t_mapdata *data, int x, int y);
|
||||||
|
|
||||||
|
#endif // COLLISION_H
|
||||||
|
|
@ -6,79 +6,80 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/08/05 12:53:06 by kcolin #+# #+# */
|
/* Created: 2025/08/05 12:53:06 by kcolin #+# #+# */
|
||||||
/* Updated: 2025/08/05 12:53:06 by kcolin ### ########.fr */
|
/* Updated: 2025/08/06 11:46:04 by kcolin ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../cub3d_data.h"
|
#include "../cub3d_data.h"
|
||||||
#include "angle.h"
|
#include "angle.h"
|
||||||
#include "../consts.h"
|
#include "../consts.h"
|
||||||
|
#include "../map/collision.h"
|
||||||
|
|
||||||
void move_player_forward(t_cub3d_data *data)
|
void move_player_forward(t_cub3d_data *data)
|
||||||
{
|
{
|
||||||
char **map;
|
t_mapdata *map;
|
||||||
double next_x;
|
double next_x;
|
||||||
double next_y;
|
double next_y;
|
||||||
float movspeed;
|
float movspeed;
|
||||||
|
|
||||||
movspeed = MOVEMENT_SPEED * data->delta;
|
movspeed = MOVEMENT_SPEED * data->delta;
|
||||||
map = data->map->map;
|
map = data->map;
|
||||||
next_x = data->player.x + data->player.dir_x * movspeed;
|
next_x = data->player.x + data->player.dir_x * movspeed;
|
||||||
next_y = data->player.y + data->player.dir_y * movspeed;
|
next_y = data->player.y + data->player.dir_y * movspeed;
|
||||||
if (map[(int)data->player.y][(int)next_x] == '0')
|
if (!blocks_movement(map, (int)data->player.y, (int)next_x))
|
||||||
data->player.x += data->player.dir_x * movspeed;
|
data->player.x += data->player.dir_x * movspeed;
|
||||||
if (map[(int)next_y][(int)data->player.x] == '0')
|
if (!blocks_movement(map, (int)next_y, (int)data->player.x))
|
||||||
data->player.y += data->player.dir_y * movspeed;
|
data->player.y += data->player.dir_y * movspeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_player_backward(t_cub3d_data *data)
|
void move_player_backward(t_cub3d_data *data)
|
||||||
{
|
{
|
||||||
char **map;
|
t_mapdata *map;
|
||||||
double next_x;
|
double next_x;
|
||||||
double next_y;
|
double next_y;
|
||||||
float movspeed;
|
float movspeed;
|
||||||
|
|
||||||
movspeed = MOVEMENT_SPEED * data->delta;
|
movspeed = MOVEMENT_SPEED * data->delta;
|
||||||
map = data->map->map;
|
map = data->map;
|
||||||
next_x = data->player.x - data->player.dir_x * movspeed;
|
next_x = data->player.x - data->player.dir_x * movspeed;
|
||||||
next_y = data->player.y - data->player.dir_y * movspeed;
|
next_y = data->player.y - data->player.dir_y * movspeed;
|
||||||
if (map[(int)data->player.y][(int)next_x] == '0')
|
if (!blocks_movement(map, (int)data->player.y, (int)next_x))
|
||||||
data->player.x -= data->player.dir_x * movspeed;
|
data->player.x -= data->player.dir_x * movspeed;
|
||||||
if (map[(int)next_y][(int)data->player.x] == '0')
|
if (!blocks_movement(map, (int)next_y, (int)data->player.x))
|
||||||
data->player.y -= data->player.dir_y * movspeed;
|
data->player.y -= data->player.dir_y * movspeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_player_strafe_left(t_cub3d_data *data)
|
void move_player_strafe_left(t_cub3d_data *data)
|
||||||
{
|
{
|
||||||
char **map;
|
t_mapdata *map;
|
||||||
double next_x;
|
double next_x;
|
||||||
double next_y;
|
double next_y;
|
||||||
float movspeed;
|
float movspeed;
|
||||||
|
|
||||||
movspeed = MOVEMENT_SPEED * data->delta;
|
movspeed = MOVEMENT_SPEED * data->delta;
|
||||||
map = data->map->map;
|
map = data->map;
|
||||||
next_x = data->player.x - data->player.plane_x * movspeed;
|
next_x = data->player.x - data->player.plane_x * movspeed;
|
||||||
next_y = data->player.y - data->player.plane_y * movspeed;
|
next_y = data->player.y - data->player.plane_y * movspeed;
|
||||||
if (map[(int)data->player.y][(int)next_x] == '0')
|
if (!blocks_movement(map, (int)data->player.y, (int)next_x))
|
||||||
data->player.x -= data->player.plane_x * movspeed;
|
data->player.x -= data->player.plane_x * movspeed;
|
||||||
if (map[(int)next_y][(int)data->player.x] == '0')
|
if (!blocks_movement(map, (int)next_y, (int)data->player.x))
|
||||||
data->player.y -= data->player.plane_y * movspeed;
|
data->player.y -= data->player.plane_y * movspeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void move_player_strafe_right(t_cub3d_data *data)
|
void move_player_strafe_right(t_cub3d_data *data)
|
||||||
{
|
{
|
||||||
char **map;
|
t_mapdata *map;
|
||||||
double next_x;
|
double next_x;
|
||||||
double next_y;
|
double next_y;
|
||||||
float movspeed;
|
float movspeed;
|
||||||
|
|
||||||
movspeed = MOVEMENT_SPEED * data->delta;
|
movspeed = MOVEMENT_SPEED * data->delta;
|
||||||
map = data->map->map;
|
map = data->map;
|
||||||
next_x = data->player.x + data->player.plane_x * movspeed;
|
next_x = data->player.x + data->player.plane_x * movspeed;
|
||||||
next_y = data->player.y + data->player.plane_y * movspeed;
|
next_y = data->player.y + data->player.plane_y * movspeed;
|
||||||
if (map[(int)data->player.y][(int)next_x] == '0')
|
if (!blocks_movement(map, (int)data->player.y, (int)next_x))
|
||||||
data->player.x += data->player.plane_x * movspeed;
|
data->player.x += data->player.plane_x * movspeed;
|
||||||
if (map[(int)next_y][(int)data->player.x] == '0')
|
if (!blocks_movement(map, (int)next_y, (int)data->player.x))
|
||||||
data->player.y += data->player.plane_y * movspeed;
|
data->player.y += data->player.plane_y * movspeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,10 +3,10 @@
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* player.c :+: :+: :+: */
|
/* player.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/29 11:42:44 by tchampio #+# #+# */
|
/* Created: 2025/08/06 11:29:14 by kcolin #+# #+# */
|
||||||
/* Updated: 2025/07/29 20:13:01 by tchampio ### ########.fr */
|
/* Updated: 2025/08/06 11:29:14 by kcolin ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -59,7 +59,6 @@ void init_player(t_player *player, t_mapdata *map)
|
||||||
dir = map->map[map->starty][map->startx];
|
dir = map->map[map->starty][map->startx];
|
||||||
player->x = map->startx + 0.5;
|
player->x = map->startx + 0.5;
|
||||||
player->y = map->starty + 0.5;
|
player->y = map->starty + 0.5;
|
||||||
map->map[map->starty][map->startx] = '0';
|
|
||||||
player->health = 100;
|
player->health = 100;
|
||||||
player->points = 500;
|
player->points = 500;
|
||||||
if (dir == 'N' || dir == 'S')
|
if (dir == 'N' || dir == 'S')
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/07/31 11:55:41 by kcolin #+# #+# */
|
/* Created: 2025/07/31 11:55:41 by kcolin #+# #+# */
|
||||||
/* Updated: 2025/07/31 11:58:02 by kcolin ### ########.fr */
|
/* Updated: 2025/08/06 11:33:27 by kcolin ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
#include "../cub3d_data.h"
|
#include "../cub3d_data.h"
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include "../map/collision.h"
|
||||||
|
|
||||||
void init_ray(t_ray *ray, int pos_x, t_player *player)
|
void init_ray(t_ray *ray, int pos_x, t_player *player)
|
||||||
{
|
{
|
||||||
|
|
@ -53,7 +54,7 @@ void ray_calculate_step(t_ray *ray, t_player *player)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void calculate_wall_dist(t_ray *ray, char **map)
|
void calculate_wall_dist(t_ray *ray, t_mapdata *map)
|
||||||
{
|
{
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
|
|
@ -69,7 +70,7 @@ void calculate_wall_dist(t_ray *ray, char **map)
|
||||||
ray->map_y += ray->step_y;
|
ray->map_y += ray->step_y;
|
||||||
ray->side = SOUTH;
|
ray->side = SOUTH;
|
||||||
}
|
}
|
||||||
if (map[ray->map_y][ray->map_x] != '0')
|
if (blocks_view(map, ray->map_y, ray->map_x))
|
||||||
break ;
|
break ;
|
||||||
}
|
}
|
||||||
if (ray->side == NORTH)
|
if (ray->side == NORTH)
|
||||||
|
|
@ -105,7 +106,7 @@ void raycaster(t_cub3d_data *data, t_ray *ray)
|
||||||
{
|
{
|
||||||
init_ray(ray, x, &data->player);
|
init_ray(ray, x, &data->player);
|
||||||
ray_calculate_step(ray, &data->player);
|
ray_calculate_step(ray, &data->player);
|
||||||
calculate_wall_dist(ray, data->map->map);
|
calculate_wall_dist(ray, data->map);
|
||||||
calculate_wall_height(ray, &data->player);
|
calculate_wall_height(ray, &data->player);
|
||||||
render_walls(data, ray, x);
|
render_walls(data, ray, x);
|
||||||
x++;
|
x++;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue