a la norme et sans leaks, manque le parsing
This commit is contained in:
37
srcs/draw.c
37
srcs/draw.c
@@ -2,14 +2,14 @@
|
||||
|
||||
void draw_image(t_fdf *fdf)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
fdf->rad_x = fdf->rot_x * M_PI / 180;
|
||||
fdf->rad_y = fdf->rot_y * M_PI / 180;
|
||||
i =-1;
|
||||
i = -1;
|
||||
while (++i < fdf->img_size_y * fdf->img_sizel)
|
||||
*(unsigned int*)(fdf->img_addr + i) = 0;
|
||||
*(unsigned int *)(fdf->img_addr + i) = 0;
|
||||
j = -1;
|
||||
while (++j < fdf->map_height)
|
||||
{
|
||||
@@ -23,8 +23,8 @@ void draw_image(t_fdf *fdf)
|
||||
|
||||
void draw_grid(t_fdf *fdf, int i, int j)
|
||||
{
|
||||
int *point_start;
|
||||
int *point_end;
|
||||
int *point_start;
|
||||
int *point_end;
|
||||
|
||||
point_start = new_coordinates(fdf, i, j);
|
||||
point_end = NULL;
|
||||
@@ -32,17 +32,22 @@ void draw_grid(t_fdf *fdf, int i, int j)
|
||||
point_end = new_coordinates(fdf, i + 1, j);
|
||||
draw_lines(fdf, point_start, point_end);
|
||||
if (j + 1 < fdf->map_height)
|
||||
{
|
||||
free(point_end);
|
||||
point_end = new_coordinates(fdf, i, j + 1);
|
||||
}
|
||||
draw_lines(fdf, point_start, point_end);
|
||||
free(point_start);
|
||||
free(point_end);
|
||||
}
|
||||
|
||||
void draw_lines(t_fdf *fdf, int *start, int *end)
|
||||
{
|
||||
int dx;
|
||||
int dy;
|
||||
int z;
|
||||
int i;
|
||||
int j;
|
||||
int dx;
|
||||
int dy;
|
||||
int z;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
if (end)
|
||||
{
|
||||
@@ -66,7 +71,7 @@ void draw_lines(t_fdf *fdf, int *start, int *end)
|
||||
|
||||
void draw_color_pixel(t_fdf *fdf, int new_x, int new_y, int z)
|
||||
{
|
||||
int color;
|
||||
int color;
|
||||
|
||||
color = 0xffffff;
|
||||
z -= (z * fdf->zoom) / fdf->offset;
|
||||
@@ -81,14 +86,14 @@ void draw_color_pixel(t_fdf *fdf, int new_x, int new_y, int z)
|
||||
|
||||
void draw_pixel(t_fdf *fdf, int x, int y, int color)
|
||||
{
|
||||
int position;
|
||||
int xmax;
|
||||
int ymax;
|
||||
int position;
|
||||
int xmax;
|
||||
int ymax;
|
||||
|
||||
xmax = fdf->img_sizel / (fdf->img_bpp / 8);
|
||||
ymax = fdf->img_size_y;
|
||||
if (x < 0 || y < 0 || x > xmax || y > ymax)
|
||||
return ;
|
||||
position = y * fdf->img_sizel + x * fdf->img_bpp / 8;
|
||||
*(unsigned int*)(fdf->img_addr + position) = color;
|
||||
*(unsigned int *)(fdf->img_addr + position) = color;
|
||||
}
|
||||
|
||||
32
srcs/fdf.c
32
srcs/fdf.c
@@ -1,22 +1,25 @@
|
||||
#include "fdf.h"
|
||||
|
||||
int shut_down(t_fdf *fdf)
|
||||
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);
|
||||
exit(0);
|
||||
mlx_destroy_display(fdf->mlx_ptr);
|
||||
free(fdf);
|
||||
exit(0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
t_fdf *init_fdf(void)
|
||||
t_fdf *init_fdf(t_fdf *fdf)
|
||||
{
|
||||
t_fdf *fdf;
|
||||
|
||||
fdf = malloc(sizeof(t_fdf));
|
||||
fdf->offset = 50;
|
||||
fdf->margin = 50;
|
||||
fdf->altitude = 3;
|
||||
fdf->map = parse_map(fdf);
|
||||
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;
|
||||
@@ -28,23 +31,22 @@ t_fdf *init_fdf(void)
|
||||
fdf->zoom = 0;
|
||||
fdf->mlx_ptr = mlx_init();
|
||||
fdf->win_ptr = mlx_new_window(fdf->mlx_ptr, fdf->win_size_x,
|
||||
fdf->win_size_y, "test");
|
||||
fdf->win_size_y, "test");
|
||||
fdf->img_ptr = mlx_new_image(fdf->mlx_ptr, fdf->img_size_x,
|
||||
fdf->img_size_y);
|
||||
fdf->img_size_y);
|
||||
fdf->img_addr = mlx_get_data_addr(fdf->img_ptr, &(fdf->img_bpp),
|
||||
&(fdf->img_sizel), &(fdf->img_endian));
|
||||
&(fdf->img_sizel), &(fdf->img_endian));
|
||||
draw_image(fdf);
|
||||
return (fdf);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
int main(void)
|
||||
{
|
||||
t_fdf *fdf;
|
||||
|
||||
(void)av;
|
||||
(void)ac;
|
||||
|
||||
fdf = init_fdf();
|
||||
fdf = malloc(sizeof(t_fdf));
|
||||
fdf->map = parse_map(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);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "fdf.h"
|
||||
|
||||
// add "print_keycode(keycode);" at begining to print keycode
|
||||
int keypress(int keycode, t_fdf *fdf)
|
||||
int keypress(int keycode, t_fdf *fdf)
|
||||
{
|
||||
if (keycode == ESCAPE)
|
||||
shut_down(fdf);
|
||||
@@ -32,43 +32,36 @@ int keypress(int keycode, t_fdf *fdf)
|
||||
// return an int[3] : int[0] = x, int[1] = y, int[2] = z
|
||||
int *new_coordinates(t_fdf *fdf, int i, int j)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
int z;
|
||||
int x;
|
||||
int y;
|
||||
int z;
|
||||
int *point;
|
||||
|
||||
point = ft_calloc(3, sizeof(int));
|
||||
// pre zoom
|
||||
x = i * (fdf->offset + fdf->zoom);
|
||||
y = j * (fdf->offset + fdf->zoom);
|
||||
// pre center
|
||||
x -= (fdf->map_size_x + fdf->zoom * fdf->map_width) / 2;
|
||||
y -= (fdf->map_size_y + fdf->zoom * fdf->map_height) / 2;
|
||||
// rotation
|
||||
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);
|
||||
// deplacements
|
||||
point[0] += fdf->margin + fdf->mov_x;
|
||||
point[1] += fdf->margin + fdf->mov_y;
|
||||
// post zoom
|
||||
point[0] -= ((fdf->map_size_x / 2) / fdf->offset) * fdf->zoom;
|
||||
point[1] -= ((fdf->map_size_y / 2) / fdf->offset) * fdf->zoom;
|
||||
// post center
|
||||
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)
|
||||
int print_keycode(int keycode)
|
||||
{
|
||||
ft_putnbr(keycode);
|
||||
ft_putchar(' ');
|
||||
return(0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void rotation_state(t_fdf *fdf)
|
||||
@@ -79,8 +72,9 @@ void rotation_state(t_fdf *fdf)
|
||||
|
||||
x = fdf->img_size_x - 10;
|
||||
y = fdf->img_size_y - 10;
|
||||
position = ft_strjoin(ft_itoa(fdf->rot_x), ft_strdup(" | "));
|
||||
position = ft_strjoin(position, ft_itoa(fdf->rot_y));
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -31,10 +31,7 @@ int **parse_map(t_fdf *fdf)
|
||||
map[9][13] = 3;
|
||||
map[9][14] = 6;
|
||||
fdf->max_z = 10;
|
||||
// size map
|
||||
fdf->map_width = j;
|
||||
fdf->map_height = i;
|
||||
fdf->map_size_x = --j * fdf->offset + 1;
|
||||
fdf->map_size_y = --i * fdf->offset + 1;
|
||||
return (map);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user