fdf/src/fdf_hooks.c
2024-12-19 15:39:53 +01:00

164 lines
5.9 KiB
C

/* ************************************************************************** */
/* */
/* ::: o_ :::::: ::: */
/* fdf_hooks.c :+: / :+::+: :+: */
/* +:+ > +:++:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
/* +#+ +#+#+ +#++#+ +#+ \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);
}