From b06ede9ae9e526f8ccf52c43a1a5b941dcf11624 Mon Sep 17 00:00:00 2001 From: djonker Date: Sun, 5 Nov 2023 04:16:49 +0100 Subject: [PATCH] little cleanup --- cub3d.h | 15 ++-------- src/draw.c | 4 +-- src/init.c | 4 +-- src/keys.c | 80 +++++++++++++++++++++++++++------------------------ src/main.c | 4 +-- src/raycast.c | 54 ++++++++++++++++++---------------- 6 files changed, 80 insertions(+), 81 deletions(-) diff --git a/cub3d.h b/cub3d.h index 391eabf..39b0848 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 10:46:35 by houtworm #+# #+# */ -/* Updated: 2023/11/05 02:59:11 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 04:15:30 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -49,7 +49,6 @@ typedef struct s_varlist mlx_texture_t *nazitext; t_sprite *sprite; int spritecount; - int fpsrefresh; int w; int h; char **map; @@ -60,33 +59,23 @@ typedef struct s_varlist double diry; double planex; double planey; - double camerax; - double cameray; int vaim; int jump; double raydirx; double raydiry; - double movespeed; - double rotspeed; - int mapx; - int mapy; double sidedistx; double sidedisty; double deltadistx; double deltadisty; double perpwalldist; int lineheight; - int stepx; - int stepy; - int hit; int side; double run; double oldmouseposx; double oldmouseposy; int32_t fcolor; int32_t ccolor; - int resize; - int *zbuffer; + int *distance; int treasure; int tottreasure; int enemies; diff --git a/src/draw.c b/src/draw.c index f1f7dc9..392376d 100644 --- a/src/draw.c +++ b/src/draw.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ -/* Updated: 2023/11/05 03:05:46 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 04:13:07 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -109,7 +109,7 @@ void ft_drawsprites(t_varlist *vl) while (x < drawendx) { texx = (int)(256 * (x - (-spritewidth / 2 + spritescreenx)) * 64 / spritewidth) / 256; - if (transformy > 0 && x > 0 && x < vl->w && transformy < vl->zbuffer[x] + 0.7) + if (transformy > 0 && x > 0 && x < vl->w && transformy < vl->distance[x] + 0.7) { y = drawstarty; while (y < drawendy) diff --git a/src/init.c b/src/init.c index 654c096..0192860 100644 --- a/src/init.c +++ b/src/init.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */ -/* Updated: 2023/11/05 02:59:50 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 04:13:14 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ t_varlist initvarlist(void) vl.w = 800; vl.h = 600; vl.sprite = ft_calloc(4096, 8); - vl.zbuffer = ft_calloc(4096, 8); + vl.distance = ft_calloc(4096, 8); vl.spritecount = 0; vl.vaim = 0; vl.jump = 0; diff --git a/src/keys.c b/src/keys.c index 3992ff7..f18b8a6 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/11/05 03:15:39 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 04:05:51 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,7 +16,11 @@ void ft_movementkeys(t_varlist *vl) { double temp; double distance; + double movespeed; + double rotspeed; + movespeed = vl->frametime * 3.0; + rotspeed = vl->frametime * 3.0; if (vl->side) distance = vl->sidedisty - vl->deltadisty; else @@ -26,80 +30,80 @@ void ft_movementkeys(t_varlist *vl) if (mlx_is_key_down(vl->mlx, MLX_KEY_SPACE) && vl->jump < 10) vl->jump = 200; else if (vl->jump > 0) - vl->jump = vl->jump - 150 * vl->movespeed; + vl->jump = vl->jump - 150 * movespeed; if (mlx_is_key_down(vl->mlx, MLX_KEY_LEFT_CONTROL)) vl->jump = -200; else if (vl->jump < 0) - vl->jump = vl->jump + 150 * vl->movespeed; + vl->jump = vl->jump + 150 * movespeed; if (mlx_is_key_down(vl->mlx, MLX_KEY_W)) { - if (vl->map[(int)(vl->posx + vl->dirx * vl->movespeed)][(int)vl->posy] == '0' && distance > 0.4) + if (vl->map[(int)(vl->posx + vl->dirx * movespeed)][(int)vl->posy] == '0' && distance > 0.4) { - vl->posx += vl->dirx * vl->movespeed * vl->run; - vl->posy += vl->diry * vl->movespeed * vl->run; + vl->posx += vl->dirx * movespeed * vl->run; + vl->posy += vl->diry * movespeed * vl->run; } - if (vl->map[(int)vl->posx][(int)(vl->posy + vl->diry * vl->movespeed)] == '0' && distance > 0.4) + if (vl->map[(int)vl->posx][(int)(vl->posy + vl->diry * movespeed)] == '0' && distance > 0.4) { - vl->posx += vl->dirx * vl->movespeed * vl->run; - vl->posy += vl->diry * vl->movespeed * vl->run; + vl->posx += vl->dirx * movespeed * vl->run; + vl->posy += vl->diry * movespeed * vl->run; } } if (mlx_is_key_down(vl->mlx, MLX_KEY_A)) { - if (vl->map[(int)(vl->posx - vl->dirx * vl->movespeed)][(int)vl->posy] == '0' && distance > 0.4) + if (vl->map[(int)(vl->posx - vl->dirx * movespeed)][(int)vl->posy] == '0' && distance > 0.4) { - vl->posx -= vl->diry * vl->movespeed * vl->run; - vl->posy += vl->dirx * vl->movespeed * vl->run; + vl->posx -= vl->diry * movespeed * vl->run; + vl->posy += vl->dirx * movespeed * vl->run; } - if (vl->map[(int)vl->posx][(int)(vl->posy - vl->diry * vl->movespeed)] == '0' && distance > 0.4) + if (vl->map[(int)vl->posx][(int)(vl->posy - vl->diry * movespeed)] == '0' && distance > 0.4) { - vl->posx -= vl->diry * vl->movespeed * vl->run; - vl->posy += vl->dirx * vl->movespeed * vl->run; + vl->posx -= vl->diry * movespeed * vl->run; + vl->posy += vl->dirx * movespeed * vl->run; } } if (mlx_is_key_down(vl->mlx, MLX_KEY_S)) { - if (vl->map[(int)(vl->posx - vl->dirx * vl->movespeed)][(int)vl->posy] == '0' && distance > 0.4) + if (vl->map[(int)(vl->posx - vl->dirx * movespeed)][(int)vl->posy] == '0' && distance > 0.4) { - vl->posx -= vl->dirx * vl->movespeed * vl->run; - vl->posy -= vl->diry * vl->movespeed * vl->run; + vl->posx -= vl->dirx * movespeed * vl->run; + vl->posy -= vl->diry * movespeed * vl->run; } - if (vl->map[(int)vl->posx][(int)(vl->posy - vl->diry * vl->movespeed)] == '0' && distance > 0.4) + if (vl->map[(int)vl->posx][(int)(vl->posy - vl->diry * movespeed)] == '0' && distance > 0.4) { - vl->posx -= vl->dirx * vl->movespeed * vl->run; - vl->posy -= vl->diry * vl->movespeed * vl->run; + vl->posx -= vl->dirx * movespeed * vl->run; + vl->posy -= vl->diry * movespeed * vl->run; } } if (mlx_is_key_down(vl->mlx, MLX_KEY_D)) { - if (vl->map[(int)(vl->posx + vl->dirx * vl->movespeed)][(int)vl->posy] == '0' && distance > 0.4) + if (vl->map[(int)(vl->posx + vl->dirx * movespeed)][(int)vl->posy] == '0' && distance > 0.4) { - vl->posx += vl->diry * vl->movespeed * vl->run; - vl->posy -= vl->dirx * vl->movespeed * vl->run; + vl->posx += vl->diry * movespeed * vl->run; + vl->posy -= vl->dirx * movespeed * vl->run; } - if (vl->map[(int)vl->posx][(int)(vl->posy + vl->diry * vl->movespeed)] == '0' && distance > 0.4) + if (vl->map[(int)vl->posx][(int)(vl->posy + vl->diry * movespeed)] == '0' && distance > 0.4) { - vl->posx += vl->diry * vl->movespeed * vl->run; - vl->posy -= vl->dirx * vl->movespeed * vl->run; + vl->posx += vl->diry * movespeed * vl->run; + vl->posy -= vl->dirx * movespeed * vl->run; } } if (mlx_is_key_down(vl->mlx, MLX_KEY_LEFT)) { temp = vl->dirx; - vl->dirx = temp * cos(vl->rotspeed) - vl->diry * sin(vl->rotspeed); - vl->diry = temp * sin(vl->rotspeed) + vl->diry * cos(vl->rotspeed); + vl->dirx = temp * cos(rotspeed) - vl->diry * sin(rotspeed); + vl->diry = temp * sin(rotspeed) + vl->diry * cos(rotspeed); temp = vl->planex; - vl->planex = temp * cos(vl->rotspeed) - vl->planey * sin(vl->rotspeed); - vl->planey = temp * sin(vl->rotspeed) + vl->planey * cos(vl->rotspeed); + vl->planex = temp * cos(rotspeed) - vl->planey * sin(rotspeed); + vl->planey = temp * sin(rotspeed) + vl->planey * cos(rotspeed); } if (mlx_is_key_down(vl->mlx, MLX_KEY_RIGHT)) { temp = vl->dirx; - vl->dirx = temp * cos(-vl->rotspeed) - vl->diry * sin(-vl->rotspeed); - vl->diry = temp * sin(-vl->rotspeed) + vl->diry * cos(-vl->rotspeed); + vl->dirx = temp * cos(-rotspeed) - vl->diry * sin(-rotspeed); + vl->diry = temp * sin(-rotspeed) + vl->diry * cos(-rotspeed); temp = vl->planex; - vl->planex = temp * cos(-vl->rotspeed) - vl->planey * sin(-vl->rotspeed); - vl->planey = temp * sin(-vl->rotspeed) + vl->planey * cos(-vl->rotspeed); + vl->planex = temp * cos(-rotspeed) - vl->planey * sin(-rotspeed); + vl->planey = temp * sin(-rotspeed) + vl->planey * cos(-rotspeed); } if (mlx_is_key_down(vl->mlx, MLX_KEY_LEFT_SHIFT)) vl->run = 2; @@ -142,12 +146,14 @@ void cursorhook(double xpos, double ypos, void *param) double olddirx; double oldplanex; double speed; + double rotspeed; vl = param; ypos++; + rotspeed = vl->frametime * 3.0; if (xpos < vl->oldmouseposx) { - speed = (vl->oldmouseposx - xpos) * vl->rotspeed / 20; + speed = (vl->oldmouseposx - xpos) * rotspeed / 20; olddirx = vl->dirx; vl->dirx = vl->dirx * cos(speed) - vl->diry * sin(speed); vl->diry = olddirx * sin(speed) + vl->diry * cos(speed); @@ -157,7 +163,7 @@ void cursorhook(double xpos, double ypos, void *param) } if (xpos > vl->oldmouseposx) { - speed = (xpos - vl->oldmouseposx) * vl->rotspeed / 20; + speed = (xpos - vl->oldmouseposx) * rotspeed / 20; olddirx = vl->dirx; vl->dirx = vl->dirx * cos(-speed) - vl->diry * sin(-speed); vl->diry = olddirx * sin(-speed) + vl->diry * cos(-speed); diff --git a/src/main.c b/src/main.c index 75813e6..3a0e022 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ -/* Updated: 2023/11/04 04:34:54 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 04:01:49 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -40,8 +40,6 @@ void ft_frametime(t_varlist *vl) char *print; vl->frametime = vl->mlx->delta_time; - vl->movespeed = vl->frametime * 3.0; - vl->rotspeed = vl->frametime * 3.0; itoa = ft_itoa(1 / vl->frametime); print = ft_strjoin(itoa, " FPS"); vl->fps = mlx_put_string(vl->mlx, print, 10, 10); diff --git a/src/raycast.c b/src/raycast.c index 31c3bdb..0df8c92 100644 --- a/src/raycast.c +++ b/src/raycast.c @@ -6,7 +6,7 @@ /* By: djonker +#+ */ /* +#+ */ /* Created: 2023/10/27 14:36:42 by djonker #+# #+# */ -/* Updated: 2023/11/05 03:03:31 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 04:12:59 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,57 +14,63 @@ void ft_raycast(t_varlist *vl) { - int x; - int drawstart; - int drawend; + int x; + int drawstart; + int drawend; + double camerax; + int mapx; + int mapy; + int stepx; + int stepy; + int hit; 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; + camerax = 2 * x / (double)vl->w - 1; + vl->raydirx = vl->dirx + vl->planex * camerax; + vl->raydiry = vl->diry + vl->planey * camerax; + mapx = (int)vl->posx; + mapy = (int)vl->posy; vl->deltadistx = fabs(1 / vl->raydirx); vl->deltadisty = fabs(1 / vl->raydiry); - vl->hit = 0; + hit = 0; if (vl->raydirx < 0) { - vl->stepx = -1; - vl->sidedistx = (vl->posx - vl->mapx) * vl->deltadistx; + stepx = -1; + vl->sidedistx = (vl->posx - mapx) * vl->deltadistx; } else { - vl->stepx = 1; - vl->sidedistx = (vl->mapx + 1.0 - vl->posx) * vl->deltadistx; + stepx = 1; + vl->sidedistx = (mapx + 1.0 - vl->posx) * vl->deltadistx; } if (vl->raydiry < 0) { - vl->stepy = -1; - vl->sidedisty = (vl->posy - vl->mapy) * vl->deltadisty; + stepy = -1; + vl->sidedisty = (vl->posy - mapy) * vl->deltadisty; } else { - vl->stepy = 1; - vl->sidedisty = (vl->mapy + 1.0 - vl->posy) * vl->deltadisty; + stepy = 1; + vl->sidedisty = (mapy + 1.0 - vl->posy) * vl->deltadisty; } - while (vl->hit == 0) + while (hit == 0) { if (vl->sidedistx < vl->sidedisty) { vl->sidedistx += vl->deltadistx; - vl->mapx += vl->stepx; + mapx += stepx; vl->side = 0; } else { vl->sidedisty += vl->deltadisty; - vl->mapy += vl->stepy; + mapy += stepy; vl->side = 1; } - if (vl->map[vl->mapx][vl->mapy] == '1') - vl->hit = 1; + if (vl->map[mapx][mapy] == '1') + hit = 1; } if (vl->side == 0) vl->perpwalldist = (vl->sidedistx - vl->deltadistx); @@ -78,7 +84,7 @@ void ft_raycast(t_varlist *vl) if (drawend >= vl->h) drawend = vl->h - 1; ft_drawline(x, vl, drawstart, drawend); - vl->zbuffer[x] = vl->perpwalldist; + vl->distance[x] = vl->perpwalldist; x++; } }