From f9272b27c7419f019287bb98401c9c5f6e2d6f81 Mon Sep 17 00:00:00 2001 From: Willem Haffmans Date: Wed, 14 May 2025 20:34:45 +0200 Subject: [PATCH] all norm exept for render_sprites --- inc/player.h | 5 +- src/game.c | 9 ++-- src/map/map_create.c | 40 ++++++++++++---- src/map/map_free.c | 4 +- src/moves.c | 74 ++++++++++++++++++++++++++++++ src/parser/parse_config_line.c | 6 +-- src/parser/print_config.c | 4 +- src/player.c | 63 +------------------------ src/render/render_sprite.c | 6 +-- src/screen.c | 24 ++++++---- src/texture/texutre_load.c | 13 +++--- src/util/initialize.c | 84 +++++++++++++--------------------- src/util/keyboard.c | 7 +-- 13 files changed, 182 insertions(+), 157 deletions(-) create mode 100644 src/moves.c diff --git a/inc/player.h b/inc/player.h index 1f779a2..1717a80 100644 --- a/inc/player.h +++ b/inc/player.h @@ -3,10 +3,10 @@ /* :::::::: */ /* player.h :+: :+: */ /* +:+ */ -/* By: qmennen +#+ */ +/* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/15 18:53:27 by qmennen #+# #+# */ -/* Updated: 2025/05/14 12:43:14 by whaffman ######## odam.nl */ +/* Updated: 2025/05/14 20:08:48 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,4 +19,5 @@ 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/src/game.c b/src/game.c index b662a06..377606e 100644 --- a/src/game.c +++ b/src/game.c @@ -3,10 +3,10 @@ /* :::::::: */ /* game.c :+: :+: */ /* +:+ */ -/* By: qmennen +#+ */ +/* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */ -/* Updated: 2025/05/09 14:51:14 by whaffman ######## odam.nl */ +/* Updated: 2025/05/14 19:59:05 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -58,12 +58,11 @@ void game_loop(void *param) player_update(game, delta_time); cast_rays(game); render_map(game); - //render_entities(game); keyboard_update(game); if (game->player->is_moving) { - game->screen->img->instances[0].x = (int)(sin((double)framecount / 4.0) * 20); - game->screen->img->instances[0].y = (int)(( 0.5 * cos((double)framecount / 2.0)) * 20); + game->screen->img->instances[0].x = sin((double)framecount / 4.0) * 20; + game->screen->img->instances[0].y = cos((double)framecount / 2.0) * 10; } } diff --git a/src/map/map_create.c b/src/map/map_create.c index cdeb5f1..90b01e0 100644 --- a/src/map/map_create.c +++ b/src/map/map_create.c @@ -3,21 +3,17 @@ /* :::::::: */ /* map_create.c :+: :+: */ /* +:+ */ -/* By: qmennen +#+ */ +/* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/23 12:21:13 by whaffman #+# #+# */ -/* Updated: 2025/05/07 11:52:14 by whaffman ######## odam.nl */ +/* Updated: 2025/05/14 20:24:32 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "cub3d.h" -int map_create(t_game **game, const char *mapfile) +static int map_allocate(t_game **game) { - t_tile **grid; - - if (!(*game)) - return (FAILURE); (*game)->map = malloc(sizeof(t_map)); if (!(*game)->map) { @@ -25,15 +21,30 @@ int map_create(t_game **game, const char *mapfile) return (FAILURE); } ft_memset((*game)->map->textures, 0, sizeof((*game)->map->textures)); + return (SUCCESS); +} + +static int map_parse_and_copy(t_game **game, + const char *mapfile, t_tile ***out_grid) +{ + t_tile **grid; + if (!parse_args(mapfile, (*game))) return (FAILURE); - grid = copy_map((*game)->map->grid, (*game)->map->width, (*game)->map->height); + grid = copy_map((*game)->map->grid, + (*game)->map->width, (*game)->map->height); if (!grid) { perror("Error copying (*game)->map"); free((*game)->map); return (FAILURE); } + *out_grid = grid; + return (SUCCESS); +} + +static int map_validate_and_finalize(t_game **game, t_tile **grid) +{ if (!enclosed_map((*game)->map)) { ft_putendl_fd("Map is not enclosed", STDERR_FILENO); @@ -46,3 +57,16 @@ int map_create(t_game **game, const char *mapfile) print_config((*game)->map); return (SUCCESS); } + +int map_create(t_game **game, const char *mapfile) +{ + t_tile **grid; + + if (!(*game)) + return (FAILURE); + if (map_allocate(game) == FAILURE) + return (FAILURE); + if (map_parse_and_copy(game, mapfile, &grid) == FAILURE) + return (FAILURE); + return (map_validate_and_finalize(game, grid)); +} diff --git a/src/map/map_free.c b/src/map/map_free.c index d09e75f..d957aed 100644 --- a/src/map/map_free.c +++ b/src/map/map_free.c @@ -6,14 +6,14 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/23 12:22:28 by whaffman #+# #+# */ -/* Updated: 2025/05/14 12:48:03 by whaffman ######## odam.nl */ +/* Updated: 2025/05/14 19:57:03 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include #include "cub3d.h" -void map_free(t_map *map) +void map_free(t_map *map) { grid_free(map->grid, map->height); free(map->north_texture); diff --git a/src/moves.c b/src/moves.c new file mode 100644 index 0000000..eec4f0d --- /dev/null +++ b/src/moves.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* moves.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/05/14 20:08:27 by whaffman #+# #+# */ +/* Updated: 2025/05/14 20:14:24 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +static void move(t_map *map, t_player *player, int dir, double delta) +{ + double xa; + double ya; + + xa = dir * player->dir.x * player->speed * delta; + ya = dir * player->dir.y * player->speed * delta; + if (xa != 0 && collision_horizontal(map, player, xa)) + { + player->pos.x += xa; + player->is_moving = 1; + } + if (ya != 0 && collision_vertical(map, player, ya)) + { + player->pos.y += ya; + player->is_moving = 1; + } +} + +static void strave(t_map *map, t_player *player, int dir, double delta) +{ + double xa; + double ya; + + xa = dir * perp(player->dir).x * player->speed * delta; + ya = dir * perp(player->dir).y * player->speed * delta; + if (xa != 0 && collision_horizontal(map, player, xa)) + { + player->pos.x += xa; + player->is_moving = 1; + } + if (ya != 0 && collision_vertical(map, player, ya)) + { + player->is_moving = 1; + player->pos.y += ya; + } +} + +static void rotate(t_player *player, double rot_speed, double delta) +{ + player->dir = rot(player->dir, rot_speed * delta); + player->camera = rot(player->camera, rot_speed * delta); +} + +void player_update(t_game *game, double delta_time) +{ + game->player->is_moving = 0; + if (get_key(game, MLX_KEY_W)) + move(game->map, game->player, 1, delta_time); + else if (get_key(game, MLX_KEY_S)) + move(game->map, game->player, -1, delta_time); + if (get_key(game, MLX_KEY_A)) + strave(game->map, game->player, -1, delta_time); + else if (get_key(game, MLX_KEY_D)) + strave(game->map, game->player, 1, delta_time); + if (get_key(game, MLX_KEY_LEFT)) + rotate(game->player, -2.5, delta_time); + else if (get_key(game, MLX_KEY_RIGHT)) + rotate(game->player, 2.5, delta_time); +} diff --git a/src/parser/parse_config_line.c b/src/parser/parse_config_line.c index 0ecbce9..6a42ca1 100644 --- a/src/parser/parse_config_line.c +++ b/src/parser/parse_config_line.c @@ -6,15 +6,15 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/22 13:10:06 by whaffman #+# #+# */ -/* Updated: 2025/05/14 12:48:03 by whaffman ######## odam.nl */ +/* Updated: 2025/05/14 20:13:13 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "cub3d.h" -int parse_config_line(const char *line, t_map *map) +int parse_config_line(const char *line, t_map *map) { - char **tokens; + char **tokens; tokens = ft_split(line, ' '); if (!tokens || !tokens[0] || !tokens[1] || tokens[2]) diff --git a/src/parser/print_config.c b/src/parser/print_config.c index 8a8bf78..28e6548 100644 --- a/src/parser/print_config.c +++ b/src/parser/print_config.c @@ -6,13 +6,13 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/22 13:11:18 by whaffman #+# #+# */ -/* Updated: 2025/05/14 12:48:03 by whaffman ######## odam.nl */ +/* Updated: 2025/05/14 20:00:32 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "cub3d.h" -void print_config(t_map *map) +void print_config(t_map *map) { printf("Map:\n"); printf("Width: %d, Height: %d\n", map->width, map->height); diff --git a/src/player.c b/src/player.c index 78f2f3e..c35c480 100644 --- a/src/player.c +++ b/src/player.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/15 18:53:19 by qmennen #+# #+# */ -/* Updated: 2025/05/14 18:31:54 by whaffman ######## odam.nl */ +/* Updated: 2025/05/14 20:14:10 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -32,67 +32,6 @@ int player_create(t_game **game) return (SUCCESS); } -static void move(t_map *map, t_player *player, int dir, double delta) -{ - double xa; - double ya; - - xa = dir * player->dir.x * player->speed * delta; - ya = dir * player->dir.y * player->speed * delta; - if (xa != 0 && collision_horizontal(map, player, xa)) - { - player->pos.x += xa; - player->is_moving = 1; - } - if (ya != 0 && collision_vertical(map, player, ya)) - { - player->pos.y += ya; - player->is_moving = 1; - } -} - -static void strave(t_map *map, t_player *player, int dir, double delta) -{ - double xa; - double ya; - - xa = dir * perp(player->dir).x * player->speed * delta; - ya = dir * perp(player->dir).y * player->speed * delta; - if (xa != 0 && collision_horizontal(map, player, xa)) - { - player->pos.x += xa; - player->is_moving = 1; - } - if (ya != 0 && collision_vertical(map, player, ya)) - { - player->is_moving = 1; - player->pos.y += ya; - } -} - -static void rotate(t_player *player, double rot_speed, double delta) -{ - player->dir = rot(player->dir, rot_speed * delta); - player->camera = rot(player->camera, rot_speed * delta); -} - -void player_update(t_game *game, double delta_time) -{ - game->player->is_moving = 0; - if (get_key(game, MLX_KEY_W)) - move(game->map, game->player, 1, delta_time); - else if (get_key(game, MLX_KEY_S)) - move(game->map, game->player, -1, delta_time); - if (get_key(game, MLX_KEY_A)) - strave(game->map, game->player, -1, delta_time); - else if (get_key(game, MLX_KEY_D)) - strave(game->map, game->player, 1, delta_time); - if (get_key(game, MLX_KEY_LEFT)) - rotate(game->player, -2.5, delta_time); - else if (get_key(game, MLX_KEY_RIGHT)) - rotate(game->player, 2.5, delta_time); -} - void player_render(t_screen *screen, t_player *player) { t_vec2 direction; diff --git a/src/render/render_sprite.c b/src/render/render_sprite.c index 7bc454d..484f0d9 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 16:10:05 by whaffman ######## odam.nl */ +/* Updated: 2025/05/14 20:06:28 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -93,8 +93,8 @@ unsigned int sample_texture_color( | (texture->pixels[index + 3] != 0) * alpha); } -static void get_start_end(t_game *game, - t_sprite *sprite, t_vec2_int *start, t_vec2_int *end) +static void get_start_end(t_game *game, + t_sprite *sprite, t_vec2_int *start, t_vec2_int *end) { double sprite_scale; diff --git a/src/screen.c b/src/screen.c index 2194432..bd6fec3 100644 --- a/src/screen.c +++ b/src/screen.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/15 15:30:27 by qmennen #+# #+# */ -/* Updated: 2025/05/14 18:31:32 by whaffman ######## odam.nl */ +/* Updated: 2025/05/14 20:19:21 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -58,13 +58,26 @@ void fill_background(t_screen *screen, int color) } } -int screen_display(t_screen *screen) +int center_window(t_screen *screen) { int m_width; int m_height; m_width = 0; m_height = 0; + mlx_get_monitor_size(0, &m_width, &m_height); + if (m_width == 0 || m_height == 0) + { + printf(RED"Failed to retrieve monitor size to center window\n"RESET); + return (FAILURE); + } + mlx_set_window_pos(screen->mlx, (m_width - screen->width) / 2, + (m_height - screen->height) / 2); + return (SUCCESS); +} + +int screen_display(t_screen *screen) +{ fill_background(screen, 0x000000FF); if (mlx_image_to_window(screen->mlx, screen->background, 0, 0) < 0) { @@ -86,12 +99,7 @@ int screen_display(t_screen *screen) printf(RED"Failed to display buffer image\n"RESET); return (FAILURE); } - mlx_get_monitor_size(0, &m_width, &m_height); - if (m_width == 0 || m_height == 0) - { - printf(RED"Failed to retrieve monitor size to center window\n"RESET); + if (!center_window(screen)) return (FAILURE); - } - mlx_set_window_pos(screen->mlx, (m_width - screen->width) / 2, (m_height - screen->height) / 2); return (SUCCESS); } diff --git a/src/texture/texutre_load.c b/src/texture/texutre_load.c index bdbcb9a..01f80fa 100644 --- a/src/texture/texutre_load.c +++ b/src/texture/texutre_load.c @@ -3,18 +3,18 @@ /* :::::::: */ /* texutre_load.c :+: :+: */ /* +:+ */ -/* By: qmennen +#+ */ +/* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/05/06 15:45:58 by qmennen #+# #+# */ -/* Updated: 2025/05/14 12:48:03 by whaffman ######## odam.nl */ +/* Updated: 2025/05/14 20:01:57 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "texture.h" -void texture_delete(t_game *game) +void texture_delete(t_game *game) { - int i; + int i; i = 0; while (i < 4) @@ -35,7 +35,7 @@ void texture_delete(t_game *game) } } -int texture_load(t_game *game) +int texture_load(t_game *game) { game->map->textures[SIDE_NORTH] = mlx_load_png(game->map->north_texture); game->map->textures[SIDE_EAST] = mlx_load_png(game->map->east_texture); @@ -43,7 +43,8 @@ int texture_load(t_game *game) game->map->textures[SIDE_WEST] = mlx_load_png(game->map->west_texture); game->map->texture_floor = mlx_load_png("./assets/floor.png"); game->map->texture_ceiling = mlx_load_png("./assets/ceiling64x64.png"); - if (!game->map->textures[SIDE_NORTH] || !game->map->textures[SIDE_EAST] || !game->map->textures[SIDE_SOUTH] || !game->map->textures[SIDE_WEST]) + if (!game->map->textures[SIDE_NORTH] || !game->map->textures[SIDE_EAST] + || !game->map->textures[SIDE_SOUTH] || !game->map->textures[SIDE_WEST]) { texture_delete(game); return (FAILURE); diff --git a/src/util/initialize.c b/src/util/initialize.c index 324ca05..4683d14 100644 --- a/src/util/initialize.c +++ b/src/util/initialize.c @@ -3,72 +3,50 @@ /* :::::::: */ /* initialize.c :+: :+: */ /* +:+ */ -/* By: qmennen +#+ */ +/* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/22 17:08:26 by qmennen #+# #+# */ -/* Updated: 2025/05/12 12:51:27 by whaffman ######## odam.nl */ +/* Updated: 2025/05/14 20:33:11 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "cub3d.h" +static t_sprite make_sprite(char *path, double x, double y, int n) +{ + t_sprite sprite; + + sprite.texture = mlx_load_png(path); + sprite.n_frames = n; + sprite.pos.x = x; + sprite.pos.y = y; + sprite.dist = 0; + sprite.cam_frac = 0; + sprite.alpha = 0; + return (sprite); +} + static int init_temp(t_game **game) { + t_sprite *sprites; + (*game)->map->sprites = malloc(sizeof(t_sprite) * 10); if (!(*game)->map->sprites) return (FAILURE); - (*game)->map->sprites[0].n_frames = 1; - (*game)->map->sprites[0].pos.x = 3.5; - (*game)->map->sprites[0].pos.y = 3.5; - (*game)->map->sprites[0].dist = 0; - (*game)->map->sprites[0].texture = mlx_load_png("./assets/battery.png"); - (*game)->map->sprites[1].n_frames = 1; - (*game)->map->sprites[1].pos.x = 2.5; - (*game)->map->sprites[1].pos.y = 3.5; - (*game)->map->sprites[1].dist = 0; - (*game)->map->sprites[1].texture = mlx_load_png("./assets/battery.png"); - (*game)->map->sprites[2].n_frames = 1; - (*game)->map->sprites[2].pos.x = 1.5; - (*game)->map->sprites[2].pos.y = 3.5; - (*game)->map->sprites[2].dist = 0; - (*game)->map->sprites[2].texture = mlx_load_png("./assets/battery.png"); - (*game)->map->sprites[3].n_frames = 1; - (*game)->map->sprites[3].pos.x = 3.5; - (*game)->map->sprites[3].pos.y = 4.5; - (*game)->map->sprites[3].dist = 0; - (*game)->map->sprites[3].texture = mlx_load_png("./assets/broken_mirror.png"); - (*game)->map->sprites[4].n_frames = 1; - (*game)->map->sprites[4].pos.x = 2.5; - (*game)->map->sprites[4].pos.y = 5.5; - (*game)->map->sprites[4].dist = 0; - (*game)->map->sprites[4].texture = mlx_load_png("./assets/lamp.png"); - (*game)->map->sprites[5].n_frames = 1; - (*game)->map->sprites[5].pos.x = 10.5; - (*game)->map->sprites[5].pos.y = 6.5; - (*game)->map->sprites[5].dist = 0; - (*game)->map->sprites[5].texture = mlx_load_png("./assets/battery.png"); - (*game)->map->sprites[6].n_frames = 1; - (*game)->map->sprites[6].pos.x = 14.5; - (*game)->map->sprites[6].pos.y = 3.5; - (*game)->map->sprites[6].dist = 0; - (*game)->map->sprites[6].texture = mlx_load_png("./assets/battery.png"); - (*game)->map->sprites[7].n_frames = 1; - (*game)->map->sprites[7].pos.x = 36.5; - (*game)->map->sprites[7].pos.y = 3.5; - (*game)->map->sprites[7].dist = 0; - (*game)->map->sprites[7].texture = mlx_load_png("./assets/battery.png"); - (*game)->map->sprites[8].n_frames = 1; - (*game)->map->sprites[8].pos.x = 42.5; - (*game)->map->sprites[8].pos.y = 4.5; - (*game)->map->sprites[8].dist = 0; - (*game)->map->sprites[8].texture = mlx_load_png("./assets/broken_mirror.png"); - (*game)->map->sprites[9].n_frames = 1; - (*game)->map->sprites[9].pos.x = 9.5; - (*game)->map->sprites[9].pos.y = 10.5; - (*game)->map->sprites[9].dist = 0; - (*game)->map->sprites[9].texture = mlx_load_png("./assets/lamp.png"); + 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[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[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); (*game)->map->n_sprites = 10; - (*game)->screen->hud = mlx_texture_to_image((*game)->screen->mlx, mlx_load_png("./assets/overlay2.png")); + (*game)->screen->hud = mlx_texture_to_image((*game)->screen->mlx, + mlx_load_png("./assets/overlay2.png")); return (SUCCESS); } diff --git a/src/util/keyboard.c b/src/util/keyboard.c index 28904df..ed6c29d 100644 --- a/src/util/keyboard.c +++ b/src/util/keyboard.c @@ -3,15 +3,16 @@ /* :::::::: */ /* keyboard.c :+: :+: */ /* +:+ */ -/* By: qmennen +#+ */ +/* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/04/17 19:29:29 by qmennen #+# #+# */ -/* Updated: 2025/05/07 11:35:41 by whaffman ######## odam.nl */ +/* Updated: 2025/05/14 20:01:04 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "keyboard.h" +// TODO: Better int keyboard_create(t_game **game) { t_keyboard *keyboard; @@ -22,7 +23,7 @@ int keyboard_create(t_game **game) return (FAILURE); (*game)->keyboard = keyboard; i = 0; - while (i < NUM_KEYS) // TODO: Better + while (i < NUM_KEYS) { (*game)->keyboard->keys[i] = 0; i++;