docs, clean, several things... *

- add docs
- remove unused things
- redo modifications i accidentally erased from spacetime
This commit is contained in:
mcolonna 2024-11-07 01:03:05 +01:00
parent 47a6b6df35
commit 26621c72c6
25 changed files with 199 additions and 140 deletions

View file

@ -6,7 +6,7 @@
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */
/* Updated: 2024/10/31 17:20:56 by mc ### ########.fr */
/* Updated: 2024/11/01 20:56:43 by mc ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,4 +15,3 @@
void *g_mlx = NULL;
void *g_win = NULL;
t_map g_map;
t_player g_player;

View file

@ -6,7 +6,7 @@
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 14:43:36 by mcolonna #+# #+# */
/* Updated: 2024/10/31 17:21:00 by mc ### ########.fr */
/* Updated: 2024/11/01 20:59:32 by mc ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,13 +28,13 @@ static void set_action(int keycode, bool value)
g_input_actions.quit = value;
}
int keypress(int keycode)
int hook_keypress(int keycode)
{
set_action(keycode, true);
return (0);
}
int keyrelease(int keycode)
int hook_keyrelease(int keycode)
{
set_action(keycode, false);
return (0);
@ -42,9 +42,5 @@ int keyrelease(int keycode)
void input_init(void)
{
g_input_actions.down = false;
g_input_actions.up = false;
g_input_actions.left = false;
g_input_actions.right = false;
g_input_actions.quit = false;
ft_memset(&g_input_actions, 0, sizeof(g_input_actions));
}

View file

@ -6,7 +6,7 @@
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 16:57:40 by mc #+# #+# */
/* Updated: 2024/10/31 17:55:18 by mc ### ########.fr */
/* Updated: 2024/11/07 00:07:35 by mc ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,32 +18,16 @@
# include "const.h"
# include "global.h"
static void load_textures(t_tex *tex)
{
tex->tex_width = 64;
tex->tex_height = 64;
tex->textures[0] = mlx_xpm_file_to_image(g_mlx, "textures/north.xpm",
&tex->tex_width, &tex->tex_height);
tex->textures[1] = mlx_xpm_file_to_image(g_mlx, "textures/south.xpm",
&tex->tex_width, &tex->tex_height);
tex->textures[2] = mlx_xpm_file_to_image(g_mlx, "textures/east.xpm",
&tex->tex_width, &tex->tex_height);
tex->textures[3] = mlx_xpm_file_to_image(g_mlx, "textures/west.xpm",
&tex->tex_width, &tex->tex_height);
}
static void draw_screen(void)
{
void *img_ptr;
u_int32_t *img_data;
t_tex tex;
load_textures(&tex);
img_ptr = mlx_new_image(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT);
img_data = get_data_addr(img_ptr);
if (!img_data)
exit(1);
render(img_data, &tex);
render(img_data);
mlx_put_image_to_window(g_mlx, g_win, img_ptr, 0, 0);
mlx_destroy_image(g_mlx, img_ptr);
}
@ -78,8 +62,8 @@ int main(int argc, char *argv[])
if (!map_from_file(&g_map, argv[1]))
return (1);
g_win = mlx_new_window(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT, "cub3d");
mlx_hook(g_win, KeyPress, KeyPressMask, keypress, NULL);
mlx_hook(g_win, KeyRelease, KeyReleaseMask, keyrelease, NULL);
mlx_hook(g_win, KeyPress, KeyPressMask, hook_keypress, NULL);
mlx_hook(g_win, KeyRelease, KeyReleaseMask, hook_keyrelease, NULL);
mlx_loop_hook(g_mlx, loop_hook, NULL);
draw_screen();
mlx_loop(g_mlx);

View file

@ -6,7 +6,7 @@
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 17:12:58 by mcolonna #+# #+# */
/* Updated: 2024/10/31 17:56:25 by mc ### ########.fr */
/* Updated: 2024/11/06 19:29:59 by mc ### ########.fr */
/* */
/* ************************************************************************** */
@ -68,15 +68,21 @@ bool map_from_file(t_map *dest, const char *file)
close(fd);
return (false);
}
return (map_from_file2(dest, &stream, fd));
if (!map_from_file2(dest, &stream, fd))
return (false);
return (load_textures(dest));
}
void map_destroy(t_map *map)
{
free((void *)map->texture_east);
free((void *)map->texture_west);
free((void *)map->texture_north);
free((void *)map->texture_south);
t_direction d;
d = 0;
while (d < 4)
{
free((void *)map->textures[d].image);
free((void *)map->texture_srcs[d]);
}
free(map->cases);
}
@ -109,6 +115,7 @@ static bool check_map2(const t_map *map, unsigned int x, unsigned int y)
}
// TODO check player
bool check_map(const t_map *map)
{
unsigned int x;

View file

@ -6,7 +6,7 @@
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/03 15:05:13 by mc #+# #+# */
/* Updated: 2024/10/31 17:56:54 by mc ### ########.fr */
/* Updated: 2024/11/06 19:34:01 by mc ### ########.fr */
/* */
/* ************************************************************************** */
@ -74,4 +74,6 @@ bool read_map_description(t_map *map, t_stream *stream);
/// @param map Map to use.
void map_init_objects(t_map *map);
bool load_textures(t_map *map);
#endif

View file

@ -6,7 +6,7 @@
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/03 15:02:09 by mc #+# #+# */
/* Updated: 2024/10/31 17:21:36 by mc ### ########.fr */
/* Updated: 2024/11/06 19:33:31 by mc ### ########.fr */
/* */
/* ************************************************************************** */
@ -74,37 +74,33 @@ bool read_string_parameter(const char *name, const char **dest,
static void read_map2(t_map *dest, bool *err, bool *rdf)
{
dest->cases = NULL;
ft_memset(dest, '\0', sizeof(dest));
dest->color_ceiling = 0xFF000000;
dest->color_floor = 0xFF000000;
dest->texture_east = NULL;
dest->texture_west = NULL;
dest->texture_north = NULL;
dest->texture_south = NULL;
*err = false;
*rdf = false;
}
bool read_map(t_map *dest, t_stream *stream)
bool read_map(t_map *dest, t_stream *s)
{
bool err;
bool rdf;
int old_stream_i;
read_map2(dest, &err, &rdf);
while (!rdf && !err && stream->str[stream->i])
while (!rdf && !err && s->str[s->i])
{
old_stream_i = stream->i;
if ((read_expected_string("\n", stream, &err), err)
&& !read_string_parameter("NO", &dest->texture_north, stream, &rdf)
&& !read_string_parameter("SO", &dest->texture_south, stream, &rdf)
&& !read_string_parameter("WE", &dest->texture_west, stream, &rdf)
&& !read_string_parameter("EA", &dest->texture_east, stream, &rdf)
&& !read_color_parameter("F", &dest->color_floor, stream, &rdf)
&& !read_color_parameter("C", &dest->color_ceiling, stream, &rdf))
old_stream_i = s->i;
if ((read_expected_string("\n", s, &err), err)
&& !read_string_parameter("NO", &dest->texture_srcs[NORTH], s, &rdf)
&& !read_string_parameter("SO", &dest->texture_srcs[SOUTH], s, &rdf)
&& !read_string_parameter("WE", &dest->texture_srcs[WEST], s, &rdf)
&& !read_string_parameter("EA", &dest->texture_srcs[EAST], s, &rdf)
&& !read_color_parameter("F", &dest->color_floor, s, &rdf)
&& !read_color_parameter("C", &dest->color_ceiling, s, &rdf))
{
stream->i = old_stream_i;
err = !read_map_description(dest, stream);
s->i = old_stream_i;
err = !read_map_description(dest, s);
break ;
}
err = false;

View file

@ -6,12 +6,14 @@
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/04 15:12:08 by mcolonna #+# #+# */
/* Updated: 2024/10/31 17:21:44 by mc ### ########.fr */
/* Updated: 2024/11/07 00:08:13 by mc ### ########.fr */
/* */
/* ************************************************************************** */
#include "map_utils.h"
#include "global.h"
/// @brief Read the map description to get the dimensions.
/// The map must be at least 1x1.
///
@ -100,3 +102,17 @@ bool read_map_description(t_map *map, t_stream *stream)
}
return (true);
}
bool load_textures(t_map *map)
{
t_direction d;
d = 0;
while (d < 4) {
map->textures[d].image = mlx_xpm_file_to_image(g_mlx,
(char *)(map->texture_srcs[d]),
&map->textures[d].width, &map->textures[d].height);
d++;
}
return (true);
}

View file

@ -6,16 +6,13 @@
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 14:55:05 by greg #+# #+# */
/* Updated: 2024/10/31 18:02:59 by mc ### ########.fr */
/* Updated: 2024/11/07 00:54:17 by mc ### ########.fr */
/* */
/* ************************************************************************** */
#include "render.h"
#include "render_utils.h"
#include "const.h"
#include "global.h"
static void calculate_perpwalldist3(int x,
t_point_double *deltadist,
t_ray *ray)
@ -134,15 +131,22 @@ static void draw_vertical_line(int x, u_int32_t *img_data,
}
}
int render(u_int32_t *img_data, t_tex *tex)
int render(u_int32_t *img_data)
{
int x;
t_ray ray;
t_tex tex;
tex.tex_width = 64; // TODO fix
tex.tex_height = 64; // TODO fix
tex.textures[NORTH] = g_map.textures[NORTH].image;
tex.textures[SOUTH] = g_map.textures[SOUTH].image;
tex.textures[WEST] = g_map.textures[WEST].image;
tex.textures[EAST] = g_map.textures[EAST].image;
x = 0;
while (x < SCREEN_WIDTH)
{
draw_vertical_line(x, img_data, &ray, tex);
draw_vertical_line(x, img_data, &ray, &tex);
x++;
}
return (0);

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* render_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/24 14:26:22 by grobledo #+# #+# */
/* Updated: 2024/10/31 18:02:20 by mc ### ########.fr */
/* Updated: 2024/11/04 14:00:25 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,16 +39,18 @@ void draw_wall(t_tex *tex, t_ray *ray, const int line_height, int x,
{
u_int32_t color;
u_int32_t *texture_data;
int texx;
int texy;
tex->tex_y = ((y - (-line_height / 2 + SCREEN_HEIGHT / 2)) \
texy = ((y - (-line_height / 2 + SCREEN_HEIGHT / 2)) \
* tex->tex_height) / line_height;
tex->tex_x = (int)(ray->wallx * (double)tex->tex_width);
texx = (int)(ray->wallx * (double)tex->tex_width);
if (ray->side == 0 && ray->dir.x > 0)
tex->tex_x = tex->tex_width - tex->tex_x - 1;
texx = tex->tex_width - texx - 1;
if (ray->side == 1 && ray->dir.y < 0)
tex->tex_x = tex->tex_height - tex->tex_x - 1;
texx = tex->tex_height - texx - 1;
texture_data = (u_int32_t *)mlx_get_data_addr(tex->textures[tex->tex_dir],
&tex->bpp, &tex->size_line, &tex->endian);
color = texture_data[tex->tex_x * tex->tex_width + tex->tex_y];
color = texture_data[texx * tex->tex_width + texy];
img_data[y * SCREEN_WIDTH + x] = color;
}

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* render_utils.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/24 14:26:22 by grobledo #+# #+# */
/* Updated: 2024/10/31 18:01:59 by mc ### ########.fr */
/* Updated: 2024/11/04 13:07:42 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: mc <mc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 12:38:40 by mcolonna #+# #+# */
/* Updated: 2024/10/31 17:25:30 by mc ### ########.fr */
/* Updated: 2024/11/07 00:05:39 by mc ### ########.fr */
/* */
/* ************************************************************************** */