read map start
This commit is contained in:
parent
1c3acdfaef
commit
f51bd1092c
132
fdf.c
132
fdf.c
@ -1,89 +1,81 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* fdf.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#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)
|
||||
ft_printf("Error: could not open file %s\n", filename);
|
||||
return (0);
|
||||
}
|
||||
while (true)
|
||||
{
|
||||
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("%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 <filename>\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);
|
||||
}
|
||||
|
||||
36
inc/fdf.h
36
inc/fdf.h
@ -0,0 +1,36 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* fdf.h :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \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 <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include <fcntl.h>
|
||||
# include <stdbool.h>
|
||||
|
||||
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
|
||||
Loading…
Reference in New Issue
Block a user