time for bed

This commit is contained in:
djonker 2023-10-26 23:48:16 +02:00
parent 686d857985
commit 2501ac8492
5 changed files with 130 additions and 13 deletions

View File

@ -6,13 +6,13 @@
# By: houtworm <codam@houtworm.net> +#+ #
# +#+ #
# Created: 2023/10/26 10:46:29 by houtworm #+# #+# #
# Updated: 2023/10/26 19:16:37 by houtworm ######## odam.nl #
# Updated: 2023/10/26 23:24:34 by houtworm ######## odam.nl #
# #
# **************************************************************************** #
NAME =cub3d
CC =gcc
FC =-Wall -Werror -Wextra -Wunreachable-code -Ofast -g #-fsanitize=address
FC =-Wall -Werror -Wextra -Wunreachable-code -Ofast -g -fsanitize=address
HEAD =-I ./include -I $(MLX)/include
RM =rm -rf
LIB =libft/libft.a getnextline/get_next_line.a mlx/build/libmlx42.a -ldl -lglfw -pthread -lm

26
cub3d.h
View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 10:46:35 by houtworm #+# #+# */
/* Updated: 2023/10/26 17:25:30 by houtworm ######## odam.nl */
/* Updated: 2023/10/26 21:52:52 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -27,9 +27,27 @@ typedef struct s_varlist
int w;
int h;
char **map;
int playerx;
int playery;
int playerd;
double posx;
double posy;
double dirx;
double diry;
double planex;
double planey;
double camerax;
double cameray;
double raydirx;
double raydiry;
int mapx;
int mapy;
double sidedistx;
double sidedisty;
double deltadistx;
double deltadisty;
double perpwalldist;
int stepx;
int stepy;
int hit;
int side;
} t_varlist;
// init.c

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */
/* Updated: 2023/10/26 20:38:07 by houtworm ######## odam.nl */
/* Updated: 2023/10/26 21:53:49 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */
/* Updated: 2023/10/26 17:19:28 by houtworm ######## odam.nl */
/* Updated: 2023/10/26 22:03:37 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -18,6 +18,12 @@ t_varlist initvarlist(void)
vl.w = 600;
vl.h = 600;
vl.posx = 22;
vl.posy = 12;
vl.dirx = -1;
vl.diry = 0;
vl.planex = 0;
vl.planey = 0.66;
vl.mlx = mlx_init(vl.w, vl.h, "Cub3D", true);
if (!vl.mlx)
ft_errorexit("MLX failed to init", "initvarlist", 1);

View File

@ -6,13 +6,105 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */
/* Updated: 2023/10/26 20:38:56 by houtworm ######## odam.nl */
/* Updated: 2023/10/26 23:47:46 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
#include <stdio.h>
void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend)
{
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++;
}
}
void mainloop(void *param)
{
t_varlist *vl;
@ -21,7 +113,8 @@ void mainloop(void *param)
mlx_delete_image(vl->mlx, vl->img);
vl->img = mlx_new_image(vl->mlx, vl->w, vl->h);
ft_movementkeys(vl);
ft_drawnextframe(vl);
ft_raycast(vl);
/*ft_drawnextframe(vl);*/
ft_putnbr(1 / vl->mlx->delta_time);
ft_putendl(" FPS");
if (!vl->img || (mlx_image_to_window(vl->mlx, vl->img, 0, 0) < 0))
@ -33,14 +126,14 @@ int main(int argc, char **argv)
t_varlist vl;
if (argc > 2)
ft_errorexit("Too many arguments", "main", 2);
ft_errorexit("Too many arguments", "", 2);
vl = initvarlist();
if (argc == 2)
vl = ft_parseconfigfile(vl, argv[1]);
else
ft_errorexit("Please include a .cub file ", "main", 2); // we could start with a menu here
ft_errorexit("Please include a .cub file ", "", 2); // we could start with a menu here
if (!vl.img || (mlx_image_to_window(vl.mlx, vl.img, 0, 0) < 0))
ft_errorexit("image failed to window ", "main", 1);
ft_errorexit("image to window failed ", "main", 1);
mlx_key_hook(vl.mlx, &keyhook, &vl);
mlx_resize_hook(vl.mlx, &resizehook, &vl);
mlx_scroll_hook(vl.mlx, &scrollhook, &vl);