]> git.dkaiser.de - 42/so_long.git/commitdiff
Finish map input
authorDominik Kaiser <dkaiser@1-C-5.42heilbronn.de>
Sat, 11 May 2024 14:26:51 +0000 (16:26 +0200)
committerDominik Kaiser <dkaiser@1-C-5.42heilbronn.de>
Sat, 11 May 2024 14:26:51 +0000 (16:26 +0200)
include/so_long.h
src/init.c
src/tilemap.c

index 2333368c96ff4d25be038dde7588190c3666b537..47443aaae188bddd581bd1166853bda8a99e98fd 100644 (file)
@@ -63,6 +63,8 @@ typedef struct s_tilemap
        t_ivector       grid_size;
        t_ivector       tile_size;
        char            *tiles;
+       t_ivector       player_start_tile;
+       t_ivector       exit_tile;
 }                              t_tilemap;
 
 typedef struct s_game
index 3c1579172d69051ee735c27c4bd419580670e03e..5277b5cf2f92cc5185601e566c9aab6fe7d643ed 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/09 14:50:09 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/05/10 13:05:13 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/05/11 16:23:54 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "so_long.h"
 
 static void    init_hooks(t_game *game);
-static void    init_player(t_player *player, int x, int y);
+static void    init_player(t_game *game);
 
 int    init(t_game *game)
 {
-       mlx_texture_t   *texture;
 
        game->mlx = mlx_init(1920, 1080, "so_long", false);
        // TODO: make size and title dynamic
        game->input_direction = ZERO;
        init_hooks(game);
-       init_player(&game->player, 960, 540);
-       // TODO: make player spawn point dynamic
-       texture = mlx_load_png("textures/player.png");
-       game->player.img = mlx_texture_to_image(game->mlx, texture);
-       mlx_resize_image(game->player.img, 48, 48);
-       mlx_image_to_window(game->mlx, game->player.img, game->player.position.x,
-               game->player.position.y);
+       init_player(game);
        return (0);
 }
 
@@ -40,10 +33,19 @@ static void init_hooks(t_game *game)
        mlx_key_hook(game->mlx, on_key_input, game);
 }
 
-static void    init_player(t_player *player, int x, int y)
+static void    init_player(t_game *game)
 {
-       player->position.x = x;
-       player->position.y = y;
+       mlx_texture_t   *texture;
+       t_player *player;
+
+       player = &game->player;
+       texture = mlx_load_png("textures/player.png");
+       player->position.x = game->map.player_start_tile.x * 48;
+       player->position.y = game->map.player_start_tile.y * 48;
        player->velocity.x = 0;
        player->velocity.y = 0;
+       player->img = mlx_texture_to_image(game->mlx, texture);
+       mlx_resize_image(player->img, 48, 48);
+       mlx_image_to_window(game->mlx, player->img, player->position.x,
+                                               player->position.y);
 }
index 45d9fa545c7c038992a3074a2ae8f8fcc60015e1..ef830c761e3f309358be7fea8c1ed21a3a5f343b 100644 (file)
@@ -6,11 +6,12 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/10 14:47:01 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/05/10 17:26:17 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/05/11 16:19:50 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "get_next_line.h"
+#include "libft.h"
 #include "so_long.h"
 
 static t_ivector       get_map_size_from_file(int fd);
@@ -80,27 +81,38 @@ static int  load_tiles_from_file(t_tilemap *tilemap, int fd)
        next_line = get_next_line(fd);
        while (next_line)
        {
-               if (load_tiles_from_line(tilemap, l, next_line))
+               if (load_tiles_from_line(tilemap, l++, next_line))
                        return (1);
                next_line = get_next_line(fd);
-               l++;
        }
        return (0);
 }
 
 static int     load_tiles_from_line(t_tilemap *tilemap, int l, char *line)
 {
-       int     x;
+       t_ivector       pos;
+       static int      found_player_start;
+       static int      found_exit;
 
-       x = 0;
-       while (line[x] && line[x] != '\n')
+       pos.x = 0;
+       pos.y = l;
+       while (line[pos.x] && line[pos.x] != '\n')
        {
-               if (line[x] == EMPTY || line[x] == WALL || line[x] == COLLECTIBLE
-                       || line[x] == PLAYER_START || line[x] == EXIT)
-                       tilemap->tiles[l * tilemap->grid_size.x + x] = line[x];
-               else
+               if (line[pos.x] != '0' && line[pos.x] != '1' && line[pos.x] != 'C'
+                       && line[pos.x] != 'P' && line[pos.x] != 'E')
                        return (1);
-               line++;
+               if (line[pos.x] == PLAYER_START && !found_player_start)
+               {
+                       tilemap->player_start_tile = pos;
+                       found_player_start = 1;
+               }
+               else if (line[pos.x] == EXIT && !found_exit)
+               {
+                       tilemap->exit_tile = pos;
+                       found_exit = 1;
+               }
+               tilemap->tiles[l * tilemap->grid_size.x + pos.x] = line[pos.x];
+               pos.x++;
        }
        return (0);
 }