optimizations

This commit is contained in:
djonker 2023-11-01 16:13:18 +01:00
parent 75c55fd629
commit 8c5d85c11d
8 changed files with 52 additions and 29 deletions

View File

@ -6,13 +6,13 @@
# By: houtworm <codam@houtworm.net> +#+ #
# +#+ #
# Created: 2023/10/26 10:46:29 by houtworm #+# #+# #
# Updated: 2023/11/01 14:10:13 by houtworm ######## odam.nl #
# Updated: 2023/11/01 14:59:05 by houtworm ######## odam.nl #
# #
# **************************************************************************** #
NAME =cub3d
CC =gcc
FC =-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

View File

@ -6,8 +6,6 @@ Cub3D is a simple raycasting game using the mlx library
### Parsing
- Flood fill the map starting from the player to check if it is a closed map
- Check if all needed elements are present in the closed part of the map
### Raycasting
- Directional textures
### Bonus
- Doors
- Minimap
@ -39,6 +37,7 @@ Cub3D is a simple raycasting game using the mlx library
- Render walls by raycasting
- Walk in 4 directions
- Rotate with arrow keys
- Directional textures
### Bonus
- Wall Collision
- Rotate with the mouse

11
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/31 16:29:47 by houtworm ######## odam.nl */
/* Updated: 2023/11/01 15:42:29 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -27,6 +27,11 @@ typedef struct s_varlist
mlx_image_t *img;
mlx_image_t *fps;
mlx_texture_t *curtext;
mlx_texture_t *northtext;
mlx_texture_t *easttext;
mlx_texture_t *southtext;
mlx_texture_t *westtext;
mlx_texture_t *barreltext;
int fpsrefresh;
int w;
int h;
@ -61,10 +66,6 @@ typedef struct s_varlist
double oldmouseposx;
int32_t fcolor;
int32_t ccolor;
char *northwt;
char *eastwt;
char *southwt;
char *westwt;
int resize;
} t_varlist;

View File

@ -1,4 +1,4 @@
C 0,100,255
C 50,50,50
F 100,100,100
NO ./assets/bluestone.png

View File

@ -6,12 +6,30 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:54:20 by houtworm #+# #+# */
/* Updated: 2023/11/01 14:16:57 by houtworm ######## odam.nl */
/* Updated: 2023/11/01 14:39:54 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void ft_selecttexture(t_varlist *vl)
{
if (vl->side == 0)
{
if (vl->raydirx > 0)
vl->curtext = vl->northtext;
else
vl->curtext = vl->southtext;
}
else
{
if (vl->raydiry > 0)
vl->curtext = vl->westtext;
else
vl->curtext = vl->easttext;
}
}
void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend)
{
int y;
@ -27,6 +45,7 @@ void ft_drawline(int x, t_varlist *vl, int drawstart, int drawend)
wallx = vl->posy + vl->perpwalldist * vl->raydiry;
else
wallx = vl->posx + vl->perpwalldist * vl->raydirx;
ft_selecttexture(vl);
wallx -= floor(wallx);
int textx;
textx = wallx * 64.0;

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:49:12 by houtworm #+# #+# */
/* Updated: 2023/11/01 14:14:08 by houtworm ######## odam.nl */
/* Updated: 2023/11/01 15:43:34 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -21,10 +21,11 @@ t_varlist initvarlist(void)
vl.hoffset = 50;
vl.ccolor = 0;
vl.fcolor = 0;
vl.northwt = NULL;
vl.eastwt = NULL;
vl.southwt = NULL;
vl.westwt = NULL;
vl.northtext = NULL;
vl.easttext = NULL;
vl.southtext = NULL;
vl.westtext = NULL;
vl.barreltext = mlx_load_png("./assets/barrel.png");
vl.mlx = mlx_init(vl.w, vl.h, "Cub3D", true);
if (!vl.mlx)
ft_errorexit("MLX failed to init", "initvarlist", 1);

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */
/* Updated: 2023/11/01 14:17:09 by houtworm ######## odam.nl */
/* Updated: 2023/11/01 15:42:14 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -56,7 +56,6 @@ int main(int argc, char **argv)
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 to window failed ", "main", 1);
vl.curtext = mlx_load_png(vl.southwt);
mlx_key_hook(vl.mlx, &keyhook, &vl);
mlx_resize_hook(vl.mlx, &resizehook, &vl);
mlx_scroll_hook(vl.mlx, &scrollhook, &vl);
@ -64,7 +63,11 @@ int main(int argc, char **argv)
mlx_cursor_hook(vl.mlx, &cursorhook, &vl);
mlx_loop_hook(vl.mlx, &mainloop, &vl);
mlx_loop(vl.mlx);
mlx_delete_texture(vl.curtext);
mlx_delete_texture(vl.northtext);
mlx_delete_texture(vl.easttext);
mlx_delete_texture(vl.southtext);
mlx_delete_texture(vl.westtext);
mlx_delete_texture(vl.barreltext);
mlx_delete_image(vl.mlx, vl.img);
mlx_terminate(vl.mlx);
exit (0);

View File

@ -6,7 +6,7 @@
/* By: houtworm <codam@houtworm.net> +#+ */
/* +#+ */
/* Created: 2023/10/26 16:48:55 by houtworm #+# #+# */
/* Updated: 2023/10/29 16:32:12 by houtworm ######## odam.nl */
/* Updated: 2023/11/01 15:42:55 by houtworm ######## odam.nl */
/* */
/* ************************************************************************** */
@ -20,13 +20,13 @@ int ft_settexture(t_varlist *vl, char *line, int direction)
while (line[i] != '.')
i++;
if (direction == 1)
vl->northwt = ft_strdup(&line[i]);
vl->northtext = mlx_load_png(&line[i]);
if (direction == 2)
vl->eastwt = ft_strdup(&line[i]);
vl->easttext = mlx_load_png(&line[i]);
if (direction == 3)
vl->southwt = ft_strdup(&line[i]);
vl->southtext = mlx_load_png(&line[i]);
if (direction == 4)
vl->westwt = ft_strdup(&line[i]);
vl->westtext = mlx_load_png(&line[i]);
return (0);
}
@ -76,7 +76,7 @@ char *ft_checkline(t_varlist *vl, char *line)
}
else if (!ft_strncmp(line, "NO ", 3))
{
if (!vl->northwt)
if (!vl->northtext)
ft_settexture(vl, line, 1);
else
return (" north texture");
@ -88,21 +88,21 @@ char *ft_checkline2(t_varlist *vl, char *line)
{
if (!ft_strncmp(line, "EA ", 3))
{
if (!vl->eastwt)
if (!vl->easttext)
ft_settexture(vl, line, 2);
else
return (" east texture");
}
else if (!ft_strncmp(line, "SO ", 3))
{
if (!vl->southwt)
if (!vl->southtext)
ft_settexture(vl, line, 3);
else
return (" south texture");
}
else if (!ft_strncmp(line, "WE ", 3))
{
if (!vl->westwt)
if (!vl->westtext)
ft_settexture(vl, line, 4);
else
return (" west texture");
@ -126,7 +126,7 @@ t_varlist ft_parseconfigfile(t_varlist vl, char *filename)
error = ft_checkline2(&vl, line);
if (error)
ft_errorexit("Double config declaration in .cub file", error, 1);
if (vl.ccolor && vl.fcolor && vl.northwt && vl.eastwt && vl.southwt && vl.westwt)
if (vl.ccolor && vl.fcolor && vl.northtext && vl.easttext && vl.southtext && vl.westtext)
{
vl.map = ft_getmap(&vl, fd);
break ;