split up the main, ceiling and floor is drawn

This commit is contained in:
djonker 2023-10-26 16:57:23 +02:00
parent 4a0671fce6
commit 42d03c95aa
9 changed files with 226 additions and 91 deletions

View File

@ -6,7 +6,7 @@
# By: houtworm <codam@houtworm.net> +#+ #
# +#+ #
# Created: 2023/10/26 10:46:29 by houtworm #+# #+# #
# Updated: 2023/10/26 10:56:42 by houtworm ######## odam.nl #
# Updated: 2023/10/26 16:56:54 by houtworm ######## odam.nl #
# #
# **************************************************************************** #
@ -16,7 +16,11 @@ FC =-Wall -Werror -Wextra -Wunreachable-code -Ofast -g #-fsanitize=address
HEAD =-I ./include -I $(MLX)/include
RM =rm -rf
LIB =libft/libft.a mlx/build/libmlx42.a -ldl -lglfw -pthread -lm
SRC =src/main.c
SRC =src/main.c\
src/init.c\
src/parse.c\
src/keys.c\
src/draw.c
OBJ =$(SRC:src/%.c=obj/%.o)
all: libft mlx/build/mlx42.a $(NAME)

View File

@ -20,6 +20,7 @@ Cub3D is a simple raycasting game using the mlx library
- Animated Sprites
- Rotate with the mouse
### Extra
- FPS counter
- A Menu?
- Levels?
- Skybox?

BIN
cub3d

Binary file not shown.

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 13:56:49 by houtworm ######## odam.nl */
/* Updated: 2023/10/26 16:56:10 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -20,11 +20,25 @@
typedef struct s_varlist
{
mlx_t *mlx; //mlx instance
mlx_image_t *img; //mlx img
mlx_image_t *font[30]; //mlx font image
int w; //actual window width used everywhere
int h; //actual window height used everywhere
mlx_t *mlx;
mlx_image_t *img;
int w;
int h;
char **map;
int playerx;
int playery;
int playerd;
} t_varlist;
// init.c
t_varlist initvarlist(void);
// parse.c
char **ft_getmap(void);
// keys.c
void ft_movementkeys(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);
// draw.c
void ft_drawnextframe(t_varlist *vl);
#endif

38
src/draw.c Normal file
View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* draw.c :+: :+: */
/* +:+ */
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */
/* Updated: 2023/10/26 16:54:33 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void ft_drawnextframe(t_varlist *vl)
{
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;
while (vl->h > y)
{
if (y <= half)
mlx_put_pixel(vl->img, x, y, 0x005566FF);
else
mlx_put_pixel(vl->img, x, y, 0x444444FF);
y++;
}
x++;
}
}

28
src/init.c Normal file
View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* init.c :+: :+: */
/* +:+ */
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */
/* Updated: 2023/10/26 16:49:14 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
t_varlist initvarlist(void)
{
t_varlist vl;
vl.w = 600;
vl.h = 600;
vl.mlx = mlx_init(vl.w, vl.h, "Cub3D", true);
if (!vl.mlx)
exit (1);
vl.img = mlx_new_image(vl.mlx, vl.w, vl.h);
vl.map = ft_getmap();
// we should validate the map here
return (vl);
}

68
src/keys.c Normal file
View File

@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* keys.c :+: :+: */
/* +:+ */
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */
/* Updated: 2023/10/26 16:50:33 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void ft_movementkeys(t_varlist *vl)
{
if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_LEFT))
ft_putendl("shoot");
if (mlx_is_key_down(vl->mlx, MLX_KEY_W))
ft_putendl("W is pressed");
if (mlx_is_key_down(vl->mlx, MLX_KEY_A))
ft_putendl("A is pressed");
if (mlx_is_key_down(vl->mlx, MLX_KEY_S))
ft_putendl("S is pressed");
if (mlx_is_key_down(vl->mlx, MLX_KEY_D))
ft_putendl("D is pressed");
}
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.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 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;
}

View File

@ -6,104 +6,26 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */
/* Updated: 2023/10/26 15:01:26 by houtworm ######## odam.nl */
/* Updated: 2023/10/26 16:54:42 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void ft_getnextframe(t_varlist *vl)
{
int y;
int x;
y = 1;
while (vl->h > y)
{
x = 1;
while (vl->w > x)
{
mlx_put_pixel(vl->img, x, y, 0x000000FF);
x++;
}
y++;
}
}
void framehook(void *param)
void mainloop(void *param)
{
t_varlist *vl;
vl = param;
mlx_delete_image(vl->mlx, vl->img);
vl->img = mlx_new_image(vl->mlx, vl->w, vl->h);
ft_getnextframe(vl);
ft_movementkeys(vl);
ft_drawnextframe(vl);
if (!vl->img || (mlx_image_to_window(vl->mlx, vl->img, 0, 0) < 0))
exit (1);
ft_putendl("frame");
}
void keyhook(mlx_key_data_t kd, void *param)
{
t_varlist *vl;
vl = param;
vl->w =vl->w;
if (mlx_is_mouse_down(vl->mlx, MLX_MOUSE_BUTTON_LEFT))
ft_putendl("shoot");
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_W && kd.action == MLX_PRESS)
ft_putendl("W is pressed");
if (kd.key == MLX_KEY_A && kd.action == MLX_PRESS)
ft_putendl("A is pressed");
if (kd.key == MLX_KEY_S && kd.action == MLX_PRESS)
ft_putendl("S is pressed");
if (kd.key == MLX_KEY_D && kd.action == MLX_PRESS)
ft_putendl("D 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 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;
}
t_varlist initvarlist(void)
{
t_varlist vl;
vl.w = 600;
vl.h = 600;
vl.mlx = mlx_init(vl.w, vl.h, "Cub3D", true);
if (!vl.mlx)
exit (1);
vl.img = mlx_new_image(vl.mlx, vl.w, vl.h);
return (vl);
}
int main(int argc, char **argv)
{
t_varlist vl;
@ -117,7 +39,7 @@ int main(int argc, char **argv)
mlx_key_hook(vl.mlx, &keyhook, &vl);
mlx_resize_hook(vl.mlx, &resizehook, &vl);
mlx_scroll_hook(vl.mlx, &scrollhook, &vl);
mlx_loop_hook(vl.mlx, &framehook, &vl);
mlx_loop_hook(vl.mlx, &mainloop, &vl);
mlx_loop(vl.mlx);
mlx_delete_image(vl.mlx, vl.img);
mlx_terminate(vl.mlx);

60
src/parse.c Normal file
View File

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* parse.c :+: :+: */
/* +:+ */
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:48:55 by houtworm #+# #+# */
/* Updated: 2023/10/26 16:49:07 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
char **ft_getmap(void)
{
int y;
int x;
char **map;
char localmap[24][24] = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,1,1,1,1,0,0,0,0,1,0,1,0,1,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1},
{1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,1,1,0,1,1,0,0,0,0,1,0,1,0,1,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};
map = ft_calloc(24, 8);
y = 0;
while (y < 24)
{
map[y] = ft_calloc(24, 8);
x = 0;
while (x < 24)
{
map[y][x] = localmap[y][x];
x++;
}
y++;
}
return (map);
}