From 3c1cb27885cb38eed9cb0da23914e85d006419ba Mon Sep 17 00:00:00 2001 From: yesimzaim Date: Tue, 9 Jan 2024 17:45:03 +0100 Subject: [PATCH 01/14] new branch to work on textures --- src/renderer/pixel_picker.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/renderer/pixel_picker.c diff --git a/src/renderer/pixel_picker.c b/src/renderer/pixel_picker.c new file mode 100644 index 0000000..bc5624f --- /dev/null +++ b/src/renderer/pixel_picker.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* game.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: yzaim +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/08 15:26:51 by yzaim #+# #+# */ +/* Updated: 2024/01/08 15:29:57 by yzaim ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "meta.h" +#include +#include + +int32_t pixel_picker(t_meta *meta, uint32_t col, uint32_t row) +{ + +} \ No newline at end of file From 837855b5be9e06847da21426887b154f4958cf6c Mon Sep 17 00:00:00 2001 From: yesimzaim Date: Mon, 15 Jan 2024 13:29:29 +0100 Subject: [PATCH 02/14] playing around with textures --- .vscode/settings.json | 3 +- Makefile | 6 ++-- app.dSYM/Contents/Info.plist | 20 ++++++++++++ include/meta.h | 53 +++++++++++++++++++++---------- include/parser.h | 9 +++--- src/cub3d.c | 47 +++++++++++++++++++++++++-- src/game/font/font_renderer.c | 22 ++++++------- src/game/player.c | 1 + src/game/raycaster.c | 31 ++++++++++-------- src/game/render_viewport.c | 35 +++++++++++++------- src/parser/parse_elements.c | 16 +++++----- src/parser/parse_textures.c | 11 +++++-- src/parser/parser.c | 1 + src/renderer/pixel_picker.c | 31 +++++++++++++++++- src/set_textures.c | 32 +++++++++++++++++++ src/test_utils.c | 2 +- src/utils/colors.c | 51 +++++++++++++++++++++++++---- src/utils/error.c | 5 +-- src/utils/free.c | 16 +++++----- test_maps/simple.cub | 11 ++++--- texture_examples/purplestone.png | Bin 0 -> 4422 bytes texture_examples/redbrick.png | Bin 0 -> 3051 bytes 22 files changed, 309 insertions(+), 94 deletions(-) create mode 100644 app.dSYM/Contents/Info.plist create mode 100644 src/set_textures.c create mode 100755 texture_examples/purplestone.png create mode 100755 texture_examples/redbrick.png 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/Makefile b/Makefile index e614a1d..b4bee97 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ RUN_CMD := ./$(NAME) test_maps/valid.cub # CFLAGS += -Wall -Wextra -Werror CFLAGS += -Wall -Wextra -# CFLAGS += -g -fsanitize=address +CFLAGS += -g -fsanitize=address # CFLAGS += -g # CFLAGS += -Ofast -flto -march=native @@ -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/app.dSYM/Contents/Info.plist b/app.dSYM/Contents/Info.plist new file mode 100644 index 0000000..f5f43fd --- /dev/null +++ b/app.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.app + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/include/meta.h b/include/meta.h index 17e2516..65991ed 100644 --- a/include/meta.h +++ b/include/meta.h @@ -110,7 +110,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 +128,23 @@ 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_vec2d map_pos; + t_vec2i texture; + t_side hit_side; + double length; + double wall_x; + double line_height; + 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,7 @@ typedef struct s_meta { t_player player; uint32_t fps; t_map map; - t_tex tex; + t_attr attributes; char *map_element; } t_meta; @@ -231,9 +244,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_vec2d 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(t_tex texture, uint32_t x, uint32_t y); +void wall_texture_position(t_tex texture, t_ray *ray, t_vec2d 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..e546805 100644 --- a/src/cub3d.c +++ b/src/cub3d.c @@ -93,6 +93,42 @@ int init_mlx_images(t_meta *meta) } return (EXIT_SUCCESS); +} + +uint32_t make_color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) +{ + + uint32_t color = 0; + + // Combine the color components using bitwise OR operations + color |= (uint32_t)r << 24; // Red component (shifted to the left by 24 bits) + color |= (uint32_t)g << 16; // Green component (shifted to the left by 16 bits) + color |= (uint32_t)b << 8; // Blue component (shifted to the left by 8 bits) + color |= (uint32_t)a; // Alpha component + + return color; +} + +void print_texture(t_meta *meta) +{ + int i = 0; + int x = 0; + int y = 0; + int32_t color; + mlx_texture_t *texture = meta->attributes.n.tex; + + while (y < 64) + { + x = 0; + while (x < 64) + { + color = make_color(texture->pixels[i], texture->pixels[i + 1], texture->pixels[i + 2], texture->pixels[i + 3]); + mlx_put_pixel(meta->image, x, y, color); + x++; + i += 4; + } + y++; + } } int cub3d(int argc, char **argv) @@ -104,13 +140,20 @@ 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); mlx_loop_hook(meta.mlx, fps_hook, &meta); - mlx_cursor_hook(meta.mlx, cursor_hook, &meta); + // mlx_cursor_hook(meta.mlx, cursor_hook, &meta); mlx_loop(meta.mlx); mlx_terminate(meta.mlx); meta_free(&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/player.c b/src/game/player.c index a9b13a6..dd8083f 100644 --- a/src/game/player.c +++ b/src/game/player.c @@ -72,6 +72,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..035038d 100644 --- a/src/game/raycaster.c +++ b/src/game/raycaster.c @@ -62,7 +62,7 @@ inline static t_vec2d calculate_step_size(t_vec2d ray_direction) 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); @@ -75,34 +75,39 @@ inline static t_side ray_move(t_vec2d *side_dist, t_vec2d *delta_dist, \ { 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_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,12 @@ 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; + if (r.hit_side == SIDE_N || r.hit_side == SIDE_S) + r.wall_x = r.map_pos.y + r.length * r.direction.y; else - r.wall_x = map_pos.x + r.length * r.direction.x; + r.wall_x = r.map_pos.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..e2a5bf4 100644 --- a/src/game/render_viewport.c +++ b/src/game/render_viewport.c @@ -12,16 +12,12 @@ #include "meta.h" -static t_vec2d calculate_line_points(double ray_length, uint32_t h) +static t_vec2d calculate_line_points(double ray_length, uint32_t h, double line_height) { //calculate lowest and highest pixel to fill in current stripe uint32_t start; uint32_t end; - double line_height; - 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) @@ -29,36 +25,53 @@ static t_vec2d calculate_line_points(double ray_length, uint32_t h) return ((t_vec2d) {start, end}); } -static void draw_column(t_meta *meta, t_vec2d line, t_side side, uint32_t col, uint32_t h) +static void draw_column(t_meta *meta, t_vec2d line, t_ray *ray, uint32_t col, uint32_t h) { uint32_t color; uint32_t row; row = 0; + while (row < h) { // ceiling if (row < line.x) - color = VIEWPORT_COLOR_CEILING; + { + color = find_color(meta->attributes.ceiling_c); + } // floor else if (row > line.y) - color = VIEWPORT_COLOR_FLOOR; + { + color = find_color(meta->attributes.floor_c); + } else { - color = find_wall_color(side); + color = find_wall_color(meta->attributes, ray, line, h); } mlx_put_pixel(meta->image, col, row, color); row++; } } +double calculate_line_height(double ray_length, uint32_t h) +{ + double line_height; + + line_height = (int)(h / ray_length); + if (line_height > h) + line_height = h; + return (line_height); +} + void render_viewport(mlx_image_t *image, t_player *p) { uint32_t i = 0; while(i < 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); + p->rays[i].line_height = calculate_line_height(p->rays[i].length, image->height); + t_vec2d line = calculate_line_points(p->rays[i].length, image->height, p->rays[i].line_height); + + draw_column(p->meta, line, &p->rays[i], i, image->height); i++; } } 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..90fcc4b 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -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); diff --git a/src/renderer/pixel_picker.c b/src/renderer/pixel_picker.c index bc5624f..6d998a9 100644 --- a/src/renderer/pixel_picker.c +++ b/src/renderer/pixel_picker.c @@ -14,7 +14,36 @@ #include #include -int32_t pixel_picker(t_meta *meta, uint32_t col, uint32_t row) +// calculate texture position +void wall_texture_position(t_tex texture, t_ray *ray, t_vec2d line_points, uint32_t h) { + // printf("wall x: %f\n", ray->wall_x); + ray->texture.x = (int)(ray->wall_x * (double)texture.tex->width); + if ((ray->hit_side == SIDE_N || ray->hit_side == SIDE_S) && ray->direction.x > 0) + { + ray->texture.x = texture.tex->width - ray->texture.x - 1; + } + if ((ray->hit_side == SIDE_E || ray->hit_side == SIDE_W) && ray->direction.y < 0) + { + ray->texture.x = texture.tex->width - ray->texture.x - 1; + } + ray->step = 1.0 * texture.tex->height / ray->line_height; + // printf("step: %f\n", ray->step); + // x is draw start and y is draw end + ray->texture_position = (line_points.x + (ray->line_height - h )/ 2) * ray->step; +} +uint32_t pixel_picker(t_tex texture, uint32_t x, uint32_t y) +{ + uint32_t color = 0; + int32_t index; + + // printf("x: %u | y: %u\n", x, y); + index = (y % texture.tex->width) * texture.tex->width * 4 +(x % texture.tex->width) * 4; + // printf("index: %d\n", index); + color |= (uint32_t)texture.tex->pixels[index] << 24; + color |= (uint32_t)texture.tex->pixels[index + 1] << 16; + color |= (uint32_t)texture.tex->pixels[index + 2] << 8; + color |= (uint32_t)texture.tex->pixels[index + 3]; + return (color); } \ No newline at end of file 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..b9fe8bd 100644 --- a/src/utils/colors.c +++ b/src/utils/colors.c @@ -18,13 +18,52 @@ 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) +// int32_t find_wall_color(t_side side) +// { +// int32_t color; + +// if (side == SIDE_N || side == SIDE_S) +// { +// color = VIEWPORT_COLOR_WALL_NS; +// } +// else +// { +// color = VIEWPORT_COLOR_WALL_EW; +// } +// return (color); +// } + +t_tex get_texture(t_side side, t_attr attributes) +{ + if (side == SIDE_N) + return (attributes.n); + else if (side == SIDE_S) + return (attributes.s); + else if (side == SIDE_E) + return (attributes.e); + return (attributes.w); +} + +int32_t find_wall_color(t_attr atrributes, t_ray *ray, t_vec2d line_points, uint32_t h) { - int32_t color; + int32_t color; + t_tex texture; - if (side == HIT_NS) - color = VIEWPORT_COLOR_WALL_NS; - else - color = VIEWPORT_COLOR_WALL_EW; + texture = get_texture(ray->hit_side, atrributes); + wall_texture_position(texture, ray, line_points, h); + ray->texture.y = (int)ray->texture_position & (texture.tex->height - 1); + ray->texture_position += ray->step; + printf("h: %d\n",h); + // printf("texture position: %f\n", ray->texture_position); + // printf("x: %d | y: %d\n", ray->texture.x, ray->texture.y); + color = pixel_picker(texture, ray->texture.x, ray->texture.y); return (color); } + +int32_t find_color(t_rgba rgba) +{ + int32_t color; + + color = set_color(rgba.r, rgba.g, rgba.b, rgba.a); + return (color); +} \ No newline at end of file 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..1e5262f 100644 --- a/src/utils/free.c +++ b/src/utils/free.c @@ -12,18 +12,18 @@ #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); } 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/texture_examples/purplestone.png b/texture_examples/purplestone.png new file mode 100755 index 0000000000000000000000000000000000000000..425bd713b3483564cdba529b0dafc62955df0962 GIT binary patch literal 4422 zcmV-M5xMS(P)00009a7bBm000XU z000XU0RWnu7ytke_(?=TRA_;fT0LkaNtXVmCKv*dEXffJK_eE?6o(knNEUG(fa5r{Kh=BubI&E9Sxid0o*#P@#Uz3;uqFTTUS0syc^gJ@V-18ZT{SR2^UuRJp3 zqi;N%gFC!)G$#ND@8JOamHS7}KKuO_Z>y8j`jTf9l(Le8XIO_TWymdzg>7a(@;5Do zw0`}p3@K6#E$DeTcwXXJ?Tm2mB%i&HZ}{?`PtErD*4+Oo!O=^+Ji;Fy;1VQ9NIHD> za7Re^Zvu4a7)^@wB~Ao=OM$`@06MvQx<5{kC@2C6bPY|QKOjwTG(rzRX8wE815hmm z0C&~(X?@9N<;Pg-Cc1WT3}rM$OM#yS&{n?~SiV0+3V`n?kUYrH5qXi^EVuDR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}mV1qjiO#@Bh10si805;KFQ5dZ$gx&FY^$WC;2Dl_I4`+t+I9J{r9t{db zmkU8J>a$I?+XH%?9ljKFCgYmb4K5kKWU{}B7Qx<<#n`)vvs;Yi#37gngV@i zE<*1+-o6we23EO{Pz(p|9ISlNv;^B!95lJuFtAE%3gbF6eiy}+H=Oc~*1o7Y+A6WB z9?<_42GM&$Q?*6i`)(I>lT&uf%_3hk0El3bN~2qn41aP@u0_jjxqfCC)UP+i9IG5f zGtw6kEKJ{$8D2ghP}&rek+%pfv9R1n;S;$2Z}xsKFLr@nBRCvlKns&0WA@W_p&e>@ zUX)9I+RsD$Z>Woy<0C%0ZdVM%uQLE>yKq`xzG}YXS1?PI z0OTSb@{$*Yz_lz+lwK(YU=|mHjyr;MM zoR2p#I1i)uL>xl$<+oo{x#iP}W1KKEkz>F3O~sG-)yPUbFY`Q?*M|pZ-uXU&-dwu_8Uf1C2BeCXt?^Z6k@rd%JX zU(9IDuW#-Ut6c#EKtmDjtTe^xD9K>xsA>+i!;rT&`4y&3+j*i>7&I5_eEvn1?-=%R zs`*uwHP(FW*H58$+z?S|=J%CshJ(aJ?UB>?9*Pn||4lT9s3{q$qOsN}@Oxx3zPP-+ z+-|pR+iJSU-#kd4q-lD5d<>v%+wFGycRhYMRgxqCx~}^hkqChQ1An5?8&X5C#sB~S M07*qoM6N<$g35zXrvLx| literal 0 HcmV?d00001 diff --git a/texture_examples/redbrick.png b/texture_examples/redbrick.png new file mode 100755 index 0000000000000000000000000000000000000000..3eb620f2faf12880091e0db90b0942e0d53bee1d GIT binary patch literal 3051 zcmV00009a7bBm000XU z000XU0RWnu7ytkZok>JNRA_fC2{&HZlq}I228A2n8J+hl0io!W@Gcj1V0(VuKC}F_;Y;y#2p> z-90;ZlAWkH`g*FXUcGu%_5SbwYR|5&uJ|>VX;BnS(-cLKbB-TUN`o4&?7D6~pO0mw zlm<0p2`3s4+IL-NFE*C79|-KaZrU`>=G$+9_OYR03tv8eZtq{deto*}iDGiPt}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V6H&DSp;2X&2=kxje$&)9mcki}v3wQ7n+^E@8JvSO+QYzz#IIgqr zR)2>UA#;3I?5q2=T9t(aI!{7>lVv&Q$Y9q1#GO;YT31p|M4LY8**g;N6#`V@nszPa zN&(!e?;rfSC9=c>ml9RNEfla8w41iv9lCv$8BL?zs6l zX&*)Ej}~Y8cHPY}AM&WWkN=(aVzDrwJrQ?!FW~G-oYkK8?I>sWd+%5|mL9bk#}tG= tDvL>Zdmr3mJD2xS#%cG5oBCgG`7dV8AE~0e6x#p*002ovPDHLkV1hbZ*IWPq literal 0 HcmV?d00001 From 10b2c2e060c7163f578a1162a3297d7123d0df3b Mon Sep 17 00:00:00 2001 From: yesimzaim Date: Mon, 15 Jan 2024 20:53:09 +0100 Subject: [PATCH 03/14] update --- include/meta.h | 10 ++++++---- src/game/raycaster.c | 9 +++++++++ src/game/render_viewport.c | 8 ++++---- src/renderer/pixel_picker.c | 31 ++++++++++++++++--------------- src/utils/colors.c | 22 ++++++++++------------ 5 files changed, 45 insertions(+), 35 deletions(-) diff --git a/include/meta.h b/include/meta.h index 65991ed..2187e6a 100644 --- a/include/meta.h +++ b/include/meta.h @@ -138,11 +138,13 @@ typedef struct s_ray { t_vec2d direction; t_vec2d end; t_vec2d map_pos; - t_vec2i texture; + t_vec2d texture_point; + t_vec2d line_point; t_side hit_side; + + double line_height; double length; double wall_x; - double line_height; double texture_position; double step; } t_ray; @@ -254,7 +256,7 @@ void meta_free(t_meta *meta); int set_textures(t_attr *attributes); //pixel_picker.c -uint32_t pixel_picker(t_tex texture, uint32_t x, uint32_t y); -void wall_texture_position(t_tex texture, t_ray *ray, t_vec2d line_points, uint32_t h); +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_vec2d line_points, uint32_t h); #endif diff --git a/src/game/raycaster.c b/src/game/raycaster.c index 035038d..8960b66 100644 --- a/src/game/raycaster.c +++ b/src/game/raycaster.c @@ -117,6 +117,15 @@ t_ray raycaster_cast(t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit, const void *par r.direction = dir; r.end = r.map_pos; + r.line_height = (int)(WINDOW_HEIGHT / r.length); + if (r.line_height > WINDOW_HEIGHT) + r.line_height = WINDOW_HEIGHT; + + // 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.line_point.y >= WINDOW_HEIGHT) + r.line_point.y = WINDOW_HEIGHT - 1; if (r.hit_side == SIDE_N || r.hit_side == SIDE_S) r.wall_x = r.map_pos.y + r.length * r.direction.y; else diff --git a/src/game/render_viewport.c b/src/game/render_viewport.c index e2a5bf4..146df72 100644 --- a/src/game/render_viewport.c +++ b/src/game/render_viewport.c @@ -12,7 +12,7 @@ #include "meta.h" -static t_vec2d calculate_line_points(double ray_length, uint32_t h, double line_height) +static t_vec2d calculate_line_points(uint32_t h, double line_height) { //calculate lowest and highest pixel to fill in current stripe uint32_t start; @@ -68,10 +68,10 @@ void render_viewport(mlx_image_t *image, t_player *p) uint32_t i = 0; while(i < image->width) { - p->rays[i].line_height = calculate_line_height(p->rays[i].length, image->height); - t_vec2d line = calculate_line_points(p->rays[i].length, image->height, p->rays[i].line_height); + // p->rays[i].line_height = calculate_line_height(p->rays[i].length, image->height); + // t_vec2d line = calculate_line_points(image->height, p->rays[i].line_height); - draw_column(p->meta, line, &p->rays[i], i, image->height); + draw_column(p->meta, p->rays[i].line_point, &p->rays[i], i, image->height); i++; } } diff --git a/src/renderer/pixel_picker.c b/src/renderer/pixel_picker.c index 6d998a9..e579601 100644 --- a/src/renderer/pixel_picker.c +++ b/src/renderer/pixel_picker.c @@ -15,35 +15,36 @@ #include // calculate texture position -void wall_texture_position(t_tex texture, t_ray *ray, t_vec2d line_points, uint32_t h) +void wall_texture_position(mlx_texture_t *texture, t_ray *ray, t_vec2d line_points, uint32_t h) { - // printf("wall x: %f\n", ray->wall_x); - ray->texture.x = (int)(ray->wall_x * (double)texture.tex->width); + 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.x = texture.tex->width - ray->texture.x - 1; + 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.x = texture.tex->width - ray->texture.x - 1; + ray->texture_point.x = texture->width - ray->texture_point.x - 1; } - ray->step = 1.0 * texture.tex->height / ray->line_height; - // printf("step: %f\n", ray->step); + ray->step = 1.0 * texture->height / ray->line_height; // x is draw start and y is draw end ray->texture_position = (line_points.x + (ray->line_height - h )/ 2) * ray->step; } -uint32_t pixel_picker(t_tex texture, uint32_t x, uint32_t y) +uint32_t pixel_picker(mlx_texture_t *texture, int32_t x, int32_t y) { - uint32_t color = 0; + 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.tex->width) * texture.tex->width * 4 +(x % texture.tex->width) * 4; + index = y * texture->width * 4 + x * 4; // printf("index: %d\n", index); - color |= (uint32_t)texture.tex->pixels[index] << 24; - color |= (uint32_t)texture.tex->pixels[index + 1] << 16; - color |= (uint32_t)texture.tex->pixels[index + 2] << 8; - color |= (uint32_t)texture.tex->pixels[index + 3]; - return (color); + 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); } \ No newline at end of file diff --git a/src/utils/colors.c b/src/utils/colors.c index b9fe8bd..5872265 100644 --- a/src/utils/colors.c +++ b/src/utils/colors.c @@ -12,6 +12,7 @@ #include "meta.h" #include +#include int32_t set_color(int32_t r, int32_t g, int32_t b, int32_t a) { @@ -33,30 +34,27 @@ int32_t set_color(int32_t r, int32_t g, int32_t b, int32_t a) // return (color); // } -t_tex get_texture(t_side side, t_attr attributes) +mlx_texture_t *get_texture(t_side side, t_attr attributes) { if (side == SIDE_N) - return (attributes.n); + return (attributes.n.tex); else if (side == SIDE_S) - return (attributes.s); + return (attributes.s.tex); else if (side == SIDE_E) - return (attributes.e); - return (attributes.w); + return (attributes.e.tex); + return (attributes.w.tex); } int32_t find_wall_color(t_attr atrributes, t_ray *ray, t_vec2d line_points, uint32_t h) { - int32_t color; - t_tex texture; + int32_t color; + mlx_texture_t *texture; texture = get_texture(ray->hit_side, atrributes); wall_texture_position(texture, ray, line_points, h); - ray->texture.y = (int)ray->texture_position & (texture.tex->height - 1); + ray->texture_point.y = (int)ray->texture_position & (texture->height - 1); ray->texture_position += ray->step; - printf("h: %d\n",h); - // printf("texture position: %f\n", ray->texture_position); - // printf("x: %d | y: %d\n", ray->texture.x, ray->texture.y); - color = pixel_picker(texture, ray->texture.x, ray->texture.y); + color = pixel_picker(texture, (int)round(ray->texture_point.x), (int)round(ray->texture_point.y)); return (color); } From c73aadef924cb51cfca086765ed1fa0551f5e819 Mon Sep 17 00:00:00 2001 From: Joppe Boeve Date: Wed, 17 Jan 2024 12:17:17 +0100 Subject: [PATCH 04/14] Removed app.dSYM Add app.dSYM to .gitignore --- .gitignore | 1 + app.dSYM/Contents/Info.plist | 20 -------------------- 2 files changed, 1 insertion(+), 20 deletions(-) delete mode 100644 app.dSYM/Contents/Info.plist 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/app.dSYM/Contents/Info.plist b/app.dSYM/Contents/Info.plist deleted file mode 100644 index f5f43fd..0000000 --- a/app.dSYM/Contents/Info.plist +++ /dev/null @@ -1,20 +0,0 @@ - - - - - CFBundleDevelopmentRegion - English - CFBundleIdentifier - com.apple.xcode.dsym.app - CFBundleInfoDictionaryVersion - 6.0 - CFBundlePackageType - dSYM - CFBundleSignature - ???? - CFBundleShortVersionString - 1.0 - CFBundleVersion - 1 - - From bf998782df5095067e4a83cd04d7989a4eb1ea52 Mon Sep 17 00:00:00 2001 From: Joppe Boeve Date: Wed, 17 Jan 2024 15:07:34 +0100 Subject: [PATCH 05/14] Got textures kinda working, only problem; they are being draw relative to the player --- MLX42/include/MLX42/MLX42_Int.h | 6 +-- Makefile | 4 +- include/meta.h | 12 ++--- src/cub3d.c | 41 ++--------------- src/game/game.c | 4 +- src/game/player.c | 5 +- src/game/raycaster.c | 4 +- src/game/render_viewport.c | 73 ++++++++++++++++++++++++++---- src/main.c | 5 +- src/renderer/pixel_picker.c | 20 ++++++-- src/utils/colors.c | 10 ++-- test_maps/valid_tex.cub | 17 +++++++ texture_examples/purplestone1.png | Bin 0 -> 804 bytes 13 files changed, 125 insertions(+), 76 deletions(-) create mode 100644 test_maps/valid_tex.cub create mode 100644 texture_examples/purplestone1.png 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 b4bee97..ff55b90 100644 --- a/Makefile +++ b/Makefile @@ -11,12 +11,12 @@ 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 CFLAGS += -Wall -Wextra -CFLAGS += -g -fsanitize=address +# CFLAGS += -g -fsanitize=address # CFLAGS += -g # CFLAGS += -Ofast -flto -march=native diff --git a/include/meta.h b/include/meta.h index 2187e6a..4fc0ef6 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/17 14:45:28 by jboeve ######## odam.nl */ /**/ /* ************************************************************************** */ @@ -138,8 +138,8 @@ typedef struct s_ray { t_vec2d direction; t_vec2d end; t_vec2d map_pos; - t_vec2d texture_point; - t_vec2d line_point; + t_vec2i texture_point; + t_vec2i line_point; t_side hit_side; double line_height; @@ -246,7 +246,7 @@ 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_attr atrributes, t_ray *ray, t_vec2d line_points, uint32_t h); +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 @@ -257,6 +257,6 @@ 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_vec2d line_points, uint32_t h); +void wall_texture_position(mlx_texture_t *texture, t_ray *ray, t_vec2i line_points, uint32_t h); #endif diff --git a/src/cub3d.c b/src/cub3d.c index e546805..9889a64 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/17 13:25:18 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ #include "MLX42/MLX42.h" +#include "MLX42/MLX42_Int.h" #include "meta.h" #include "parser.h" #include @@ -95,42 +96,6 @@ int init_mlx_images(t_meta *meta) return (EXIT_SUCCESS); } -uint32_t make_color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) -{ - - uint32_t color = 0; - - // Combine the color components using bitwise OR operations - color |= (uint32_t)r << 24; // Red component (shifted to the left by 24 bits) - color |= (uint32_t)g << 16; // Green component (shifted to the left by 16 bits) - color |= (uint32_t)b << 8; // Blue component (shifted to the left by 8 bits) - color |= (uint32_t)a; // Alpha component - - return color; -} - -void print_texture(t_meta *meta) -{ - int i = 0; - int x = 0; - int y = 0; - int32_t color; - mlx_texture_t *texture = meta->attributes.n.tex; - - while (y < 64) - { - x = 0; - while (x < 64) - { - color = make_color(texture->pixels[i], texture->pixels[i + 1], texture->pixels[i + 2], texture->pixels[i + 3]); - mlx_put_pixel(meta->image, x, y, color); - x++; - i += 4; - } - y++; - } -} - int cub3d(int argc, char **argv) { t_meta meta; 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 dd8083f..653db45 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 13:06:35 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); diff --git a/src/game/raycaster.c b/src/game/raycaster.c index 8960b66..b8e2b9d 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/17 13:10:46 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/game/render_viewport.c b/src/game/render_viewport.c index 146df72..2086a1d 100644 --- a/src/game/render_viewport.c +++ b/src/game/render_viewport.c @@ -1,16 +1,19 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* 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/17 15:02:31 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ +#include "MLX42/MLX42.h" #include "meta.h" +#include +#include static t_vec2d calculate_line_points(uint32_t h, double line_height) { @@ -25,7 +28,7 @@ static t_vec2d calculate_line_points(uint32_t h, double line_height) return ((t_vec2d) {start, end}); } -static void draw_column(t_meta *meta, t_vec2d line, t_ray *ray, uint32_t col, uint32_t h) +static void draw_column1(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, uint32_t h) { uint32_t color; uint32_t row; @@ -37,7 +40,7 @@ static void draw_column(t_meta *meta, t_vec2d line, t_ray *ray, uint32_t col, ui // ceiling if (row < line.x) { - color = find_color(meta->attributes.ceiling_c); + color = find_color(meta->attributes.ceiling_c); } // floor else if (row > line.y) @@ -47,12 +50,66 @@ static void draw_column(t_meta *meta, t_vec2d line, t_ray *ray, uint32_t col, ui else { color = find_wall_color(meta->attributes, ray, line, h); + // color = set_color(row / 1, row / 2, row / 4, 255); + // color = find_color(meta->attributes.floor_c); } mlx_put_pixel(meta->image, col, row, color); row++; } } +mlx_texture_t *get_texture(t_side side, t_attr attributes); +#include + +static void draw_column(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, uint32_t h) +{ + int32_t color; + mlx_texture_t *texture; + + texture = get_texture(ray->hit_side, meta->attributes); + + ray->texture_point.x = (int)(ray->wall_x * (double)texture->width); + printf("texture_point [%lf]\n", ray->wall_x); + + + 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 = (ray->line_point.x - (double) (h / 2.0) + ray->line_height / 2.0) * ray->step; + + int32_t row; + row = 0; + while (row < (int32_t) h) + { + // ceiling + if (row < line.x) + { + color = find_color(meta->attributes.ceiling_c); + } + // floor + else if (row > line.y) + { + color = find_color(meta->attributes.floor_c); + } + else + { + 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++; + } + +} double calculate_line_height(double ray_length, uint32_t h) { double line_height; @@ -65,14 +122,14 @@ double calculate_line_height(double ray_length, uint32_t h) 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) { // p->rays[i].line_height = calculate_line_height(p->rays[i].length, image->height); // t_vec2d line = calculate_line_points(image->height, p->rays[i].line_height); - draw_column(p->meta, p->rays[i].line_point, &p->rays[i], i, image->height); - i++; + draw_column(p->meta, p->rays[col].line_point, &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/renderer/pixel_picker.c b/src/renderer/pixel_picker.c index e579601..89226d6 100644 --- a/src/renderer/pixel_picker.c +++ b/src/renderer/pixel_picker.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* game.c :+: :+: :+: */ +/* pixel_picker.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 14:49:38 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,9 +15,13 @@ #include // calculate texture position -void wall_texture_position(mlx_texture_t *texture, t_ray *ray, t_vec2d line_points, uint32_t h) +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; @@ -28,7 +32,13 @@ void wall_texture_position(mlx_texture_t *texture, t_ray *ray, t_vec2d line_poin } ray->step = 1.0 * texture->height / ray->line_height; // x is draw start and y is draw end - ray->texture_position = (line_points.x + (ray->line_height - h )/ 2) * ray->step; + + { + 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) @@ -47,4 +57,4 @@ uint32_t pixel_picker(mlx_texture_t *texture, int32_t x, int32_t y) b = (uint32_t)texture->pixels[index + 2]; a = (uint32_t)texture->pixels[index + 3]; return (r << 24 | g << 16 | b << 8 | a); -} \ No newline at end of file +} diff --git a/src/utils/colors.c b/src/utils/colors.c index 5872265..d955161 100644 --- a/src/utils/colors.c +++ b/src/utils/colors.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* 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 14:44:54 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,15 +45,13 @@ mlx_texture_t *get_texture(t_side side, t_attr attributes) return (attributes.w.tex); } -int32_t find_wall_color(t_attr atrributes, t_ray *ray, t_vec2d line_points, uint32_t h) +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); - 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)); return (color); } @@ -64,4 +62,4 @@ int32_t find_color(t_rgba rgba) color = set_color(rgba.r, rgba.g, rgba.b, rgba.a); return (color); -} \ No newline at end of file +} diff --git a/test_maps/valid_tex.cub b/test_maps/valid_tex.cub new file mode 100644 index 0000000..0390d32 --- /dev/null +++ b/test_maps/valid_tex.cub @@ -0,0 +1,17 @@ +NO texture_examples/purplestone1.png +WE texture_examples/redbrick.png +EA texture_examples/redbrick.png +SO texture_examples/purplestone1.png +F 255,255,255 +C 12, 255, 12 + +11111111111111 +1000010001 +1000010001 +1000110001 +1000100001 +1000100001 +100000000111 +100000000001 +1000000E011 +1111111111 diff --git a/texture_examples/purplestone1.png b/texture_examples/purplestone1.png new file mode 100644 index 0000000000000000000000000000000000000000..5aae8cda0b315560402568240e3dfabc093b2a55 GIT binary patch literal 804 zcmeAS@N?(olHy`uVBq!ia0vp^4j|0I1|(Ny7T#lEU~J2Db`J1#c2+1T%1_J8No8Qr zm{>c}*5h!1NUMMJ(nX?z)*TipEBwSr=d8xY+1@i^`bEB-D z*FG}P_Tu_z`EiD$xJuoVU2i@~KW{o*H&4>+(#pz51{(u1l6Uq-UJU3=J|gIPRpb8r z!pMhvjz;Q+M}D67r*+|+ZJW-NMjiG@5cC#MbZ`?@osy{NwcD+$Q)KRO+ds~I@ha1O z)(J^iiUl-so&4DR|KR(ZvwoiBG}jbiw~EjCoy@}Vj;VK6ZoT~Nvd1hOHq7Bsr{h;L zU!K3@dQ{XCju*Gt^wves+QT06j-lCqSBgiU$bzTmpErIt{ z@dAo)7I;J!Gcf2WgD_*oQu{KXAbW|YuPgftMnP^%?lRVi*MLHjC9V-A&iT2ysd*&~ z&PAz-C8;S2<(VZJ3hti10pX2&;y^{eJY5_^DsH{KwwL#i15ew-^9MZzRk+v^6~r8p zG;W=7Y`(#y=D@}3@j^l5k7vUI&ccJgx9Yw7bEI56TJO}cRa1BMzkE@>HeI>*byVQV zq*?VTn_joBoB#4WGtg{2z(G7yCGs#!%Q4mN-~W1Jr)__2;j#X9<-7^gUeBwMzf!-f zS$%5d|G#e5UA#3Co40tT-U~OKyV_%C%JLb?8+>Qa@4K;lm#5aG)BUZB`WEfqJ8OUJ s-P|krfA7t2oZ Date: Wed, 17 Jan 2024 16:11:21 +0100 Subject: [PATCH 06/14] Textures still not working. --- src/game/player.c | 2 +- src/game/raycaster.c | 2 +- src/game/render_viewport.c | 61 +++++++++++-------------------------- src/renderer/pixel_picker.c | 2 +- src/utils/colors.c | 17 +---------- 5 files changed, 21 insertions(+), 63 deletions(-) diff --git a/src/game/player.c b/src/game/player.c index 653db45..fc524c4 100644 --- a/src/game/player.c +++ b/src/game/player.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:27:23 by yzaim #+# #+# */ -/* Updated: 2024/01/17 13:06:35 by jboeve ######## odam.nl */ +/* Updated: 2024/01/17 15:55:18 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/game/raycaster.c b/src/game/raycaster.c index b8e2b9d..1c24f0b 100644 --- a/src/game/raycaster.c +++ b/src/game/raycaster.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:27:33 by yzaim #+# #+# */ -/* Updated: 2024/01/17 13:10:46 by jboeve ######## odam.nl */ +/* Updated: 2024/01/17 15:58:39 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/game/render_viewport.c b/src/game/render_viewport.c index 2086a1d..a0fdda8 100644 --- a/src/game/render_viewport.c +++ b/src/game/render_viewport.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ */ /* +#+ */ /* Created: 2024/01/08 15:28:08 by yzaim #+# #+# */ -/* Updated: 2024/01/17 15:02:31 by jboeve ######## odam.nl */ +/* Updated: 2024/01/17 16:10:04 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #include "meta.h" #include #include +#include static t_vec2d calculate_line_points(uint32_t h, double line_height) { @@ -28,38 +29,7 @@ static t_vec2d calculate_line_points(uint32_t h, double line_height) return ((t_vec2d) {start, end}); } -static void draw_column1(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, uint32_t h) -{ - uint32_t color; - uint32_t row; - - row = 0; - - while (row < h) - { - // ceiling - if (row < line.x) - { - color = find_color(meta->attributes.ceiling_c); - } - // floor - else if (row > line.y) - { - color = find_color(meta->attributes.floor_c); - } - else - { - color = find_wall_color(meta->attributes, ray, line, h); - // color = set_color(row / 1, row / 2, row / 4, 255); - // color = find_color(meta->attributes.floor_c); - } - mlx_put_pixel(meta->image, col, row, color); - row++; - } -} - mlx_texture_t *get_texture(t_side side, t_attr attributes); -#include static void draw_column(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, uint32_t h) { @@ -68,9 +38,7 @@ static void draw_column(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, ui texture = get_texture(ray->hit_side, meta->attributes); - ray->texture_point.x = (int)(ray->wall_x * (double)texture->width); - printf("texture_point [%lf]\n", ray->wall_x); - + ray->texture_point.x = (int)(ray->wall_x * (int)texture->width); if ((ray->hit_side == SIDE_N || ray->hit_side == SIDE_S) && ray->direction.x > 0) { @@ -80,22 +48,27 @@ static void draw_column(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, ui { 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 = (ray->line_point.x - (double) (h / 2.0) + ray->line_height / 2.0) * ray->step; + ray->texture_position = ((ray->line_point.x - (double) (h / 2.0) + ray->line_height / 2.0) * ray->step); + + printf("line_height [%lf]\n", ray->line_height); + printf("step [%lf]\n", ray->step); + printf("texture_point [%d]\n", ray->texture_point.x); + printf("texture_position [%lf]\n", ray->texture_position); - int32_t row; - row = 0; - while (row < (int32_t) h) + int32_t y; + y = 0; + while (y < (int32_t) h) { // ceiling - if (row < line.x) + if (y < line.x) { color = find_color(meta->attributes.ceiling_c); } // floor - else if (row > line.y) + else if (y > line.y) { color = find_color(meta->attributes.floor_c); } @@ -105,8 +78,8 @@ static void draw_column(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, ui 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++; } } diff --git a/src/renderer/pixel_picker.c b/src/renderer/pixel_picker.c index 89226d6..50d1a47 100644 --- a/src/renderer/pixel_picker.c +++ b/src/renderer/pixel_picker.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:26:51 by yzaim #+# #+# */ -/* Updated: 2024/01/17 14:49:38 by jboeve ######## odam.nl */ +/* Updated: 2024/01/17 15:42:56 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/utils/colors.c b/src/utils/colors.c index d955161..ee390a5 100644 --- a/src/utils/colors.c +++ b/src/utils/colors.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ */ /* +#+ */ /* Created: 2024/01/08 16:09:11 by yzaim #+# #+# */ -/* Updated: 2024/01/17 14:44:54 by jboeve ######## odam.nl */ +/* Updated: 2024/01/17 16:03:40 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,21 +19,6 @@ 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) -// { -// int32_t color; - -// if (side == SIDE_N || side == SIDE_S) -// { -// color = VIEWPORT_COLOR_WALL_NS; -// } -// else -// { -// color = VIEWPORT_COLOR_WALL_EW; -// } -// return (color); -// } - mlx_texture_t *get_texture(t_side side, t_attr attributes) { if (side == SIDE_N) From 4436119afd3e2cc9c5f81954f5afa75ce93b981d Mon Sep 17 00:00:00 2001 From: Yesim Zaim Date: Wed, 17 Jan 2024 16:14:09 +0100 Subject: [PATCH 07/14] fixed texture leaks --- Makefile | 2 +- src/utils/free.c | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index ff55b90..8c230da 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ RUN_CMD := ./$(NAME) test_maps/valid_tex.cub # CFLAGS += -Wall -Wextra -Werror CFLAGS += -Wall -Wextra -# CFLAGS += -g -fsanitize=address +CFLAGS += -g -fsanitize=address # CFLAGS += -g # CFLAGS += -Ofast -flto -march=native diff --git a/src/utils/free.c b/src/utils/free.c index 1e5262f..cf7f5fa 100644 --- a/src/utils/free.c +++ b/src/utils/free.c @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ @@ -18,6 +18,10 @@ static void free_t_tex(t_attr *attributes) 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) From a108cc090466614987ad8b84a15ada7381312ae5 Mon Sep 17 00:00:00 2001 From: Joppe Boeve Date: Wed, 17 Jan 2024 16:28:49 +0100 Subject: [PATCH 08/14] IT WORKKK!??!?!?! DANKU DUCOO --- src/cub3d.c | 4 ++-- src/game/raycaster.c | 7 ++++--- src/game/render_viewport.c | 17 +++-------------- 3 files changed, 9 insertions(+), 19 deletions(-) diff --git a/src/cub3d.c b/src/cub3d.c index 9889a64..16fd9bd 100644 --- a/src/cub3d.c +++ b/src/cub3d.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:24:47 by yzaim #+# #+# */ -/* Updated: 2024/01/17 13:25:18 by jboeve ######## odam.nl */ +/* Updated: 2024/01/17 16:28:16 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -118,7 +118,7 @@ int cub3d(int argc, char **argv) mlx_set_cursor_mode(meta.mlx, MLX_MOUSE_HIDDEN); mlx_loop_hook(meta.mlx, game_loop, &meta); mlx_loop_hook(meta.mlx, fps_hook, &meta); - // mlx_cursor_hook(meta.mlx, cursor_hook, &meta); + mlx_cursor_hook(meta.mlx, cursor_hook, &meta); mlx_loop(meta.mlx); mlx_terminate(meta.mlx); meta_free(&meta); diff --git a/src/game/raycaster.c b/src/game/raycaster.c index 1c24f0b..a9a5d60 100644 --- a/src/game/raycaster.c +++ b/src/game/raycaster.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:27:33 by yzaim #+# #+# */ -/* Updated: 2024/01/17 15:58:39 by jboeve ######## odam.nl */ +/* Updated: 2024/01/17 16:27:30 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -126,10 +126,11 @@ t_ray raycaster_cast(t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit, const void *par r.line_point.y = r.line_height / 2 + ((double)WINDOW_HEIGHT) / 2; if (r.line_point.y >= WINDOW_HEIGHT) r.line_point.y = WINDOW_HEIGHT - 1; + if (r.hit_side == SIDE_N || r.hit_side == SIDE_S) - r.wall_x = r.map_pos.y + r.length * r.direction.y; + r.wall_x = pp.y + r.length * r.direction.y; else - r.wall_x = r.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 a0fdda8..d5d1ab7 100644 --- a/src/game/render_viewport.c +++ b/src/game/render_viewport.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ */ /* +#+ */ /* Created: 2024/01/08 15:28:08 by yzaim #+# #+# */ -/* Updated: 2024/01/17 16:10:04 by jboeve ######## odam.nl */ +/* Updated: 2024/01/17 16:22:52 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -38,20 +38,15 @@ static void draw_column(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, ui texture = get_texture(ray->hit_side, meta->attributes); - ray->texture_point.x = (int)(ray->wall_x * (int)texture->width); - + 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; + ray->texture_position = (ray->line_point.x + (ray->line_height - h) / 2) * ray->step; - ray->texture_position = ((ray->line_point.x - (double) (h / 2.0) + ray->line_height / 2.0) * ray->step); printf("line_height [%lf]\n", ray->line_height); printf("step [%lf]\n", ray->step); @@ -62,16 +57,10 @@ static void draw_column(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, ui y = 0; while (y < (int32_t) h) { - // ceiling if (y < line.x) - { color = find_color(meta->attributes.ceiling_c); - } - // floor else if (y > line.y) - { color = find_color(meta->attributes.floor_c); - } else { ray->texture_point.y = ((int) ray->texture_position) & (texture->height - 1); From 957b873b13391f54ca31c1058f0f9c66e2a88429 Mon Sep 17 00:00:00 2001 From: Joppe Boeve Date: Wed, 17 Jan 2024 16:30:31 +0100 Subject: [PATCH 09/14] Removed prints --- Makefile | 2 +- src/game/render_viewport.c | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 8c230da..ff55b90 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ RUN_CMD := ./$(NAME) test_maps/valid_tex.cub # CFLAGS += -Wall -Wextra -Werror CFLAGS += -Wall -Wextra -CFLAGS += -g -fsanitize=address +# CFLAGS += -g -fsanitize=address # CFLAGS += -g # CFLAGS += -Ofast -flto -march=native diff --git a/src/game/render_viewport.c b/src/game/render_viewport.c index d5d1ab7..5c55e00 100644 --- a/src/game/render_viewport.c +++ b/src/game/render_viewport.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ */ /* +#+ */ /* Created: 2024/01/08 15:28:08 by yzaim #+# #+# */ -/* Updated: 2024/01/17 16:22:52 by jboeve ######## odam.nl */ +/* Updated: 2024/01/17 16:29:48 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -48,10 +48,10 @@ static void draw_column(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, ui ray->texture_position = (ray->line_point.x + (ray->line_height - h) / 2) * ray->step; - printf("line_height [%lf]\n", ray->line_height); - printf("step [%lf]\n", ray->step); - printf("texture_point [%d]\n", ray->texture_point.x); - printf("texture_position [%lf]\n", ray->texture_position); + // printf("line_height [%lf]\n", ray->line_height); + // printf("step [%lf]\n", ray->step); + // printf("texture_point [%d]\n", ray->texture_point.x); + // printf("texture_position [%lf]\n", ray->texture_position); int32_t y; y = 0; From 4180a5f3d5b2613f8ed38cd2a529b11751cfb77a Mon Sep 17 00:00:00 2001 From: joppe Date: Wed, 17 Jan 2024 23:58:06 +0100 Subject: [PATCH 10/14] Found bug with the center of the textures, weird artifacting.. --- include/meta.h | 2 +- src/game/render_viewport.c | 38 +++--------------------------- texture_examples/purplestone1.png | Bin 804 -> 797 bytes 3 files changed, 4 insertions(+), 36 deletions(-) diff --git a/include/meta.h b/include/meta.h index 4fc0ef6..ae70eaa 100644 --- a/include/meta.h +++ b/include/meta.h @@ -6,7 +6,7 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/01 20:07:37 by jboeve#+##+# */ -/* Updated: 2024/01/17 14:45:28 by jboeve ######## odam.nl */ +/* Updated: 2024/01/17 23:56:13 by joppe ######## odam.nl */ /**/ /* ************************************************************************** */ diff --git a/src/game/render_viewport.c b/src/game/render_viewport.c index 5c55e00..1bea8fc 100644 --- a/src/game/render_viewport.c +++ b/src/game/render_viewport.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ */ /* +#+ */ /* Created: 2024/01/08 15:28:08 by yzaim #+# #+# */ -/* Updated: 2024/01/17 16:29:48 by jboeve ######## odam.nl */ +/* Updated: 2024/01/17 23:57:43 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,29 +16,17 @@ #include #include -static t_vec2d calculate_line_points(uint32_t h, double line_height) -{ - //calculate lowest and highest pixel to fill in current stripe - uint32_t start; - uint32_t end; - - 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}); -} - mlx_texture_t *get_texture(t_side side, t_attr attributes); static void draw_column(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, uint32_t h) { + int32_t y; int32_t color; mlx_texture_t *texture; texture = get_texture(ray->hit_side, meta->attributes); - ray->texture_point.x = (int)(ray->wall_x * (double)texture->width); + 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) @@ -47,13 +35,6 @@ static void draw_column(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, ui ray->step = 1.0 * texture->height / ray->line_height; ray->texture_position = (ray->line_point.x + (ray->line_height - h) / 2) * ray->step; - - // printf("line_height [%lf]\n", ray->line_height); - // printf("step [%lf]\n", ray->step); - // printf("texture_point [%d]\n", ray->texture_point.x); - // printf("texture_position [%lf]\n", ray->texture_position); - - int32_t y; y = 0; while (y < (int32_t) h) { @@ -70,16 +51,6 @@ static void draw_column(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, ui mlx_put_pixel(meta->image, col, y, color); y++; } - -} -double calculate_line_height(double ray_length, uint32_t h) -{ - double line_height; - - line_height = (int)(h / ray_length); - if (line_height > h) - line_height = h; - return (line_height); } void render_viewport(mlx_image_t *image, t_player *p) @@ -87,9 +58,6 @@ void render_viewport(mlx_image_t *image, t_player *p) uint32_t col = 0; while(col < image->width) { - // p->rays[i].line_height = calculate_line_height(p->rays[i].length, image->height); - // t_vec2d line = calculate_line_points(image->height, p->rays[i].line_height); - draw_column(p->meta, p->rays[col].line_point, &p->rays[col], col, image->height); col++; } diff --git a/texture_examples/purplestone1.png b/texture_examples/purplestone1.png index 5aae8cda0b315560402568240e3dfabc093b2a55..87317f7a5b1d4e3220c144732a06948d8c2fe7a8 100644 GIT binary patch delta 710 zcmZ3&HkWOJN_}glvvYu_v$H}$QGQxxPAUU~#>Co*wjPHAL|XlWmnw-0c6V5$tnd@N z(q)k)+7+O9g=?+Vj6GlYXPKyI>h(2cA3Qk!=&I)K&FlEqH7WdHe)Q(Sk{3#<_oj

