finalizing
This commit is contained in:
parent
d66beb964d
commit
92fb7e8687
81
inc/fdf.h
81
inc/fdf.h
@ -16,20 +16,16 @@
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include <fcntl.h>
|
||||
# include <stdbool.h>
|
||||
# include <math.h>
|
||||
# include <errno.h>
|
||||
# include "libft.h"
|
||||
# include "MLX42.h"
|
||||
# include <stdio.h>
|
||||
|
||||
# define SUCCESS 1
|
||||
# define FAILURE 0
|
||||
|
||||
# define WRONG_LINE_LENGTH "Error: wrong line length.\n"
|
||||
# define MALLOC_ERROR "Error: malloc failed\n"
|
||||
# define FILE_ERROR "Error: could not open file %s\n"
|
||||
# define USAGE_ERROR "Usage: %s <filename>\n"
|
||||
# define WRONG_LINE_LENGTH "Error: wrong line length."
|
||||
# define MALLOC_ERROR "Error: malloc failed"
|
||||
# define FILE_ERROR "Error: could not open file for reading"
|
||||
# define USAGE_ERROR "Usage: ./fdf <filename>"
|
||||
|
||||
# ifndef M_PI
|
||||
# define M_PI 3.14159265358979323846
|
||||
@ -79,6 +75,7 @@ typedef struct s_map
|
||||
int width;
|
||||
int height;
|
||||
int z_max;
|
||||
int z_min;
|
||||
} t_map;
|
||||
|
||||
typedef struct s_fdf
|
||||
@ -101,48 +98,48 @@ typedef struct s_fdf
|
||||
t_projection projection;
|
||||
} t_fdf;
|
||||
|
||||
void free_line_and_split(char **line, char ***split);
|
||||
int get_map_sizes(char *filename, t_map *map);
|
||||
int get_map_sizes(char *filename, t_fdf *fdf);
|
||||
int load_map_from_file(char *filename, t_fdf *fdf);
|
||||
int parse_map(char *filename, t_fdf *fdf);
|
||||
void parse_map(char *filename, t_fdf *fdf);
|
||||
void set_map_point(t_fdf *fdf, int x, int y, char **str);
|
||||
int parse_color_string(char *str);
|
||||
void get_z_max(t_fdf *fdf);
|
||||
void handle_error(t_fdf *fdf, char *error);
|
||||
void get_z_min_max(t_fdf *fdf);
|
||||
|
||||
int fdf_hooks(t_fdf *fdf);
|
||||
void fdf_hooks(t_fdf *fdf);
|
||||
void resize_hook(int width, int height, void *param);
|
||||
void draw_hook(void *param);
|
||||
void key_hook(mlx_key_data_t keydata, void *param);
|
||||
|
||||
mlx_image_t *draw_menu(t_fdf *fdf);
|
||||
void apply_rotation(t_fdf *fdf);
|
||||
int get_z_color(double z, int z_max);
|
||||
void rotate_x(t_point_3d **points, double angle, int size);
|
||||
void rotate_y(t_point_3d **points, double angle, int size);
|
||||
void rotate_z(t_point_3d **points, double angle, int size);
|
||||
void project_isometric(t_fdf *fdf);
|
||||
int interpolate_color(t_point_2d start,
|
||||
t_point_2d end, t_point_2d current);
|
||||
void draw_line(t_fdf *fdf, t_point_2d start,
|
||||
t_point_2d end);
|
||||
bool put_pixel(t_fdf *fdf, t_point_2d point);
|
||||
void fdf_set_background(mlx_image_t *img, int color);
|
||||
double deg2rad(double deg);
|
||||
t_fdf *initialise_fdf(void);
|
||||
bool clean_fdf(t_fdf *fdf);
|
||||
int init_mlx(t_fdf *fdf);
|
||||
void close_hook(void *param);
|
||||
int get_gradient_color(double z, int z_max);
|
||||
void project_parallel(t_fdf *fdf);
|
||||
void project_trimetric(t_fdf *fdf);
|
||||
|
||||
void project(t_fdf *fdf);
|
||||
int check_filename(char *filename);
|
||||
void get_pixel_color(t_fdf *fdf, t_point_2d *point);
|
||||
void reset_fdf(t_fdf *fdf);
|
||||
void prepare_draw(t_fdf *fdf);
|
||||
void key_hook(mlx_key_data_t keydata, void *param);
|
||||
void key_hook_options(mlx_key_data_t keydata, t_fdf *fdf);
|
||||
void key_hook_transform(mlx_key_data_t keydata, t_fdf *fdf);
|
||||
|
||||
mlx_image_t *draw_menu(t_fdf *fdf);
|
||||
void apply_rotation(t_fdf *fdf);
|
||||
void rotate_x(t_point_3d **points, double angle, int size);
|
||||
void rotate_y(t_point_3d **points, double angle, int size);
|
||||
void rotate_z(t_point_3d **points, double angle, int size);
|
||||
void project(t_fdf *fdf);
|
||||
void project_isometric(t_fdf *fdf);
|
||||
void project_parallel(t_fdf *fdf);
|
||||
void project_trimetric(t_fdf *fdf);
|
||||
void get_pixel_color(t_fdf *fdf, t_point_2d *point);
|
||||
int get_z_color(double z, int z_min, int z_max);
|
||||
int get_gradient_color(double z, int z_min, int z_max);
|
||||
int interpolate_color(t_point_2d start,
|
||||
t_point_2d end, t_point_2d current);
|
||||
void prepare_draw(t_fdf *fdf);
|
||||
void draw_line(t_fdf *fdf, t_point_2d start,
|
||||
t_point_2d end);
|
||||
void put_pixel(t_fdf *fdf, t_point_2d point);
|
||||
void fdf_set_background(mlx_image_t *img, int color);
|
||||
|
||||
t_fdf *initialise_fdf(void);
|
||||
void free_line_and_split(char **line, char ***split);
|
||||
void init_mlx(t_fdf *fdf);
|
||||
void clean_fdf(t_fdf *fdf);
|
||||
int check_filename(char *filename);
|
||||
void reset_fdf(t_fdf *fdf);
|
||||
double deg2rad(double deg);
|
||||
void handle_error(t_fdf *fdf, char *error);
|
||||
|
||||
#endif
|
||||
|
||||
@ -10,11 +10,13 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int get_gradient_color(double z, int z_max)
|
||||
int get_gradient_color(double z, int z_min, int z_max)
|
||||
{
|
||||
const int colors[] = {0x0058fbff, 0x007efeff, 0x008fd7ff, 0x0098a3ff,
|
||||
0x009c7aff, 0x1fa46bff, 0x5eb13dff, 0x83c052ff,
|
||||
0xa5cf69ff, 0xc3de81ff, 0xeeffffff, 0xffffffff, 0xffffffff};
|
||||
|
||||
return (colors[(int)(z * 12 / z_max)]);
|
||||
if (z < z_min || z_max == z_min)
|
||||
return (0xffffffff);
|
||||
return (colors[(int)((z - z_min) * 12 / (z_max - z_min))]);
|
||||
}
|
||||
|
||||
@ -17,7 +17,13 @@ void get_pixel_color(t_fdf *fdf, t_point_2d *point)
|
||||
if (fdf->colormode == COLOR_MODE_DEFAULT)
|
||||
point->color = point->color;
|
||||
else if (fdf->colormode == COLOR_MODE_Z)
|
||||
point->color = get_z_color(point->orig_z, fdf->map->z_max);
|
||||
{
|
||||
point->color = get_z_color(point->orig_z,
|
||||
fdf->map->z_min, fdf->map->z_max);
|
||||
}
|
||||
else if (fdf->colormode == COLOR_MODE_GRADIENT)
|
||||
point->color = get_gradient_color(point->orig_z, fdf->map->z_max);
|
||||
{
|
||||
point->color = get_gradient_color(point->orig_z,
|
||||
fdf->map->z_min, fdf->map->z_max);
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,7 +10,7 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int get_z_color(double z, int z_max)
|
||||
int get_z_color(double z, int z_min, int z_max)
|
||||
{
|
||||
int red;
|
||||
int green;
|
||||
@ -19,11 +19,11 @@ int get_z_color(double z, int z_max)
|
||||
red = 0;
|
||||
green = 0;
|
||||
blue = 0;
|
||||
if (z < 0)
|
||||
blue = 127;
|
||||
if (z < 0 || z_max == z_min)
|
||||
blue = 200;
|
||||
else
|
||||
{
|
||||
red = 255 * z / z_max;
|
||||
red = 255 * (z - z_min) / (z_max - z_min);
|
||||
green = 255 - red;
|
||||
}
|
||||
return (red << 24 | green << 16 | blue << 8 | 0x00FF);
|
||||
|
||||
@ -1,6 +1,18 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* prepare_draw.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/20 15:18:36 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/20 15:18:36 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
void prepare_draw(t_fdf *fdf)
|
||||
void prepare_draw(t_fdf *fdf)
|
||||
{
|
||||
fdf_set_background(fdf->img, 0x000000FF);
|
||||
if (fdf->animate_z)
|
||||
|
||||
@ -12,11 +12,10 @@
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
bool put_pixel(t_fdf *fdf, t_point_2d point)
|
||||
void put_pixel(t_fdf *fdf, t_point_2d point)
|
||||
{
|
||||
if (point.x < 0 || point.x >= (int) fdf->img->width
|
||||
|| point.y < 0 || point.y >= (int) fdf->img->height)
|
||||
return (false);
|
||||
return ;
|
||||
mlx_put_pixel(fdf->img, point.x, point.y, point.color);
|
||||
return (true);
|
||||
}
|
||||
|
||||
@ -21,8 +21,7 @@ int main(int argc, char *argv[])
|
||||
handle_error(fdf, "Usage: ./fdf <filename>");
|
||||
if (!check_filename(argv[1]))
|
||||
handle_error(fdf, "Error: wrong file extension");
|
||||
if (!parse_map(argv[1], fdf))
|
||||
handle_error(fdf, "Error: failed to parse map");
|
||||
parse_map(argv[1], fdf);
|
||||
init_mlx(fdf);
|
||||
fdf->menu = draw_menu(fdf);
|
||||
fdf->menu->enabled = false;
|
||||
|
||||
@ -13,11 +13,10 @@
|
||||
#include "fdf.h"
|
||||
#include "MLX42.h"
|
||||
|
||||
int fdf_hooks(t_fdf *fdf)
|
||||
void fdf_hooks(t_fdf *fdf)
|
||||
{
|
||||
mlx_loop_hook(fdf->mlx, draw_hook, fdf);
|
||||
mlx_key_hook(fdf->mlx, key_hook, fdf);
|
||||
mlx_close_hook(fdf->mlx, close_hook, fdf);
|
||||
mlx_resize_hook(fdf->mlx, resize_hook, fdf);
|
||||
return (1);
|
||||
}
|
||||
|
||||
@ -12,14 +12,16 @@
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
int get_map_sizes(char *filename, t_map *map)
|
||||
int get_map_sizes(char *filename, t_fdf *fdf)
|
||||
{
|
||||
const int fd = open(filename, O_RDONLY);
|
||||
char *line;
|
||||
int width;
|
||||
t_map *map;
|
||||
|
||||
map = fdf->map;
|
||||
if (fd < 0)
|
||||
return (ft_printf(FILE_ERROR, filename), 0);
|
||||
handle_error(fdf, FILE_ERROR);
|
||||
width = 0;
|
||||
while (true)
|
||||
{
|
||||
@ -29,7 +31,7 @@ int get_map_sizes(char *filename, t_map *map)
|
||||
width = ft_count_words(line, ' ');
|
||||
free(line);
|
||||
if (map->width != 0 && map->width != width)
|
||||
return (ft_printf(WRONG_LINE_LENGTH), 0);
|
||||
handle_error(fdf, WRONG_LINE_LENGTH);
|
||||
else if (map->width == 0)
|
||||
map->width = width;
|
||||
map->height++;
|
||||
|
||||
@ -1,27 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* get_z_max.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/13 15:23:15 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/20 11:21:33 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
void get_z_max(t_fdf *fdf)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
fdf->map->z_max = 0;
|
||||
while (i < fdf->map->width * fdf->map->height)
|
||||
{
|
||||
if (fdf->map->orig[i].z > fdf->map->z_max)
|
||||
fdf->map->z_max = fdf->map->orig[i].z;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@ -12,11 +12,11 @@
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
int parse_map(char *filename, t_fdf *fdf)
|
||||
void parse_map(char *filename, t_fdf *fdf)
|
||||
{
|
||||
int map_size;
|
||||
|
||||
if (!get_map_sizes(filename, fdf->map))
|
||||
if (!get_map_sizes(filename, fdf))
|
||||
handle_error(fdf, "Error: failed to get map sizes");
|
||||
map_size = fdf->map->width * fdf->map->height;
|
||||
fdf->map->orig = malloc(map_size * sizeof(t_point_3d));
|
||||
@ -26,6 +26,5 @@ int parse_map(char *filename, t_fdf *fdf)
|
||||
handle_error(fdf, MALLOC_ERROR);
|
||||
if (!load_map_from_file(filename, fdf))
|
||||
handle_error(fdf, "Error: failed to read map");
|
||||
get_z_max(fdf);
|
||||
return (SUCCESS);
|
||||
get_z_min_max(fdf);
|
||||
}
|
||||
|
||||
@ -14,8 +14,8 @@
|
||||
|
||||
void set_map_point(t_fdf *fdf, int x, int y, char **str)
|
||||
{
|
||||
fdf->map->orig[y * fdf->map->width + x].x = x - fdf->map->width / 2;
|
||||
fdf->map->orig[y * fdf->map->width + x].y = fdf->map->height / 2 - y;
|
||||
fdf->map->orig[y * fdf->map->width + x].x = x - fdf->map->width / 2.0 + 1;
|
||||
fdf->map->orig[y * fdf->map->width + x].y = fdf->map->height / 2.0 - y;
|
||||
fdf->map->orig[y * fdf->map->width + x].z = ft_atoi(str[x]);
|
||||
fdf->map->orig[y * fdf->map->width + x].color = parse_color_string(str[x]);
|
||||
}
|
||||
|
||||
@ -12,15 +12,18 @@
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
bool clean_fdf(t_fdf *fdf)
|
||||
void clean_fdf(t_fdf *fdf)
|
||||
{
|
||||
if (fdf->map)
|
||||
if (fdf && fdf->map)
|
||||
{
|
||||
free(fdf->map->orig);
|
||||
free(fdf->map->rot);
|
||||
free(fdf->map->proj);
|
||||
if (fdf->map->orig)
|
||||
free(fdf->map->orig);
|
||||
if (fdf->map->rot)
|
||||
free(fdf->map->rot);
|
||||
if (fdf->map->proj)
|
||||
free(fdf->map->proj);
|
||||
free(fdf->map);
|
||||
}
|
||||
free(fdf);
|
||||
return (true);
|
||||
if (fdf)
|
||||
free(fdf);
|
||||
}
|
||||
|
||||
@ -16,9 +16,16 @@ void handle_error(t_fdf *fdf, char *error)
|
||||
{
|
||||
if (errno)
|
||||
perror(error);
|
||||
else
|
||||
ft_putendl_fd(error, 2);
|
||||
if (mlx_errno)
|
||||
ft_putendl_fd(mlx_strerror(mlx_errno), 2);
|
||||
ft_putendl_fd(error, 2);
|
||||
if (fdf && fdf->img)
|
||||
mlx_delete_image(fdf->mlx, fdf->img);
|
||||
if (fdf && fdf->menu)
|
||||
mlx_delete_image(fdf->mlx, fdf->menu);
|
||||
if (fdf && fdf->mlx)
|
||||
mlx_terminate(fdf->mlx);
|
||||
clean_fdf(fdf);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@ -12,7 +12,7 @@
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
int init_mlx(t_fdf *fdf)
|
||||
void init_mlx(t_fdf *fdf)
|
||||
{
|
||||
mlx_t *mlx;
|
||||
|
||||
@ -24,5 +24,4 @@ int init_mlx(t_fdf *fdf)
|
||||
if (!fdf->img || (mlx_image_to_window(fdf->mlx, fdf->img, 0, 0) < 0))
|
||||
handle_error(fdf, "Error: failed to create image");
|
||||
fdf_hooks(fdf);
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
@ -21,9 +21,9 @@ t_fdf *initialise_fdf(void)
|
||||
if (!fdf)
|
||||
handle_error(fdf, MALLOC_ERROR);
|
||||
map = malloc(sizeof(t_map));
|
||||
fdf->map = map;
|
||||
if (!map)
|
||||
handle_error(fdf, MALLOC_ERROR);
|
||||
fdf->map = map;
|
||||
fdf->mlx = NULL;
|
||||
fdf->img = NULL;
|
||||
fdf->last_width = WIDTH;
|
||||
@ -34,6 +34,7 @@ t_fdf *initialise_fdf(void)
|
||||
fdf->map->width = 0;
|
||||
fdf->map->height = 0;
|
||||
fdf->map->z_max = 0;
|
||||
fdf->map->z_min = 0;
|
||||
reset_fdf(fdf);
|
||||
return (fdf);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user