From e15684fd69b94cb4085b872a20dfa350506f88b7 Mon Sep 17 00:00:00 2001 From: djonker Date: Sun, 5 Nov 2023 06:38:58 +0100 Subject: [PATCH] little cleanup --- Makefile | 7 +- cub3d.h | 10 ++- src/draw/draw.c | 8 +- src/draw/raycast.c | 169 ++++++++++++++++++++++++--------------- src/input/game.c | 53 +++++++++++++ src/input/keys.c | 193 --------------------------------------------- src/input/move.c | 105 ++++++++++++++++++++++++ src/input/rest.c | 51 ++++++++++++ src/input/turn.c | 82 +++++++++++++++++++ src/main/main.c | 6 +- 10 files changed, 413 insertions(+), 271 deletions(-) create mode 100644 src/input/game.c delete mode 100644 src/input/keys.c create mode 100644 src/input/move.c create mode 100644 src/input/rest.c create mode 100644 src/input/turn.c diff --git a/Makefile b/Makefile index 7fb8375..dabe242 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: houtworm +#+ # # +#+ # # Created: 2023/10/26 10:46:29 by houtworm #+# #+# # -# Updated: 2023/11/05 04:43:47 by houtworm ######## odam.nl # +# Updated: 2023/11/05 05:18:12 by houtworm ######## odam.nl # # # # **************************************************************************** # @@ -21,7 +21,10 @@ SRC =src/main/main.c\ src/draw/draw.c\ src/main/error.c\ src/draw/raycast.c\ - src/input/keys.c\ + src/input/game.c\ + src/input/move.c\ + src/input/turn.c\ + src/input/rest.c\ src/parse/parse.c\ src/parse/map.c OBJ =$(SRC:src/%.c=obj/%.o) diff --git a/cub3d.h b/cub3d.h index 89649f9..dc3ea6d 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 04:50:43 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 06:38:03 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -68,7 +68,7 @@ typedef struct s_varlist double sidedisty; double deltadistx; double deltadisty; - double perpwalldist; + double walldist; int lineheight; int side; double oldmouseposx; @@ -93,12 +93,16 @@ t_varlist ft_parseconfigfile(t_varlist vl, char *filename); char **ft_getmap(t_varlist *vl, int fd); // keys.c void ft_processinput(t_varlist *vl); +void ft_processturn(t_varlist *vl, double rotspeed); +void ft_processmove(t_varlist *vl, double movespeed); +void ft_processacro(t_varlist *vl, double movespeed); +void ft_processguns(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); void cursorhook(double x, double y, void *param); // raycast.c -void ft_drawwalls(t_varlist *vl); +void ft_drawmap(t_varlist *vl); // draw.c void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend); void ft_drawsprites(t_varlist *vl); diff --git a/src/draw/draw.c b/src/draw/draw.c index 4157b80..3f2690a 100644 --- a/src/draw/draw.c +++ b/src/draw/draw.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */ -/* Updated: 2023/11/05 04:42:11 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 06:38:15 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -168,9 +168,9 @@ void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend) } double wallx; if (vl->side == 0) - wallx = vl->posy + vl->perpwalldist * vl->raydiry; + wallx = vl->posy + vl->walldist * vl->raydiry; else - wallx = vl->posx + vl->perpwalldist * vl->raydirx; + wallx = vl->posx + vl->walldist * vl->raydirx; ft_selecttexture(vl); wallx -= floor(wallx); int textx; @@ -182,7 +182,7 @@ void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend) double step; step = 64.0 / vl->lineheight; double textpos; - textpos = (drawstart - vl->vaim - (vl->jump / vl->perpwalldist) - vl->h / 2 + vl->lineheight / 2) * step; + textpos = (drawstart - vl->vaim - (vl->jump / vl->walldist) - vl->h / 2 + vl->lineheight / 2) * step; while (y < drawend) { int texty; diff --git a/src/draw/raycast.c b/src/draw/raycast.c index 34ca66f..e446c79 100644 --- a/src/draw/raycast.c +++ b/src/draw/raycast.c @@ -6,85 +6,124 @@ /* By: djonker +#+ */ /* +#+ */ /* Created: 2023/10/27 14:36:42 by djonker #+# #+# */ -/* Updated: 2023/11/05 04:50:09 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 06:38:23 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ #include "../../cub3d.h" -void ft_drawwalls(t_varlist *vl) +int ft_getstepx(t_varlist *vl, int mapx) +{ + int stepx; + + if (vl->raydirx < 0) + { + stepx = -1; + vl->sidedistx = (vl->posx - mapx) * vl->deltadistx; + } + else + { + stepx = 1; + vl->sidedistx = (mapx + 1.0 - vl->posx) * vl->deltadistx; + } + return (stepx); +} + +int ft_getstepy(t_varlist *vl, int mapy) +{ + int stepy; + + if (vl->raydiry < 0) + { + stepy = -1; + vl->sidedisty = (vl->posy - mapy) * vl->deltadisty; + } + else + { + stepy = 1; + vl->sidedisty = (mapy + 1.0 - vl->posy) * vl->deltadisty; + } + return (stepy); +} + +void ft_prepcast(t_varlist *vl, int x) { - int x; - int drawstart; - int drawend; double camerax; - int mapx; - int mapy; + + camerax = 2 * x / (double)vl->w - 1; + vl->raydirx = vl->dirx + vl->planex * camerax; + vl->raydiry = vl->diry + vl->planey * camerax; + vl->deltadistx = fabs(1 / vl->raydirx); + vl->deltadisty = fabs(1 / vl->raydiry); +} + +void ft_raycast(t_varlist *vl, int mapx, int mapy) +{ + int hit; int stepx; int stepy; - int hit; - x = 1; + stepx = ft_getstepx(vl, mapx); + stepy = ft_getstepy(vl, mapy); + hit = 0; + while (hit == 0) + { + if (vl->sidedistx < vl->sidedisty) + { + vl->sidedistx += vl->deltadistx; + mapx += stepx; + vl->side = 0; + } + else + { + vl->sidedisty += vl->deltadisty; + mapy += stepy; + vl->side = 1; + } + if (vl->map[mapx][mapy] == '1') + hit = 1; + } +} + +int ft_getwallheight(t_varlist *vl, int mode) +{ + int ret; + + if (mode == 1) + { + ret = -vl->lineheight / 2 + vl->h / 2 + vl->vaim + (vl->jump / vl->walldist); + if (ret < 0) + ret = 0; + } + if (mode == 2) + { + ret = vl->lineheight / 2 + vl->h / 2 + vl->vaim + (vl->jump / vl->walldist); + if (ret >= vl->h) + ret = vl->h - 1; + } + return (ret); +} + +void ft_drawmap(t_varlist *vl) +{ + int x; + int mapx; + int mapy; + + mapx = (int)vl->posx; + mapy = (int)vl->posy; + x = 0; while (x <= vl->w) { - 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); - hit = 0; - if (vl->raydirx < 0) - { - stepx = -1; - vl->sidedistx = (vl->posx - mapx) * vl->deltadistx; - } - else - { - stepx = 1; - vl->sidedistx = (mapx + 1.0 - vl->posx) * vl->deltadistx; - } - if (vl->raydiry < 0) - { - stepy = -1; - vl->sidedisty = (vl->posy - mapy) * vl->deltadisty; - } - else - { - stepy = 1; - vl->sidedisty = (mapy + 1.0 - vl->posy) * vl->deltadisty; - } - while (hit == 0) - { - if (vl->sidedistx < vl->sidedisty) - { - vl->sidedistx += vl->deltadistx; - mapx += stepx; - vl->side = 0; - } - else - { - vl->sidedisty += vl->deltadisty; - mapy += stepy; - vl->side = 1; - } - if (vl->map[mapx][mapy] == '1') - hit = 1; - } + ft_prepcast(vl, x); + ft_raycast(vl, mapx, mapy); + ft_drawline(x, vl, ft_getwallheight(vl, 1), ft_getwallheight(vl, 2)); if (vl->side == 0) - vl->perpwalldist = (vl->sidedistx - vl->deltadistx); + vl->walldist = (vl->sidedistx - vl->deltadistx); else - vl->perpwalldist = (vl->sidedisty - vl->deltadisty); - vl->lineheight = vl->h / vl->perpwalldist; - drawstart = -vl->lineheight / 2 + vl->h / 2 + vl->vaim + (vl->jump / vl->perpwalldist); - if (drawstart < 0) - drawstart = 0; - drawend = vl->lineheight / 2 + vl->h / 2 + vl->vaim + (vl->jump / vl->perpwalldist); - if (drawend >= vl->h) - drawend = vl->h - 1; - ft_drawline(x, vl, drawstart, drawend); - vl->distance[x] = vl->perpwalldist; + vl->walldist = (vl->sidedisty - vl->deltadisty); + vl->lineheight = vl->h / vl->walldist; + vl->distance[x] = vl->walldist; x++; } } diff --git a/src/input/game.c b/src/input/game.c new file mode 100644 index 0000000..bd1b33b --- /dev/null +++ b/src/input/game.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* game.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ +/* Updated: 2023/11/05 05:18:53 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void keyhook(mlx_key_data_t kd, void *param) +{ + t_varlist *vl; + + vl = param; + vl->w =vl->w; + if (mlx_is_key_down(vl->mlx, MLX_KEY_ESCAPE)) + { + ft_putendl("escape is pressed"); + mlx_close_window(vl->mlx); + return ; + } + if ((kd.key == MLX_KEY_H || kd.key == MLX_KEY_F1) && kd.action == MLX_PRESS) + ft_putendl("H is pressed"); +} + +void resizehook(int x, int y, void *param) +{ + t_varlist *vl; + + vl = param; + vl->h = y; + vl->w = x; + vl->mlx->height = y; + vl->mlx->width = x; +} + +void ft_processinput(t_varlist *vl) +{ + double movespeed; + double rotspeed; + + movespeed = vl->frametime * 3.0; + rotspeed = vl->frametime * 3.0; + ft_processacro(vl, movespeed); + ft_processmove(vl, rotspeed); + ft_processturn(vl, rotspeed); + ft_processguns(vl); +} diff --git a/src/input/keys.c b/src/input/keys.c deleted file mode 100644 index fca90e7..0000000 --- a/src/input/keys.c +++ /dev/null @@ -1,193 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* keys.c :+: :+: */ -/* +:+ */ -/* By: houtworm +#+ */ -/* +#+ */ -/* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ -/* Updated: 2023/11/05 04:42:20 by houtworm ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "../../cub3d.h" - -void ft_processinput(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 - distance = vl->sidedistx - vl->deltadistx; - if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_LEFT)) - ft_putendl("shoot"); - 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 * 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 * movespeed; - if (mlx_is_key_down(vl->mlx, MLX_KEY_W)) - { - if (vl->map[(int)(vl->posx + vl->dirx * movespeed)][(int)vl->posy] == '0' && distance > 0.4) - { - 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 * movespeed)] == '0' && distance > 0.4) - { - 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 * movespeed)][(int)vl->posy] == '0' && distance > 0.4) - { - 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 * movespeed)] == '0' && distance > 0.4) - { - 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 * movespeed)][(int)vl->posy] == '0' && distance > 0.4) - { - 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 * movespeed)] == '0' && distance > 0.4) - { - 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 * movespeed)][(int)vl->posy] == '0' && distance > 0.4) - { - 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 * movespeed)] == '0' && distance > 0.4) - { - 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(rotspeed) - vl->diry * sin(rotspeed); - vl->diry = temp * sin(rotspeed) + vl->diry * cos(rotspeed); - temp = vl->planex; - 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(-rotspeed) - vl->diry * sin(-rotspeed); - vl->diry = temp * sin(-rotspeed) + vl->diry * cos(-rotspeed); - temp = vl->planex; - 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; - else - vl->run = 1; -} - -void keyhook(mlx_key_data_t kd, void *param) -{ - t_varlist *vl; - - vl = param; - vl->w =vl->w; - if (mlx_is_key_down(vl->mlx, MLX_KEY_ESCAPE)) - { - ft_putendl("escape is pressed"); - mlx_close_window(vl->mlx); - return ; - } - if ((kd.key == MLX_KEY_H || kd.key == MLX_KEY_F1) && kd.action == MLX_PRESS) - ft_putendl("H is pressed"); -} - -void scrollhook(double xdelta, double ydelta, void *param) -{ - t_varlist *vl; - - vl = param; - vl = vl; - xdelta++; - if (ydelta > 0) - ft_putendl("scroll up"); - if (ydelta < 0) - ft_putendl("scroll down"); -} - -void cursorhook(double xpos, double ypos, void *param) -{ - t_varlist *vl; - double olddirx; - double oldplanex; - double speed; - double rotspeed; - - vl = param; - ypos++; - rotspeed = vl->frametime * 3.0; - if (xpos < vl->oldmouseposx) - { - 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); - oldplanex = vl->planex; - vl->planex = vl->planex * cos(speed) - vl->planey * sin(speed); - vl->planey = oldplanex * sin(speed) + vl->planey * cos(speed); - } - if (xpos > vl->oldmouseposx) - { - 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); - oldplanex = vl->planex; - vl->planex = vl->planex * cos(-speed) - vl->planey * sin(-speed); - vl->planey = oldplanex * sin(-speed) + vl->planey * cos(-speed); - } - vl->oldmouseposx = xpos; - if (ypos < vl->oldmouseposy) - if (vl->vaim < 300) - vl->vaim = vl->vaim + 3; - if (ypos > vl->oldmouseposy) - if (vl->vaim > -300) - vl->vaim = vl->vaim - 3; - vl->oldmouseposy = ypos; -} - -void resizehook(int x, int y, void *param) -{ - t_varlist *vl; - - vl = param; - vl->h = y; - vl->w = x; - vl->mlx->height = y; - vl->mlx->width = x; -} diff --git a/src/input/move.c b/src/input/move.c new file mode 100644 index 0000000..cd97feb --- /dev/null +++ b/src/input/move.c @@ -0,0 +1,105 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* move.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ +/* Updated: 2023/11/05 05:47:25 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void ft_moveforward(t_varlist *vl, double movespeed) +{ + double distance; + + if (vl->side) + distance = vl->sidedisty - vl->deltadisty; + else + distance = vl->sidedistx - vl->deltadistx; + if (vl->map[(int)(vl->posx + vl->dirx * movespeed)][(int)vl->posy] == '0' && distance > 0.4) + { + 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 * movespeed)] == '0' && distance > 0.4) + { + vl->posx += vl->dirx * movespeed * vl->run; + vl->posy += vl->diry * movespeed * vl->run; + } +} + +void ft_movebackward(t_varlist *vl, double movespeed) +{ + double distance; + + if (vl->side) + distance = vl->sidedisty - vl->deltadisty; + else + distance = vl->sidedistx - vl->deltadistx; + if (vl->map[(int)(vl->posx - vl->dirx * movespeed)][(int)vl->posy] == '0' && distance > 0.4) + { + 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 * movespeed)] == '0' && distance > 0.4) + { + vl->posx -= vl->dirx * movespeed * vl->run; + vl->posy -= vl->diry * movespeed * vl->run; + } +} + +void ft_moveleft(t_varlist *vl, double movespeed) +{ + double distance; + + if (vl->side) + distance = vl->sidedisty - vl->deltadisty; + else + distance = vl->sidedistx - vl->deltadistx; + if (vl->map[(int)(vl->posx - vl->dirx * movespeed)][(int)vl->posy] == '0' && distance > 0.4) + { + 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 * movespeed)] == '0' && distance > 0.4) + { + vl->posx -= vl->diry * movespeed * vl->run; + vl->posy += vl->dirx * movespeed * vl->run; + } +} + +void ft_moveright(t_varlist *vl, double movespeed) +{ + double distance; + + if (vl->side) + distance = vl->sidedisty - vl->deltadisty; + else + distance = vl->sidedistx - vl->deltadistx; + if (vl->map[(int)(vl->posx + vl->dirx * movespeed)][(int)vl->posy] == '0' && distance > 0.4) + { + 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 * movespeed)] == '0' && distance > 0.4) + { + vl->posx += vl->diry * movespeed * vl->run; + vl->posy -= vl->dirx * movespeed * vl->run; + } +} + +void ft_processmove(t_varlist *vl, double movespeed) +{ + if (mlx_is_key_down(vl->mlx, MLX_KEY_W)) + ft_moveforward(vl, movespeed); + if (mlx_is_key_down(vl->mlx, MLX_KEY_A)) + ft_moveleft(vl, movespeed); + if (mlx_is_key_down(vl->mlx, MLX_KEY_S)) + ft_movebackward(vl, movespeed); + if (mlx_is_key_down(vl->mlx, MLX_KEY_D)) + ft_moveright(vl, movespeed); +} diff --git a/src/input/rest.c b/src/input/rest.c new file mode 100644 index 0000000..28a20a6 --- /dev/null +++ b/src/input/rest.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* rest.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ +/* Updated: 2023/11/05 05:42:54 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void ft_processacro(t_varlist *vl, double movespeed) +{ + 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 * 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 * movespeed; + if (mlx_is_key_down(vl->mlx, MLX_KEY_LEFT_SHIFT)) + vl->run = 2; + else + vl->run = 1; +} + +void ft_processguns(t_varlist *vl) +{ + if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_LEFT)) + ft_putendl("shoot"); + if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_RIGHT)) + ft_putendl("zoom"); +} + +void scrollhook(double xdelta, double ydelta, void *param) +{ + t_varlist *vl; + + vl = param; + vl = vl; + xdelta++; + if (ydelta > 0) + ft_putendl("Previous Weapon"); + if (ydelta < 0) + ft_putendl("Next Weapon"); +} + diff --git a/src/input/turn.c b/src/input/turn.c new file mode 100644 index 0000000..77db1aa --- /dev/null +++ b/src/input/turn.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* turn.c :+: :+: */ +/* +:+ */ +/* By: houtworm +#+ */ +/* +#+ */ +/* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ +/* Updated: 2023/11/05 05:41:53 by houtworm ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../../cub3d.h" + +void ft_processturn(t_varlist *vl, double rotspeed) +{ + double temp; + + if (mlx_is_key_down(vl->mlx, MLX_KEY_LEFT)) + { + temp = vl->dirx; + 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(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(-rotspeed) - vl->diry * sin(-rotspeed); + vl->diry = temp * sin(-rotspeed) + vl->diry * cos(-rotspeed); + temp = vl->planex; + vl->planex = temp * cos(-rotspeed) - vl->planey * sin(-rotspeed); + vl->planey = temp * sin(-rotspeed) + vl->planey * cos(-rotspeed); + } +} + +void ft_turnvertical(double ypos, t_varlist *vl) +{ + if (ypos < vl->oldmouseposy) + if (vl->vaim < 300) + vl->vaim = vl->vaim + 3; + if (ypos > vl->oldmouseposy) + if (vl->vaim > -300) + vl->vaim = vl->vaim - 3; + vl->oldmouseposy = ypos; +} + +void ft_turnhorizontal(double xpos, t_varlist *vl) +{ + double temp; + + if (xpos < vl->oldmouseposx) + { + temp = vl->dirx; + vl->dirx = temp * cos(0.01) - vl->diry * sin(0.01); + vl->diry = temp * sin(0.01) + vl->diry * cos(0.01); + temp = vl->planex; + vl->planex = temp * cos(0.01) - vl->planey * sin(0.01); + vl->planey = temp * sin(0.01) + vl->planey * cos(0.01); + } + if (xpos > vl->oldmouseposx) + { + temp = vl->dirx; + vl->dirx = temp * cos(-0.01) - vl->diry * sin(-0.01); + vl->diry = temp * sin(-0.01) + vl->diry * cos(-0.01); + temp = vl->planex; + vl->planex = temp * cos(-0.01) - vl->planey * sin(-0.01); + vl->planey = temp * sin(-0.01) + vl->planey * cos(-0.01); + } + vl->oldmouseposx = xpos; +} + +void cursorhook(double xpos, double ypos, void *param) +{ + t_varlist *vl; + + vl = param; + ft_turnhorizontal(xpos, vl); + ft_turnvertical(ypos, vl); +} diff --git a/src/main/main.c b/src/main/main.c index ded717b..0561aae 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ -/* Updated: 2023/11/05 04:53:15 by houtworm ######## odam.nl */ +/* Updated: 2023/11/05 04:55:32 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -75,9 +75,7 @@ void mainloop(void *param) vl = param; ft_replaceimage(vl); - /*ft_drawceiling(vl);*/ - ft_drawwalls(vl); - /*ft_drawfloor(vl);*/ + ft_drawmap(vl); ft_pickup(vl); ft_drawsprites(vl); /*ft_drawweapon(vl);*/