Compare commits

..

No commits in common. "f42a00a27b787adba2781e7772c64a8bc6d96cad" and "d66beb964dedbfd96037ad01c42c37dc29065d3f" have entirely different histories.

18 changed files with 70 additions and 108 deletions

View File

@ -16,16 +16,20 @@
# include <stdlib.h> # include <stdlib.h>
# include <unistd.h> # include <unistd.h>
# include <fcntl.h> # include <fcntl.h>
# include <stdbool.h>
# include <math.h> # include <math.h>
# include <errno.h> # include <errno.h>
# include "libft.h" # include "libft.h"
# include "MLX42.h" # include "MLX42.h"
# include <stdio.h> # include <stdio.h>
# define WRONG_LINE_LENGTH "Error: wrong line length." # define SUCCESS 1
# define MALLOC_ERROR "Error: malloc failed" # define FAILURE 0
# define FILE_ERROR "Error: could not open file for reading"
# define USAGE_ERROR "Usage: ./fdf <filename>" # 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"
# ifndef M_PI # ifndef M_PI
# define M_PI 3.14159265358979323846 # define M_PI 3.14159265358979323846
@ -75,7 +79,6 @@ typedef struct s_map
int width; int width;
int height; int height;
int z_max; int z_max;
int z_min;
} t_map; } t_map;
typedef struct s_fdf typedef struct s_fdf
@ -98,48 +101,48 @@ typedef struct s_fdf
t_projection projection; t_projection projection;
} t_fdf; } t_fdf;
int get_map_sizes(char *filename, t_fdf *fdf); void free_line_and_split(char **line, char ***split);
int get_map_sizes(char *filename, t_map *map);
int load_map_from_file(char *filename, t_fdf *fdf); int load_map_from_file(char *filename, t_fdf *fdf);
void parse_map(char *filename, t_fdf *fdf); int parse_map(char *filename, t_fdf *fdf);
void set_map_point(t_fdf *fdf, int x, int y, char **str); void set_map_point(t_fdf *fdf, int x, int y, char **str);
int parse_color_string(char *str); int parse_color_string(char *str);
void get_z_min_max(t_fdf *fdf); void get_z_max(t_fdf *fdf);
void handle_error(t_fdf *fdf, char *error);
void fdf_hooks(t_fdf *fdf); int fdf_hooks(t_fdf *fdf);
void resize_hook(int width, int height, void *param); void resize_hook(int width, int height, void *param);
void draw_hook(void *param); void draw_hook(void *param);
void close_hook(void *param);
void key_hook(mlx_key_data_t keydata, void *param); 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); mlx_image_t *draw_menu(t_fdf *fdf);
void apply_rotation(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_x(t_point_3d **points, double angle, int size);
void rotate_y(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 rotate_z(t_point_3d **points, double angle, int size);
void project(t_fdf *fdf);
void project_isometric(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, int interpolate_color(t_point_2d start,
t_point_2d end, t_point_2d current); t_point_2d end, t_point_2d current);
void prepare_draw(t_fdf *fdf);
void draw_line(t_fdf *fdf, t_point_2d start, void draw_line(t_fdf *fdf, t_point_2d start,
t_point_2d end); t_point_2d end);
void put_pixel(t_fdf *fdf, t_point_2d point); bool put_pixel(t_fdf *fdf, t_point_2d point);
void fdf_set_background(mlx_image_t *img, int color); 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); double deg2rad(double deg);
void handle_error(t_fdf *fdf, char *error); 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_options(mlx_key_data_t keydata, t_fdf *fdf);
void key_hook_transform(mlx_key_data_t keydata, t_fdf *fdf);
#endif #endif

View File

@ -1,5 +0,0 @@
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

View File

@ -1,4 +0,0 @@
0 0 0 15
0 0 0 0
0 0 0 0
0 0 0 10

View File

@ -10,13 +10,11 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
int get_gradient_color(double z, int z_min, int z_max) int get_gradient_color(double z, int z_max)
{ {
const int colors[] = {0x0058fbff, 0x007efeff, 0x008fd7ff, 0x0098a3ff, const int colors[] = {0x0058fbff, 0x007efeff, 0x008fd7ff, 0x0098a3ff,
0x009c7aff, 0x1fa46bff, 0x5eb13dff, 0x83c052ff, 0x009c7aff, 0x1fa46bff, 0x5eb13dff, 0x83c052ff,
0xa5cf69ff, 0xc3de81ff, 0xeeffffff, 0xffffffff, 0xffffffff}; 0xa5cf69ff, 0xc3de81ff, 0xeeffffff, 0xffffffff, 0xffffffff};
if (z < z_min || z_max == z_min) return (colors[(int)(z * 12 / z_max)]);
return (0xffffffff);
return (colors[(int)((z - z_min) * 12 / (z_max - z_min))]);
} }

View File

@ -17,13 +17,7 @@ void get_pixel_color(t_fdf *fdf, t_point_2d *point)
if (fdf->colormode == COLOR_MODE_DEFAULT) if (fdf->colormode == COLOR_MODE_DEFAULT)
point->color = point->color; point->color = point->color;
else if (fdf->colormode == COLOR_MODE_Z) 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) 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);
}
} }

