From f51bd1092c066090828c61ba0930bd0e88bcc27d Mon Sep 17 00:00:00 2001 From: whaffman Date: Fri, 6 Dec 2024 13:12:01 +0100 Subject: [PATCH] read map start --- fdf.c | 134 +++++++++++++++++++++++++----------------------------- inc/fdf.h | 36 +++++++++++++++ 2 files changed, 99 insertions(+), 71 deletions(-) diff --git a/fdf.c b/fdf.c index 08b95a4..d5fc40f 100644 --- a/fdf.c +++ b/fdf.c @@ -1,89 +1,81 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* fdf.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/06 11:07:30 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/06 11:19:30 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ -// ----------------------------------------------------------------------------- -// Codam Coding College, Amsterdam @ 2022-2023 by W2Wizard. -// See README in the root project for more information. -// ----------------------------------------------------------------------------- +#include "fdf.h" -#include -#include -#include -#include "MLX42.h" -#define WIDTH 512 -#define HEIGHT 512 +//# include "MLX42.h" -static mlx_image_t* image; -// ----------------------------------------------------------------------------- - -int32_t ft_pixel(int32_t r, int32_t g, int32_t b, int32_t a) +int get_map_sizes(char *filename, t_map *map) { - return (r << 24 | g << 16 | b << 8 | a); + int fd; + char *line; + int width; + int height; + + fd = open(filename, O_RDONLY); + if (fd < 0) + { + ft_printf("Error: could not open file %s\n", filename); + return (0); + } + width = 0; + height = 0; + while (true) + { + line = get_next_line(fd); + if (!line) + break ; + width = ft_count_words(line, ' '); + height++; + free(line); + } + close(fd); + map->width = width; + map->height = height; + return (1); } -void ft_randomize(void* param) +int read_map(char *filename, t_map *map) { - (void)param; - for (uint32_t i = 0; i < image->width; ++i) + int fd; + char *line; + + get_map_sizes(filename, map); + fd = open(filename, O_RDONLY); + if (fd < 0) { - for (uint32_t y = 0; y < image->height; ++y) - { - uint32_t color = ft_pixel( - rand() % 0xFF, // R - rand() % 0xFF, // G - rand() % 0xFF, // B - rand() % 0xFF // A - ); - mlx_put_pixel(image, i, y, color); - } + ft_printf("Error: could not open file %s\n", filename); + return (0); } + while (true) + { + ft_printf("%s\n", line); + free(line); + line = get_next_line(fd); + } + close(fd); + return (1); } -void ft_hook(void* param) +int main(int argc, char *argv[]) { - mlx_t* mlx = param; - - if (mlx_is_key_down(mlx, MLX_KEY_ESCAPE)) - mlx_close_window(mlx); - if (mlx_is_key_down(mlx, MLX_KEY_UP)) - image->instances[0].y -= 5; - if (mlx_is_key_down(mlx, MLX_KEY_DOWN)) - image->instances[0].y += 5; - if (mlx_is_key_down(mlx, MLX_KEY_LEFT)) - image->instances[0].x -= 5; - if (mlx_is_key_down(mlx, MLX_KEY_RIGHT)) - image->instances[0].x += 5; -} - -// ----------------------------------------------------------------------------- - -int32_t main(void) -{ - mlx_t* mlx; - - // Gotta error check this stuff - if (!(mlx = mlx_init(WIDTH, HEIGHT, "MLX42", true))) + if (argc != 2) { - puts(mlx_strerror(mlx_errno)); - return(EXIT_FAILURE); + ft_printf("Usage: %s \n", argv[0]); + return (EXIT_FAILURE); } - if (!(image = mlx_new_image(mlx, 128, 128))) - { - mlx_close_window(mlx); - puts(mlx_strerror(mlx_errno)); - return(EXIT_FAILURE); - } - if (mlx_image_to_window(mlx, image, 0, 0) == -1) - { - mlx_close_window(mlx); - puts(mlx_strerror(mlx_errno)); - return(EXIT_FAILURE); - } - - mlx_loop_hook(mlx, ft_randomize, mlx); - mlx_loop_hook(mlx, ft_hook, mlx); - - mlx_loop(mlx); - mlx_terminate(mlx); + if(!read_map(argv[1])) + return (EXIT_FAILURE); return (EXIT_SUCCESS); } diff --git a/inc/fdf.h b/inc/fdf.h index e69de29..0e48111 100644 --- a/inc/fdf.h +++ b/inc/fdf.h @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* fdf.h :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/06 11:07:39 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/06 11:11:56 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#ifndef FDF_H +# define FDF_H +# include "libft.h" +# include +# include +# include +# include + +typedef struct s_point +{ + int x; + int y; + int z; +} t_point; + +typedef struct s_map +{ + t_point *points; + int width; + int height; +} t_map; + + +#endif