diff --git a/Makefile b/Makefile index b9e74fb..a47d100 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ MLX42 = $(MLX42_PATH)/build/libmlx42.a OBJ_PATH = obj -VPATH = src +VPATH = src:src/parse:src/draw:src/hooks:src/util:src/transform SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) diff --git a/inc/fdf.h b/inc/fdf.h index c6743ef..56b37e4 100644 --- a/inc/fdf.h +++ b/inc/fdf.h @@ -18,10 +18,10 @@ # include # include # include -# include # include # include "libft.h" # include "MLX42.h" +# include # define SUCCESS 1 # define FAILURE 0 @@ -101,64 +101,48 @@ typedef struct s_fdf t_projection projection; } t_fdf; -int interpolate_color(t_point_2d start, - t_point_2d end, t_point_2d current); -int parse_color_string(char *str); -int get_map_sizes(char *filename, t_map *map); 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 parse_map(char *filename, t_fdf *fdf); -void copy_map(t_fdf *fdf); -void fdf_apply_rotation(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); +int 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); -bool fdf_put_pixel(t_fdf *fdf, t_point_2d point); -void fdf_draw_line(t_fdf *fdf, t_point_2d start, - t_point_2d end); -int 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); -void fdf_set_background(mlx_image_t *img, int color); 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); -void get_z_max(t_fdf *fdf); int init_mlx(t_fdf *fdf); -void set_map_point(t_fdf *fdf, int x, int y, char **str); -mlx_image_t *draw_menu(t_fdf *fdf); -int get_z_color(double z, int z_max); 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 - -// FdF by whaffman -// ============================ - -// [?] Help menu -// [a] / [d] Rotate around Z-axis -// [w] / [s] Rotate around X-axis -// [q] / [e] Rotate around Y-axis -// [z] / [x] Change Z-scale -// [=] / [-] Change Zoom -// [UP] / [DOWN] Change X-offset -// [LEFT] / [RIGHT] Change Y-offset -// [[] / []] Change animation speed -// [c] Change colormode -// [p] Change projection mode -// [ESC] Close window - -// ============================ diff --git a/menu.png b/menu.png index bac62f4..aaa512d 100644 Binary files a/menu.png and b/menu.png differ diff --git a/scratch.txt b/scratch.txt new file mode 100644 index 0000000..8ab3585 --- /dev/null +++ b/scratch.txt @@ -0,0 +1,33 @@ + +// FdF by whaffman +// ============================ + +// [?] Help menu +// [a] / [d] Rotate around Z-axis +// [w] / [s] Rotate around X-axis +// [q] / [e] Rotate around Y-axis +// [z] / [x] Change Z-scale +// [=] / [-] Change Zoom +// [UP] / [DOWN] Change X-offset +// [LEFT] / [RIGHT] Change Y-offset +// [[] / []] Change animation speed +// [c] Change colormode +// [p] Change projection mode +// [ESC] Close window + +// ============================ + // const int x = 20; + // const int line_height = 25; + // int y; + + // y = 1; + // mlx_put_string(fdf->mlx, "[?] Help menu", x, y++ * line_height); + // mlx_put_string(fdf->mlx, "[a] / [d] Rotate around Z-axis", x, y++ * line_height); + // mlx_put_string(fdf->mlx, "[w] / [s] Rotate around X-axis", x, y++ * line_height); + // mlx_put_string(fdf->mlx, "[q] / [e] Rotate around Y-axis", x, y++ * line_height); + // mlx_put_string(fdf->mlx, "[z] / [x] Change Z-scale", x, y++ * line_height); + // mlx_put_string(fdf->mlx, "[=] / [-] Change Zoom", x, y++ * line_height); + // mlx_put_string(fdf->mlx, "[UP] / [DOWN] Change X-offset", x, y++ * line_height); + // mlx_put_string(fdf->mlx, "[LEFT] / [RIGHT] Change Y-offset", x, y++ * line_height); + // mlx_put_string(fdf->mlx, "[[] / []] Change animation speed", x, y++ * line_height); + // mlx_put_string(fdf->mlx, "[ESC] Close window", x, y++ * line_height); diff --git a/src/fdf_draw.c b/src/draw/draw_line.c similarity index 72% rename from src/fdf_draw.c rename to src/draw/draw_line.c index 30c2882..a45fbc7 100644 --- a/src/fdf_draw.c +++ b/src/draw/draw_line.c @@ -1,26 +1,17 @@ /* ************************************************************************** */ /* */ /* ::: o_ :::::: ::: */ -/* fdf_draw.c :+: / :+::+: :+: */ +/* draw_line.c :+: / :+::+: :+: */ /* +:+ > +:++:+ +:+ */ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/12/08 11:45:12 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:12 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/12/20 11:21:15 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ #include "fdf.h" -bool fdf_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); - mlx_put_pixel(fdf->img, point.x, point.y, point.color); - return (true); -} - -void fdf_draw_line_low(t_fdf *fdf, t_point_2d start, t_point_2d end) +void draw_line_low(t_fdf *fdf, t_point_2d start, t_point_2d end) { int delta; int yi; @@ -37,7 +28,7 @@ void fdf_draw_line_low(t_fdf *fdf, t_point_2d start, t_point_2d end) while (current.x <= end.x) { current.color = interpolate_color(start, end, current); - fdf_put_pixel(fdf, current); + put_pixel(fdf, current); if (delta > 0) { current.y += yi; @@ -48,7 +39,7 @@ void fdf_draw_line_low(t_fdf *fdf, t_point_2d start, t_point_2d end) } } -void fdf_draw_line_high(t_fdf *fdf, t_point_2d start, t_point_2d end) +void draw_line_high(t_fdf *fdf, t_point_2d start, t_point_2d end) { int delta; int xi; @@ -65,7 +56,7 @@ void fdf_draw_line_high(t_fdf *fdf, t_point_2d start, t_point_2d end) while (current.y <= end.y) { current.color = interpolate_color(start, end, current); - fdf_put_pixel(fdf, current); + put_pixel(fdf, current); if (delta > 0) { current.x += xi; @@ -76,7 +67,7 @@ void fdf_draw_line_high(t_fdf *fdf, t_point_2d start, t_point_2d end) } } -void fdf_draw_line(t_fdf *fdf, t_point_2d start, t_point_2d end) +void draw_line(t_fdf *fdf, t_point_2d start, t_point_2d end) { if ((start.x < 0 || start.x >= (int) fdf->img->width || start.y < 0 || start.y >= (int) fdf->img->height) @@ -86,15 +77,15 @@ void fdf_draw_line(t_fdf *fdf, t_point_2d start, t_point_2d end) if (abs(end.y - start.y) < abs(end.x - start.x)) { if (start.x > end.x) - fdf_draw_line_low(fdf, end, start); + draw_line_low(fdf, end, start); else - fdf_draw_line_low(fdf, start, end); + draw_line_low(fdf, start, end); } else { if (start.y > end.y) - fdf_draw_line_high(fdf, end, start); + draw_line_high(fdf, end, start); else - fdf_draw_line_high(fdf, start, end); + draw_line_high(fdf, start, end); } } diff --git a/src/draw/draw_menu.c b/src/draw/draw_menu.c new file mode 100644 index 0000000..3486c2a --- /dev/null +++ b/src/draw/draw_menu.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* draw_menu.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:20 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:20 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +mlx_image_t *draw_menu(t_fdf *fdf) +{ + mlx_texture_t *texture; + mlx_image_t *img; + + texture = mlx_load_png("menu.png"); + img = mlx_texture_to_image(fdf->mlx, texture); + mlx_delete_texture(texture); + mlx_image_to_window(fdf->mlx, img, 10, 10); + return (img); +} diff --git a/src/draw/get_gradient_color.c b/src/draw/get_gradient_color.c new file mode 100644 index 0000000..51cf4de --- /dev/null +++ b/src/draw/get_gradient_color.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* get_gradient_color.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:22 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:22 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +int get_gradient_color(double z, 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)]); +} diff --git a/src/draw/get_pixel_color.c b/src/draw/get_pixel_color.c new file mode 100644 index 0000000..4a147aa --- /dev/null +++ b/src/draw/get_pixel_color.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* get_pixel_color.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:23 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:23 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +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); + else if (fdf->colormode == COLOR_MODE_GRADIENT) + point->color = get_gradient_color(point->orig_z, fdf->map->z_max); +} diff --git a/src/draw/get_z_color.c b/src/draw/get_z_color.c new file mode 100644 index 0000000..d1a1e66 --- /dev/null +++ b/src/draw/get_z_color.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* get_z_color.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:24 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:24 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +int get_z_color(double z, int z_max) +{ + int red; + int green; + int blue; + + red = 0; + green = 0; + blue = 0; + if (z < 0) + blue = 127; + else + { + red = 255 * z / z_max; + green = 255 - red; + } + return (red << 24 | green << 16 | blue << 8 | 0x00FF); +} diff --git a/src/draw/interpolate_color.c b/src/draw/interpolate_color.c new file mode 100644 index 0000000..4e04a23 --- /dev/null +++ b/src/draw/interpolate_color.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* interpolate_color.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:25 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:25 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +int interpolate_color(t_point_2d start, t_point_2d end, t_point_2d current) +{ + int dcur; + int dtotal; + int red; + int green; + int blue; + + dcur = fmax(abs(current.x - start.x), abs(current.y - start.y)); + dtotal = fmax(abs(end.x - start.x), abs(end.y - start.y)); + if (dtotal == 0) + return (start.color); + red = (int)((1 - (double) dcur / dtotal) * (start.color >> 24 & 0xFF) + + (double) dcur / dtotal * (end.color >> 24 & 0xFF)); + green = (int)((1 - (double) dcur / dtotal) * (start.color >> 16 & 0xFF) + + (double) dcur / dtotal * (end.color >> 16 & 0xFF)); + blue = (int)((1 - (double) dcur / dtotal) * (start.color >> 8 & 0xFF) + + (double) dcur / dtotal * (end.color >> 8 & 0xFF)); + return (red << 24 | green << 16 | blue << 8 | 0xFF); +} diff --git a/src/draw/parse_color_string.c b/src/draw/parse_color_string.c new file mode 100644 index 0000000..92e017b --- /dev/null +++ b/src/draw/parse_color_string.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* parse_color_string.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/13 15:23:11 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:25 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +int parse_color_string(char *str) +{ + int color; + int i; + char *color_str; + + color = 0; + i = 1; + color_str = ft_strchr(str, ','); + if (!color_str || *++color_str != '0' || *++color_str != 'x') + return (0xFFFFFFFF); + while (color_str[i]) + { + if (ft_isdigit(color_str[i])) + color = color * 16 + color_str[i] - '0'; + else if (ft_tolower(color_str[i]) >= 'a' + && ft_tolower(color_str[i]) <= 'f') + color = color * 16 + ft_tolower(color_str[i]) - 'a' + 10; + else + break ; + i++; + } + return ((color << 8) + 0xFF); +} diff --git a/src/draw/prepare_draw.c b/src/draw/prepare_draw.c new file mode 100644 index 0000000..4fbd486 --- /dev/null +++ b/src/draw/prepare_draw.c @@ -0,0 +1,10 @@ +#include "fdf.h" + +void prepare_draw(t_fdf *fdf) +{ + fdf_set_background(fdf->img, 0x000000FF); + if (fdf->animate_z) + fdf->angle_z += deg2rad(fdf->animate_z); + apply_rotation(fdf); + project(fdf); +} diff --git a/src/draw/put_pixel.c b/src/draw/put_pixel.c new file mode 100644 index 0000000..ed28028 --- /dev/null +++ b/src/draw/put_pixel.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* put_pixel.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:26 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:26 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +bool 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); + mlx_put_pixel(fdf->img, point.x, point.y, point.color); + return (true); +} diff --git a/src/set_background.c b/src/draw/set_background.c similarity index 94% rename from src/set_background.c rename to src/draw/set_background.c index 371b5d0..ca7b0ad 100644 --- a/src/set_background.c +++ b/src/draw/set_background.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/12/13 15:23:20 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:20 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/12/20 11:21:27 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/src/fdf.c b/src/fdf.c index dacff2b..a01e304 100644 --- a/src/fdf.c +++ b/src/fdf.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/12/06 11:07:30 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:10 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/12/20 11:21:28 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ @@ -17,9 +17,9 @@ int main(int argc, char *argv[]) t_fdf *fdf; fdf = initialise_fdf(); - if (argc != 2 ) + if (argc != 2) handle_error(fdf, "Usage: ./fdf "); - if(!check_filename(argv[1])) + 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"); diff --git a/src/fdf_color.c b/src/fdf_color.c deleted file mode 100644 index f0e0727..0000000 --- a/src/fdf_color.c +++ /dev/null @@ -1,99 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: o_ :::::: ::: */ -/* fdf_color.c :+: / :+::+: :+: */ -/* +:+ > +:++:+ +:+ */ -/* By: whaffman +#+ +:+ +#++#++:++#++ */ -/* +#+ +#+#+ +#++#+ +#+ \o/ */ -/* Created: 2024/12/13 15:23:11 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:11 by whaffman ### ### ### ### / \ */ -/* */ -/* ************************************************************************** */ - -#include "fdf.h" - -int parse_color_string(char *str) -{ - int color; - int i; - char *color_str; - - color = 0; - i = 1; - color_str = ft_strchr(str, ','); - if (!color_str || *++color_str != '0' || *++color_str != 'x') - return (0xFFFFFFFF); - while (color_str[i]) - { - if (ft_isdigit(color_str[i])) - color = color * 16 + color_str[i] - '0'; - else if (ft_tolower(color_str[i]) >= 'a' - && ft_tolower(color_str[i]) <= 'f') - color = color * 16 + ft_tolower(color_str[i]) - 'a' + 10; - else - break ; - i++; - } - return ((color << 8) + 0xFF); -} - -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); - else if (fdf->colormode == COLOR_MODE_GRADIENT) - point->color = get_gradient_color(point->orig_z, fdf->map->z_max); - -} - -int interpolate_color(t_point_2d start, t_point_2d end, t_point_2d current) -{ - int dcur; - int dtotal; - int red; - int green; - int blue; - - dcur = fmax(abs(current.x - start.x), abs(current.y - start.y)); - dtotal = fmax(abs(end.x - start.x), abs(end.y - start.y)); - if (dtotal == 0) - return (start.color); - red = (int)((1 - (double) dcur / dtotal) * (start.color >> 24 & 0xFF) - + (double) dcur / dtotal * (end.color >> 24 & 0xFF)); - green = (int)((1 - (double) dcur / dtotal) * (start.color >> 16 & 0xFF) - + (double) dcur / dtotal * (end.color >> 16 & 0xFF)); - blue = (int)((1 - (double) dcur / dtotal) * (start.color >> 8 & 0xFF) - + (double) dcur / dtotal * (end.color >> 8 & 0xFF)); - return (red << 24 | green << 16 | blue << 8 | 0xFF); -} - -int get_z_color(double z, int z_max) -{ - int red; - int green; - int blue; - - red = 0; - green = 0; - blue = 0; - if(z < 0) - blue = 127; - else - { - red = 255 * z / z_max; - green = 255 - red; - } - return (red << 24 | green << 16 | blue << 8 | 0x00FF); -} - -int get_gradient_color(double z, 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)]); -} - diff --git a/src/fdf_hooks.c b/src/fdf_hooks.c deleted file mode 100644 index 8c76e3f..0000000 --- a/src/fdf_hooks.c +++ /dev/null @@ -1,163 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: o_ :::::: ::: */ -/* fdf_hooks.c :+: / :+::+: :+: */ -/* +:+ > +:++:+ +:+ */ -/* By: whaffman +#+ +:+ +#++#++:++#++ */ -/* +#+ +#+#+ +#++#+ +#+ \o/ */ -/* Created: 2024/12/13 15:23:12 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:12 by whaffman ### ### ### ### / \ */ -/* */ -/* ************************************************************************** */ - -#include "fdf.h" -#include "MLX42.h" - -int 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); -} - -void resize_hook(int width, int height, void *param) -{ - t_fdf *fdf; - - (void)width; - (void)height; - fdf = (t_fdf *)param; - mlx_delete_image(fdf->mlx, fdf->img); - fdf->img = mlx_new_image(fdf->mlx, fdf->mlx->width, fdf->mlx->height); - if (!fdf->img - || (mlx_image_to_window(fdf->mlx, fdf->img, 0, 0) < 0) - || (mlx_image_to_window(fdf->mlx, fdf->menu, 10, 10) < 0)) - exit(1); - fdf->offset_x += (fdf->mlx->width - fdf->last_width) / 2 ; - fdf->offset_y += (fdf->mlx->height - fdf->last_height) / 2; - fdf->zoom = 0; - fdf->last_height = fdf->mlx->height; - fdf->last_width = fdf->mlx->width; -} - -void draw_hook(void *param) -{ - t_fdf *fdf; - int i; - - fdf = (t_fdf *)param; - if (fdf->last_width != fdf->mlx->width - || fdf->last_height != fdf->mlx->height) - { - resize_hook(fdf->mlx->width, fdf->mlx->height, fdf); - return ; - } - fdf_set_background(fdf->img, 0x000000FF); - if (fdf->animate_z) - fdf->angle_z += deg2rad(fdf->animate_z); - fdf_apply_rotation(fdf); - project(fdf); - i = 0; - while (i < fdf->map->width * fdf->map->height) - { - fdf_put_pixel(fdf, fdf->map->proj[i]); - if (i % fdf->map->width != fdf->map->width - 1) - fdf_draw_line(fdf, fdf->map->proj[i], fdf->map->proj[i + 1]); - if (i / fdf->map->width != fdf->map->height - 1) - fdf_draw_line(fdf, fdf->map->proj[i], fdf->map->proj[i + fdf->map->width]); - i++; - } - // draw_menu(fdf); -} -mlx_image_t *draw_menu(t_fdf *fdf) -{ - // const int x = 20; - // const int line_height = 25; - // int y; - mlx_texture_t *texture; - mlx_image_t *img; - - // y = 1; - - texture = mlx_load_png("menu.png"); - img = mlx_texture_to_image(fdf->mlx, texture); - mlx_delete_texture(texture); - mlx_image_to_window(fdf->mlx, img, 10, 10); - // mlx_put_string(fdf->mlx, "[?] Help menu", x, y++ * line_height); - // mlx_put_string(fdf->mlx, "[a] / [d] Rotate around Z-axis", x, y++ * line_height); - // mlx_put_string(fdf->mlx, "[w] / [s] Rotate around X-axis", x, y++ * line_height); - // mlx_put_string(fdf->mlx, "[q] / [e] Rotate around Y-axis", x, y++ * line_height); - // mlx_put_string(fdf->mlx, "[z] / [x] Change Z-scale", x, y++ * line_height); - // mlx_put_string(fdf->mlx, "[=] / [-] Change Zoom", x, y++ * line_height); - // mlx_put_string(fdf->mlx, "[UP] / [DOWN] Change X-offset", x, y++ * line_height); - // mlx_put_string(fdf->mlx, "[LEFT] / [RIGHT] Change Y-offset", x, y++ * line_height); - // mlx_put_string(fdf->mlx, "[[] / []] Change animation speed", x, y++ * line_height); - // mlx_put_string(fdf->mlx, "[ESC] Close window", x, y++ * line_height); - - return (img); -} - -void close_hook(void *param) -{ - t_fdf *fdf; - - fdf = (t_fdf *)param; - mlx_close_window(fdf->mlx); - mlx_delete_image(fdf->mlx, fdf->img); - mlx_delete_image(fdf->mlx, fdf->menu); - mlx_terminate(fdf->mlx); - clean_fdf(fdf); - exit(0); -} - -void key_hook(mlx_key_data_t keydata, void *param) -{ - t_fdf *fdf; - - fdf = (t_fdf *)param; - if (keydata.key == MLX_KEY_ESCAPE) - close_hook(fdf); - if (keydata.key == MLX_KEY_A && keydata.action != MLX_RELEASE) - fdf->angle_z -= deg2rad(5); - if (keydata.key == MLX_KEY_D && keydata.action != MLX_RELEASE) - fdf->angle_z += deg2rad(5); - if (keydata.key == MLX_KEY_W && keydata.action != MLX_RELEASE) - fdf->angle_x -= deg2rad(5); - if (keydata.key == MLX_KEY_S && keydata.action != MLX_RELEASE) - fdf->angle_x += deg2rad(5); - if (keydata.key == MLX_KEY_Q && keydata.action != MLX_RELEASE) - fdf->angle_y -= deg2rad(5); - if (keydata.key == MLX_KEY_E && keydata.action != MLX_RELEASE) - fdf->angle_y += deg2rad(5); - if (keydata.key == MLX_KEY_X && keydata.action != MLX_RELEASE) - fdf->z_scale *= 1.05; - if (keydata.key == MLX_KEY_Z && keydata.action != MLX_RELEASE) - fdf->z_scale /= 1.05; - if (keydata.key == MLX_KEY_UP && keydata.action != MLX_RELEASE) - fdf->offset_y -= 10; - if (keydata.key == MLX_KEY_DOWN && keydata.action != MLX_RELEASE) - fdf->offset_y += 10; - if (keydata.key == MLX_KEY_LEFT && keydata.action != MLX_RELEASE) - fdf->offset_x -= 10; - if (keydata.key == MLX_KEY_RIGHT && keydata.action != MLX_RELEASE) - fdf->offset_x += 10; - if (keydata.key == MLX_KEY_EQUAL && keydata.action != MLX_RELEASE) - fdf->zoom *= 1.1; - if (keydata.key == MLX_KEY_MINUS && keydata.action != MLX_RELEASE) - fdf->zoom /= 1.1; - if (keydata.key == MLX_KEY_LEFT_BRACKET && keydata.action == MLX_PRESS) - fdf->animate_z -= 0.5; - if (keydata.key == MLX_KEY_RIGHT_BRACKET && keydata.action == MLX_PRESS) - fdf->animate_z += 0.5; - if (keydata.key == MLX_KEY_SLASH && keydata.action == MLX_PRESS) - fdf->menu->enabled = !fdf->menu->enabled; - if (keydata.key == MLX_KEY_C && keydata.action == MLX_PRESS) - fdf->colormode = (fdf->colormode + 1) % 3; - if (keydata.key == MLX_KEY_P && keydata.action == MLX_PRESS) - fdf->projection = (fdf->projection + 1) % 3; - if (keydata.key == MLX_KEY_SPACE && keydata.action == MLX_PRESS) - reset_fdf(fdf); - -} diff --git a/src/fdf_rotations.c b/src/fdf_rotations.c deleted file mode 100644 index 683df8b..0000000 --- a/src/fdf_rotations.c +++ /dev/null @@ -1,83 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: o_ :::::: ::: */ -/* fdf_rotations.c :+: / :+::+: :+: */ -/* +:+ > +:++:+ +:+ */ -/* By: whaffman +#+ +:+ +#++#++:++#++ */ -/* +#+ +#+#+ +#++#+ +#+ \o/ */ -/* Created: 2024/12/13 15:23:13 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:13 by whaffman ### ### ### ### / \ */ -/* */ -/* ************************************************************************** */ - -#include "fdf.h" - -void fdf_apply_rotation(t_fdf *fdf) -{ - copy_map(fdf); - rotate_x(&fdf->map->rot, fdf->angle_x, fdf->map->width * fdf->map->height); - rotate_y(&fdf->map->rot, fdf->angle_y, fdf->map->width * fdf->map->height); - rotate_z(&fdf->map->rot, fdf->angle_z, fdf->map->width * fdf->map->height); -} - -void copy_map(t_fdf *fdf) -{ - int i; - - i = 0; - while (i < fdf->map->width * fdf->map->height) - { - fdf->map->rot[i].x = fdf->map->orig[i].x; - fdf->map->rot[i].y = fdf->map->orig[i].y; - fdf->map->rot[i].z = fdf->map->orig[i].z * fdf->z_scale; - fdf->map->rot[i].color = fdf->map->orig[i].color; - i++; - } -} - -void rotate_x(t_point_3d **points, double angle, int size) -{ - int i; - double previous_y; - - i = 0; - while (i < size) - { - previous_y = (*points)[i].y; - (*points)[i].y = previous_y * cos(angle) + (*points)[i].z * sin(angle); - (*points)[i].z = -previous_y * sin(angle) + (*points)[i].z * cos(angle); - i++; - } -} - -void rotate_y(t_point_3d **points, double angle, int size) -{ - int i; - double previous_x; - - i = 0; - while (i < size) - { - previous_x = (*points)[i].x; - (*points)[i].x = previous_x * cos(angle) + (*points)[i].z * sin(angle); - (*points)[i].z = -previous_x * sin(angle) + (*points)[i].z * cos(angle); - i++; - } -} - -void rotate_z(t_point_3d **points, double angle, int size) -{ - int i; - double previous_x; - double previous_y; - - i = 0; - while (i < size) - { - previous_x = (*points)[i].x; - previous_y = (*points)[i].y; - (*points)[i].x = previous_x * cos(angle) - previous_y * sin(angle); - (*points)[i].y = previous_x * sin(angle) + previous_y * cos(angle); - i++; - } -} diff --git a/src/hooks/close_hook.c b/src/hooks/close_hook.c new file mode 100644 index 0000000..e6054dc --- /dev/null +++ b/src/hooks/close_hook.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* close_hook.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:28 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:28 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +void close_hook(void *param) +{ + t_fdf *fdf; + + fdf = (t_fdf *)param; + mlx_close_window(fdf->mlx); + mlx_delete_image(fdf->mlx, fdf->img); + mlx_delete_image(fdf->mlx, fdf->menu); + mlx_terminate(fdf->mlx); + clean_fdf(fdf); + exit(0); +} diff --git a/src/hooks/draw_hook.c b/src/hooks/draw_hook.c new file mode 100644 index 0000000..5b9ac8a --- /dev/null +++ b/src/hooks/draw_hook.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* draw_hook.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:29 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:29 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +void draw_hook(void *param) +{ + t_fdf *fdf; + int i; + + fdf = (t_fdf *)param; + if (fdf->last_width != fdf->mlx->width + || fdf->last_height != fdf->mlx->height) + { + resize_hook(fdf->mlx->width, fdf->mlx->height, fdf); + return ; + } + prepare_draw(fdf); + i = 0; + while (i < fdf->map->width * fdf->map->height) + { + put_pixel(fdf, fdf->map->proj[i]); + if (i % fdf->map->width != fdf->map->width - 1) + draw_line(fdf, fdf->map->proj[i], fdf->map->proj[i + 1]); + if (i / fdf->map->width != fdf->map->height - 1) + draw_line(fdf, fdf->map->proj[i], + fdf->map->proj[i + fdf->map->width]); + i++; + } +} diff --git a/src/hooks/fdf_hooks.c b/src/hooks/fdf_hooks.c new file mode 100644 index 0000000..13da142 --- /dev/null +++ b/src/hooks/fdf_hooks.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* fdf_hooks.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/13 15:23:12 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:30 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" +#include "MLX42.h" + +int 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); +} diff --git a/src/hooks/key_hook.c b/src/hooks/key_hook.c new file mode 100644 index 0000000..031a374 --- /dev/null +++ b/src/hooks/key_hook.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* key_hook.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:30 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:30 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +void key_hook(mlx_key_data_t keydata, void *param) +{ + t_fdf *fdf; + + fdf = (t_fdf *)param; + key_hook_transform(keydata, fdf); + key_hook_options(keydata, fdf); +} diff --git a/src/hooks/key_hook_options.c b/src/hooks/key_hook_options.c new file mode 100644 index 0000000..9fb02fb --- /dev/null +++ b/src/hooks/key_hook_options.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* key_hook_options.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:40:59 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:40:59 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +void key_hook_options(mlx_key_data_t keydata, t_fdf *fdf) +{ + if (keydata.key == MLX_KEY_X && keydata.action != MLX_RELEASE) + fdf->z_scale *= 1.05; + if (keydata.key == MLX_KEY_Z && keydata.action != MLX_RELEASE) + fdf->z_scale /= 1.05; + if (keydata.key == MLX_KEY_EQUAL && keydata.action != MLX_RELEASE) + fdf->zoom *= 1.1; + if (keydata.key == MLX_KEY_MINUS && keydata.action != MLX_RELEASE) + fdf->zoom /= 1.1; + if (keydata.key == MLX_KEY_SLASH && keydata.action == MLX_PRESS) + fdf->menu->enabled = !fdf->menu->enabled; + if (keydata.key == MLX_KEY_C && keydata.action == MLX_PRESS) + fdf->colormode = (fdf->colormode + 1) % 3; + if (keydata.key == MLX_KEY_P && keydata.action == MLX_PRESS) + fdf->projection = (fdf->projection + 1) % 3; + if (keydata.key == MLX_KEY_SPACE && keydata.action == MLX_PRESS) + reset_fdf(fdf); + if (keydata.key == MLX_KEY_LEFT_BRACKET && keydata.action == MLX_PRESS) + fdf->animate_z -= 0.5; + if (keydata.key == MLX_KEY_RIGHT_BRACKET && keydata.action == MLX_PRESS) + fdf->animate_z += 0.5; + if (keydata.key == MLX_KEY_ESCAPE) + close_hook(fdf); +} diff --git a/src/hooks/key_hook_transform.c b/src/hooks/key_hook_transform.c new file mode 100644 index 0000000..4da14ca --- /dev/null +++ b/src/hooks/key_hook_transform.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* key_hook_transform.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:41:05 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:41:05 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +void key_hook_transform(mlx_key_data_t keydata, t_fdf *fdf) +{ + if (keydata.key == MLX_KEY_A && keydata.action != MLX_RELEASE) + fdf->angle_z -= deg2rad(5); + if (keydata.key == MLX_KEY_D && keydata.action != MLX_RELEASE) + fdf->angle_z += deg2rad(5); + if (keydata.key == MLX_KEY_W && keydata.action != MLX_RELEASE) + fdf->angle_x -= deg2rad(5); + if (keydata.key == MLX_KEY_S && keydata.action != MLX_RELEASE) + fdf->angle_x += deg2rad(5); + if (keydata.key == MLX_KEY_Q && keydata.action != MLX_RELEASE) + fdf->angle_y -= deg2rad(5); + if (keydata.key == MLX_KEY_E && keydata.action != MLX_RELEASE) + fdf->angle_y += deg2rad(5); + if (keydata.key == MLX_KEY_UP && keydata.action != MLX_RELEASE) + fdf->offset_y -= 10; + if (keydata.key == MLX_KEY_DOWN && keydata.action != MLX_RELEASE) + fdf->offset_y += 10; + if (keydata.key == MLX_KEY_LEFT && keydata.action != MLX_RELEASE) + fdf->offset_x -= 10; + if (keydata.key == MLX_KEY_RIGHT && keydata.action != MLX_RELEASE) + fdf->offset_x += 10; +} diff --git a/src/hooks/resize_hook.c b/src/hooks/resize_hook.c new file mode 100644 index 0000000..8a5f9a4 --- /dev/null +++ b/src/hooks/resize_hook.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* resize_hook.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:31 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:31 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +void resize_hook(int width, int height, void *param) +{ + t_fdf *fdf; + + (void)width; + (void)height; + fdf = (t_fdf *)param; + mlx_delete_image(fdf->mlx, fdf->img); + fdf->img = mlx_new_image(fdf->mlx, fdf->mlx->width, fdf->mlx->height); + if (!fdf->img + || (mlx_image_to_window(fdf->mlx, fdf->img, 0, 0) < 0) + || (mlx_image_to_window(fdf->mlx, fdf->menu, 10, 10) < 0)) + exit(1); + fdf->offset_x += (fdf->mlx->width - fdf->last_width) / 2 ; + fdf->offset_y += (fdf->mlx->height - fdf->last_height) / 2; + fdf->zoom = 0; + fdf->last_height = fdf->mlx->height; + fdf->last_width = fdf->mlx->width; +} diff --git a/src/get_map_sizes.c b/src/parse/get_map_sizes.c similarity index 95% rename from src/get_map_sizes.c rename to src/parse/get_map_sizes.c index b7d70a8..f38bb95 100644 --- a/src/get_map_sizes.c +++ b/src/parse/get_map_sizes.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/12/13 15:23:14 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:14 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/12/20 11:21:32 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/src/get_z_max.c b/src/parse/get_z_max.c similarity index 94% rename from src/get_z_max.c rename to src/parse/get_z_max.c index 0143a48..d69900d 100644 --- a/src/get_z_max.c +++ b/src/parse/get_z_max.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/12/13 15:23:15 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:15 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/12/20 11:21:33 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/src/read_map_from_file.c b/src/parse/load_map_from_file.c similarity index 61% rename from src/read_map_from_file.c rename to src/parse/load_map_from_file.c index 451cd37..5208794 100644 --- a/src/read_map_from_file.c +++ b/src/parse/load_map_from_file.c @@ -1,29 +1,17 @@ /* ************************************************************************** */ /* */ /* ::: o_ :::::: ::: */ -/* load_map_from_file.c :+: / :+::+: :+: */ +/* load_map_from_file.c :+: / :+::+: :+: */ /* +:+ > +:++:+ +:+ */ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ -/* Created: 2024/12/13 15:23:19 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:19 by whaffman ### ### ### ### / \ */ +/* Created: 2024/12/20 11:21:34 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:34 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ #include "fdf.h" -int check_filename(char *filename) -{ - int i; - - i = 0; - while (filename[i]) - i++; - if (i < 5 || ft_strncmp(&filename[i - 4], ".fdf", 4)) - return (0); - return (1); -} - int load_map_from_file(char *filename, t_fdf *fdf) { int fd; @@ -51,11 +39,3 @@ int load_map_from_file(char *filename, t_fdf *fdf) close(fd); return (1); } - -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].z = ft_atoi(str[x]); - fdf->map->orig[y * fdf->map->width + x].color = parse_color_string(str[x]); -} diff --git a/src/parse_map.c b/src/parse/parse_map.c similarity index 95% rename from src/parse_map.c rename to src/parse/parse_map.c index 3602b4c..c00201a 100644 --- a/src/parse_map.c +++ b/src/parse/parse_map.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/12/13 15:23:18 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:18 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/12/20 11:21:34 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/src/parse/set_map_point.c b/src/parse/set_map_point.c new file mode 100644 index 0000000..adb0a93 --- /dev/null +++ b/src/parse/set_map_point.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* set_map_point.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:45 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:45 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +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].z = ft_atoi(str[x]); + fdf->map->orig[y * fdf->map->width + x].color = parse_color_string(str[x]); +} diff --git a/src/transform/apply_rotation.c b/src/transform/apply_rotation.c new file mode 100644 index 0000000..6e78b68 --- /dev/null +++ b/src/transform/apply_rotation.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* apply_rotation.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:46 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:46 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +void apply_rotation(t_fdf *fdf) +{ + int i; + + i = 0; + while (i < fdf->map->width * fdf->map->height) + { + fdf->map->rot[i].x = fdf->map->orig[i].x; + fdf->map->rot[i].y = fdf->map->orig[i].y; + fdf->map->rot[i].z = fdf->map->orig[i].z * fdf->z_scale; + fdf->map->rot[i].color = fdf->map->orig[i].color; + i++; + } + rotate_x(&fdf->map->rot, fdf->angle_x, fdf->map->width * fdf->map->height); + rotate_y(&fdf->map->rot, fdf->angle_y, fdf->map->width * fdf->map->height); + rotate_z(&fdf->map->rot, fdf->angle_z, fdf->map->width * fdf->map->height); +} diff --git a/src/deg2rad.c b/src/transform/deg2rad.c similarity index 93% rename from src/deg2rad.c rename to src/transform/deg2rad.c index b55debb..b56f781 100644 --- a/src/deg2rad.c +++ b/src/transform/deg2rad.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/12/13 15:23:10 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:10 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/12/20 11:21:47 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/src/transform/project.c b/src/transform/project.c new file mode 100644 index 0000000..ad775a1 --- /dev/null +++ b/src/transform/project.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* project.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:47 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:47 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +void project(t_fdf *fdf) +{ + if (fdf->projection == PROJECTION_ISOMETRIC) + project_isometric(fdf); + else if (fdf->projection == PROJECTION_PARALLEL) + project_parallel(fdf); + else if (fdf->projection == PROJECTION_TRIMETRIC) + project_trimetric(fdf); +} diff --git a/src/project_isometric.c b/src/transform/project_isometric.c similarity index 50% rename from src/project_isometric.c rename to src/transform/project_isometric.c index a1e4ef4..2f50f0b 100644 --- a/src/project_isometric.c +++ b/src/transform/project_isometric.c @@ -6,54 +6,12 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/12/13 15:23:19 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:19 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/12/20 11:21:48 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ #include "fdf.h" -void project_trimetric(t_fdf *fdf) -{ - int i; - double x; - double y; - - i = 0; - if (fdf->zoom == 0) - fdf->zoom = fmin(fdf->mlx->width / fdf->map->width, - fdf->mlx->height / fdf->map->height) / 2; - while (i < fdf->map->width * fdf->map->height) - { - x = fdf->map->rot[i].x - fdf->map->rot[i].y; - y = fdf->map->rot[i].x + fdf->map->rot[i].y - fdf->map->rot[i].z; - fdf->map->proj[i].x = x * fdf->zoom + fdf->offset_x; - fdf->map->proj[i].y = y * fdf->zoom + fdf->offset_y; - fdf->map->proj[i].orig_z = fdf->map->orig[i].z; - fdf->map->proj[i].color = fdf->map->orig[i].color; - get_pixel_color(fdf, &fdf->map->proj[i]); - i++; - } -} - -void project_parallel(t_fdf *fdf) -{ - int i; - - i = 0; - if (fdf->zoom == 0) - fdf->zoom = fmin(fdf->mlx->width / fdf->map->width, - fdf->mlx->height / fdf->map->height) / 2; - while (i < fdf->map->width * fdf->map->height) - { - fdf->map->proj[i].x = fdf->map->rot[i].x * fdf->zoom + fdf->offset_x; - fdf->map->proj[i].y = fdf->map->rot[i].y * fdf->zoom + fdf->offset_y; - fdf->map->proj[i].orig_z = fdf->map->orig[i].z; - fdf->map->proj[i].color = fdf->map->orig[i].color; - get_pixel_color(fdf, &fdf->map->proj[i]); - i++; - } -} - void project_isometric(t_fdf *fdf) { int i; @@ -77,13 +35,3 @@ void project_isometric(t_fdf *fdf) i++; } } - -void project(t_fdf *fdf) -{ - if (fdf->projection == PROJECTION_ISOMETRIC) - project_isometric(fdf); - else if (fdf->projection == PROJECTION_PARALLEL) - project_parallel(fdf); - else if (fdf->projection == PROJECTION_TRIMETRIC) - project_trimetric(fdf); -} diff --git a/src/transform/project_parallel.c b/src/transform/project_parallel.c new file mode 100644 index 0000000..94820fe --- /dev/null +++ b/src/transform/project_parallel.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* project_parallel.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:50 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:50 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +void project_parallel(t_fdf *fdf) +{ + int i; + + i = 0; + if (fdf->zoom == 0) + fdf->zoom = fmin(fdf->mlx->width / fdf->map->width, + fdf->mlx->height / fdf->map->height) / 2; + while (i < fdf->map->width * fdf->map->height) + { + fdf->map->proj[i].x = fdf->map->rot[i].x * fdf->zoom + fdf->offset_x; + fdf->map->proj[i].y = fdf->map->rot[i].y * fdf->zoom + fdf->offset_y; + fdf->map->proj[i].orig_z = fdf->map->orig[i].z; + fdf->map->proj[i].color = fdf->map->orig[i].color; + get_pixel_color(fdf, &fdf->map->proj[i]); + i++; + } +} diff --git a/src/transform/project_trimetric.c b/src/transform/project_trimetric.c new file mode 100644 index 0000000..3a2ad7a --- /dev/null +++ b/src/transform/project_trimetric.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* project_trimetric.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:51 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:51 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +void project_trimetric(t_fdf *fdf) +{ + int i; + double x; + double y; + + i = 0; + if (fdf->zoom == 0) + fdf->zoom = fmin(fdf->mlx->width / fdf->map->width, + fdf->mlx->height / fdf->map->height) / 2; + while (i < fdf->map->width * fdf->map->height) + { + x = fdf->map->rot[i].x - fdf->map->rot[i].y; + y = fdf->map->rot[i].x + fdf->map->rot[i].y - fdf->map->rot[i].z; + fdf->map->proj[i].x = x * fdf->zoom + fdf->offset_x; + fdf->map->proj[i].y = y * fdf->zoom + fdf->offset_y; + fdf->map->proj[i].orig_z = fdf->map->orig[i].z; + fdf->map->proj[i].color = fdf->map->orig[i].color; + get_pixel_color(fdf, &fdf->map->proj[i]); + i++; + } +} diff --git a/src/transform/rotate_x.c b/src/transform/rotate_x.c new file mode 100644 index 0000000..ff703d7 --- /dev/null +++ b/src/transform/rotate_x.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* rotate_x.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:52 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:52 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +void rotate_x(t_point_3d **points, double angle, int size) +{ + int i; + double previous_y; + + i = 0; + while (i < size) + { + previous_y = (*points)[i].y; + (*points)[i].y = previous_y * cos(angle) + (*points)[i].z * sin(angle); + (*points)[i].z = -previous_y * sin(angle) + (*points)[i].z * cos(angle); + i++; + } +} diff --git a/src/transform/rotate_y.c b/src/transform/rotate_y.c new file mode 100644 index 0000000..2472bd5 --- /dev/null +++ b/src/transform/rotate_y.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* rotate_y.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:53 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:53 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +void rotate_y(t_point_3d **points, double angle, int size) +{ + int i; + double previous_x; + + i = 0; + while (i < size) + { + previous_x = (*points)[i].x; + (*points)[i].x = previous_x * cos(angle) + (*points)[i].z * sin(angle); + (*points)[i].z = -previous_x * sin(angle) + (*points)[i].z * cos(angle); + i++; + } +} diff --git a/src/transform/rotate_z.c b/src/transform/rotate_z.c new file mode 100644 index 0000000..8b71e8c --- /dev/null +++ b/src/transform/rotate_z.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* rotate_z.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:55 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:55 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +void rotate_z(t_point_3d **points, double angle, int size) +{ + int i; + double previous_x; + double previous_y; + + i = 0; + while (i < size) + { + previous_x = (*points)[i].x; + previous_y = (*points)[i].y; + (*points)[i].x = previous_x * cos(angle) - previous_y * sin(angle); + (*points)[i].y = previous_x * sin(angle) + previous_y * cos(angle); + i++; + } +} diff --git a/src/util/check_filename.c b/src/util/check_filename.c new file mode 100644 index 0000000..ccf69ea --- /dev/null +++ b/src/util/check_filename.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* check_filename.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:21:56 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:21:56 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +int check_filename(char *filename) +{ + int i; + + i = 0; + while (filename[i]) + i++; + if (i < 5 || ft_strncmp(&filename[i - 4], ".fdf", 4)) + return (0); + return (1); +} diff --git a/src/clean_fdf.c b/src/util/clean_fdf.c similarity index 94% rename from src/clean_fdf.c rename to src/util/clean_fdf.c index 3920881..0acf6ba 100644 --- a/src/clean_fdf.c +++ b/src/util/clean_fdf.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/12/13 15:15:51 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:09 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/12/20 11:21:57 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/src/free_line_and_split.c b/src/util/free_line_and_split.c similarity index 94% rename from src/free_line_and_split.c rename to src/util/free_line_and_split.c index 035e1c5..e0a3fc7 100644 --- a/src/free_line_and_split.c +++ b/src/util/free_line_and_split.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/12/13 15:23:14 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:14 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/12/20 11:21:58 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/src/handle_error.c b/src/util/handle_error.c similarity index 94% rename from src/handle_error.c rename to src/util/handle_error.c index 03fb15c..2ebfa54 100644 --- a/src/handle_error.c +++ b/src/util/handle_error.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/12/13 15:23:16 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:16 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/12/20 11:21:59 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ diff --git a/src/init_mlx.c b/src/util/init_mlx.c similarity index 94% rename from src/init_mlx.c rename to src/util/init_mlx.c index f257fcc..f67cb96 100644 --- a/src/init_mlx.c +++ b/src/util/init_mlx.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/12/13 15:23:17 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:17 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/12/20 11:22:00 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ int init_mlx(t_fdf *fdf) { mlx_t *mlx; - + mlx = mlx_init(WIDTH, HEIGHT, "FdF", true); if (!mlx) handle_error(fdf, "Error: failed to initialise MLX"); diff --git a/src/initialise_fdf.c b/src/util/initialise_fdf.c similarity index 73% rename from src/initialise_fdf.c rename to src/util/initialise_fdf.c index c1729d7..dd5d0cf 100644 --- a/src/initialise_fdf.c +++ b/src/util/initialise_fdf.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ +:+ +#++#++:++#++ */ /* +#+ +#+#+ +#++#+ +#+ \o/ */ /* Created: 2024/12/13 15:23:17 by whaffman #+#+# #+#+# #+# #+# | */ -/* Updated: 2024/12/13 15:23:17 by whaffman ### ### ### ### / \ */ +/* Updated: 2024/12/20 11:22:00 by whaffman ### ### ### ### / \ */ /* */ /* ************************************************************************** */ @@ -26,7 +26,6 @@ t_fdf *initialise_fdf(void) fdf->map = map; fdf->mlx = NULL; fdf->img = NULL; - fdf->last_width = WIDTH; fdf->last_height = HEIGHT; fdf->map->orig = NULL; @@ -34,29 +33,7 @@ t_fdf *initialise_fdf(void) fdf->map->proj = NULL; fdf->map->width = 0; fdf->map->height = 0; - fdf->map->z_max = 0; - + fdf->map->z_max = 0; reset_fdf(fdf); return (fdf); } - -void reset_fdf(t_fdf *fdf) -{ - fdf->offset_x = WIDTH / 2; - fdf->offset_y = HEIGHT / 2; - if (fdf->mlx) - { - fdf->offset_x = fdf->mlx->width / 2; - fdf->offset_y = fdf->mlx->height / 2; - } - - - fdf->angle_x = 0; - fdf->angle_y = 0; - fdf->angle_z = 0; - fdf->zoom = 0; - fdf->z_scale = 0.1; - fdf->animate_z = 0; - fdf->colormode = COLOR_MODE_DEFAULT; - fdf->projection = PROJECTION_ISOMETRIC; -} diff --git a/src/util/reset_fdf.c b/src/util/reset_fdf.c new file mode 100644 index 0000000..b2d3371 --- /dev/null +++ b/src/util/reset_fdf.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: o_ :::::: ::: */ +/* reset_fdf.c :+: / :+::+: :+: */ +/* +:+ > +:++:+ +:+ */ +/* By: whaffman +#+ +:+ +#++#++:++#++ */ +/* +#+ +#+#+ +#++#+ +#+ \o/ */ +/* Created: 2024/12/20 11:22:01 by whaffman #+#+# #+#+# #+# #+# | */ +/* Updated: 2024/12/20 11:22:01 by whaffman ### ### ### ### / \ */ +/* */ +/* ************************************************************************** */ + +#include "fdf.h" + +void reset_fdf(t_fdf *fdf) +{ + fdf->offset_x = WIDTH / 2; + fdf->offset_y = HEIGHT / 2; + if (fdf->mlx) + { + fdf->offset_x = fdf->mlx->width / 2; + fdf->offset_y = fdf->mlx->height / 2; + } + fdf->angle_x = 0; + fdf->angle_y = 0; + fdf->angle_z = 0; + fdf->zoom = 0; + fdf->z_scale = 0.1; + fdf->animate_z = 0; + fdf->colormode = COLOR_MODE_DEFAULT; + fdf->projection = PROJECTION_ISOMETRIC; +}