View File

@ -10,7 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
int get_z_color(double z, int z_min, int z_max) int get_z_color(double z, int z_max)
{ {
int red; int red;
int green; int green;
@ -19,11 +19,11 @@ int get_z_color(double z, int z_min, int z_max)
red = 0; red = 0;
green = 0; green = 0;
blue = 0; blue = 0;
if (z < 0 || z_max == z_min) if (z < 0)
blue = 200; blue = 127;
else else
{ {
red = 255 * (z - z_min) / (z_max - z_min); red = 255 * z / z_max;
green = 255 - red; green = 255 - red;
} }
return (red << 24 | green << 16 | blue << 8 | 0x00FF); return (red << 24 | green << 16 | blue << 8 | 0x00FF);

View File

@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: 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" #include "fdf.h"
void prepare_draw(t_fdf *fdf) void prepare_draw(t_fdf *fdf)

View File

@ -12,10 +12,11 @@
#include "fdf.h" #include "fdf.h"
void put_pixel(t_fdf *fdf, t_point_2d point) bool put_pixel(t_fdf *fdf, t_point_2d point)
{ {
if (point.x < 0 || point.x >= (int) fdf->img->width if (point.x < 0 || point.x >= (int) fdf->img->width
|| point.y < 0 || point.y >= (int) fdf->img->height) || point.y < 0 || point.y >= (int) fdf->img->height)
return ; return (false);
mlx_put_pixel(fdf->img, point.x, point.y, point.color); mlx_put_pixel(fdf->img, point.x, point.y, point.color);
return (true);
} }

View File

@ -21,7 +21,8 @@ int main(int argc, char *argv[])
handle_error(fdf, "Usage: ./fdf <filename>"); handle_error(fdf, "Usage: ./fdf <filename>");
if (!check_filename(argv[1])) if (!check_filename(argv[1]))
handle_error(fdf, "Error: wrong file extension"); handle_error(fdf, "Error: wrong file extension");
parse_map(argv[1], fdf); if (!parse_map(argv[1], fdf))
handle_error(fdf, "Error: failed to parse map");
init_mlx(fdf); init_mlx(fdf);
fdf->menu = draw_menu(fdf); fdf->menu = draw_menu(fdf);
fdf->menu->enabled = false; fdf->menu->enabled = false;

View File

@ -13,10 +13,11 @@
#include "fdf.h" #include "fdf.h"
#include "MLX42.h" #include "MLX42.h"
void fdf_hooks(t_fdf *fdf) int fdf_hooks(t_fdf *fdf)
{ {
mlx_loop_hook(fdf->mlx, draw_hook, fdf); mlx_loop_hook(fdf->mlx, draw_hook, fdf);
mlx_key_hook(fdf->mlx, key_hook, fdf); mlx_key_hook(fdf->mlx, key_hook, fdf);
mlx_close_hook(fdf->mlx, close_hook, fdf); mlx_close_hook(fdf->mlx, close_hook, fdf);
mlx_resize_hook(fdf->mlx, resize_hook, fdf); mlx_resize_hook(fdf->mlx, resize_hook, fdf);
return (1);
} }

View File

@ -12,16 +12,14 @@
#include "fdf.h" #include "fdf.h"
int get_map_sizes(char *filename, t_fdf *fdf) int get_map_sizes(char *filename, t_map *map)
{ {
const int fd = open(filename, O_RDONLY); const int fd = open(filename, O_RDONLY);
char *line; char *line;
int width; int width;
t_map *map;
map = fdf->map;
if (fd < 0) if (fd < 0)
handle_error(fdf, FILE_ERROR); return (ft_printf(FILE_ERROR, filename), 0);
width = 0; width = 0;
while (true) while (true)
{ {
@ -31,7 +29,7 @@ int get_map_sizes(char *filename, t_fdf *fdf)
width = ft_count_words(line, ' '); width = ft_count_words(line, ' ');
free(line); free(line);
if (map->width != 0 && map->width != width) if (map->width != 0 && map->width != width)
handle_error(fdf, WRONG_LINE_LENGTH); return (ft_printf(WRONG_LINE_LENGTH), 0);
else if (map->width == 0) else if (map->width == 0)
map->width = width; map->width = width;
map->height++; map->height++;

View File

@ -1,7 +1,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: o_ :::::: ::: */ /* ::: o_ :::::: ::: */
/* get_z_min_max.c :+: / :+::+: :+: */ /* get_z_max.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */ /* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */ /* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \o/ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */
@ -11,21 +11,17 @@
/* ************************************************************************** */ /* ************************************************************************** */
#include "fdf.h" #include "fdf.h"
#include <limits.h>
void get_z_min_max(t_fdf *fdf) void get_z_max(t_fdf *fdf)
{ {
int i; int i;
i = 0; i = 0;
fdf->map->z_max = INT_MIN; fdf->map->z_max = 0;
fdf->map->z_min = INT_MAX;
while (i < fdf->map->width * fdf->map->height) while (i < fdf->map->width * fdf->map->height)
{ {
if (fdf->map->orig[i].z > fdf->map->z_max) if (fdf->map->orig[i].z > fdf->map->z_max)
fdf->map->z_max = fdf->map->orig[i].z; fdf->map->z_max = fdf->map->orig[i].z;
if (fdf->map->orig[i].z < fdf->map->z_min)
fdf->map->z_min = fdf->map->orig[i].z;
i++; i++;
} }
} }

View File

@ -12,11 +12,11 @@
#include "fdf.h" #include "fdf.h"
void parse_map(char *filename, t_fdf *fdf) int parse_map(char *filename, t_fdf *fdf)
{ {
int map_size; int map_size;
if (!get_map_sizes(filename, fdf)) if (!get_map_sizes(filename, fdf->map))
handle_error(fdf, "Error: failed to get map sizes"); handle_error(fdf, "Error: failed to get map sizes");
map_size = fdf->map->width * fdf->map->height; map_size = fdf->map->width * fdf->map->height;
fdf->map->orig = malloc(map_size * sizeof(t_point_3d)); fdf->map->orig = malloc(map_size * sizeof(t_point_3d));
@ -26,5 +26,6 @@ void parse_map(char *filename, t_fdf *fdf)
handle_error(fdf, MALLOC_ERROR); handle_error(fdf, MALLOC_ERROR);
if (!load_map_from_file(filename, fdf)) if (!load_map_from_file(filename, fdf))
handle_error(fdf, "Error: failed to read map"); handle_error(fdf, "Error: failed to read map");
get_z_min_max(fdf); get_z_max(fdf);
return (SUCCESS);
} }

View File

@ -14,8 +14,8 @@
void set_map_point(t_fdf *fdf, int x, int y, char **str) 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.0 + 1; 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.0 - y; fdf->map->orig[y * fdf->map->width + x].y = fdf->map->height / 2 - y;
fdf->map->orig[y * fdf->map->width + x].z = ft_atoi(str[x]); 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]); fdf->map->orig[y * fdf->map->width + x].color = parse_color_string(str[x]);
} }

View File

@ -12,18 +12,15 @@
#include "fdf.h" #include "fdf.h"
void clean_fdf(t_fdf *fdf) bool clean_fdf(t_fdf *fdf)
{ {
if (fdf && fdf->map) if (fdf->map)
{ {
if (fdf->map->orig)
free(fdf->map->orig); free(fdf->map->orig);
if (fdf->map->rot)
free(fdf->map->rot); free(fdf->map->rot);
if (fdf->map->proj)
free(fdf->map->proj); free(fdf->map->proj);
free(fdf->map); free(fdf->map);
} }
if (fdf)
free(fdf); free(fdf);
return (true);
} }

View File

@ -16,16 +16,9 @@ void handle_error(t_fdf *fdf, char *error)
{ {
if (errno) if (errno)
perror(error); perror(error);
else
ft_putendl_fd(error, 2);
if (mlx_errno) if (mlx_errno)
ft_putendl_fd(mlx_strerror(mlx_errno), 2); ft_putendl_fd(mlx_strerror(mlx_errno), 2);
if (fdf && fdf->img) ft_putendl_fd(error, 2);
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); clean_fdf(fdf);
exit(1); exit(1);
} }

View File

@ -12,7 +12,7 @@
#include "fdf.h" #include "fdf.h"
void init_mlx(t_fdf *fdf) int init_mlx(t_fdf *fdf)
{ {
mlx_t *mlx; mlx_t *mlx;
@ -24,4 +24,5 @@ void init_mlx(t_fdf *fdf)
if (!fdf->img || (mlx_image_to_window(fdf->mlx, fdf->img, 0, 0) < 0)) if (!fdf->img || (mlx_image_to_window(fdf->mlx, fdf->img, 0, 0) < 0))
handle_error(fdf, "Error: failed to create image"); handle_error(fdf, "Error: failed to create image");
fdf_hooks(fdf); fdf_hooks(fdf);
return (SUCCESS);
} }

View File

@ -21,9 +21,9 @@ t_fdf *initialise_fdf(void)
if (!fdf) if (!fdf)
handle_error(fdf, MALLOC_ERROR); handle_error(fdf, MALLOC_ERROR);
map = malloc(sizeof(t_map)); map = malloc(sizeof(t_map));
fdf->map = map;
if (!map) if (!map)
handle_error(fdf, MALLOC_ERROR); handle_error(fdf, MALLOC_ERROR);
fdf->map = map;
fdf->mlx = NULL; fdf->mlx = NULL;
fdf->img = NULL; fdf->img = NULL;
fdf->last_width = WIDTH; fdf->last_width = WIDTH;
@ -34,7 +34,6 @@ t_fdf *initialise_fdf(void)
fdf->map->width = 0; fdf->map->width = 0;
fdf->map->height = 0; fdf->map->height = 0;
fdf->map->z_max = 0; fdf->map->z_max = 0;
fdf->map->z_min = 0;
reset_fdf(fdf); reset_fdf(fdf);
return (fdf); return (fdf);
} }