little cleanup

This commit is contained in:
djonker 2023-11-05 06:38:58 +01:00
parent 30c67f5dc6
commit e15684fd69
10 changed files with 413 additions and 271 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/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/draw/draw.c\
src/main/error.c\ src/main/error.c\
src/draw/raycast.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/parse.c\
src/parse/map.c src/parse/map.c
OBJ =$(SRC:src/%.c=obj/%.o) OBJ =$(SRC:src/%.c=obj/%.o)

10
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/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 sidedisty;
double deltadistx; double deltadistx;
double deltadisty; double deltadisty;
double perpwalldist; double walldist;
int lineheight; int lineheight;
int side; int side;
double oldmouseposx; double oldmouseposx;
@ -93,12 +93,16 @@ t_varlist ft_parseconfigfile(t_varlist vl, char *filename);
char **ft_getmap(t_varlist *vl, int fd); char **ft_getmap(t_varlist *vl, int fd);
// keys.c // keys.c
void ft_processinput(t_varlist *vl); 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 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);
void cursorhook(double x, double y, void *param); void cursorhook(double x, double y, void *param);
// raycast.c // raycast.c
void ft_drawwalls(t_varlist *vl); void ft_drawmap(t_varlist *vl);
// draw.c // draw.c
void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend); void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend);
void ft_drawsprites(t_varlist *vl); void ft_drawsprites(t_varlist *vl);

View File

@ -6,7 +6,7 @@
/* 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/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; double wallx;
if (vl->side == 0) if (vl->side == 0)
wallx = vl->posy + vl->perpwalldist * vl->raydiry; wallx = vl->posy + vl->walldist * vl->raydiry;
else else
wallx = vl->posx + vl->perpwalldist * vl->raydirx; wallx = vl->posx + vl->walldist * vl->raydirx;
ft_selecttexture(vl); ft_selecttexture(vl);
wallx -= floor(wallx); wallx -= floor(wallx);
int textx; int textx;
@ -182,7 +182,7 @@ void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend)
double step; double step;
step = 64.0 / vl->lineheight; step = 64.0 / vl->lineheight;
double textpos; 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) while (y < drawend)
{ {
int texty; int texty;

View File

@ -6,35 +6,16 @@
/* By: djonker <codam@houtworm.net> +#+ */ /* By: djonker <codam@houtworm.net> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2023/10/27 14:36:42 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" #include "../../cub3d.h"
void ft_drawwalls(t_varlist *vl) int ft_getstepx(t_varlist *vl, int mapx)
{ {
int x;
int drawstart;
int drawend;
double camerax;
int mapx;
int mapy;
int stepx; int stepx;
int stepy;
int hit;
x = 1;
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) if (vl->raydirx < 0)
{ {
stepx = -1; stepx = -1;
@ -45,6 +26,13 @@ void ft_drawwalls(t_varlist *vl)
stepx = 1; stepx = 1;
vl->sidedistx = (mapx + 1.0 - vl->posx) * vl->deltadistx; 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) if (vl->raydiry < 0)
{ {
stepy = -1; stepy = -1;
@ -55,6 +43,29 @@ void ft_drawwalls(t_varlist *vl)
stepy = 1; stepy = 1;
vl->sidedisty = (mapy + 1.0 - vl->posy) * vl->deltadisty; vl->sidedisty = (mapy + 1.0 - vl->posy) * vl->deltadisty;
} }
return (stepy);
}
void ft_prepcast(t_varlist *vl, int x)
{
double camerax;
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;
stepx = ft_getstepx(vl, mapx);
stepy = ft_getstepy(vl, mapy);
hit = 0;
while (hit == 0) while (hit == 0)
{ {
if (vl->sidedistx < vl->sidedisty) if (vl->sidedistx < vl->sidedisty)
@ -72,19 +83,47 @@ void ft_drawwalls(t_varlist *vl)
if (vl->map[mapx][mapy] == '1') if (vl->map[mapx][mapy] == '1')
hit = 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)
{
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) if (vl->side == 0)
vl->perpwalldist = (vl->sidedistx - vl->deltadistx); vl->walldist = (vl->sidedistx - vl->deltadistx);
else else
vl->perpwalldist = (vl->sidedisty - vl->deltadisty); vl->walldist = (vl->sidedisty - vl->deltadisty);
vl->lineheight = vl->h / vl->perpwalldist; vl->lineheight = vl->h / vl->walldist;
drawstart = -vl->lineheight / 2 + vl->h / 2 + vl->vaim + (vl->jump / vl->perpwalldist); vl->distance[x] = vl->walldist;
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;
x++; x++;
} }
} }

53
src/input/game.c Normal file
View File

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

View File

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

105
src/input/move.c Normal file
View File

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

51
src/input/rest.c Normal file
View File

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* rest.c :+: :+: */
/* +:+ */
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* 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");
}

82
src/input/turn.c Normal file
View File

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

View File

@ -6,7 +6,7 @@
/* 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/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; vl = param;
ft_replaceimage(vl); ft_replaceimage(vl);
/*ft_drawceiling(vl);*/ ft_drawmap(vl);
ft_drawwalls(vl);
/*ft_drawfloor(vl);*/
ft_pickup(vl); ft_pickup(vl);
ft_drawsprites(vl); ft_drawsprites(vl);
/*ft_drawweapon(vl);*/ /*ft_drawweapon(vl);*/