Files
42_INT_04_fdf/srcs/modifs.c
2021-07-23 17:19:42 +02:00

96 lines
2.5 KiB
C

#include "fdf.h"
// add "print_keycode(keycode);" at begining to print keycode
int keypress(int keycode, t_fdf *fdf)
{
if (keycode == ESCAPE)
shut_down(fdf);
else if (keycode == LEFT)
(fdf->rot_x) += 1;
else if (keycode == RIGHT)
(fdf->rot_x) -= 1;
else if (keycode == UP)
(fdf->rot_y) += 1;
else if (keycode == DOWN)
(fdf->rot_y) -= 1;
else if (keycode == Q)
(fdf->mov_x) -= 6;
else if (keycode == D)
(fdf->mov_x) += 6;
else if (keycode == Z)
(fdf->mov_y) -= 6;
else if (keycode == S)
(fdf->mov_y) += 6;
else if (keycode == A)
(fdf->zoom) += 6;
else if (keycode == W)
(fdf->zoom) -= 6;
draw_image(fdf);
return (0);
}
/*
** int[0] = x, int[1] = y, int[2] = z
** quick explanation :
** zoom :
** x = i * (fdf->offset + fdf->zoom);
** center before 3d rotation :
** x -= (fdf->map_size_x + fdf->zoom * fdf->map_width) / 2;
** z changes according to zoom :
** z += (z * fdf->zoom) / fdf->offset;
** deplacement :
** point[0] += fdf->margin + fdf->mov_x;
** center zoom after :
** point[0] -= ((fdf->map_size_x / 2) / fdf->offset) * fdf->zoom;
** center after 3d rotation :
** point[0] += (fdf->map_size_x + fdf->zoom * fdf->map_width) / 2;
*/
int *new_coordinates(t_fdf *fdf, int i, int j)
{
int x;
int y;
int z;
int *point;
point = ft_calloc(3, sizeof(int));
x = i * (fdf->offset + fdf->zoom);
y = j * (fdf->offset + fdf->zoom);
x -= (fdf->map_size_x + fdf->zoom * fdf->map_width) / 2;
y -= (fdf->map_size_y + fdf->zoom * fdf->map_height) / 2;
z = fdf->map[j][i] * fdf->altitude;
z += (z * fdf->zoom) / fdf->offset;
point[0] = x * cos(fdf->rad_x) + y * sin(fdf->rad_x);
point[1] = y * cos(fdf->rad_x) - x * sin(fdf->rad_x);
point[1] = point[1] * cos(fdf->rad_y) - -z * sin(fdf->rad_y);
point[0] += fdf->margin + fdf->mov_x;
point[1] += fdf->margin + fdf->mov_y;
point[0] -= ((fdf->map_size_x / 2) / fdf->offset) * fdf->zoom;
point[1] -= ((fdf->map_size_y / 2) / fdf->offset) * fdf->zoom;
point[0] += (fdf->map_size_x + fdf->zoom * fdf->map_width) / 2;
point[1] += (fdf->map_size_y + fdf->zoom * fdf->map_height) / 2;
point[2] = z;
return (point);
}
int print_keycode(int keycode)
{
ft_putnbr(keycode);
ft_putchar(' ');
return (0);
}
void rotation_state(t_fdf *fdf)
{
char *position;
int x;
int y;
x = fdf->img_size_x - 10;
y = fdf->img_size_y - 10;
position = ft_strjoinfree(ft_itoa(fdf->rot_x), ft_strdup(" | "));
position = ft_strjoinfree(position, ft_itoa(fdf->rot_y));
x -= ft_strlen(position) * 6;
mlx_string_put(fdf->mlx_ptr, fdf->win_ptr, x, y, 0xffffff, position);
free(position);
}