/* 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);
}
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);
}
/* 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);
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);
}