help menu z_color and auto depends in Makefile

This commit is contained in:
Willem Haffmans 2024-12-15 17:02:15 +01:00
parent 68e9533b91
commit 12cb357b0b
10 changed files with 71 additions and 9 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
fdf
*.a
*.o
*.d

View File

@ -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 $@

View File

@ -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

BIN
menu.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@ -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);

View File

@ -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);

View File

@ -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);
}

View File

@ -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;
}

View File

@ -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);
}

View File

@ -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++;
}
}