From 9a113374a6205a8c4fa31139cf40d36aa91a8b91 Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Wed, 6 Aug 2025 14:47:29 +0200 Subject: [PATCH] norm: normed code (except sprites) --- Makefile | 1 + src/consts.h | 38 ++++++++++----------- src/raycast/walls.c | 3 +- src/sprites/sort_sprites.c | 70 ++++++++++++++++++++++++++++++++++++++ src/sprites/sort_sprites.h | 20 +++++++++++ src/utils/inits.h | 4 +-- 6 files changed, 113 insertions(+), 23 deletions(-) create mode 100644 src/sprites/sort_sprites.c create mode 100644 src/sprites/sort_sprites.h diff --git a/Makefile b/Makefile index d033447..e98abc2 100644 --- a/Makefile +++ b/Makefile @@ -28,6 +28,7 @@ SOURCEFILES = \ src/raycast/ray.c \ src/raycast/walls.c \ src/renderer/render.c \ + src/sprites/sort_sprites.c \ src/sprites/sprite_caster.c \ OBJECTS = $(SOURCEFILES:.c=.o) diff --git a/src/consts.h b/src/consts.h index 325c356..d2ab696 100644 --- a/src/consts.h +++ b/src/consts.h @@ -6,31 +6,31 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:54:36 by kcolin #+# #+# */ -/* Updated: 2025/08/06 13:44:29 by tchampio ### ########.fr */ +/* Updated: 2025/08/06 14:23:35 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef CONSTS_H -#define CONSTS_H +# define CONSTS_H -#define HEIGHT 800 -#define WIDTH 800 +# define HEIGHT 800 +# define WIDTH 800 -#define SIZE 64 -#define MAP_SIZE 10 -#define PLAYER_SIZE 6 -#define RESSOURCE_DIR "ressources" -#define MOVEMENT_SPEED 0.000005 -#define ROTATION_SPEED 0.000002 -#define PLANE_VALUE 0.6 -#define TEXTURE_SIZE 64 +# define SIZE 64 +# define MAP_SIZE 10 +# define PLAYER_SIZE 6 +# define RESSOURCE_DIR "ressources" +# define MOVEMENT_SPEED 0.000005 +# define ROTATION_SPEED 0.000002 +# define PLANE_VALUE 0.6 +# define TEXTURE_SIZE 64 +# define SPRITE_TRANPARENCY_COLOR 0xff00dc // 4 static ones, 3 perks, the box and 25 zombies at max -#define SPRITE_TRANPARENCY_COLOR 0xff00dc -#define MAX_SPRITES 1 // FIXME: Change to 30 -#ifdef BONUS -#define COMPILED_TEXT "Compiled with bonuses" -#else -#define COMPILED_TEXT " " -#endif +# define MAX_SPRITES 1 // FIXME: Change to 30 +# ifdef BONUS +# define COMPILED_TEXT "Compiled with bonuses" +# else +# define COMPILED_TEXT " " +# endif #endif diff --git a/src/raycast/walls.c b/src/raycast/walls.c index ae26477..151b6b0 100644 --- a/src/raycast/walls.c +++ b/src/raycast/walls.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/31 13:17:39 by kcolin #+# #+# */ -/* Updated: 2025/08/06 12:08:33 by tchampio ### ########.fr */ +/* Updated: 2025/08/06 14:15:08 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,7 +34,6 @@ t_cardinal_dir get_cardinal(t_ray *ray) } } - static int get_color(t_cub3d_data *data, t_ray *ray, int tex_y) { t_cardinal_dir dir; diff --git a/src/sprites/sort_sprites.c b/src/sprites/sort_sprites.c new file mode 100644 index 0000000..6f4c57d --- /dev/null +++ b/src/sprites/sort_sprites.c @@ -0,0 +1,70 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort_sprites.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/08/06 14:17:58 by tchampio #+# #+# */ +/* Updated: 2025/08/06 14:21:16 by tchampio ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../cub3d_data.h" + +static void swap(int i, int j, int *spriteorder, double *spritedist) +{ + double tmp_dist; + int tmp_order; + + tmp_order = spriteorder[i]; + tmp_dist = spritedist[i]; + spriteorder[i] = spriteorder[j]; + spritedist[i] = spritedist[j]; + spriteorder[j] = tmp_order; + spritedist[j] = tmp_dist; +} + +static void bubble_sort(double *dist, int *order) +{ + int j; + int k; + + j = 0; + while (j < MAX_SPRITES) + { + k = 0; + while (j < MAX_SPRITES - j - 1) + { + if (dist[k] < dist[k + 1] && dist[k] != -1) + swap(k, k + 1, order, dist); + k++; + } + j++; + } +} + +// the sorting part is a readaptation of the std::sort function from c++ +// more info on here: https://stackoverflow.com/questions/23816797/how-does-stdsort-work-for-list-of-pairs +void sort_sprites(int *spriteorder, double *spritedist, t_cub3d_data *data) +{ + int i; + int order[MAX_SPRITES]; + double dist[MAX_SPRITES]; + + i = 0; + while (i < MAX_SPRITES) + { + order[i] = i; + dist[i] = ((data->player.x - data->static_sprite[i].x) * (data->player.x - data->static_sprite[i].x) + (data->player.y - data->static_sprite[i].y) * (data->player.y - data->static_sprite[i].y)); + i++; + } + bubble_sort(dist, order); + i = 0; + while (i < MAX_SPRITES) + { + spritedist[i] = dist[MAX_SPRITES - i - 1]; + spriteorder[i] = order[MAX_SPRITES - i - 1]; + i++; + } +} diff --git a/src/sprites/sort_sprites.h b/src/sprites/sort_sprites.h new file mode 100644 index 0000000..4ba83cc --- /dev/null +++ b/src/sprites/sort_sprites.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* sort_sprites.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/08/06 14:18:47 by tchampio #+# #+# */ +/* Updated: 2025/08/06 14:19:23 by tchampio ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef SORT_SPRITES_H +# define SORT_SPRITES_H + +# include "../cub3d_data.h" + +void sort_sprites(int *spriteorder, double *spritedist, t_cub3d_data *data); + +#endif // SORT_SPRITES_H diff --git a/src/utils/inits.h b/src/utils/inits.h index 121857d..55268bd 100644 --- a/src/utils/inits.h +++ b/src/utils/inits.h @@ -6,7 +6,7 @@ /* By: tchampio