split functions in files
This commit is contained in:
parent
d11133f122
commit
54c6cfb9a3
12
inc/fdf.h
12
inc/fdf.h
@ -60,8 +60,8 @@ typedef struct s_point_2d
|
||||
typedef struct s_map
|
||||
{
|
||||
t_point_3d *original;
|
||||
t_point_3d *rotated;
|
||||
t_point_2d *projected;
|
||||
t_point_3d *rot;
|
||||
t_point_2d *proj;
|
||||
int width;
|
||||
int height;
|
||||
int z_max;
|
||||
@ -107,9 +107,11 @@ void resize_hook(int width, int height, void *param);
|
||||
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_fdf *fdf);
|
||||
void project_isometric(t_fdf *fdf);
|
||||
double deg2rad(double deg);
|
||||
|
||||
|
||||
t_fdf *initialise_fdf(void);
|
||||
bool clean_fdf(t_fdf *fdf);
|
||||
void get_z_max(t_fdf *fdf);
|
||||
int init_mlx(t_fdf *fdf);
|
||||
|
||||
#endif
|
||||
|
||||
8
scratch.txt
Normal file
8
scratch.txt
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
while (i < alle punten)
|
||||
{
|
||||
x' = (x - y) / SQRT2;
|
||||
y' = (x + y - 2 * z) / SQRT6;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
26
src/clean_fdf.c
Normal file
26
src/clean_fdf.c
Normal file
@ -0,0 +1,26 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* clean_fdf.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/13 15:15:51 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/13 15:23:09 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
bool clean_fdf(t_fdf *fdf)
|
||||
{
|
||||
if (fdf->map)
|
||||
{
|
||||
free(fdf->map->original);
|
||||
free(fdf->map->rot);
|
||||
free(fdf->map->proj);
|
||||
free(fdf->map);
|
||||
}
|
||||
free(fdf);
|
||||
return (true);
|
||||
}
|
||||
18
src/deg2rad.c
Normal file
18
src/deg2rad.c
Normal file
@ -0,0 +1,18 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* deg2rad.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/13 15:23:10 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/13 15:23:10 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
double deg2rad(double deg)
|
||||
{
|
||||
return (deg / 180.0 * M_PI);
|
||||
}
|
||||
215
src/fdf.c
215
src/fdf.c
@ -6,225 +6,12 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/06 11:07:30 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/06 11:19:30 by whaffman ### ### ### ### / \ */
|
||||
/* Updated: 2024/12/13 15:23:10 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
int get_map_sizes(char *filename, t_map *map)
|
||||
{
|
||||
const int fd = open(filename, O_RDONLY);
|
||||
char *line;
|
||||
int width;
|
||||
|
||||
if (fd < 0)
|
||||
return (ft_printf(FILE_ERROR, filename), 0);
|
||||
width = 0;
|
||||
while (true)
|
||||
{
|
||||
line = get_next_line(fd);
|
||||
if (!line)
|
||||
break ;
|
||||
width = ft_count_words(line, ' ');
|
||||
free(line);
|
||||
if (map->width != 0 && map->width != width)
|
||||
return (ft_printf(WRONG_LINE_LENGTH, map->width, width, map->height), 0);
|
||||
else if (map->width == 0)
|
||||
map->width = width;
|
||||
map->height++;
|
||||
}
|
||||
return (close(fd), 1);
|
||||
}
|
||||
|
||||
void free_line_and_split(char **line, char ***split)
|
||||
{
|
||||
char **split_start;
|
||||
|
||||
split_start = *split;
|
||||
free(*line);
|
||||
*line = NULL;
|
||||
while (**split)
|
||||
{
|
||||
free(**split);
|
||||
**split = NULL;
|
||||
(*split)++;
|
||||
}
|
||||
free(split_start);
|
||||
*split = NULL;
|
||||
}
|
||||
|
||||
int read_map(char *filename, t_fdf *fdf)
|
||||
{
|
||||
int fd;
|
||||
char *line;
|
||||
int y;
|
||||
int x;
|
||||
char **split;
|
||||
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0)
|
||||
handle_error(NULL, FILE_ERROR);
|
||||
y = fdf->map->height - 1;
|
||||
while (true)
|
||||
{
|
||||
x = 0;
|
||||
line = get_next_line(fd);
|
||||
if (!line)
|
||||
break ;
|
||||
split = ft_split(line, ' ');
|
||||
while (split[x])
|
||||
{
|
||||
fdf->map->original[y * fdf->map->width + x].x = x - fdf->map->width / 2;
|
||||
fdf->map->original[y * fdf->map->width + x].y = fdf->map->height / 2 - y;
|
||||
fdf->map->original[y * fdf->map->width + x].z = ft_atoi(split[x]);
|
||||
fdf->map->original[y * fdf->map->width + x].color = get_color(split[x]);
|
||||
x++;
|
||||
}
|
||||
free_line_and_split(&line, &split);
|
||||
y--;
|
||||
}
|
||||
close(fd);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int parse_map(char *filename, t_fdf *fdf)
|
||||
{
|
||||
if (!get_map_sizes(filename, fdf->map))
|
||||
handle_error(fdf, "Error: failed to get map sizes");
|
||||
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)
|
||||
handle_error(fdf, MALLOC_ERROR);
|
||||
if (!read_map(filename, fdf))
|
||||
handle_error(fdf, "Error: failed to read map");
|
||||
|
||||
return (SUCCESS);
|
||||
}
|
||||
void fdf_set_background(mlx_image_t *img, int color)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
y = 0;
|
||||
while (y < (int) img->height)
|
||||
{
|
||||
x = 0;
|
||||
while (x < (int) img->width)
|
||||
{
|
||||
mlx_put_pixel(img, x, y, color);
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
}
|
||||
|
||||
void fdf_project_isometric(t_fdf *fdf)
|
||||
{
|
||||
int i;
|
||||
double x;
|
||||
double y;
|
||||
|
||||
i = 0;
|
||||
if(fdf->zoom == 0)
|
||||
fdf->zoom = fmin(fdf->mlx->width / fdf->map->width, fdf->mlx->height / fdf->map->height) / SQRT2;
|
||||
while (i < fdf->map->width * fdf->map->height)
|
||||
{
|
||||
x = (fdf->map->rotated[i].x - fdf->map->rotated[i].y) / SQRT2;
|
||||
y = (fdf->map->rotated[i].x + fdf->map->rotated[i].y - 2 * fdf->map->rotated[i].z) / SQRT6;
|
||||
fdf->map->projected[i].x = x * fdf->zoom + fdf->offset_x;
|
||||
fdf->map->projected[i].y = y * fdf->zoom + fdf->offset_y;
|
||||
fdf->map->projected[i].color = fdf->map->rotated[i].color;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
double deg2rad(double deg)
|
||||
{
|
||||
return (deg / 180.0 * M_PI);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int init_mlx(t_fdf *fdf)
|
||||
{
|
||||
mlx_t *mlx;
|
||||
|
||||
printf("init_mlx\n");
|
||||
mlx = mlx_init(WIDTH, HEIGHT, "FdF", true);
|
||||
if (!mlx)
|
||||
handle_error(fdf, "Error: failed to initialise 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))
|
||||
handle_error(fdf, "Error: failed to create image");
|
||||
fdf_hooks(fdf);
|
||||
return (SUCCESS);
|
||||
}
|
||||
|
||||
t_fdf *initialise_fdf()
|
||||
{
|
||||
t_map *map;
|
||||
t_fdf *fdf;
|
||||
|
||||
fdf = malloc(sizeof(t_fdf));
|
||||
if (!fdf)
|
||||
handle_error(fdf, MALLOC_ERROR);
|
||||
map = malloc(sizeof(t_map));
|
||||
if (!map)
|
||||
handle_error(fdf, MALLOC_ERROR);
|
||||
fdf->map = map;
|
||||
fdf->mlx = NULL;
|
||||
fdf->img = NULL;
|
||||
fdf->angle_x = 0;
|
||||
fdf->angle_y = 0;
|
||||
fdf->angle_z = 0;
|
||||
fdf->zoom = 0;
|
||||
fdf->offset_x = WIDTH / 2;
|
||||
fdf->offset_y = HEIGHT / 2;
|
||||
fdf->z_scale = 0.1;
|
||||
fdf->last_width = WIDTH;
|
||||
fdf->last_height = HEIGHT;
|
||||
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);
|
||||
}
|
||||
free(fdf);
|
||||
return (true);
|
||||
}
|
||||
|
||||
void handle_error(t_fdf *fdf, char *error)
|
||||
{
|
||||
if (errno)
|
||||
perror(error);
|
||||
if (mlx_errno)
|
||||
ft_putendl_fd(mlx_strerror(mlx_errno), 2);
|
||||
ft_putendl_fd(error , 2);
|
||||
clean_fdf(fdf);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
t_fdf *fdf;
|
||||
|
||||
@ -1,12 +1,26 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* fdf_color.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/13 15:23:11 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/13 15:23:11 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
int get_color(char *str)
|
||||
int get_color(char *str)
|
||||
{
|
||||
int color;
|
||||
int i;
|
||||
|
||||
color = 0;
|
||||
i = 0;
|
||||
if (str[i] && str[i] == '-')
|
||||
i++;
|
||||
while (str[i] && ft_isdigit(str[i]))
|
||||
i++;
|
||||
i++;
|
||||
@ -24,28 +38,28 @@ int get_color(char *str)
|
||||
break ;
|
||||
i++;
|
||||
}
|
||||
if(color == 0)
|
||||
if (color == 0)
|
||||
return (0xFFFFFFFF);
|
||||
return ((color << 8) + 0xFF);
|
||||
}
|
||||
|
||||
int interpolate_color(t_point_2d start, t_point_2d end, t_point_2d current)
|
||||
{
|
||||
int delta_current;
|
||||
int delta_total;
|
||||
int dcur;
|
||||
int dtotal;
|
||||
int red;
|
||||
int green;
|
||||
int blue;
|
||||
|
||||
delta_current = fmax(abs(current.x - start.x), abs(current.y - start.y));
|
||||
delta_total = fmax(abs(end.x - start.x), abs(end.y - start.y));
|
||||
if (delta_total == 0)
|
||||
dcur = fmax(abs(current.x - start.x), abs(current.y - start.y));
|
||||
dtotal = fmax(abs(end.x - start.x), abs(end.y - start.y));
|
||||
if (dtotal == 0)
|
||||
return (start.color);
|
||||
red = (int) ((1 - (double) delta_current / delta_total) * (start.color >> 24 & 0xFF)
|
||||
+ (double) delta_current / delta_total * (end.color >> 24 & 0xFF));
|
||||
green = (int) ((1 - (double) delta_current / delta_total) * (start.color >> 16 & 0xFF)
|
||||
+ (double) delta_current / delta_total * (end.color >> 16 & 0xFF));
|
||||
blue = (int) ((1 - (double) delta_current / delta_total) * (start.color >> 8 & 0xFF)
|
||||
+ (double) delta_current / delta_total * (end.color >> 8 & 0xFF));
|
||||
red = (int)((1 - (double) dcur / dtotal) * (start.color >> 24 & 0xFF)
|
||||
+ (double) dcur / dtotal * (end.color >> 24 & 0xFF));
|
||||
green = (int)((1 - (double) dcur / dtotal) * (start.color >> 16 & 0xFF)
|
||||
+ (double) dcur / dtotal * (end.color >> 16 & 0xFF));
|
||||
blue = (int)((1 - (double) dcur / dtotal) * (start.color >> 8 & 0xFF)
|
||||
+ (double) dcur / dtotal * (end.color >> 8 & 0xFF));
|
||||
return (red << 24 | green << 16 | blue << 8 | 0xFF);
|
||||
}
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/08 11:45:12 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/08 11:45:12 by whaffman ### ### ### ### / \ */
|
||||
/* Updated: 2024/12/13 15:23:12 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "fdf.h"
|
||||
@ -20,7 +20,7 @@ bool fdf_put_pixel(mlx_image_t *img, t_point_2d point)
|
||||
return (true);
|
||||
}
|
||||
|
||||
void fdf_draw_line_low(mlx_image_t *img, t_point_2d start, t_point_2d end)
|
||||
void fdf_draw_line_low(mlx_image_t *img, t_point_2d start, t_point_2d end)
|
||||
{
|
||||
int delta;
|
||||
int yi;
|
||||
@ -82,7 +82,7 @@ void fdf_draw_line_high(mlx_image_t *img, t_point_2d start, t_point_2d end)
|
||||
}
|
||||
}
|
||||
|
||||
void fdf_draw_line(mlx_image_t *img, t_point_2d start, t_point_2d end)
|
||||
void fdf_draw_line(mlx_image_t *img, t_point_2d start, t_point_2d end)
|
||||
{
|
||||
if ((start.x < 0 || start.x >= (int) img->width
|
||||
|| start.y < 0 || start.y >= (int) img->height)
|
||||
@ -104,5 +104,3 @@ void fdf_draw_line(mlx_image_t *img, t_point_2d start, t_point_2d end)
|
||||
fdf_draw_line_high(img, start, end);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -1,3 +1,15 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: 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"
|
||||
|
||||
@ -8,7 +20,7 @@ int fdf_hooks(t_fdf *fdf)
|
||||
mlx_resize_hook(fdf->mlx, resize_hook, fdf);
|
||||
return (1);
|
||||
}
|
||||
void resize_hook(int width, int height, void *param)
|
||||
void resize_hook(int width, int height, void *param)
|
||||
{
|
||||
t_fdf *fdf;
|
||||
|
||||
@ -25,6 +37,7 @@ void resize_hook(int width, int height, void *param)
|
||||
fdf->last_height = fdf->mlx->height;
|
||||
fdf->last_width = fdf->mlx->width;
|
||||
}
|
||||
|
||||
void draw_hook(void *param)
|
||||
{
|
||||
t_fdf *fdf;
|
||||
@ -32,7 +45,8 @@ void draw_hook(void *param)
|
||||
|
||||
|
||||
fdf = (t_fdf *)param;
|
||||
if (fdf->last_width != fdf->mlx->width || fdf->last_height != fdf->mlx->height)
|
||||
if (fdf->last_width != fdf->mlx->width
|
||||
|| fdf->last_height != fdf->mlx->height)
|
||||
{
|
||||
resize_hook(fdf->mlx->width, fdf->mlx->height, fdf);
|
||||
return ;
|
||||
@ -41,15 +55,15 @@ void draw_hook(void *param)
|
||||
if (fdf->animate_z)
|
||||
fdf->angle_z += deg2rad(fdf->animate_z);
|
||||
fdf_apply_rotation(fdf);
|
||||
fdf_project_isometric(fdf);
|
||||
project_isometric(fdf);
|
||||
i = 0;
|
||||
while (i < fdf->map->width * fdf->map->height)
|
||||
{
|
||||
fdf_put_pixel(fdf->img, fdf->map->projected[i]);
|
||||
fdf_put_pixel(fdf->img, fdf->map->proj[i]);
|
||||
if (i % fdf->map->width != fdf->map->width - 1)
|
||||
fdf_draw_line(fdf->img, fdf->map->projected[i], fdf->map->projected[i + 1]);
|
||||
fdf_draw_line(fdf->img, fdf->map->proj[i], fdf->map->proj[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]);
|
||||
fdf_draw_line(fdf->img, fdf->map->proj[i], fdf->map->proj[i + fdf->map->width]);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
@ -88,11 +102,11 @@ void key_hook(mlx_key_data_t keydata, void *param)
|
||||
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;
|
||||
if (keydata.key == MLX_KEY_MINUS && 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 /= 1.1;
|
||||
fdf->animate_z -= 0.5;
|
||||
if (keydata.key == MLX_KEY_RIGHT_BRACKET && keydata.action == MLX_PRESS)
|
||||
fdf->animate_z += 1;
|
||||
fdf->animate_z += 0.5;
|
||||
}
|
||||
|
||||
@ -1,31 +1,43 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* fdf_rotations.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/13 15:23:13 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/13 15:23:13 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
void fdf_apply_rotation(t_fdf *fdf)
|
||||
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);
|
||||
rotate_x(&fdf->map->rot, fdf->angle_x, fdf->map->width * fdf->map->height);
|
||||
rotate_y(&fdf->map->rot, fdf->angle_y, fdf->map->width * fdf->map->height);
|
||||
rotate_z(&fdf->map->rot, fdf->angle_z, fdf->map->width * fdf->map->height);
|
||||
}
|
||||
|
||||
void copy_map(t_fdf *fdf)
|
||||
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;
|
||||
fdf->map->rotated[i].color = fdf->map->original[i].color;
|
||||
fdf->map->rot[i].x = fdf->map->original[i].x;
|
||||
fdf->map->rot[i].y = fdf->map->original[i].y;
|
||||
fdf->map->rot[i].z = fdf->map->original[i].z * fdf->z_scale;
|
||||
fdf->map->rot[i].color = fdf->map->original[i].color;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void rotate_x(t_point_3d **points, double angle, int size)
|
||||
{
|
||||
int i;
|
||||
int i;
|
||||
double previous_y;
|
||||
|
||||
i = 0;
|
||||
@ -37,6 +49,7 @@ void rotate_x(t_point_3d **points, double angle, int size)
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void rotate_y(t_point_3d **points, double angle, int size)
|
||||
{
|
||||
int i;
|
||||
@ -51,6 +64,7 @@ void rotate_y(t_point_3d **points, double angle, int size)
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
void rotate_z(t_point_3d **points, double angle, int size)
|
||||
{
|
||||
int i;
|
||||
|
||||
30
src/free_line_and_split.c
Normal file
30
src/free_line_and_split.c
Normal file
@ -0,0 +1,30 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* free_line_and_split.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/13 15:23:14 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/13 15:23:14 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
void free_line_and_split(char **line, char ***split)
|
||||
{
|
||||
char **split_start;
|
||||
|
||||
split_start = *split;
|
||||
free(*line);
|
||||
*line = NULL;
|
||||
while (**split)
|
||||
{
|
||||
free(**split);
|
||||
**split = NULL;
|
||||
(*split)++;
|
||||
}
|
||||
free(split_start);
|
||||
*split = NULL;
|
||||
}
|
||||
38
src/get_map_sizes.c
Normal file
38
src/get_map_sizes.c
Normal file
@ -0,0 +1,38 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* get_map_sizes.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/13 15:23:14 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/13 15:23:14 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
int get_map_sizes(char *filename, t_map *map)
|
||||
{
|
||||
const int fd = open(filename, O_RDONLY);
|
||||
char *line;
|
||||
int width;
|
||||
|
||||
if (fd < 0)
|
||||
return (ft_printf(FILE_ERROR, filename), 0);
|
||||
width = 0;
|
||||
while (true)
|
||||
{
|
||||
line = get_next_line(fd);
|
||||
if (!line)
|
||||
break ;
|
||||
width = ft_count_words(line, ' ');
|
||||
free(line);
|
||||
if (map->width != 0 && map->width != width)
|
||||
return (ft_printf(WRONG_LINE_LENGTH, map->width, width, map->height), 0);
|
||||
else if (map->width == 0)
|
||||
map->width = width;
|
||||
map->height++;
|
||||
}
|
||||
return (close(fd), 1);
|
||||
}
|
||||
27
src/get_z_max.c
Normal file
27
src/get_z_max.c
Normal file
@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* get_z_max.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/13 15:23:15 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/13 15:23:15 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
void get_z_max(t_fdf *fdf)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
fdf->map->z_max = 0;
|
||||
while (i < fdf->map->width * fdf->map->height)
|
||||
{
|
||||
if (fdf->map->original[i].z > fdf->map->z_max)
|
||||
fdf->map->z_max = fdf->map->original[i].z;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
24
src/handle_error.c
Normal file
24
src/handle_error.c
Normal file
@ -0,0 +1,24 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* handle_error.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/13 15:23:16 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/13 15:23:16 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
void handle_error(t_fdf *fdf, char *error)
|
||||
{
|
||||
if (errno)
|
||||
perror(error);
|
||||
if (mlx_errno)
|
||||
ft_putendl_fd(mlx_strerror(mlx_errno), 2);
|
||||
ft_putendl_fd(error, 2);
|
||||
clean_fdf(fdf);
|
||||
exit(1);
|
||||
}
|
||||
29
src/init_mlx.c
Normal file
29
src/init_mlx.c
Normal file
@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* init_mlx.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/13 15:23:17 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/13 15:23:17 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
int init_mlx(t_fdf *fdf)
|
||||
{
|
||||
mlx_t *mlx;
|
||||
|
||||
printf("init_mlx\n");
|
||||
mlx = mlx_init(WIDTH, HEIGHT, "FdF", true);
|
||||
if (!mlx)
|
||||
handle_error(fdf, "Error: failed to initialise 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))
|
||||
handle_error(fdf, "Error: failed to create image");
|
||||
fdf_hooks(fdf);
|
||||
return (SUCCESS);
|
||||
}
|
||||
47
src/initialise_fdf.c
Normal file
47
src/initialise_fdf.c
Normal file
@ -0,0 +1,47 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* initialise_fdf.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/13 15:23:17 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/13 15:23:17 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
t_fdf *initialise_fdf(void)
|
||||
{
|
||||
t_map *map;
|
||||
t_fdf *fdf;
|
||||
|
||||
fdf = malloc(sizeof(t_fdf));
|
||||
if (!fdf)
|
||||
handle_error(fdf, MALLOC_ERROR);
|
||||
map = malloc(sizeof(t_map));
|
||||
if (!map)
|
||||
handle_error(fdf, MALLOC_ERROR);
|
||||
fdf->map = map;
|
||||
fdf->mlx = NULL;
|
||||
fdf->img = NULL;
|
||||
fdf->angle_x = 0;
|
||||
fdf->angle_y = 0;
|
||||
fdf->angle_z = 0;
|
||||
fdf->zoom = 0;
|
||||
fdf->offset_x = WIDTH / 2;
|
||||
fdf->offset_y = HEIGHT / 2;
|
||||
fdf->z_scale = 0.1;
|
||||
fdf->last_width = WIDTH;
|
||||
fdf->last_height = HEIGHT;
|
||||
fdf->map->original = NULL;
|
||||
fdf->map->rot = NULL;
|
||||
fdf->map->proj = NULL;
|
||||
fdf->map->width = 0;
|
||||
fdf->map->height = 0;
|
||||
fdf->map->z_max = 0;
|
||||
|
||||
|
||||
return (fdf);
|
||||
}
|
||||
28
src/parse_map.c
Normal file
28
src/parse_map.c
Normal file
@ -0,0 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* parse_map.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/13 15:23:18 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/13 15:23:18 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
int parse_map(char *filename, t_fdf *fdf)
|
||||
{
|
||||
if (!get_map_sizes(filename, fdf->map))
|
||||
handle_error(fdf, "Error: failed to get map sizes");
|
||||
fdf->map->original = malloc(fdf->map->width * fdf->map->height * sizeof(t_point_3d));
|
||||
fdf->map->rot = malloc(fdf->map->width * fdf->map->height * sizeof(t_point_3d));
|
||||
fdf->map->proj = malloc(fdf->map->width * fdf->map->height * sizeof(t_point_2d));
|
||||
if (!fdf->map->original || !fdf->map->rot || !fdf->map->proj)
|
||||
handle_error(fdf, MALLOC_ERROR);
|
||||
if (!read_map(filename, fdf))
|
||||
handle_error(fdf, "Error: failed to read map");
|
||||
|
||||
return (SUCCESS);
|
||||
}
|
||||
33
src/project_isometric.c
Normal file
33
src/project_isometric.c
Normal file
@ -0,0 +1,33 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* project_isometric.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/13 15:23:19 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/13 15:23:19 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
void project_isometric(t_fdf *fdf)
|
||||
{
|
||||
int i;
|
||||
double x;
|
||||
double y;
|
||||
|
||||
i = 0;
|
||||
if(fdf->zoom == 0)
|
||||
fdf->zoom = fmin(fdf->mlx->width / fdf->map->width, fdf->mlx->height / fdf->map->height) / SQRT2;
|
||||
while (i < fdf->map->width * fdf->map->height)
|
||||
{
|
||||
x = (fdf->map->rot[i].x - fdf->map->rot[i].y) / SQRT2;
|
||||
y = (fdf->map->rot[i].x + fdf->map->rot[i].y - 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;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
48
src/read_map.c
Normal file
48
src/read_map.c
Normal file
@ -0,0 +1,48 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* read_map.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/13 15:23:19 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/13 15:23:19 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
int read_map(char *filename, t_fdf *fdf)
|
||||
{
|
||||
int fd;
|
||||
char *line;
|
||||
int y;
|
||||
int x;
|
||||
char **split;
|
||||
|
||||
|
||||
fd = open(filename, O_RDONLY);
|
||||
if (fd < 0)
|
||||
handle_error(NULL, FILE_ERROR);
|
||||
y = fdf->map->height - 1;
|
||||
while (true)
|
||||
{
|
||||
x = 0;
|
||||
line = get_next_line(fd);
|
||||
if (!line)
|
||||
break ;
|
||||
split = ft_split(line, ' ');
|
||||
while (split[x])
|
||||
{
|
||||
fdf->map->original[y * fdf->map->width + x].x = x - fdf->map->width / 2;
|
||||
fdf->map->original[y * fdf->map->width + x].y = fdf->map->height / 2 - y;
|
||||
fdf->map->original[y * fdf->map->width + x].z = ft_atoi(split[x]);
|
||||
fdf->map->original[y * fdf->map->width + x].color = get_color(split[x]);
|
||||
x++;
|
||||
}
|
||||
free_line_and_split(&line, &split);
|
||||
y--;
|
||||
}
|
||||
close(fd);
|
||||
return (1);
|
||||
}
|
||||
31
src/set_background.c
Normal file
31
src/set_background.c
Normal file
@ -0,0 +1,31 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: o_ :::::: ::: */
|
||||
/* set_background.c :+: / :+::+: :+: */
|
||||
/* +:+ > +:++:+ +:+ */
|
||||
/* By: whaffman <whaffman@student.codam.nl> +#+ +:+ +#++#++:++#++ */
|
||||
/* +#+ +#+#+ +#++#+ +#+ \o/ */
|
||||
/* Created: 2024/12/13 15:23:20 by whaffman #+#+# #+#+# #+# #+# | */
|
||||
/* Updated: 2024/12/13 15:23:20 by whaffman ### ### ### ### / \ */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "fdf.h"
|
||||
|
||||
void fdf_set_background(mlx_image_t *img, int color)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
|
||||
y = 0;
|
||||
while (y < (int) img->height)
|
||||
{
|
||||
x = 0;
|
||||
while (x < (int) img->width)
|
||||
{
|
||||
mlx_put_pixel(img, x, y, color);
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user