diff --git a/.gitignore b/.gitignore index ae8d043..e2c80e3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ fdf *.a *.o +*.d diff --git a/Makefile b/Makefile index a0291d9..c6ea504 100644 --- a/Makefile +++ b/Makefile @@ -30,12 +30,13 @@ VPATH = src SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) +DEPENDS = ${OBJECTS:.o=.d} CC = cc RM = rm -rf INCLUDES = -I./$(INC_PATH) -I./$(LIBFT_INC_PATH) -I./$(MLX42_INC_PATH) -CFLAGS = -Wall -Wextra -Werror +CFLAGS = -Wall -Wextra -Werror -MMD UNAME_S := $(shell uname -s) ifeq ($(UNAME_S),Linux) @@ -44,6 +45,11 @@ endif all: $(NAME) +$(NAME): $(MLX42) $(LIBFT) $(OBJECTS) + $(CC) $(CFLAGS) $(OBJECTS) $(LDLIBS) -o $(NAME) + +-include ${DEPENDS} + $(MLX42): $(MLX42_PATH) cmake $(MLX42_PATH) -B $(MLX42_PATH)/build; make -C$(MLX42_PATH)/build -j4; @@ -57,9 +63,6 @@ $(LIBFT): $(LIBFT_PATH) $(LIBFT_PATH): git submodule add git@duinvoetje.nl:willem/libft.git $(LIBFT_PATH) -$(NAME): $(MLX42) $(LIBFT) $(OBJECTS) - $(CC) $(CFLAGS) $(OBJECTS) $(LDLIBS) -o $(NAME) - $(OBJ_PATH): mkdir -p $@ diff --git a/inc/fdf.h b/inc/fdf.h index f6eebf9..7d8f44e 100644 --- a/inc/fdf.h +++ b/inc/fdf.h @@ -37,8 +37,8 @@ # define SQRT2 1.41421356237309514547 # define SQRT6 2.44948974278317788134 -# define WIDTH 2000 -# define HEIGHT 1500 +# define WIDTH 500 +# define HEIGHT 500 typedef struct s_point_3d { @@ -71,6 +71,7 @@ typedef struct s_fdf t_map *map; mlx_t *mlx; mlx_image_t *img; + mlx_image_t *menu; double angle_x; double angle_y; double angle_z; @@ -113,5 +114,7 @@ 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); #endif diff --git a/menu.png b/menu.png new file mode 100644 index 0000000..bac62f4 Binary files /dev/null and b/menu.png differ diff --git a/src/clean_fdf.c b/src/clean_fdf.c index 3920881..2862f63 100644 --- a/src/clean_fdf.c +++ b/src/clean_fdf.c @@ -14,12 +14,16 @@ bool clean_fdf(t_fdf *fdf) { + mlx_delete_image(fdf->mlx, fdf->img); + mlx_delete_image(fdf->mlx, fdf->menu); + if (fdf->map) { free(fdf->map->orig); free(fdf->map->rot); free(fdf->map->proj); free(fdf->map); + } free(fdf); return (true); diff --git a/src/fdf.c b/src/fdf.c index e6be768..93762f5 100644 --- a/src/fdf.c +++ b/src/fdf.c @@ -22,6 +22,8 @@ int main(int argc, char *argv[]) if (!parse_map(argv[1], fdf)) handle_error(fdf, "Error: failed to parse map"); init_mlx(fdf); + fdf->menu = draw_menu(fdf); + fdf->menu->enabled = false; mlx_loop(fdf->mlx); mlx_terminate(fdf->mlx); clean_fdf(fdf); diff --git a/src/fdf_color.c b/src/fdf_color.c index 5a72e06..31cf3aa 100644 --- a/src/fdf_color.c +++ b/src/fdf_color.c @@ -57,3 +57,16 @@ int interpolate_color(t_point_2d start, t_point_2d end, t_point_2d current) + (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 temp; + + temp = 255 * z / z_max; + red = temp; + green = 255 - temp; + return (red << 24 | green << 16 | 0x00FF); +} \ No newline at end of file diff --git a/src/fdf_hooks.c b/src/fdf_hooks.c index 7c031f1..bc21927 100644 --- a/src/fdf_hooks.c +++ b/src/fdf_hooks.c @@ -30,7 +30,9 @@ void resize_hook(int width, int height, void *param) 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)) + 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; @@ -66,6 +68,34 @@ void draw_hook(void *param) fdf_draw_line(fdf->img, 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 key_hook(mlx_key_data_t keydata, void *param) @@ -110,4 +140,7 @@ void key_hook(mlx_key_data_t keydata, void *param) 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; + +} \ No newline at end of file diff --git a/src/parse_map.c b/src/parse_map.c index ce2580c..0ae6993 100644 --- a/src/parse_map.c +++ b/src/parse_map.c @@ -26,5 +26,7 @@ int parse_map(char *filename, t_fdf *fdf) handle_error(fdf, MALLOC_ERROR); if (!read_map(filename, fdf)) handle_error(fdf, "Error: failed to read map"); + get_z_max(fdf); + ft_printf("%d\n", fdf->map->z_max); return (SUCCESS); } diff --git a/src/project_isometric.c b/src/project_isometric.c index ad5ed0d..4b00d95 100644 --- a/src/project_isometric.c +++ b/src/project_isometric.c @@ -29,7 +29,8 @@ void project_isometric(t_fdf *fdf) - 2 * fdf->map->rot[i].z) / SQRT6; 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].color = fdf->map->rot[i].color; + fdf->map->proj[i].color = get_z_color(fdf->map->orig[i].z, fdf->map->z_max); + // fdf->map->proj[i].color = fdf->map->rot[i].color; i++; } }