map fits window

This commit is contained in:
hugogogo
2021-07-25 12:51:15 +02:00
parent 4d3fc2779c
commit c06489fd99
13 changed files with 152 additions and 73 deletions
+5 -3
View File
@@ -20,7 +20,8 @@ LIBS = $(_LIBS:lib%.a=%)
SRCS = fdf.c \
draw.c \
parse.c \
modifs.c
modifs.c \
keypress.c
ODIR = ./builds
OBJS = $(SRCS:%.c=$(ODIR)/%.o)
@@ -55,8 +56,9 @@ $(ODIR)/%.o: %.c
debug: CFLAGS += -fsanitize=address
debug: clean $(NAME)
leaks: clean $(NAME)
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME)
leaks: $(NAME)
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) maps/42_color.fdf
# valgrind --leak-check=full --leak-resolution=low --show-reachable=yes ./$(NAME) maps/42_color.fdf
clean:
/bin/rm -f $(OBJS)
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
Executable
BIN
View File
Binary file not shown.
+4 -2
View File
@@ -16,7 +16,6 @@ typedef struct s_fdf
char *img_addr;
int **map;
int offset;
int margin;
int min_z;
int max_z;
int z_amplitude;
@@ -41,13 +40,16 @@ typedef struct s_fdf
int img_endian;
} t_fdf;
t_fdf *init_fdf(t_fdf *fdf);
void init_fdf(t_fdf *fdf);
void init_offset(t_fdf *fdf);
void init_server(t_fdf *fdf);
int print_keycode(int keycode);
int shut_down(t_fdf *fdf);
void draw_image(t_fdf *fdf);
void draw_pixel(t_fdf *fdf, int x, int y, int color);
void draw_color_pixel(t_fdf *fdf, int new_x, int new_y, int z);
int keypress(int keycode, t_fdf *fdf);
void keypress_more(int keycode, t_fdf *fdf);
void position_state(t_fdf *fdf);
int **parse_map(t_fdf *fdf, int fd);
int *new_coordinates(t_fdf *fdf, int i, int j);
+1 -1
Submodule libft updated: 60ebf29333...1f234c4e58
+66 -28
View File
@@ -2,43 +2,79 @@
int shut_down(t_fdf *fdf)
{
while (fdf->map_height--)
free(fdf->map[fdf->map_height]);
free(fdf->map);
mlx_destroy_image(fdf->mlx_ptr, fdf->img_ptr);
mlx_destroy_window(fdf->mlx_ptr, fdf->win_ptr);
mlx_destroy_display(fdf->mlx_ptr);
free(fdf);
if (fdf->map)
{
while (fdf->map_height-- && fdf->map[fdf->map_height])
free(fdf->map[fdf->map_height]);
free(fdf->map);
}
if (fdf->img_ptr)
mlx_destroy_image(fdf->mlx_ptr, fdf->img_ptr);
if (fdf->win_ptr)
mlx_destroy_window(fdf->mlx_ptr, fdf->win_ptr);
if (fdf->mlx_ptr)
mlx_destroy_display(fdf->mlx_ptr);
if (fdf)
free(fdf);
exit(0);
return (0);
}
t_fdf *init_fdf(t_fdf *fdf)
void init_server(t_fdf *fdf)
{
fdf->offset = 50;
fdf->margin = 50;
fdf->mlx_ptr = mlx_init();
if (!fdf->mlx_ptr)
shut_down(fdf);
fdf->win_ptr = mlx_new_window(fdf->mlx_ptr, fdf->win_size_x,
fdf->win_size_y, "test");
if (!fdf->win_ptr)
shut_down(fdf);
fdf->img_ptr = mlx_new_image(fdf->mlx_ptr, fdf->img_size_x,
fdf->img_size_y);
if (!fdf->img_ptr)
shut_down(fdf);
fdf->img_addr = mlx_get_data_addr(fdf->img_ptr, &(fdf->img_bpp),
&(fdf->img_sizel), &(fdf->img_endian));
if (!fdf->img_addr)
shut_down(fdf);
}
void init_offset(t_fdf *fdf)
{
int diagonal;
int x;
x = (fdf->map_width) * (fdf->map_width);
x += (fdf->map_height) * (fdf->map_height);
fdf->offset = 1;
diagonal = 1;
while (diagonal < fdf->win_size_x)
{
diagonal = x * (fdf->offset * fdf->offset);
diagonal = ft_sqrt(diagonal);
fdf->offset++;
}
fdf->offset--;
}
void init_fdf(t_fdf *fdf)
{
fdf->win_size_x = 700;
fdf->win_size_y = 700;
fdf->img_size_x = fdf->win_size_x;
fdf->img_size_y = fdf->win_size_y;
init_offset(fdf);
fdf->altitude = 3;
fdf->z_amplitude = fdf->max_z - fdf->min_z;
fdf->map_size_x = (fdf->map_width - 1) * fdf->offset + 1;
fdf->map_size_y = (fdf->map_height - 1) * fdf->offset + 1;
fdf->win_size_x = fdf->map_size_x + 2 * fdf->margin;
fdf->win_size_y = fdf->map_size_y + 2 * fdf->margin;
fdf->img_size_x = fdf->win_size_x;
fdf->img_size_y = fdf->win_size_y;
fdf->rot_x = -30;
fdf->rot_y = -60;
fdf->mov_x = 0;
fdf->mov_y = 0;
fdf->zoom = -5;
fdf->mlx_ptr = mlx_init();
fdf->win_ptr = mlx_new_window(fdf->mlx_ptr, fdf->win_size_x,
fdf->win_size_y, "test");
fdf->img_ptr = mlx_new_image(fdf->mlx_ptr, fdf->img_size_x,
fdf->img_size_y);
fdf->img_addr = mlx_get_data_addr(fdf->img_ptr, &(fdf->img_bpp),
&(fdf->img_sizel), &(fdf->img_endian));
fdf->rot_x = -45;
fdf->rot_y = -35;
fdf->mov_x = (fdf->win_size_x - fdf->map_size_x) / 2;
fdf->mov_y = (fdf->win_size_y - fdf->map_size_y) / 2;
fdf->zoom = 0;
init_server(fdf);
draw_image(fdf);
return (fdf);
}
int main(int ac, char **av)
@@ -50,8 +86,10 @@ int main(int ac, char **av)
return (0);
fdf = malloc(sizeof(t_fdf));
fd = open(av[1], O_RDONLY);
if (!fd || !fdf)
shut_down(fdf);
fdf->map = parse_map(fdf, fd);
fdf = init_fdf(fdf);
init_fdf(fdf);
mlx_hook(fdf->win_ptr, 2, 1L << 0, keypress, fdf);
mlx_hook(fdf->win_ptr, 17, 1L << 17, shut_down, fdf);
mlx_loop(fdf->mlx_ptr);
+60
View File
@@ -0,0 +1,60 @@
#include "fdf.h"
/*
** U -> up wiew
** I -> isometric view
** F -> fit to window
** C -> center
*/
void keypress_more(int keycode, t_fdf *fdf)
{
if (keycode == ESCAPE)
shut_down(fdf);
if (keycode == U)
{
fdf->rot_x = 0;
fdf->rot_y = 0;
}
else if (keycode == I)
{
fdf->rot_x = -30;
fdf->rot_y = -60;
}
}
/*
** Q -> move left
** D -> move right
** Z -> move up
** S -> move down
** A -> zoom
** W -> unzoom
*/
int keypress(int keycode, t_fdf *fdf)
{
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;
else
keypress_more(keycode, fdf);
draw_image(fdf);
return (0);
}
+12 -39
View File
@@ -1,34 +1,5 @@
#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 :
@@ -39,7 +10,7 @@ int keypress(int keycode, t_fdf *fdf)
** z changes according to zoom :
** z += (z * fdf->zoom) / fdf->offset;
** deplacement :
** point[0] += fdf->margin + fdf->mov_x;
** point[0] += fdf->mov_x;
** center zoom :
** point[0] -= ((fdf->map_size_x / 2) / fdf->offset) * fdf->zoom;
** center after 3d rotation :
@@ -62,8 +33,8 @@ int *new_coordinates(t_fdf *fdf, int i, int j)
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->mov_x;
point[1] += 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;
@@ -72,13 +43,6 @@ int *new_coordinates(t_fdf *fdf, int i, int j)
return (point);
}
int print_keycode(int keycode)
{
ft_putnbr(keycode);
ft_putchar(' ');
return (0);
}
void position_state(t_fdf *fdf)
{
char *position;
@@ -101,3 +65,12 @@ void position_state(t_fdf *fdf)
mlx_string_put(fdf->mlx_ptr, fdf->win_ptr, x, y, 0xffffff, position);
free(position);
}
/*
** int print_keycode(int keycode)
** {
** ft_putnbr(keycode);
** ft_putchar(' ');
** return (0);
** }
*/
+4
View File
@@ -15,10 +15,14 @@ int **split_to_map(t_fdf *fdf, char *raw)
int **map;
map = ft_calloc(fdf->map_height, sizeof(map));
if (!map)
shut_down(fdf);
i = -1;
while (++i < fdf->map_height)
{
map[i] = ft_calloc(fdf->map_width, sizeof(*map));
if (!map[i])
shut_down(fdf);
j = -1;
while (++j < fdf->map_width)
{