Libft and ftprintf added
This commit is contained in:
129
src/main.c
129
src/main.c
@@ -6,112 +6,41 @@
|
||||
/* By: aortigos <aortigos@student.42malaga.com +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/11/14 09:35:53 by aortigos #+# #+# */
|
||||
/* Updated: 2025/11/14 09:43:55 by aortigos ### ########.fr */
|
||||
/* Updated: 2025/11/15 10:29:24 by aortigos ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/cub3d.h"
|
||||
void draw_player(mlx_image_t *img, t_player *player, int color)
|
||||
|
||||
// Main ->
|
||||
// parseamos el archivo .cub ->
|
||||
// Inicializamos player / map / textures ->
|
||||
// Inicializamos mlx ->
|
||||
// Game loop ->
|
||||
// -> -> mover jugador
|
||||
// -> -> lanzar raycasting
|
||||
// -> -> dibujar paredes
|
||||
// -> -> mostrar imagen en ventana
|
||||
// salir y liberar memoria
|
||||
|
||||
int check_extension(char *str)
|
||||
{
|
||||
for (int i = 0; i < 20; i++)
|
||||
for (int j = 0; j < 20; j++)
|
||||
mlx_put_pixel(img, player->player_x + i, player->player_y + j, color);
|
||||
}
|
||||
int i;
|
||||
|
||||
void draw_map(mlx_image_t *img, char **map)
|
||||
{
|
||||
int x, y, i, j;
|
||||
|
||||
i = 0;
|
||||
while (map[i])
|
||||
{
|
||||
j = 0;
|
||||
while (map[i][j])
|
||||
{
|
||||
if (map[i][j] == '1')
|
||||
{
|
||||
for (y = 0; y < TILE_SIZE; y++)
|
||||
for (x = 0; x < TILE_SIZE; x++)
|
||||
mlx_put_pixel(img, j * TILE_SIZE + x, i * TILE_SIZE + y, 0xFFFFFFFF);
|
||||
}
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------ Input ------------------
|
||||
|
||||
void key_hook(mlx_key_data_t keydata, void *param)
|
||||
{
|
||||
t_player *player = (t_player *)param;
|
||||
|
||||
if (keydata.key == MLX_KEY_W && keydata.action == MLX_PRESS)
|
||||
player->player_y -= 10;
|
||||
if (keydata.key == MLX_KEY_S && keydata.action == MLX_PRESS)
|
||||
player->player_y += 10;
|
||||
if (keydata.key == MLX_KEY_A && keydata.action == MLX_PRESS)
|
||||
player->player_x -= 10;
|
||||
if (keydata.key == MLX_KEY_D && keydata.action == MLX_PRESS)
|
||||
player->player_x += 10;
|
||||
}
|
||||
|
||||
// ------------------ Game Loop ------------------
|
||||
|
||||
void game_loop(void *param)
|
||||
{
|
||||
t_mlx *mlx_data = (t_mlx *)param;
|
||||
|
||||
for (int y = 0; y < S_H; y++)
|
||||
for (int x = 0; x < S_W; x++)
|
||||
mlx_put_pixel(mlx_data->img, x, y, 0x000000FF); // negro
|
||||
draw_map(mlx_data->img, mlx_data->map); // dibujar mapa
|
||||
draw_player(mlx_data->img, mlx_data->player, 0xFF0000FF); // dibujar jugador
|
||||
}
|
||||
|
||||
// ------------------ Main ------------------
|
||||
|
||||
int main(void)
|
||||
{
|
||||
mlx_t *mlx;
|
||||
mlx_image_t *img;
|
||||
t_player player;
|
||||
t_mlx mlx_data;
|
||||
|
||||
// mapa simple
|
||||
char *map[] = {
|
||||
"111111111",
|
||||
"100000011",
|
||||
"101000001",
|
||||
"100001001",
|
||||
"111111111",
|
||||
NULL
|
||||
};
|
||||
|
||||
// inicializar MLX
|
||||
mlx = mlx_init(S_W, S_H, "Cub3D", false);
|
||||
if (!mlx)
|
||||
i = ft_strlen(str);
|
||||
if (i < 4)
|
||||
return (0);
|
||||
if (ft_strncmp(str + (i - 4), ".cub", 4) == 0)
|
||||
return (1);
|
||||
|
||||
img = mlx_new_image(mlx, S_W, S_H);
|
||||
mlx_image_to_window(mlx, img, 0, 0);
|
||||
|
||||
// inicializar jugador
|
||||
player.player_x = 100;
|
||||
player.player_y = 100;
|
||||
|
||||
// inicializar estructura de MLX
|
||||
mlx_data.mlx_ptr = mlx;
|
||||
mlx_data.img = img;
|
||||
mlx_data.player = &player;
|
||||
mlx_data.map = map;
|
||||
|
||||
// hooks
|
||||
mlx_key_hook(mlx, key_hook, &player);
|
||||
mlx_loop_hook(mlx, game_loop, &mlx_data);
|
||||
|
||||
// loop
|
||||
mlx_loop(mlx);
|
||||
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc != 2 || !check_extension(argv[1]))
|
||||
{
|
||||
ft_printf("Hace falta un archivo .cub\n");
|
||||
return (1);
|
||||
}
|
||||
ft_printf("Archivo recibido: %s\n", argv[1]);
|
||||
}
|
||||
Reference in New Issue
Block a user