test
This commit is contained in:
parent
5558fd3d3d
commit
b551e2bc55
@ -58,7 +58,8 @@ typedef struct s_point_2d
|
||||
|
||||
typedef struct s_map
|
||||
{
|
||||
t_point_3d *points;
|
||||
t_point_3d *original;
|
||||
t_point_3d *rotated;
|
||||
t_point_2d *projected;
|
||||
int width;
|
||||
int height;
|
||||
@ -84,6 +85,9 @@ void free_line_and_split(char **line, char ***split);
|
||||
int read_map(char *filename, t_map *map);
|
||||
int parse_map(char *filename, t_fdf *fdf);
|
||||
void print_map(t_map *map);
|
||||
void copy_map(t_fdf *fdf);
|
||||
void fdf_apply_rotation(t_fdf *fdf);
|
||||
|
||||
void rotate_x(t_point_3d **points, double angle, int size);
|
||||
void rotate_y(t_point_3d **points, double angle, int size);
|
||||
void rotate_z(t_point_3d **points, double angle, int size);
|
||||
|
||||
130
src/fdf.c
130
src/fdf.c
@ -76,10 +76,10 @@ int read_map(char *filename, t_map *map)
|
||||
split = ft_split(line, ' ');
|
||||
while (split[x])
|
||||
{
|
||||
map->points[y * map->width + x].x = x - map->width / 2;
|
||||
map->points[y * map->width + x].y = map->height / 2 - y;
|
||||
map->points[y * map->width + x].z = ft_atoi(split[x]) * 0.08;
|
||||
map->points[y * map->width + x].color = 0xFFFFFFFF;
|
||||
map->original[y * map->width + x].x = x - map->width / 2;
|
||||
map->original[y * map->width + x].y = map->height / 2 - y;
|
||||
map->original[y * map->width + x].z = ft_atoi(split[x]);
|
||||
map->original[y * map->width + x].color = 0xFFFFFFFF;
|
||||
x++;
|
||||
}
|
||||
free_line_and_split(&line, &split);
|
||||
@ -89,43 +89,18 @@ int read_map(char *filename, t_map *map)
|
||||
return (1);
|
||||
}
|
||||
|
||||
void print_map(t_map *map)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
y = 0;
|
||||
while (y < map->height)
|
||||
{
|
||||
x = 0;
|
||||
while (x < map->width)
|
||||
{
|
||||
printf("(%.2f, %.2f, %.2f)\n",
|
||||
map->points[y * map->width + x].x,
|
||||
map->points[y * map->width + x].y,
|
||||
map->points[y * map->width + x].z);
|
||||
x++;
|
||||
}
|
||||
ft_printf("\n");
|
||||
y++;
|
||||
}
|
||||
}
|
||||
|
||||
int parse_map(char *filename, t_fdf *fdf)
|
||||
{
|
||||
t_map *map;
|
||||
if (!get_map_sizes(filename, fdf->map))
|
||||
return (FAILURE);
|
||||
fdf->map->original = malloc(fdf->map->width * fdf->map->height * sizeof(t_point_3d));
|
||||
fdf->map->rotated = malloc(fdf->map->width * fdf->map->height * sizeof(t_point_3d));
|
||||
fdf->map->projected = malloc(fdf->map->width * fdf->map->height * sizeof(t_point_2d));
|
||||
if (!fdf->map->original || !fdf->map->rotated || !fdf->map->projected)
|
||||
return (FAILURE);
|
||||
if (!read_map(filename, fdf->map))
|
||||
return (FAILURE);
|
||||
|
||||
map = malloc(sizeof(t_map));
|
||||
if (!map)
|
||||
return (ft_printf(MALLOC_ERROR), FAILURE);
|
||||
if (!get_map_sizes(filename, map))
|
||||
return (free(map), FAILURE);
|
||||
map->points = malloc(map->width * map->height * sizeof(t_point_3d));
|
||||
if (!map->points)
|
||||
return (free(map), ft_printf(MALLOC_ERROR), FAILURE);
|
||||
if (!read_map(filename, map))
|
||||
return (free(map->points), free(map), FAILURE);
|
||||
fdf->map = map;
|
||||
return (SUCCESS);
|
||||
}
|
||||
void fdf_set_background(mlx_image_t *img, int color)
|
||||
@ -159,11 +134,11 @@ void fdf_project_isometric(t_map *map)
|
||||
return ;
|
||||
while (i < map->width * map->height)
|
||||
{
|
||||
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;
|
||||
x = (map->rotated[i].x - map->rotated[i].y) / SQRT2;
|
||||
y = (map->rotated[i].x + map->rotated[i].y - 2 * map->rotated[i].z) / SQRT6;
|
||||
map->projected[i].x = x * WIDTH / scale + WIDTH / 2;
|
||||
map->projected[i].y = y * HEIGHT / scale + HEIGHT / 2;
|
||||
map->projected[i].color = map->points[i].color;
|
||||
map->projected[i].color = map->rotated[i].color;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@ -179,30 +154,85 @@ double deg2rad(double deg)
|
||||
|
||||
int init_mlx(t_fdf *fdf)
|
||||
{
|
||||
fdf->mlx = mlx_init(WIDTH, HEIGHT, "FdF", false);
|
||||
if (!fdf->mlx)
|
||||
return (FAILURE);
|
||||
mlx_t *mlx;
|
||||
|
||||
printf("init_mlx\n");
|
||||
mlx = mlx_init(WIDTH, HEIGHT, "FdF", false);
|
||||
if (!mlx)
|
||||
return (printf("mlx_init failed\n"), FAILURE);
|
||||
printf("mlx: %p\n", mlx);
|
||||
fdf->mlx = mlx;
|
||||
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);
|
||||
printf("img: %p\n", fdf->img);
|
||||
fdf_hooks(fdf);
|
||||
mlx_loop(fdf->mlx);
|
||||
mlx_terminate(fdf->mlx);
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
t_fdf *initialise_fdf()
|
||||
{
|
||||
t_map *map;
|
||||
t_fdf *fdf;
|
||||
|
||||
fdf = malloc(sizeof(t_fdf));
|
||||
if (!fdf)
|
||||
return (ft_printf(MALLOC_ERROR), NULL);
|
||||
map = malloc(sizeof(t_map));
|
||||
if (!map)
|
||||
return (ft_printf(MALLOC_ERROR), NULL);
|
||||
fdf->map = map;
|
||||
fdf->mlx = NULL;
|
||||
fdf->img = NULL;
|
||||
fdf->angle_x = 0;
|
||||
fdf->angle_y = 0;
|
||||
fdf->angle_z = 0;
|
||||
fdf->zoom = 1;
|
||||
fdf->offset_x = 0;
|
||||
fdf->offset_y = 0;
|
||||
fdf->z_scale = 1;
|
||||
fdf->map->original = NULL;
|
||||
fdf->map->rotated = NULL;
|
||||
fdf->map->projected = NULL;
|
||||
fdf->map->width = 0;
|
||||
fdf->map->height = 0;
|
||||
fdf->map->z_max = 0;
|
||||
|
||||
|
||||
return (fdf);
|
||||
}
|
||||
|
||||
bool clean_fdf(t_fdf *fdf)
|
||||
{
|
||||
if (fdf->map)
|
||||
{
|
||||
free(fdf->map->original);
|
||||
free(fdf->map->rotated);
|
||||
free(fdf->map->projected);
|
||||
free(fdf->map);
|
||||
}
|
||||
return (true);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
t_fdf fdf;
|
||||
|
||||
t_fdf *fdf;
|
||||
|
||||
fdf = initialise_fdf();
|
||||
if (argc != 2)
|
||||
{
|
||||
ft_printf(USAGE_ERROR, argv[0]);
|
||||
return (EXIT_FAILURE);
|
||||
}
|
||||
if (!parse_map(argv[1], &fdf))
|
||||
if (!parse_map(argv[1], fdf))
|
||||
return (EXIT_FAILURE);
|
||||
init_mlx(&fdf);
|
||||
|
||||
init_mlx(fdf);
|
||||
printf("fdf: %p\n", fdf);
|
||||
mlx_loop(fdf->mlx);
|
||||
|
||||
|
||||
mlx_terminate(fdf->mlx);
|
||||
clean_fdf(fdf);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ int fdf_hooks(t_fdf *fdf)
|
||||
{
|
||||
mlx_loop_hook(fdf->mlx, draw_hook, fdf);
|
||||
mlx_key_hook(fdf->mlx, key_hook, fdf);
|
||||
printf("hooks set\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
@ -15,7 +16,11 @@ void draw_hook(void *param)
|
||||
|
||||
fdf = (t_fdf *)param;
|
||||
fdf_set_background(fdf->img, 0x000000FF);
|
||||
printf("background set\n");
|
||||
fdf_apply_rotation(fdf);
|
||||
printf("rotation applied\n");
|
||||
fdf_project_isometric(fdf->map);
|
||||
printf("projection applied\n");
|
||||
i = 0;
|
||||
while (i < fdf->map->width * fdf->map->height)
|
||||
{
|
||||
@ -38,12 +43,11 @@ void key_hook(mlx_key_data_t keydata, void *param)
|
||||
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);
|
||||
fdf->angle_z -= deg2rad(5);
|
||||
if (keydata.key == MLX_KEY_RIGHT && keydata.action == MLX_PRESS)
|
||||
rotate_z(&fdf->map->points, deg2rad(5), fdf->map->width * fdf->map->height);
|
||||
fdf->angle_z += deg2rad(5);
|
||||
if (keydata.key == MLX_KEY_UP && keydata.action == MLX_PRESS)
|
||||
rotate_x(&fdf->map->points, deg2rad(-5), fdf->map->width * fdf->map->height);
|
||||
fdf->angle_x -= deg2rad(5);
|
||||
if (keydata.key == MLX_KEY_DOWN && keydata.action == MLX_PRESS)
|
||||
rotate_x(&fdf->map->points, deg2rad(5), fdf->map->width * fdf->map->height);
|
||||
|
||||
fdf->angle_x += deg2rad(5);
|
||||
}
|
||||
|
||||
@ -1,5 +1,27 @@
|
||||
#include "fdf.h"
|
||||
|
||||
void fdf_apply_rotation(t_fdf *fdf)
|
||||
{
|
||||
copy_map(fdf);
|
||||
rotate_x(&fdf->map->rotated, fdf->angle_x, fdf->map->width * fdf->map->height);
|
||||
rotate_y(&fdf->map->rotated, fdf->angle_y, fdf->map->width * fdf->map->height);
|
||||
rotate_z(&fdf->map->rotated, fdf->angle_z, fdf->map->width * fdf->map->height);
|
||||
}
|
||||
|
||||
void copy_map(t_fdf *fdf)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i < fdf->map->width * fdf->map->height)
|
||||
{
|
||||
fdf->map->rotated[i].x = fdf->map->original[i].x;
|
||||
fdf->map->rotated[i].y = fdf->map->original[i].y;
|
||||
fdf->map->rotated[i].z = fdf->map->original[i].z * fdf->z_scale;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void rotate_x(t_point_3d **points, double angle, int size)
|
||||
{
|
||||
int i;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user