rendering the walls

This commit is contained in:
Danny Jonker 2023-10-27 15:18:02 +02:00
parent ef3ad8ff25
commit accf28619f
8 changed files with 156 additions and 126 deletions

View File

@ -6,7 +6,7 @@
# By: houtworm <codam@houtworm.net> +#+ # # By: houtworm <codam@houtworm.net> +#+ #
# +#+ # # +#+ #
# Created: 2023/10/26 10:46:29 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/parse.c\
src/keys.c\ src/keys.c\
src/draw.c\ src/draw.c\
src/error.c src/error.c\
src/raycast.c\
src/map.c
OBJ =$(SRC:src/%.c=obj/%.o) OBJ =$(SRC:src/%.c=obj/%.o)
all: libft getnextline mlx/build/mlx42.a $(NAME) all: libft getnextline mlx/build/mlx42.a $(NAME)

13
cub3d.h
View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */ /* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2023/10/26 10:46:35 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 w;
int h; int h;
char **map; char **map;
double frametime;
double posx; double posx;
double posy; double posy;
double dirx; double dirx;
@ -37,6 +38,8 @@ typedef struct s_varlist
double cameray; double cameray;
double raydirx; double raydirx;
double raydiry; double raydiry;
double movespeed;
double rotspeed;
int mapx; int mapx;
int mapy; int mapy;
double sidedistx; double sidedistx;
@ -53,15 +56,19 @@ typedef struct s_varlist
// init.c // init.c
t_varlist initvarlist(void); t_varlist initvarlist(void);
// parse.c // parse.c
char **ft_getmap(void);
t_varlist ft_parseconfigfile(t_varlist vl, char *filename); t_varlist ft_parseconfigfile(t_varlist vl, char *filename);
char **ft_getmap(void);
// map.c
char **ft_getmap(void);
// keys.c // keys.c
void ft_movementkeys(t_varlist *vl); void ft_movementkeys(t_varlist *vl);
void keyhook(mlx_key_data_t kd, void *param); void keyhook(mlx_key_data_t kd, void *param);
void scrollhook(double xdelta, double ydelta, void *param); void scrollhook(double xdelta, double ydelta, void *param);
void resizehook(int x, int y, void *param); void resizehook(int x, int y, void *param);
// raycast.c
void ft_raycast(t_varlist *vl);
// draw.c // draw.c
void ft_drawnextframe(t_varlist *vl); void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend);
// error.c // error.c
int ft_errorexit(char *reason, char *function, int code); int ft_errorexit(char *reason, char *function, int code);
#endif #endif

View File

@ -6,33 +6,25 @@
/* By: houtworm <codam@houtworm.net> +#+ */ /* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2023/10/26 16:54:20 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" #include "../cub3d.h"
void ft_drawnextframe(t_varlist *vl) void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend)
{ {
int y; int y;
int x;
int half;
x = 1;
half = vl->h / 2;
while (vl->w > x)
{
// 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; y = 1;
while (vl->h > y) while (vl->h >= y)
{ {
if (y <= half) if (y < drawstart)
mlx_put_pixel(vl->img, x, y, 0x005566FF); mlx_put_pixel(vl->img, x, y, 0x005566FF);
else if (y < drawend)
mlx_put_pixel(vl->img, x, y, 0x440000FF);
else else
mlx_put_pixel(vl->img, x, y, 0x444444FF); mlx_put_pixel(vl->img, x, y, 0x444444FF);
y++; y++;
} }
x++;
}
} }

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */ /* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2023/10/26 16:50:23 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) void ft_movementkeys(t_varlist *vl)
{ {
double olddirx;
double oldplanex;
if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_LEFT)) if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_LEFT))
ft_putendl("shoot"); ft_putendl("shoot");
if (mlx_is_key_down(vl->mlx, MLX_KEY_W)) 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)) if (mlx_is_key_down(vl->mlx, MLX_KEY_A))
ft_putendl("A is pressed"); ft_putendl("A is pressed");
if (mlx_is_key_down(vl->mlx, MLX_KEY_S)) 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)) if (mlx_is_key_down(vl->mlx, MLX_KEY_D))
ft_putendl("D is pressed"); 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) 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->height = y;
vl->mlx->width = x; vl->mlx->width = x;
} }

View File

@ -6,103 +6,19 @@
/* By: houtworm <codam@houtworm.net> +#+ */ /* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2023/10/26 14:13:07 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 "../cub3d.h"
#include <stdio.h>
void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend) void ft_frametime(t_varlist *vl)
{ {
int y; vl->frametime = vl->mlx->delta_time;
ft_putnbr(1 / vl->frametime);
y = 1; ft_putendl(" FPS");
printf("%d, %d\n\n", drawstart, drawend); vl->movespeed = vl->frametime * 5.0;
while (vl->h >= y) vl->rotspeed = vl->frametime * 3.0;
{
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++;
}
} }
void mainloop(void *param) void mainloop(void *param)
@ -112,11 +28,9 @@ void mainloop(void *param)
vl = param; vl = param;
mlx_delete_image(vl->mlx, vl->img); mlx_delete_image(vl->mlx, vl->img);
vl->img = mlx_new_image(vl->mlx, vl->w, vl->h); vl->img = mlx_new_image(vl->mlx, vl->w, vl->h);
ft_movementkeys(vl);
ft_raycast(vl); ft_raycast(vl);
/*ft_drawnextframe(vl);*/ ft_frametime(vl);
ft_putnbr(1 / vl->mlx->delta_time); ft_movementkeys(vl);
ft_putendl(" FPS");
if (!vl->img || (mlx_image_to_window(vl->mlx, vl->img, 0, 0) < 0)) if (!vl->img || (mlx_image_to_window(vl->mlx, vl->img, 0, 0) < 0))
ft_errorexit("image to window failed ", "mainloop", 1); ft_errorexit("image to window failed ", "mainloop", 1);
} }

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */ /* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2023/10/26 17:33:50 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 */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */ /* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2023/10/26 16:48:55 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); free(line);
} }
vl.map = ft_getmap();
return (vl); return (vl);
} }

84
src/raycast.c Normal file
View File

@ -0,0 +1,84 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* raycast.c :+: :+: */
/* +:+ */
/* By: djonker <codam@houtworm.net> +#+ */
/* +#+ */
/* 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++;
}
}