From accf28619f0b3037f91e094a8b9d6cea6a08fad9 Mon Sep 17 00:00:00 2001 From: Danny Jonker Date: Fri, 27 Oct 2023 15:18:02 +0200 Subject: [PATCH] rendering the walls --- Makefile | 6 ++- cub3d.h | 13 +++++-- src/draw.c | 32 ++++++---------- src/keys.c | 38 ++++++++++++++++-- src/main.c | 104 +++++--------------------------------------------- src/map.c | 2 +- src/parse.c | 3 +- src/raycast.c | 84 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 156 insertions(+), 126 deletions(-) create mode 100644 src/raycast.c diff --git a/Makefile b/Makefile index a4f74c5..4476e9c 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: houtworm +#+ # # +#+ # # Created: 2023/10/26 10:46:29 by houtworm #+# #+# # -# Updated: 2023/10/26 23:24:34 by houtworm ######## odam.nl # +# Updated: 2023/10/27 14:45:10 by djonker ######## odam.nl # # # # **************************************************************************** # @@ -21,7 +21,9 @@ SRC =src/main.c\ src/parse.c\ src/keys.c\ src/draw.c\ - src/error.c + src/error.c\ + src/raycast.c\ + src/map.c OBJ =$(SRC:src/%.c=obj/%.o) all: libft getnextline mlx/build/mlx42.a $(NAME) diff --git a/cub3d.h b/cub3d.h index 3432e1a..e9298bb 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 10:46:35 by houtworm #+# #+# */ -/* Updated: 2023/10/26 21:52:52 by houtworm ######## odam.nl */ +/* Updated: 2023/10/27 14:58:39 by djonker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -27,6 +27,7 @@ typedef struct s_varlist int w; int h; char **map; + double frametime; double posx; double posy; double dirx; @@ -37,6 +38,8 @@ typedef struct s_varlist double cameray; double raydirx; double raydiry; + double movespeed; + double rotspeed; int mapx; int mapy; double sidedistx; @@ -53,15 +56,19 @@ typedef struct s_varlist // init.c t_varlist initvarlist(void); // parse.c -char **ft_getmap(void); t_varlist ft_parseconfigfile(t_varlist vl, char *filename); +char **ft_getmap(void); +// map.c +char **ft_getmap(void); // keys.c void ft_movementkeys(t_varlist *vl); void keyhook(mlx_key_data_t kd, void *param); void scrollhook(double xdelta, double ydelta, void *param); void resizehook(int x, int y, void *param); +// raycast.c +void ft_raycast(t_varlist *vl); // draw.c -void ft_drawnextframe(t_varlist *vl); +void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend); // error.c int ft_errorexit(char *reason, char *function, int code); #endif diff --git a/src/draw.c b/src/draw.c index b3f1f23..261a618 100644 --- a/src/draw.c +++ b/src/draw.c @@ -6,33 +6,25 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ -/* Updated: 2023/10/26 21:53:49 by houtworm ######## odam.nl */ +/* Updated: 2023/10/27 14:36:03 by djonker ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../cub3d.h" -void ft_drawnextframe(t_varlist *vl) +void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend) { - int y; - int x; - int half; + int y; - x = 1; - half = vl->h / 2; - while (vl->w > x) + y = 1; + while (vl->h >= y) { - // send out ray to check for walls - // if we hit a wall we get the distance so we know what size it is and how many pixels to draw in color - y = 1; - while (vl->h > y) - { - if (y <= half) - mlx_put_pixel(vl->img, x, y, 0x005566FF); - else - mlx_put_pixel(vl->img, x, y, 0x444444FF); - y++; - } - x++; + if (y < drawstart) + mlx_put_pixel(vl->img, x, y, 0x005566FF); + else if (y < drawend) + mlx_put_pixel(vl->img, x, y, 0x440000FF); + else + mlx_put_pixel(vl->img, x, y, 0x444444FF); + y++; } } diff --git a/src/keys.c b/src/keys.c index d971796..bcdb357 100644 --- a/src/keys.c +++ b/src/keys.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ -/* Updated: 2023/10/26 16:50:33 by houtworm ######## odam.nl */ +/* Updated: 2023/10/27 15:16:02 by djonker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,16 +14,47 @@ void ft_movementkeys(t_varlist *vl) { + double olddirx; + double oldplanex; + if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_LEFT)) ft_putendl("shoot"); if (mlx_is_key_down(vl->mlx, MLX_KEY_W)) - ft_putendl("W is pressed"); + { + if (vl->map[(int)vl->posx + (int)vl->dirx * (int)vl->movespeed][(int)vl->posy] == false) + vl->posx += vl->dirx * vl->movespeed; + if (vl->map[(int)vl->posx][(int)vl->posy + (int)vl->diry * (int)vl->movespeed] == false) + vl->posx += vl->dirx * vl->movespeed; + } if (mlx_is_key_down(vl->mlx, MLX_KEY_A)) ft_putendl("A is pressed"); if (mlx_is_key_down(vl->mlx, MLX_KEY_S)) - ft_putendl("S is pressed"); + { + if (vl->map[(int)vl->posx - (int)vl->dirx * (int)vl->movespeed][(int)vl->posy] == false) + vl->posx -= vl->dirx * vl->movespeed; + if (vl->map[(int)vl->posx][(int)vl->posy - (int)vl->diry * (int)vl->movespeed] == false) + vl->posx -= vl->dirx * vl->movespeed; + } if (mlx_is_key_down(vl->mlx, MLX_KEY_D)) ft_putendl("D is pressed"); + if (mlx_is_key_down(vl->mlx, MLX_KEY_LEFT)) + { + olddirx = vl->dirx; + vl->dirx = vl->dirx * cos(vl->rotspeed) - vl->diry * sin(vl->rotspeed); + vl->diry = olddirx * sin(vl->rotspeed) + vl->diry * cos(vl->rotspeed); + oldplanex = vl->planex; + vl->planex = vl->planex * cos(vl->rotspeed) - vl->planey * sin(vl->rotspeed); + vl->planey = oldplanex * sin(vl->rotspeed) + vl->planey * cos(vl->rotspeed); + } + if (mlx_is_key_down(vl->mlx, MLX_KEY_RIGHT)) + { + olddirx = vl->dirx; + vl->dirx = vl->dirx * cos(-vl->rotspeed) - vl->diry * sin(-vl->rotspeed); + vl->diry = olddirx * sin(-vl->rotspeed) + vl->diry * cos(-vl->rotspeed); + oldplanex = vl->planex; + vl->planex = vl->planex * cos(-vl->rotspeed) - vl->planey * sin(-vl->rotspeed); + vl->planey = oldplanex * sin(-vl->rotspeed) + vl->planey * cos(-vl->rotspeed); + } } void keyhook(mlx_key_data_t kd, void *param) @@ -65,4 +96,3 @@ void resizehook(int x, int y, void *param) vl->mlx->height = y; vl->mlx->width = x; } - diff --git a/src/main.c b/src/main.c index d7574e9..674ff06 100644 --- a/src/main.c +++ b/src/main.c @@ -6,103 +6,19 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ -/* Updated: 2023/10/26 23:53:09 by houtworm ######## odam.nl */ +/* Updated: 2023/10/27 14:59:03 by djonker ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../cub3d.h" -#include -void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend) +void ft_frametime(t_varlist *vl) { - int y; - - y = 1; - printf("%d, %d\n\n", drawstart, drawend); - while (vl->h >= y) - { - if (y < drawstart) - mlx_put_pixel(vl->img, x, y, 0x005566FF); - else if (y < drawend) - mlx_put_pixel(vl->img, x, y, 0x440000FF); - else - mlx_put_pixel(vl->img, x, y, 0x444444FF); - y++; - } - -} - -void ft_raycast(t_varlist *vl) -{ - int x; - int lineheight; - int drawstart; - int drawend; - - x = 1; - while (x <= vl->w) - { - vl->camerax = 2 * x / (double)vl->w - 1; - vl->raydirx = vl->dirx + vl->planex * vl->camerax; - vl->raydiry = vl->diry + vl->planey * vl->camerax; - vl->mapx = (int)vl->posx; - vl->mapy = (int)vl->posy; - vl->deltadistx = fabs(1 / vl->raydirx); - vl->deltadisty = fabs(1 / vl->raydiry); - vl->hit = 0; - if (vl->raydirx < 0) - { - vl->stepx = -1; - vl->sidedistx = (vl->posx - vl->mapx) * vl->deltadistx; - } - else - { - vl->stepx = 1; - vl->sidedistx = (vl->posx + 1.0 - vl->mapx) * vl->deltadistx; - } - if (vl->raydiry < 0) - { - vl->stepy = -1; - vl->sidedisty = (vl->posy - vl->mapy) * vl->deltadisty; - } - else - { - vl->stepy = 1; - vl->sidedisty = (vl->posy + 1.0 - vl->mapy) * vl->deltadisty; - } - while (vl->hit == 0) - { - if (vl->sidedistx < vl->sidedisty) - { - vl->sidedistx += vl->deltadistx; - vl->mapx += vl->stepx; - vl->side = 0; - } - else - { - vl->sidedisty += vl->deltadisty; - vl->mapy += vl->stepy; - vl->side = 1; - } - printf("mapx: %d, mapy: %d\n", vl->mapx, vl->mapy); - if (vl->map[vl->mapx][vl->mapy] > 0) - vl->hit = 1; - } - if (vl->side == 0) - vl->perpwalldist = (vl->sidedistx - vl->deltadistx); - else - vl->perpwalldist = (vl->sidedisty - vl->deltadisty); - lineheight = (int)vl->h / vl->perpwalldist; - drawstart = -lineheight / 2 + vl->h / 2; - if (drawstart < 0) - drawstart = 0; - drawend = lineheight / 2 + vl->h / 2; - if (drawend >= vl->h) - drawend = vl->h - 1; - ft_drawline(x, vl, drawstart, drawend); - x++; - } - + vl->frametime = vl->mlx->delta_time; + ft_putnbr(1 / vl->frametime); + ft_putendl(" FPS"); + vl->movespeed = vl->frametime * 5.0; + vl->rotspeed = vl->frametime * 3.0; } void mainloop(void *param) @@ -112,11 +28,9 @@ void mainloop(void *param) vl = param; mlx_delete_image(vl->mlx, vl->img); vl->img = mlx_new_image(vl->mlx, vl->w, vl->h); - ft_movementkeys(vl); ft_raycast(vl); - /*ft_drawnextframe(vl);*/ - ft_putnbr(1 / vl->mlx->delta_time); - ft_putendl(" FPS"); + ft_frametime(vl); + ft_movementkeys(vl); if (!vl->img || (mlx_image_to_window(vl->mlx, vl->img, 0, 0) < 0)) ft_errorexit("image to window failed ", "mainloop", 1); } diff --git a/src/map.c b/src/map.c index 9ddaf51..57db6c5 100644 --- a/src/map.c +++ b/src/map.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 17:33:50 by houtworm #+# #+# */ -/* Updated: 2023/10/26 17:34:02 by houtworm ######## odam.nl */ +/* Updated: 2023/10/27 13:44:41 by djonker ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/parse.c b/src/parse.c index b989c4b..1226668 100644 --- a/src/parse.c +++ b/src/parse.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:48:55 by houtworm #+# #+# */ -/* Updated: 2023/10/26 17:33:49 by houtworm ######## odam.nl */ +/* Updated: 2023/10/27 13:49:00 by djonker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -24,5 +24,6 @@ t_varlist ft_parseconfigfile(t_varlist vl, char *filename) { free(line); } + vl.map = ft_getmap(); return (vl); } diff --git a/src/raycast.c b/src/raycast.c new file mode 100644 index 0000000..f3bd164 --- /dev/null +++ b/src/raycast.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* raycast.c :+: :+: */ +/* +:+ */ +/* By: djonker +#+ */ +/* +#+ */ +/* Created: 2023/10/27 14:36:42 by djonker #+# #+# */ +/* Updated: 2023/10/27 15:02:06 by djonker ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../cub3d.h" + +void ft_raycast(t_varlist *vl) +{ + int x; + int lineheight; + int drawstart; + int drawend; + + x = 1; + while (x <= vl->w) + { + vl->camerax = 2 * x / (double)vl->w - 1; + vl->raydirx = vl->dirx + vl->planex * vl->camerax; + vl->raydiry = vl->diry + vl->planey * vl->camerax; + vl->mapx = (int)vl->posx; + vl->mapy = (int)vl->posy; + vl->deltadistx = fabs(1 / vl->raydirx); + vl->deltadisty = fabs(1 / vl->raydiry); + vl->hit = 0; + if (vl->raydirx < 0) + { + vl->stepx = -1; + vl->sidedistx = (vl->posx - vl->mapx) * vl->deltadistx; + } + else + { + vl->stepx = 1; + vl->sidedistx = (vl->posx + 1.0 - vl->mapx) * vl->deltadistx; + } + if (vl->raydiry < 0) + { + vl->stepy = -1; + vl->sidedisty = (vl->posy - vl->mapy) * vl->deltadisty; + } + else + { + vl->stepy = 1; + vl->sidedisty = (vl->posy + 1.0 - vl->mapy) * vl->deltadisty; + } + while (vl->hit == 0) + { + if (vl->sidedistx < vl->sidedisty) + { + vl->sidedistx += vl->deltadistx; + vl->mapx += vl->stepx; + vl->side = 0; + } + else + { + vl->sidedisty += vl->deltadisty; + vl->mapy += vl->stepy; + vl->side = 1; + } + if (vl->map[vl->mapx][vl->mapy] > 0) + vl->hit = 1; + } + if (vl->side == 0) + vl->perpwalldist = (vl->sidedistx - vl->deltadistx); + else + vl->perpwalldist = (vl->sidedisty - vl->deltadisty); + lineheight = (int)vl->h / vl->perpwalldist; + drawstart = -lineheight / 2 + vl->h / 2; + if (drawstart < 0) + drawstart = 0; + drawend = lineheight / 2 + vl->h / 2; + if (drawend >= vl->h) + drawend = vl->h - 1; + ft_drawline(x, vl, drawstart, drawend); + x++; + } +}