diff --git a/.gitignore b/.gitignore index cdf1786..53550e5 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ .vscode/ obj/ app +app.dSYM compile_commands.json libft/build/libft.a Session.vim diff --git a/.vscode/settings.json b/.vscode/settings.json index cc134d0..947e08f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "files.associations": { - "cstdlib": "c" + "cstdlib": "c", + "stdio.h": "c" } } \ No newline at end of file diff --git a/MLX42/include/MLX42/MLX42_Int.h b/MLX42/include/MLX42/MLX42_Int.h index c6ab66a..5fa5558 100644 --- a/MLX42/include/MLX42/MLX42_Int.h +++ b/MLX42/include/MLX42/MLX42_Int.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* MLX42_Int.h :+: :+: */ +/* MLX42_Int.h :+: :+: */ /* +:+ */ /* By: W2Wizard +#+ */ /* +#+ */ /* Created: 2021/12/27 23:55:34 by W2Wizard #+# #+# */ -/* Updated: 2022/07/21 10:46:43 by sbos ######## odam.nl */ +/* Updated: 2024/01/17 12:49:14 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ # include /* va_arg, va_end, ... */ # include /* assert, static_assert, ... */ # ifndef MLX_SWAP_INTERVAL -# define MLX_SWAP_INTERVAL 1 +# define MLX_SWAP_INTERVAL 0 # endif # ifndef MLX_BATCH_SIZE # define MLX_BATCH_SIZE 12000 diff --git a/Makefile b/Makefile index e614a1d..ff55b90 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ else ifeq ($(shell uname -m),x86_64) endif NAME := app -RUN_CMD := ./$(NAME) test_maps/valid.cub +RUN_CMD := ./$(NAME) test_maps/valid_tex.cub # RUN_CMD := ./$(NAME) test_maps/small.cub # CFLAGS += -Wall -Wextra -Werror @@ -48,12 +48,14 @@ SRCS = parser/check_elements.c \ game/render_minimap.c \ game/render_viewport.c \ game/font/font_renderer.c \ + renderer/pixel_picker.c \ vector/vec2i.c \ vector/vec2d.c \ vector/vec_utils.c \ cub3d.c \ test_utils.c \ - timer.c + timer.c \ + set_textures.c HEADER_DIR := include HEADERS := meta.h \ diff --git a/include/meta.h b/include/meta.h index 17e2516..d0fb002 100644 --- a/include/meta.h +++ b/include/meta.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /**/ /* :::::::: */ -/* meta.h:+::+: */ +/* meta.h :+: :+: */ /*+:+ */ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/01 20:07:37 by jboeve#+##+# */ -/* Updated: 2024/01/07 03:51:44 by joppe ######## odam.nl */ +/* Updated: 2024/01/18 11:56:06 by jboeve ######## odam.nl */ /**/ /* ************************************************************************** */ @@ -56,8 +56,6 @@ #define WINDOW_WIDTH 1280 #define WINDOW_HEIGHT 720 -#define WINDOW_TITLE "Gammoe" - // Game @@ -110,7 +108,7 @@ typedef struct s_font_atlas unsigned int font_w; unsigned int font_h; unsigned int bpp; - char *pixels; + char *pixels; } t_font_atlas; @@ -128,17 +126,25 @@ typedef union s_rgba typedef enum e_side { - HIT_NONE, - HIT_NS, - HIT_EW, + SIDE_N, + SIDE_S, + SIDE_E, + SIDE_W, } t_side; typedef struct s_ray { - t_vec2d direction; - t_vec2d end; - t_side hit_side; - double length; - double wall_x; + t_vec2d direction; + t_vec2d end; + t_vec2i map_pos; + t_vec2i texture_point; + t_vec2i line_point; + t_side hit_side; + + double line_height; + double length; + double wall_x; + double texture_position; + double step; } t_ray; typedef struct s_player { @@ -158,14 +164,21 @@ typedef struct s_map { char player_start_dir; } t_map; + typedef struct s_tex { - char *no; - char *so; - char *we; - char *ea; + char *tex_path; + mlx_texture_t *tex; +} t_tex; + +typedef struct s_attr { + t_tex n; + t_tex s; + t_tex e; + t_tex w; t_rgba floor_c; t_rgba ceiling_c; -} t_tex; +} t_attr; + typedef struct s_minimap { mlx_image_t *minimap_image; @@ -183,7 +196,8 @@ typedef struct s_meta { t_player player; uint32_t fps; t_map map; - t_tex tex; + t_attr attributes; + const char *scene_name; char *map_element; } t_meta; @@ -231,9 +245,17 @@ t_ray raycaster_cast(t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit, const void *pa // colors.c int32_t set_color(int32_t r, int32_t g, int32_t b, int32_t a); -int32_t find_wall_color(t_side side); +int32_t find_wall_color(t_attr atrributes, t_ray *ray, t_vec2i line_points, uint32_t h); +int32_t find_color(t_rgba rgba); // free.c void meta_free(t_meta *meta); +// set_textures.c +int set_textures(t_attr *attributes); + +//pixel_picker.c +uint32_t pixel_picker(mlx_texture_t *texture, int32_t x, int32_t y); +void wall_texture_position(mlx_texture_t *texture, t_ray *ray, t_vec2i line_points, uint32_t h); + #endif diff --git a/include/parser.h b/include/parser.h index 48abb2d..8a6036e 100644 --- a/include/parser.h +++ b/include/parser.h @@ -28,7 +28,7 @@ # define ELEMENT_MISSING "One or more definition of elements is missing\n" # define NO_MAP "Map element is missing\n" # define INV_COLOR_CODE "Please check color codes for F and C elements\nRGB values should be between 0-255\n" - +# define MLX_ERR "MLX error occured" # include "meta.h" typedef enum e_err { @@ -47,6 +47,7 @@ NO_PLAYER, MISSING_ELEMENTS, MISSING_MAP, COLOR_CODE_WRONG, +MLX_ERROR, } t_err; // error.c @@ -70,9 +71,9 @@ bool is_map_line(char *file); int input_map(t_meta *meta, char *file); // parse_elements.c -int input_texture(t_tex *tex, char *file); -int input_colour(t_tex *tex, char *file); -int save_elements(t_tex *tex, char *file); +int input_texture(t_attr *attributes, char *file); +int input_colour(t_attr *attributes, char *file); +int save_elements(t_attr *attributes, char *file); int parse_elements(t_meta *meta, char *file); // check_colors.c diff --git a/src/cub3d.c b/src/cub3d.c index f246742..d1cdad1 100644 --- a/src/cub3d.c +++ b/src/cub3d.c @@ -1,16 +1,17 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* cub3d.c :+: :+: :+: */ +/* cub3d.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:24:47 by yzaim #+# #+# */ -/* Updated: 2024/01/08 15:25:06 by yzaim ### ########.fr */ +/* Updated: 2024/01/18 10:45:29 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ #include "MLX42/MLX42.h" +#include "MLX42/MLX42_Int.h" #include "meta.h" #include "parser.h" #include @@ -51,7 +52,7 @@ void leaks(void) // change to create a different image for the minimap vs. main viewport int init_mlx_images(t_meta *meta) { - meta->mlx = mlx_init(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE, true); + meta->mlx = mlx_init(WINDOW_WIDTH, WINDOW_HEIGHT, meta->scene_name, true); if (!meta->mlx) { ft_error(); @@ -93,7 +94,7 @@ int init_mlx_images(t_meta *meta) } return (EXIT_SUCCESS); -} +} int cub3d(int argc, char **argv) { @@ -104,8 +105,15 @@ int cub3d(int argc, char **argv) ft_bzero(&meta, sizeof(t_meta)); if (parser(&meta, argv[1])) return(meta_free(&meta), EXIT_FAILURE); - // TODO Error check. + if (set_textures(&meta.attributes)) + return (EXIT_FAILURE); + printf("Tex_N: %s\n", meta.attributes.n.tex_path); + printf("Tex_S: %s\n", meta.attributes.s.tex_path); + printf("Tex_E: %s\n", meta.attributes.e.tex_path); + printf("Tex_W: %s\n", meta.attributes.w.tex_path); + init_mlx_images(&meta); + // TODO Error check. game_init(&meta); mlx_set_cursor_mode(meta.mlx, MLX_MOUSE_HIDDEN); mlx_loop_hook(meta.mlx, game_loop, &meta); diff --git a/src/game/font/font_renderer.c b/src/game/font/font_renderer.c index 7d55bf5..25948b7 100644 --- a/src/game/font/font_renderer.c +++ b/src/game/font/font_renderer.c @@ -1,13 +1,13 @@ /* ************************************************************************** */ -/**/ -/* :::::::: */ -/* font_renderer.c :+::+: */ -/*+:+ */ -/* By: joppe +#+ */ -/* +#+ */ -/* Created: 2024/01/05 00:02:23 by joppe #+##+# */ -/* Updated: 2024/01/07 03:28:05 by joppe ######## odam.nl */ -/**/ +/* */ +/* ::: :::::::: */ +/* game.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yzaim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/08 15:26:51 by yzaim #+# #+# */ +/* Updated: 2024/01/08 15:29:57 by yzaim ### ########.fr */ +/* */ /* ************************************************************************** */ /** @@ -15,7 +15,7 @@ * but with the ability to specifiy what font to draw. */ -#include "MLX42/MLX42_Int.h" +// #include "MLX42/MLX42_Int.h" #include "font_dejavu_14.h" #include "font_comicsans.h" #include "font_mlx.h" @@ -72,7 +72,7 @@ mlx_image_t *cube_put_string(mlx_image_t *image, const char *s, const t_font_atl if (image_len != image->width) { - ft_bzero(image->pixels, image->width * image->height * BPP); + ft_bzero(image->pixels, image->width * image->height * sizeof(int32_t)); if (!mlx_resize_image(image, image_len, atlas->font_h)) return (NULL); } diff --git a/src/game/game.c b/src/game/game.c index 04be52f..e506713 100644 --- a/src/game/game.c +++ b/src/game/game.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* game.c :+: :+: :+: */ +/* game.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:26:51 by yzaim #+# #+# */ -/* Updated: 2024/01/08 15:29:57 by yzaim ### ########.fr */ +/* Updated: 2024/01/17 13:04:46 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/game/player.c b/src/game/player.c index a9b13a6..fc524c4 100644 --- a/src/game/player.c +++ b/src/game/player.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* player.c :+: :+: :+: */ +/* player.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:27:23 by yzaim #+# #+# */ -/* Updated: 2024/01/08 15:27:25 by yzaim ### ########.fr */ +/* Updated: 2024/01/17 15:55:18 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -39,6 +39,7 @@ void player_move(t_player *p, t_vec2d transform) new_position.x = (p->position.x + (transform.x)); new_position.y = (p->position.y + (transform.y)); + // TODO Fix. t_ray r = raycaster_cast(p->position, vec2d_normalize(transform), bound_check, p->meta); // print_ray("bound_ray", &r); @@ -72,6 +73,7 @@ void player_raycast(t_player *p) camera_x = (2 * col / ((double) w) - 1); ray_start = vec2d_add(p->direction, vec2d_scalar_product(p->cam_plane, camera_x)); p->rays[col] = raycaster_cast(p->position, ray_start, bound_check, p->meta); + // printf("wall x: %f\n", p->rays[col].wall_x); col++; } } diff --git a/src/game/raycaster.c b/src/game/raycaster.c index 97902de..343780c 100644 --- a/src/game/raycaster.c +++ b/src/game/raycaster.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* raycaster.c :+: :+: :+: */ +/* raycaster.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:27:33 by yzaim #+# #+# */ -/* Updated: 2024/01/08 15:27:37 by yzaim ### ########.fr */ +/* Updated: 2024/01/18 12:05:53 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ inline static t_vec2d calculate_delta_dist(t_vec2d ray_direction) } inline static t_vec2d calculate_side_dist(t_vec2d ray_direction, \ - t_vec2d player_pos, t_vec2d map_pos, t_vec2d delta_dist) + t_vec2d player_pos, t_vec2i map_pos, t_vec2d delta_dist) { t_vec2d side_dist; @@ -49,60 +49,65 @@ inline static t_vec2d calculate_side_dist(t_vec2d ray_direction, \ return (side_dist); } -inline static t_vec2d calculate_step_size(t_vec2d ray_direction) +inline static t_vec2i calculate_step_size(t_vec2d ray_direction) { const bool comp_x = (ray_direction.x < 0); const bool comp_y = (ray_direction.y < 0); const int8_t dir_x = (comp_x * -1) + (!comp_x * 1); const int8_t dir_y = (comp_y * -1) + (!comp_y * 1); - return ((t_vec2d){dir_x, dir_y}); + return ((t_vec2i){dir_x, dir_y}); } inline static double calculate_ray_length(t_side hit_side, \ t_vec2d side_dist, t_vec2d delta_dist) { - if (hit_side == HIT_NS) + if (hit_side == SIDE_N || hit_side == SIDE_S) return (side_dist.x - delta_dist.x); else return (side_dist.y - delta_dist.y); } inline static t_side ray_move(t_vec2d *side_dist, t_vec2d *delta_dist, \ - t_vec2d step_size, t_vec2d *map_pos) + t_vec2i step_size, t_vec2i *map_pos) { if (side_dist->x < side_dist->y) { side_dist->x += delta_dist->x; map_pos->x += step_size.x; - return (HIT_NS); + if (step_size.x > 0) + return (SIDE_N); + else + return(SIDE_S); } else { side_dist->y += delta_dist->y; map_pos->y += step_size.y; - return (HIT_EW); + if (step_size.y > 0) + return (SIDE_E); + else + return (SIDE_W); } } t_ray raycaster_cast(t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit, const void *param) { t_ray r; - t_vec2d map_pos; t_vec2d side_dist; - t_vec2d step_size; + t_vec2i step_size; t_vec2d delta_dist; - map_pos.x = (int)pp.x; - map_pos.y = (int)pp.y; + r.map_pos.x = (int)pp.x; + r.map_pos.y = (int)pp.y; delta_dist = calculate_delta_dist(dir); - side_dist = calculate_side_dist(dir, pp, map_pos, delta_dist); + side_dist = calculate_side_dist(dir, pp, r.map_pos, delta_dist); step_size = calculate_step_size(dir); size_t limit = 25; while (limit) { - r.hit_side = ray_move(&side_dist, &delta_dist, step_size, &map_pos); - if (hit && hit(param, map_pos.x, map_pos.y)) + r.hit_side = ray_move(&side_dist, &delta_dist, step_size, &r.map_pos); + if (hit && hit(param, r.map_pos.x, r.map_pos.y)) break; limit--; } @@ -110,12 +115,18 @@ t_ray raycaster_cast(t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit, const void *par WARNING("Raycaster limit reached!"); r.length = calculate_ray_length(r.hit_side, side_dist, delta_dist); r.direction = dir; - r.end = map_pos; + // r.end = r.map_pos; - if (r.hit_side == HIT_NS) - r.wall_x = map_pos.y + r.length * r.direction.y; + r.line_height = (int)(WINDOW_HEIGHT / r.length); + + // draw start and draw end + r.line_point.x = -r.line_height / 2 + ((double)WINDOW_HEIGHT) / 2; + r.line_point.y = r.line_height / 2 + ((double)WINDOW_HEIGHT) / 2; + + if (r.hit_side == SIDE_N || r.hit_side == SIDE_S) + r.wall_x = pp.y + r.length * r.direction.y; else - r.wall_x = map_pos.x + r.length * r.direction.x; + r.wall_x = pp.x + r.length * r.direction.x; r.wall_x -= floor(r.wall_x); return (r); } diff --git a/src/game/render_viewport.c b/src/game/render_viewport.c index d60dc14..a1ec418 100644 --- a/src/game/render_viewport.c +++ b/src/game/render_viewport.c @@ -1,65 +1,70 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* render_viewport.c :+: :+: */ +/* render_viewport.c :+: :+: */ /* +:+ */ /* By: yzaim +#+ */ /* +#+ */ /* Created: 2024/01/08 15:28:08 by yzaim #+# #+# */ -/* Updated: 2024/01/08 16:13:27 by yzaim ######## odam.nl */ +/* Updated: 2024/01/18 12:27:01 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ +#include "MLX42/MLX42.h" #include "meta.h" +#include "vector.h" +#include +#include +#include -static t_vec2d calculate_line_points(double ray_length, uint32_t h) +mlx_texture_t *get_texture(t_side side, t_attr attributes); + +static void draw_column(t_meta *meta, t_ray *ray, uint32_t col, uint32_t h) { - //calculate lowest and highest pixel to fill in current stripe - uint32_t start; - uint32_t end; - double line_height; + int32_t y; + int32_t color; + mlx_texture_t *texture; - line_height = (int)(h / ray_length); - if (line_height > h) - line_height = h; - start = -line_height / 2 + ((double)h) / 2; - end = line_height / 2 + ((double)h) / 2; - if (end >= h) - end = h - 1; - return ((t_vec2d) {start, end}); -} + texture = get_texture(ray->hit_side, meta->attributes); -static void draw_column(t_meta *meta, t_vec2d line, t_side side, uint32_t col, uint32_t h) -{ - uint32_t color; - uint32_t row; + ray->texture_point.x = (int)(ray->wall_x * texture->width); + if ((ray->hit_side == SIDE_N || ray->hit_side == SIDE_S) && ray->direction.x > 0) + ray->texture_point.x = texture->width - ray->texture_point.x - 1; + if ((ray->hit_side == SIDE_E || ray->hit_side == SIDE_W) && ray->direction.y < 0) + ray->texture_point.x = texture->width - ray->texture_point.x - 1; - row = 0; - while (row < h) + + double offset = 0; + if (ray->line_height > h) + offset = (ray->line_height - h) / 2; + + ray->step = texture->height / ray->line_height; + ray->texture_position = ((ray->line_point.x + offset) + (ray->line_height - h) / 2) * ray->step; + + y = 0; + while (y < (int32_t) h) { - // ceiling - if (row < line.x) - color = VIEWPORT_COLOR_CEILING; - // floor - else if (row > line.y) - color = VIEWPORT_COLOR_FLOOR; + if (y < ray->line_point.x) + color = find_color(meta->attributes.ceiling_c); + else if (y >= ray->line_point.y) + color = find_color(meta->attributes.floor_c); else { - color = find_wall_color(side); + ray->texture_point.y = ((int) ray->texture_position) & (texture->height - 1); + ray->texture_position += ray->step; + color = pixel_picker(texture, (int)round(ray->texture_point.x), (int)round(ray->texture_point.y)); } - mlx_put_pixel(meta->image, col, row, color); - row++; + mlx_put_pixel(meta->image, col, y, color); + y++; } } void render_viewport(mlx_image_t *image, t_player *p) { - uint32_t i = 0; - while(i < image->width) + uint32_t col = 0; + while(col < image->width) { - t_vec2d line = calculate_line_points(p->rays[i].length, image->height); - draw_column(p->meta, line, p->rays[i].hit_side, i, image->height); - i++; + draw_column(p->meta, &p->rays[col], col, image->height); + col++; } } - diff --git a/src/main.c b/src/main.c index ad6c4b9..a617bc4 100644 --- a/src/main.c +++ b/src/main.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* main.c :+: :+: :+: */ +/* main.c :+: :+: */ /* +:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:25:21 by yzaim #+# #+# */ -/* Updated: 2024/01/08 15:25:23 by yzaim ### ########.fr */ +/* Updated: 2024/01/17 15:04:48 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,4 +25,5 @@ int main(int argc, char *argv[]) // // } // printf("\n"); + } diff --git a/src/parser/parse_elements.c b/src/parser/parse_elements.c index 5d629fa..59b090b 100644 --- a/src/parser/parse_elements.c +++ b/src/parser/parse_elements.c @@ -13,10 +13,10 @@ #include "parser.h" // saves path value and checks of value us empty -int input_texture(t_tex *tex, char *file) +int input_texture(t_attr *attributes, char *file) { char element[4] = {'N', 'S', 'W', 'E'}; - char** path[4] = {&tex->no, &tex->so, &tex->we, &tex->ea}; + char** path[4] = {&attributes->n.tex_path, &attributes->s.tex_path, &attributes->w.tex_path, &attributes->e.tex_path}; int i; i = 0; @@ -38,10 +38,10 @@ int input_texture(t_tex *tex, char *file) } // add check for RBG code correctness? -int input_colour(t_tex *tex, char *file) +int input_colour(t_attr *attributes, char *file) { char tx[2] = {'F', 'C'}; - t_rgba *st[2] = {&tex->floor_c, &tex->ceiling_c}; + t_rgba *st[2] = {&attributes->floor_c, &attributes->ceiling_c}; int i; i = 0; @@ -52,19 +52,19 @@ int input_colour(t_tex *tex, char *file) return (EXIT_SUCCESS); } -int save_elements(t_tex *tex, char *file) +int save_elements(t_attr *attributes, char *file) { while (*file) { skip_spaces(&file); if (is_texture(file)) { - if (input_texture(tex, file)) + if (input_texture(attributes, file)) return (EXIT_FAILURE); } else if (is_colour(file)) { - if (input_colour(tex, file)) + if (input_colour(attributes, file)) return (EXIT_FAILURE); } skip_line(&file); @@ -76,7 +76,7 @@ int parse_elements(t_meta *meta, char *file) { if (!elements_order(file) || is_duplicate(file) || is_missing(file) || !colors_valid(file)) return (free(file), EXIT_FAILURE); - if (save_elements(&(meta->tex), file)) + if (save_elements(&(meta->attributes), file)) return (free(file), EXIT_FAILURE); if (input_map(meta, file)) return (free(file),EXIT_FAILURE); diff --git a/src/parser/parse_textures.c b/src/parser/parse_textures.c index ca6aef1..a7c864d 100644 --- a/src/parser/parse_textures.c +++ b/src/parser/parse_textures.c @@ -17,13 +17,18 @@ void get_colour_value(char *file, t_rgba *col) { skip_spaces(&file); - col->r = (uint8_t)ft_atoi(file); + col->r = ft_atoi(file); while (*file && *file != ',') file++; - col->g = ft_atoi(file + 1); + if (*file == ',') + file++; + col->g = ft_atoi(file); while (*file && *file != ',') file++; - col->b = ft_atoi(file + 1); + if (*file == ',') + file++; + col->b = ft_atoi(file); + col->a = 255; } // mallocs the paths to NO, SO, WE, EA elements diff --git a/src/parser/parser.c b/src/parser/parser.c index 384b8df..25622b4 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /**/ /*::: :::::::: */ -/* parser.c :+::+: */ +/* parser.c :+: :+: */ /*+:+ +:+ +:+ */ /* By: yzaim +#+ +:+ +#+*/ /*+#+#+#+#+#+ +#+ */ /* Created: 2023/11/09 18:08:19 by yzaim #+##+# */ -/* Updated: 2024/01/02 18:57:56 by joppe ######## odam.nl */ +/* Updated: 2024/01/18 10:45:18 by jboeve ######## odam.nl */ /**/ /* ************************************************************************** */ @@ -67,6 +67,7 @@ char *parse_file(char *map_file) if (fd == -1) return (pr_err(INV_FILE), NULL); file = file_to_string(fd); + close(fd); if (!file) return(pr_err(MALL_ERR), NULL); return (file); @@ -90,6 +91,7 @@ int parser(t_meta *meta, char *map_file) { char *file = NULL; + meta->scene_name = map_file; file = parse_file(map_file); if (!file) return(EXIT_FAILURE); diff --git a/src/renderer/pixel_picker.c b/src/renderer/pixel_picker.c new file mode 100644 index 0000000..50d1a47 --- /dev/null +++ b/src/renderer/pixel_picker.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* pixel_picker.c :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yzaim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/08 15:26:51 by yzaim #+# #+# */ +/* Updated: 2024/01/17 15:42:56 by jboeve ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "meta.h" +#include +#include + +// calculate texture position +void wall_texture_position(mlx_texture_t *texture, t_ray *ray, t_vec2i line_points, uint32_t h) +{ + static double x_old = 0; + static double y_old = 0; + + ray->texture_point.x = (int)(ray->wall_x * (double)texture->width); + + if ((ray->hit_side == SIDE_N || ray->hit_side == SIDE_S) && ray->direction.x > 0) + { + ray->texture_point.x = texture->width - ray->texture_point.x - 1; + } + if ((ray->hit_side == SIDE_E || ray->hit_side == SIDE_W) && ray->direction.y < 0) + { + ray->texture_point.x = texture->width - ray->texture_point.x - 1; + } + ray->step = 1.0 * texture->height / ray->line_height; + // x is draw start and y is draw end + + { + ray->texture_position = (line_points.x - (double) (h / 2.0) + ray->line_height / 2.0) * ray->step; + ray->texture_point.y = ((int) ray->texture_position) & (texture->height - 1); + + ray->texture_position += ray->step; + } +} + +uint32_t pixel_picker(mlx_texture_t *texture, int32_t x, int32_t y) +{ + int32_t r; + int32_t g; + int32_t b; + int32_t a; + int32_t index; + + // printf("x: %u | y: %u\n", x, y); + index = y * texture->width * 4 + x * 4; + // printf("index: %d\n", index); + r = (uint32_t)texture->pixels[index]; + g = (uint32_t)texture->pixels[index + 1]; + b = (uint32_t)texture->pixels[index + 2]; + a = (uint32_t)texture->pixels[index + 3]; + return (r << 24 | g << 16 | b << 8 | a); +} diff --git a/src/set_textures.c b/src/set_textures.c new file mode 100644 index 0000000..3f872cd --- /dev/null +++ b/src/set_textures.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* set_textures.c :+: :+: */ +/* +:+ */ +/* By: yzaim +#+ */ +/* +#+ */ +/* Created: 2024/01/08 15:56:45 by yzaim #+# #+# */ +/* Updated: 2024/01/08 15:59:17 by yzaim ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" +#include "meta.h" + +int set_textures(t_attr *attributes) +{ + attributes->n.tex = mlx_load_png(attributes->n.tex_path); + if (attributes->n.tex == NULL) + return (pr_err(MLX_ERROR)); + attributes->s.tex = mlx_load_png(attributes->s.tex_path); + if (attributes->s.tex == NULL) + return (pr_err(MLX_ERROR)); + attributes->e.tex = mlx_load_png(attributes->e.tex_path); + if (attributes->e.tex == NULL) + return (pr_err(MLX_ERROR)); + attributes->w.tex = mlx_load_png(attributes->w.tex_path); + if (attributes->w.tex == NULL) + return (pr_err(MLX_ERROR)); + return (EXIT_SUCCESS); +} + diff --git a/src/test_utils.c b/src/test_utils.c index 9eef7d4..8c23404 100644 --- a/src/test_utils.c +++ b/src/test_utils.c @@ -24,7 +24,7 @@ const char *CELL_NAMES[] = { void print_ray(const char *s, const t_ray *r) { char *side_text; - if (r->hit_side == HIT_NS) + if (r->hit_side == SIDE_S || r->hit_side == SIDE_N) side_text = "NS"; else side_text = "EW"; diff --git a/src/utils/colors.c b/src/utils/colors.c index 49300dd..ee390a5 100644 --- a/src/utils/colors.c +++ b/src/utils/colors.c @@ -1,30 +1,50 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* colors.c :+: :+: */ +/* colors.c :+: :+: */ /* +:+ */ /* By: yzaim +#+ */ /* +#+ */ /* Created: 2024/01/08 16:09:11 by yzaim #+# #+# */ -/* Updated: 2024/01/08 16:09:14 by yzaim ######## odam.nl */ +/* Updated: 2024/01/17 16:03:40 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ #include "meta.h" #include +#include int32_t set_color(int32_t r, int32_t g, int32_t b, int32_t a) { return ((r << 24) | (g << 16) | (b << 8) | a); } -int32_t find_wall_color(t_side side) +mlx_texture_t *get_texture(t_side side, t_attr attributes) { - int32_t color; + if (side == SIDE_N) + return (attributes.n.tex); + else if (side == SIDE_S) + return (attributes.s.tex); + else if (side == SIDE_E) + return (attributes.e.tex); + return (attributes.w.tex); +} + +int32_t find_wall_color(t_attr atrributes, t_ray *ray, t_vec2i line_points, uint32_t h) +{ + int32_t color; + mlx_texture_t *texture; + + texture = get_texture(ray->hit_side, atrributes); + wall_texture_position(texture, ray, line_points, h); + color = pixel_picker(texture, (int)round(ray->texture_point.x), (int)round(ray->texture_point.y)); + return (color); +} + +int32_t find_color(t_rgba rgba) +{ + int32_t color; - if (side == HIT_NS) - color = VIEWPORT_COLOR_WALL_NS; - else - color = VIEWPORT_COLOR_WALL_EW; + color = set_color(rgba.r, rgba.g, rgba.b, rgba.a); return (color); } diff --git a/src/utils/error.c b/src/utils/error.c index 7de2e85..5477588 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -36,10 +36,11 @@ int pr_err(t_err type) NO_PLAYER_POS, ELEMENT_MISSING, NO_MAP, - INV_COLOR_CODE}; + INV_COLOR_CODE, + MLX_ERR}; write(2, "Error\n", 6); - if (type >= 0 && type < 15) + if (type >= 0 && type < 16) return (write(2, msg[type], ft_strlen(msg[type])), 1); return (0); } diff --git a/src/utils/free.c b/src/utils/free.c index 861d659..cf7f5fa 100644 --- a/src/utils/free.c +++ b/src/utils/free.c @@ -6,24 +6,28 @@ /* By: yzaim +#+ */ /* +#+ */ /* Created: 2024/01/08 15:53:55 by yzaim #+# #+# */ -/* Updated: 2024/01/08 15:55:28 by yzaim ######## odam.nl */ +/* Updated: 2024/01/17 16:13:14 by yzaim ######## odam.nl */ /* */ /* ************************************************************************** */ #include "meta.h" -static void free_t_tex(t_tex *tex) +static void free_t_tex(t_attr *attributes) { - free(tex->no); - free(tex->so); - free(tex->ea); - free(tex->we); + free(attributes->n.tex_path); + free(attributes->s.tex_path); + free(attributes->e.tex_path); + free(attributes->w.tex_path); + mlx_delete_texture(attributes->n.tex); + mlx_delete_texture(attributes->s.tex); + mlx_delete_texture(attributes->e.tex); + mlx_delete_texture(attributes->w.tex); } void meta_free(t_meta *meta) { - free_t_tex(&(meta->tex)); + free_t_tex(&(meta->attributes)); free(meta->map.level); - if (meta->map_element) - free(meta->map_element); + // if (meta->map_element) + // free(meta->map_element); } diff --git a/test_maps/simple.cub b/test_maps/simple.cub index e427f64..858368f 100644 --- a/test_maps/simple.cub +++ b/test_maps/simple.cub @@ -1,9 +1,10 @@ -NO path_to_north -WE path_to_west -EA path_to_east -SO path_to_south -F 123,234,213 +NO texture_examples/redbrick.png +WE texture_examples/purplestone.png +EA texture_examples/purplestone.png +SO texture_examples/redbrick.png +F 123,234,2 C 12,34,3 + 11111 10001 10E01 diff --git a/test_maps/valid_tex.cub b/test_maps/valid_tex.cub new file mode 100644 index 0000000..1b23e53 --- /dev/null +++ b/test_maps/valid_tex.cub @@ -0,0 +1,17 @@ +NO texture_examples/purplestone2.png +WE texture_examples/redbrick.png +EA texture_examples/redbrick.png +SO texture_examples/purplestone2.png +F 255,255,255 +C 12, 12, 12 + +11111111111111 +1000010001 +1000010001 +1000110001 +1000100001 +1000100001 +100000000111 +100000000001 +1000000E011 +1111111111 diff --git a/texture_examples/purplestone.png b/texture_examples/purplestone.png new file mode 100755 index 0000000..425bd71 Binary files /dev/null and b/texture_examples/purplestone.png differ diff --git a/texture_examples/purplestone1.png b/texture_examples/purplestone1.png new file mode 100644 index 0000000..87317f7 Binary files /dev/null and b/texture_examples/purplestone1.png differ diff --git a/texture_examples/purplestone2.png b/texture_examples/purplestone2.png new file mode 100644 index 0000000..c77bd77 Binary files /dev/null and b/texture_examples/purplestone2.png differ diff --git a/texture_examples/redbrick.png b/texture_examples/redbrick.png new file mode 100755 index 0000000..3eb620f Binary files /dev/null and b/texture_examples/redbrick.png differ diff --git a/texture_examples/wood.png b/texture_examples/wood.png new file mode 100755 index 0000000..c30c317 Binary files /dev/null and b/texture_examples/wood.png differ