diff --git a/.normignore b/.normignore new file mode 100644 index 0000000..c0310b8 --- /dev/null +++ b/.normignore @@ -0,0 +1 @@ +lib/MLX42/ diff --git a/.vscode/settings.json b/.vscode/settings.json index 660b980..fd65915 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -8,7 +8,8 @@ "vec_math.h": "c", "texture.h": "c", "libft.h": "c", - "render.h": "c" + "render.h": "c", + "math.h": "c" }, "cmake.ignoreCMakeListsMissing": true } \ No newline at end of file diff --git a/Makefile b/Makefile index 3f80ddf..c26e434 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: whaffman +#+ # # +#+ # # Created: 2024/10/15 11:48:46 by whaffman #+# #+# # -# Updated: 2025/05/14 16:06:31 by whaffman ######## odam.nl # +# Updated: 2025/05/15 17:44:48 by whaffman ######## odam.nl # # # # **************************************************************************** # @@ -45,7 +45,7 @@ SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) # Build configurations BUILD_CONFIGS = release debug asan tsan -release_CFLAGS = -Wall -Werror -Werror -flto -Ofast -march=native -mtune=native -ffast-math +release_CFLAGS = -Wall -Werror -Werror -flto -Ofast -march=native -mtune=native -ffast-math debug_CFLAGS = -Wall -Werror -Werror -g3 -DDEBUG -DDBG='fprintf(stderr, RED "DEBUG: " RESET "%s:%d (%s)\n", __FILE__, __LINE__, __PRETTY_FUNCTION__);' asan_CFLAGS = -Wall -Werror -Werror -fsanitize=address,leak,undefined -g3 tsan_CFLAGS = -Wall -Werror -Werror -fsanitize=thread -g3 diff --git a/assets/plant.png b/assets/plant.png new file mode 100644 index 0000000..7762cee Binary files /dev/null and b/assets/plant.png differ diff --git a/inc/cub3d.h b/inc/cub3d.h index 4bcaab5..4be7300 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -6,7 +6,7 @@ /* By: qmennen +#+ */ /* +#+ */ /* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */ -/* Updated: 2025/05/14 12:42:27 by whaffman ######## odam.nl */ +/* Updated: 2025/05/16 15:01:40 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,6 +20,10 @@ # define HEIGHT 1080 # define TITLE "Cub3D" +# ifndef M_PI +# define M_PI 3.14159265358979323846 +# endif + # define RESET "\033[0m" # define BLACK "\033[0;30m" # define RED "\033[0;31m" diff --git a/inc/player.h b/inc/player.h index 1717a80..5c7da6b 100644 --- a/inc/player.h +++ b/inc/player.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/15 18:53:27 by qmennen #+# #+# */ -/* Updated: 2025/05/14 20:08:48 by whaffman ######## odam.nl */ +/* Updated: 2025/05/15 13:28:11 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,5 +19,4 @@ int player_create(t_game **game); void player_render(t_screen *screen, t_player *player); void player_update(t_game *game, double delta_time); - #endif diff --git a/inc/render.h b/inc/render.h index 17ad5e2..6a30483 100644 --- a/inc/render.h +++ b/inc/render.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/15 16:28:16 by qmennen #+# #+# */ -/* Updated: 2025/05/14 21:24:48 by whaffman ######## odam.nl */ +/* Updated: 2025/05/15 14:25:46 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -36,8 +36,7 @@ void sort_sprites(t_game *game); void calculate_sprite_dist(t_game *game); void cam_fraction(t_game *game, t_sprite *sprite); unsigned int calculate_alpha(int x, int y, double dist); -unsigned int sample_texture_color(mlx_texture_t *texture, - t_vec2_int tex, int alpha); - +unsigned int sample_texture_color(t_sprite *sprite, + t_vec2_int tex, int n); #endif diff --git a/inc/types.h b/inc/types.h index 121b644..bbcf2ad 100644 --- a/inc/types.h +++ b/inc/types.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */ -/* Updated: 2025/05/15 13:14:53 by whaffman ######## odam.nl */ +/* Updated: 2025/05/16 14:59:09 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -62,7 +62,7 @@ typedef struct s_sprite int n_frames; double dist; double cam_frac; - double alpha; + int alpha; mlx_texture_t *texture; t_vec2 pos; } t_sprite; @@ -130,6 +130,8 @@ typedef struct s_game t_player *player; t_screen *screen; t_keyboard *keyboard; + int framecount; + int fps; } t_game; #endif diff --git a/src/game.c b/src/game.c index 377606e..12795c6 100644 --- a/src/game.c +++ b/src/game.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */ -/* Updated: 2025/05/14 19:59:05 by whaffman ######## odam.nl */ +/* Updated: 2025/05/16 15:01:28 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,10 +17,8 @@ int game_create(t_game **game) *game = malloc(sizeof(t_game)); if (!game) return (FAILURE); - (*game)->player = NULL; - (*game)->map = NULL; - (*game)->screen = NULL; - (*game)->keyboard = NULL; + memset(*game, 0, sizeof(t_game)); + (*game)->fps = 20; return (SUCCESS); } @@ -42,15 +40,15 @@ void game_loop(void *param) { t_game *game; double delta_time; - static int framecount = 0; static int fps = 0; - framecount++; game = (t_game *)param; + game->framecount++; delta_time = game->screen->mlx->delta_time; fps += (int)(1.f / delta_time); - if (framecount % 20 == 0) + if (game->framecount % 20 == 0) { + game->fps = (int)(fps / 20); fprintf(stderr, "FPS: %d\n", fps / 20); fps = 0; } @@ -61,8 +59,8 @@ void game_loop(void *param) keyboard_update(game); if (game->player->is_moving) { - game->screen->img->instances[0].x = sin((double)framecount / 4.0) * 20; - game->screen->img->instances[0].y = cos((double)framecount / 2.0) * 10; + game->screen->img->instances[0].x = sin(game->framecount / 4.0) * 20; + game->screen->img->instances[0].y = cos(game->framecount / 2.0) * 10; } } diff --git a/src/render/render_minimap.c b/src/render/render_minimap.c index 01d28f2..29ea441 100644 --- a/src/render/render_minimap.c +++ b/src/render/render_minimap.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/05/12 11:31:34 by whaffman #+# #+# */ -/* Updated: 2025/05/12 11:31:43 by whaffman ######## odam.nl */ +/* Updated: 2025/05/16 13:35:05 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/render/render_sprite.c b/src/render/render_sprite.c index 7f341d7..bc81da2 100644 --- a/src/render/render_sprite.c +++ b/src/render/render_sprite.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/05/08 12:23:17 by qmennen #+# #+# */ -/* Updated: 2025/05/14 21:21:55 by whaffman ######## odam.nl */ +/* Updated: 2025/05/15 17:51:57 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,10 +19,11 @@ static void get_start_end(t_game *game, sprite_scale = 16.0 / sprite->dist; start->x = 0.5 * (game->screen->width * (1.0 + sprite->cam_frac) - - sprite->texture->width * sprite_scale); + - sprite->texture->width / sprite->n_frames * sprite_scale); start->y = 0.5 * (game->screen->height - sprite->texture->height * sprite_scale); - end->x = start->x + (sprite->texture->width * sprite_scale); + end->x = start->x; + end->x += (sprite->texture->width / sprite->n_frames * sprite_scale); end->y = start->y + (sprite->texture->height * sprite_scale); } @@ -42,21 +43,23 @@ static void draw_sprite_column( { t_vec2_int tex; t_vec2 inv_range; - int alpha; int y; unsigned int color; inv_range.y = 1.0 / (column.end.y - column.start.y); inv_range.x = 1.0 / (column.end.x - column.start.x); - alpha = calculate_alpha(column.start.x, column.start.y, sprite->dist); - tex.x = (column.x - column.start.x) * sprite->texture->width; + sprite->alpha = calculate_alpha(column.start.x, + column.start.y, sprite->dist); + tex.x = (column.x - column.start.x); + tex.x *= sprite->texture->width / sprite->n_frames; tex.x *= inv_range.x; y = column.start.y; while (y < column.end.y) { tex.y = (y - column.start.y) * sprite->texture->height; tex.y *= inv_range.y; - color = sample_texture_color(sprite->texture, tex, alpha); + color = sample_texture_color(sprite, tex, + (int)(game->framecount / 20) % sprite->n_frames); if (y > 0 && y < game->screen->height && (color & 0xFF) != 0) mlx_put_pixel(game->screen->img, column.x, y, color); y++; diff --git a/src/render/render_sprite_utils.c b/src/render/render_sprite_utils.c index bf76153..9364485 100644 --- a/src/render/render_sprite_utils.c +++ b/src/render/render_sprite_utils.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/05/14 21:21:57 by whaffman #+# #+# */ -/* Updated: 2025/05/14 21:25:24 by whaffman ######## odam.nl */ +/* Updated: 2025/05/15 15:10:39 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -79,16 +79,25 @@ unsigned int calculate_alpha(int x, int y, double dist) return (alpha); } -unsigned int sample_texture_color( - mlx_texture_t *texture, - t_vec2_int tex, int alpha +unsigned int + sample_texture_color( + t_sprite *sprite, + t_vec2_int tex, + int n ) { - int index; + mlx_texture_t *texture; + int index; + int width; + int n_frames; - index = (tex.x + tex.y * texture->width) * texture->bytes_per_pixel; + n_frames = sprite->n_frames; + width = sprite->texture->width / n_frames; + texture = sprite->texture; + index = (tex.x + n * width + tex.y * width * n_frames); + index *= texture->bytes_per_pixel; return (texture->pixels[index] << 24 | texture->pixels[index + 1] << 16 | texture->pixels[index + 2] << 8 - | (texture->pixels[index + 3] != 0) * alpha); + | (texture->pixels[index + 3] != 0) * sprite->alpha); } diff --git a/src/util/initialize.c b/src/util/initialize.c index 4683d14..e6c5259 100644 --- a/src/util/initialize.c +++ b/src/util/initialize.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */ -/* Updated: 2025/05/14 20:33:11 by whaffman ######## odam.nl */ +/* Updated: 2025/05/15 17:09:38 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -35,12 +35,12 @@ static int init_temp(t_game **game) return (FAILURE); sprites = (*game)->map->sprites; sprites[0] = make_sprite("./assets/battery.png", 3.5, 3.5, 1); - sprites[1] = make_sprite("./assets/battery.png", 2.5, 3.5, 1); - sprites[2] = make_sprite("./assets/battery.png", 1.5, 3.5, 1); + sprites[1] = make_sprite("./assets/plant.png", 2.5, 3.5, 4); + sprites[2] = make_sprite("./assets/plant.png", 1.5, 3.5, 4); sprites[3] = make_sprite("./assets/broken_mirror.png", 3.5, 4.5, 1); sprites[4] = make_sprite("./assets/lamp.png", 2.5, 5.5, 1); sprites[5] = make_sprite("./assets/battery.png", 10.5, 6.5, 1); - sprites[6] = make_sprite("./assets/battery.png", 14.5, 3.5, 1); + sprites[6] = make_sprite("./assets/plant.png", 14.5, 3.5, 4); sprites[7] = make_sprite("./assets/battery.png", 36.5, 3.5, 1); sprites[8] = make_sprite("./assets/broken_mirror.png", 42.5, 4.5, 1); sprites[9] = make_sprite("./assets/lamp.png", 9.5, 10.5, 1);