fdf struct and draw_hook and key_hook rotations

This commit is contained in:
whaffman 2024-12-09 14:55:44 +01:00
parent 741f552e5d
commit 5558fd3d3d
4 changed files with 98 additions and 53 deletions

View File

@ -27,7 +27,7 @@ MLX42 = $(MLX42_PATH)/build/libmlx42.a
OBJ_PATH = obj OBJ_PATH = obj
VPATH = src VPATH = src
SOURCES = fdf.c fdf_rotations.c fdf_draw.c SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c"))
OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o))

View File

@ -22,6 +22,9 @@
# include "libft.h" # include "libft.h"
# include "MLX42.h" # include "MLX42.h"
# define SUCCESS 1
# define FAILURE 0
# define WRONG_LINE_LENGTH "Error: wrong line length. \ # define WRONG_LINE_LENGTH "Error: wrong line length. \
Expected %d, got %d in line nr %d\n" Expected %d, got %d in line nr %d\n"
# define MALLOC_ERROR "Error: malloc failed\n" # define MALLOC_ERROR "Error: malloc failed\n"
@ -34,8 +37,8 @@
# define SQRT2 1.41421356237309514547 # define SQRT2 1.41421356237309514547
# define SQRT6 2.44948974278317788134 # define SQRT6 2.44948974278317788134
# define WIDTH 1024 # define WIDTH 2000
# define HEIGHT 640 # define HEIGHT 1500
typedef struct s_point_3d typedef struct s_point_3d
{ {
@ -79,7 +82,7 @@ typedef struct s_fdf
int get_map_sizes(char *filename, t_map *map); int get_map_sizes(char *filename, t_map *map);
void free_line_and_split(char **line, char ***split); void free_line_and_split(char **line, char ***split);
int read_map(char *filename, t_map *map); int read_map(char *filename, t_map *map);
t_map *parse_map(char *filename); int parse_map(char *filename, t_fdf *fdf);
void print_map(t_map *map); void print_map(t_map *map);
void rotate_x(t_point_3d **points, double angle, int size); void rotate_x(t_point_3d **points, double angle, int size);
void rotate_y(t_point_3d **points, double angle, int size); void rotate_y(t_point_3d **points, double angle, int size);
@ -87,5 +90,13 @@ void rotate_z(t_point_3d **points, double angle, int size);
bool fdf_put_pixel(mlx_image_t *img, t_point_2d point); bool fdf_put_pixel(mlx_image_t *img, t_point_2d point);
void fdf_draw_line(mlx_image_t *img, t_point_2d start, void fdf_draw_line(mlx_image_t *img, t_point_2d start,
t_point_2d end); t_point_2d end);
int fdf_hooks(t_fdf *fdf);
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 fdf_project_isometric(t_map *map);
double deg2rad(double deg);
#endif #endif

View File

@ -111,26 +111,27 @@ void print_map(t_map *map)
} }
} }
t_map *parse_map(char *filename) int parse_map(char *filename, t_fdf *fdf)
{ {
t_map *map; t_map *map;
map = malloc(sizeof(t_map)); map = malloc(sizeof(t_map));
if (!map) if (!map)
return (ft_printf(MALLOC_ERROR), NULL); return (ft_printf(MALLOC_ERROR), FAILURE);
if (!get_map_sizes(filename, map)) if (!get_map_sizes(filename, map))
return (free(map), NULL); return (free(map), FAILURE);
map->points = malloc(map->width * map->height * sizeof(t_point_3d)); map->points = malloc(map->width * map->height * sizeof(t_point_3d));
if (!map->points) if (!map->points)
return (free(map), ft_printf(MALLOC_ERROR), NULL); return (free(map), ft_printf(MALLOC_ERROR), FAILURE);
if (!read_map(filename, map)) if (!read_map(filename, map))
return (free(map->points), free(map), NULL); return (free(map->points), free(map), FAILURE);
return (map); fdf->map = map;
return (SUCCESS);
} }
void fdf_set_background(mlx_image_t *img, int color) void fdf_set_background(mlx_image_t *img, int color)
{ {
int x; int x;
int y; int y;
y = 0; y = 0;
while (y < (int) img->height) while (y < (int) img->height)
@ -147,10 +148,10 @@ void fdf_set_background(mlx_image_t *img, int color)
void fdf_project_isometric(t_map *map) void fdf_project_isometric(t_map *map)
{ {
int i; int i;
double x; double x;
double y; double y;
const int scale = fmax(map->height, map->width) * SQRT2; const int scale = fmax(map->height, map->width) * SQRT2;
i = 0; i = 0;
map->projected = malloc(map->width * map->height * sizeof(t_point_2d)); map->projected = malloc(map->width * map->height * sizeof(t_point_2d));
@ -158,9 +159,6 @@ void fdf_project_isometric(t_map *map)
return ; return ;
while (i < map->width * map->height) while (i < map->width * map->height)
{ {
// x = (map->points[i].x - map->points[i].y) * cos(0.523599);
// y = -map->points[i].z + (map->points[i].x + map->points[i].y) * sin(0.523599);
x = (map->points[i].x - map->points[i].y) / SQRT2; x = (map->points[i].x - map->points[i].y) / SQRT2;
y = (map->points[i].x + map->points[i].y - 2 * map->points[i].z) / SQRT6; y = (map->points[i].x + map->points[i].y - 2 * map->points[i].z) / SQRT6;
map->projected[i].x = x * WIDTH / scale + WIDTH / 2; map->projected[i].x = x * WIDTH / scale + WIDTH / 2;
@ -170,54 +168,41 @@ void fdf_project_isometric(t_map *map)
} }
} }
int draw_map(t_map *map)
{
mlx_t* mlx;
mlx_image_t* img;
int i;
mlx = mlx_init(WIDTH, HEIGHT, "FdF", false);
if (!mlx)
return (EXIT_FAILURE);
img = mlx_new_image(mlx, mlx->width, mlx->height);
i = 0;
if (!img || (mlx_image_to_window(mlx, img, 0, 0) < 0))
return (EXIT_FAILURE);
fdf_set_background(img, 0x000000FF);
fdf_project_isometric(map);
while (i < map->width * map->height)
{
fdf_put_pixel(img, map->projected[i]);
if (i % map->width != map->width - 1)
fdf_draw_line(img, map->projected[i], map->projected[i + 1]);
if (i / map->width != map->height - 1)
fdf_draw_line(img, map->projected[i], map->projected[i + map->width]);
i++;
}
mlx_loop(mlx);
mlx_terminate(mlx);
return (EXIT_SUCCESS);
}
double deg2rad(double deg) double deg2rad(double deg)
{ {
return (deg / 180.0 * M_PI); return (deg / 180.0 * M_PI);
} }
int init_mlx(t_fdf *fdf)
{
fdf->mlx = mlx_init(WIDTH, HEIGHT, "FdF", false);
if (!fdf->mlx)
return (FAILURE);
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))
return (FAILURE);
fdf_hooks(fdf);
mlx_loop(fdf->mlx);
mlx_terminate(fdf->mlx);
return (SUCCESS);
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
t_map *map; t_fdf fdf;
if (argc != 2) if (argc != 2)
{ {
ft_printf(USAGE_ERROR, argv[0]); ft_printf(USAGE_ERROR, argv[0]);
return (EXIT_FAILURE); return (EXIT_FAILURE);
} }
map = parse_map(argv[1]); if (!parse_map(argv[1], &fdf))
if (!map)
return (EXIT_FAILURE); return (EXIT_FAILURE);
// rotate_z(&map->points, deg2rad(30) , map->width * map->height); init_mlx(&fdf);
//rotate_x(&map->points, deg2rad(70), map->width * map->height);
draw_map(map);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

49
src/fdf_hooks.c Normal file
View File

@ -0,0 +1,49 @@
#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);
return (1);
}
void draw_hook(void *param)
{
t_fdf *fdf;
int i;
fdf = (t_fdf *)param;
fdf_set_background(fdf->img, 0x000000FF);
fdf_project_isometric(fdf->map);
i = 0;
while (i < fdf->map->width * fdf->map->height)
{
fdf_put_pixel(fdf->img, fdf->map->projected[i]);
if (i % fdf->map->width != fdf->map->width - 1)
fdf_draw_line(fdf->img, fdf->map->projected[i], fdf->map->projected[i + 1]);
if (i / fdf->map->width != fdf->map->height - 1)
fdf_draw_line(fdf->img, fdf->map->projected[i], fdf->map->projected[i + fdf->map->width]);
i++;
}
}
void key_hook(mlx_key_data_t keydata, void *param)
{
t_fdf *fdf;
fdf = (t_fdf *)param;
if (keydata.key == MLX_KEY_ESCAPE)
{
mlx_close_window(((t_fdf *)param)->mlx);
exit(0);
}
if (keydata.key == MLX_KEY_LEFT && keydata.action == MLX_PRESS)
rotate_z(&fdf->map->points, deg2rad(-5), fdf->map->width * fdf->map->height);
if (keydata.key == MLX_KEY_RIGHT && keydata.action == MLX_PRESS)
rotate_z(&fdf->map->points, deg2rad(5), fdf->map->width * fdf->map->height);
if (keydata.key == MLX_KEY_UP && keydata.action == MLX_PRESS)
rotate_x(&fdf->map->points, deg2rad(-5), fdf->map->width * fdf->map->height);
if (keydata.key == MLX_KEY_DOWN && keydata.action == MLX_PRESS)
rotate_x(&fdf->map->points, deg2rad(5), fdf->map->width * fdf->map->height);
}