zTN-@VwIb~IKKs?~XQOyNbROA!#vu0*@1vK#w;$~Bmq}ucRJBu=Gk)20q(3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8enO-#EkZi9H^hp;AA(=P)K5 W)e|g#qpSZ6B Date: Thu, 18 Jan 2024 00:13:10 +0100 Subject: [PATCH 11/14] Add new texture to debug issue. --- src/game/render_viewport.c | 2 +- test_maps/valid_tex.cub | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/game/render_viewport.c b/src/game/render_viewport.c index 1bea8fc..5872d51 100644 --- a/src/game/render_viewport.c +++ b/src/game/render_viewport.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ */ /* +#+ */ /* Created: 2024/01/08 15:28:08 by yzaim #+# #+# */ -/* Updated: 2024/01/17 23:57:43 by joppe ######## odam.nl */ +/* Updated: 2024/01/17 23:59:00 by joppe ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/test_maps/valid_tex.cub b/test_maps/valid_tex.cub index 0390d32..1b23e53 100644 --- a/test_maps/valid_tex.cub +++ b/test_maps/valid_tex.cub @@ -1,9 +1,9 @@ -NO texture_examples/purplestone1.png +NO texture_examples/purplestone2.png WE texture_examples/redbrick.png EA texture_examples/redbrick.png -SO texture_examples/purplestone1.png +SO texture_examples/purplestone2.png F 255,255,255 -C 12, 255, 12 +C 12, 12, 12 11111111111111 1000010001 From ac24f1a0cc5e044731d71d9a75e2d122b0f56ad3 Mon Sep 17 00:00:00 2001 From: joppe Date: Thu, 18 Jan 2024 00:13:28 +0100 Subject: [PATCH 12/14] Add image --- texture_examples/purplestone2.png | Bin 0 -> 5050 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 texture_examples/purplestone2.png diff --git a/texture_examples/purplestone2.png b/texture_examples/purplestone2.png new file mode 100644 index 0000000000000000000000000000000000000000..c77bd77eb9438f125ac671850c5a19243e6818d6 GIT binary patch literal 5050 zcmeHKX;@QN8oq!EDj<7qYN1JPHnPZS*g|Cyf`ZcO&CN|nAq&Zc1h5rkU{Iur z($OlLiWPlC3ql#SRtnNWLBWDp77-AY;#k>6YtcF<0TpNZ$LE>;GkNazo%egs_r2#k z=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] Window title is scene file name. --- include/meta.h | 5 ++--- src/cub3d.c | 4 ++-- src/game/raycaster.c | 2 +- src/game/render_viewport.c | 3 +-- src/parser/parser.c | 5 +++-- texture_examples/wood.png | Bin 0 -> 1388 bytes 6 files changed, 9 insertions(+), 10 deletions(-) create mode 100755 texture_examples/wood.png diff --git a/include/meta.h b/include/meta.h index ae70eaa..7c2a36f 100644 --- a/include/meta.h +++ b/include/meta.h @@ -6,7 +6,7 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/01 20:07:37 by jboeve#+##+# */ -/* Updated: 2024/01/17 23:56:13 by joppe ######## odam.nl */ +/* Updated: 2024/01/18 11:13:46 by jboeve ######## odam.nl */ /**/ /* ************************************************************************** */ @@ -56,8 +56,6 @@ #define WINDOW_WIDTH 1280 #define WINDOW_HEIGHT 720 -#define WINDOW_TITLE "Gammoe" - // Game @@ -199,6 +197,7 @@ typedef struct s_meta { uint32_t fps; t_map map; t_attr attributes; + const char *scene_name; char *map_element; } t_meta; diff --git a/src/cub3d.c b/src/cub3d.c index 16fd9bd..d1cdad1 100644 --- a/src/cub3d.c +++ b/src/cub3d.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:24:47 by yzaim #+# #+# */ -/* Updated: 2024/01/17 16:28:16 by jboeve ######## odam.nl */ +/* Updated: 2024/01/18 10:45:29 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -52,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(); diff --git a/src/game/raycaster.c b/src/game/raycaster.c index a9a5d60..103e16b 100644 --- a/src/game/raycaster.c +++ b/src/game/raycaster.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:27:33 by yzaim #+# #+# */ -/* Updated: 2024/01/17 16:27:30 by jboeve ######## odam.nl */ +/* Updated: 2024/01/18 11:13:21 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/game/render_viewport.c b/src/game/render_viewport.c index 5872d51..7a87e8a 100644 --- a/src/game/render_viewport.c +++ b/src/game/render_viewport.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ */ /* +#+ */ /* Created: 2024/01/08 15:28:08 by yzaim #+# #+# */ -/* Updated: 2024/01/17 23:59:00 by joppe ######## odam.nl */ +/* Updated: 2024/01/18 11:14:19 by jboeve ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -62,4 +62,3 @@ void render_viewport(mlx_image_t *image, t_player *p) col++; } } - diff --git a/src/parser/parser.c b/src/parser/parser.c index 90fcc4b..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 */ /**/ /* ************************************************************************** */ @@ -91,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/texture_examples/wood.png b/texture_examples/wood.png new file mode 100755 index 0000000000000000000000000000000000000000..c30c3174c37c936c13d925588fe428a5bfc98c96 GIT binary patch literal 1388 zcmV-y1(W)TP)00009a7bBm000XU z000XU0RWnu7ytkT9!W$&RA_uf}kS@Z++4HHK4!|9!vq zPk-F<+d}{UxGj<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- include/meta.h | 4 ++-- src/game/raycaster.c | 18 +++++++----------- src/game/render_viewport.c | 20 +++++++++++++------- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/include/meta.h b/include/meta.h index 7c2a36f..d0fb002 100644 --- a/include/meta.h +++ b/include/meta.h @@ -6,7 +6,7 @@ /* By: jboeve +#+ */ /* +#+ */ /* Created: 2023/11/01 20:07:37 by jboeve#+##+# */ -/* Updated: 2024/01/18 11:13:46 by jboeve ######## odam.nl */ +/* Updated: 2024/01/18 11:56:06 by jboeve ######## odam.nl */ /**/ /* ************************************************************************** */ @@ -135,7 +135,7 @@ typedef enum e_side { typedef struct s_ray { t_vec2d direction; t_vec2d end; - t_vec2d map_pos; + t_vec2i map_pos; t_vec2i texture_point; t_vec2i line_point; t_side hit_side; diff --git a/src/game/raycaster.c b/src/game/raycaster.c index 103e16b..343780c 100644 --- a/src/game/raycaster.c +++ b/src/game/raycaster.c @@ -6,7 +6,7 @@ /* By: yzaim +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/08 15:27:33 by yzaim #+# #+# */ -/* Updated: 2024/01/18 11:13:21 by jboeve ######## odam.nl */ +/* 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,14 +49,14 @@ 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, \ @@ -69,7 +69,7 @@ inline static double calculate_ray_length(t_side hit_side, \ } 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) { @@ -95,7 +95,7 @@ t_ray raycaster_cast(t_vec2d pp, t_vec2d dir, t_ray_hitfunc hit, const void *par { t_ray r; t_vec2d side_dist; - t_vec2d step_size; + t_vec2i step_size; t_vec2d delta_dist; r.map_pos.x = (int)pp.x; @@ -115,17 +115,13 @@ 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 = r.map_pos; + // r.end = r.map_pos; r.line_height = (int)(WINDOW_HEIGHT / r.length); - if (r.line_height > WINDOW_HEIGHT) - r.line_height = WINDOW_HEIGHT; // 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.line_point.y >= WINDOW_HEIGHT) - r.line_point.y = WINDOW_HEIGHT - 1; if (r.hit_side == SIDE_N || r.hit_side == SIDE_S) r.wall_x = pp.y + r.length * r.direction.y; diff --git a/src/game/render_viewport.c b/src/game/render_viewport.c index 7a87e8a..a1ec418 100644 --- a/src/game/render_viewport.c +++ b/src/game/render_viewport.c @@ -6,19 +6,20 @@ /* By: yzaim +#+ */ /* +#+ */ /* Created: 2024/01/08 15:28:08 by yzaim #+# #+# */ -/* Updated: 2024/01/18 11:14:19 by jboeve ######## 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 mlx_texture_t *get_texture(t_side side, t_attr attributes); -static void draw_column(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, uint32_t h) +static void draw_column(t_meta *meta, t_ray *ray, uint32_t col, uint32_t h) { int32_t y; int32_t color; @@ -32,15 +33,20 @@ static void draw_column(t_meta *meta, t_vec2i line, t_ray *ray, uint32_t col, ui 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; - ray->texture_position = (ray->line_point.x + (ray->line_height - h) / 2) * ray->step; + + 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) { - if (y < line.x) + if (y < ray->line_point.x) color = find_color(meta->attributes.ceiling_c); - else if (y > line.y) + else if (y >= ray->line_point.y) color = find_color(meta->attributes.floor_c); else { @@ -58,7 +64,7 @@ void render_viewport(mlx_image_t *image, t_player *p) uint32_t col = 0; while(col < image->width) { - draw_column(p->meta, p->rays[col].line_point, &p->rays[col], col, image->height); + draw_column(p->meta, &p->rays[col], col, image->height); col++; } }