en cours ecriture drawing

This commit is contained in:
hugogogo
2021-07-18 17:38:28 +02:00
parent 3bef66f8ec
commit ab39114203
4 changed files with 88 additions and 55 deletions

Binary file not shown.

BIN
fdf

Binary file not shown.

View File

@@ -11,23 +11,28 @@
# define LEFT 65361
# define RIGHT 65363
typedef struct s_map
typedef struct s_fdf
{
void *mlx_ptr;
void *win_ptr;
void *img_ptr;
char *img_addr;
int **map;
int offset;
int margin;
int win_size_x;
int win_size_y;
int img_size_x;
int img_size_y;
int map_size_x;
int map_size_y;
int map_length;
int map_height;
int rot_x;
int rot_y;
int img_bpp;
int img_sizel;
int img_endian;
int rot_x;
int rot_y;
} t_map;
} t_fdf;
#endif

View File

@@ -1,67 +1,68 @@
#include "fdf.h"
t_map *init_map(void);
t_fdf *init_fdf(void);
int print_keycode(int keycode);
int shut_down(t_map *map);
void draw_pixel(t_map *map, int x, int y, int color);
int keypress(int keycode, t_map *map);
void rotation_state(t_map *map);
int shut_down(t_fdf *fdf);
void draw_pixel(t_fdf *fdf, int x, int y, int color);
int keypress(int keycode, t_fdf *fdf);
void rotation_state(t_fdf *fdf);
int **parse_map(t_fdf *fdf);
void draw_image(t_map *map)
void draw_image(t_fdf *fdf)
{
int x;
int y;
y = 0;
while (y < map->img_size_y)
while (y < fdf->img_size_y)
{
x = 0;
while ((x * map->img_bpp / 8) < map->img_sizel)
while ((x * fdf->img_bpp / 8) < fdf->img_sizel)
{
draw_pixel(map, x, y, 0xffffff);
x += 4;
draw_pixel(fdf, x, y, 0xffffff);
x += fdf->offset;
}
y += 4;
y += fdf->offset;
}
// put image on screen
mlx_put_image_to_window(map->mlx_ptr, map->win_ptr, map->img_ptr, 0, 0);
mlx_put_image_to_window(fdf->mlx_ptr, fdf->win_ptr, fdf->img_ptr, 0, 0);
// put rotation on screen
rotation_state(map);
rotation_state(fdf);
}
int main(int ac, char **av)
{
t_map *map;
t_fdf *fdf;
(void)av;
(void)ac;
map = init_map();
fdf = init_fdf();
// receive a keypress event
mlx_hook(map->win_ptr, 2, 1L << 0, keypress, map);
mlx_hook(fdf->win_ptr, 2, 1L << 0, keypress, fdf);
// receive event when clicking the button to close the window
mlx_hook(map->win_ptr, 17, 1L << 17, shut_down, map);
mlx_hook(fdf->win_ptr, 17, 1L << 17, shut_down, fdf);
// run window when no events occurs
mlx_loop(map->mlx_ptr);
mlx_loop(fdf->mlx_ptr);
return (0);
}
int keypress(int keycode, t_map *map)
int keypress(int keycode, t_fdf *fdf)
{
if (keycode == ESCAPE)
shut_down(map);
shut_down(fdf);
else if (keycode == LEFT)
(map->rot_x) -= 1;
(fdf->rot_x) -= 1;
else if (keycode == RIGHT)
(map->rot_x) += 1;
(fdf->rot_x) += 1;
else if (keycode == UP)
(map->rot_y) += 1;
(fdf->rot_y) += 1;
else if (keycode == DOWN)
(map->rot_y) -= 1;
(fdf->rot_y) -= 1;
else
print_keycode(keycode);
// draw image
draw_image(map);
draw_image(fdf);
return (0);
}
@@ -72,59 +73,86 @@ int print_keycode(int keycode)
return(0);
}
void draw_pixel(t_map *map, int x, int y, int color)
void draw_pixel(t_fdf *fdf, int x, int y, int color)
{
int position;
position = y * map->img_sizel + x * map->img_bpp / 8;
*(unsigned int*)(map->img_addr + position) = color;
position = y * fdf->img_sizel + x * fdf->img_bpp / 8;
*(unsigned int*)(fdf->img_addr + position) = color;
}
void rotation_state(t_map *map)
void rotation_state(t_fdf *fdf)
{
char *position;
int x;
int y;
x = map->img_size_x - 10;
y = map->img_size_y - 10;
position = ft_strjoin(ft_itoa(map->rot_x), " | ");
position = ft_strjoin(position, ft_itoa(map->rot_y));
x = fdf->img_size_x - 10;
y = fdf->img_size_y - 10;
position = ft_strjoin(ft_itoa(fdf->rot_x), " | ");
position = ft_strjoin(position, ft_itoa(fdf->rot_y));
x -= ft_strlen(position) * 6;
mlx_string_put(map->mlx_ptr, map->win_ptr, x, y, 0xffffff, position);
mlx_string_put(fdf->mlx_ptr, fdf->win_ptr, x, y, 0xffffff, position);
}
t_map *init_map(void)
t_fdf *init_fdf(void)
{
t_map *map;
t_fdf *fdf;
map = malloc(sizeof(t_map));
fdf = malloc(sizeof(t_fdf));
// map offset and margin
fdf->offset = 10;
fdf->margin = 50;
// parse map
fdf->map = parse_map(fdf);
// size window
map->win_size_x = 500;
map->win_size_y = 500;
fdf->win_size_x = fdf->map_size_x + 2 * fdf->margin;
fdf->win_size_y = fdf->map_size_y + 2 * fdf->margin;
// size image
map->img_size_x = map->win_size_x;
map->img_size_y = map->win_size_y;
fdf->img_size_x = fdf->win_size_x;
fdf->img_size_y = fdf->win_size_y;
// view rotation
map->rot_x = 0;
map->rot_y = 0;
fdf->rot_x = 0;
fdf->rot_y = 0;
// init connexion to server
map->mlx_ptr = mlx_init();
fdf->mlx_ptr = mlx_init();
// create the window
map->win_ptr = mlx_new_window(map->mlx_ptr, map->win_size_x, map->win_size_y, "test");
fdf->win_ptr = mlx_new_window(fdf->mlx_ptr, fdf->win_size_x, fdf->win_size_y, "test");
// create image
map->img_ptr = mlx_new_image(map->mlx_ptr, map->img_size_x, map->img_size_y);
map->img_addr = mlx_get_data_addr(map->img_ptr, &(map->img_bpp), &(map->img_sizel), &(map->img_endian));
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));
// draw image
draw_image(map);
draw_image(fdf);
return (fdf);
}
int **parse_map(t_fdf *fdf)
{
int **map;
int i;
int j;
map = ft_calloc(10, sizeof(map));
i = -1;
while (++i < 10)
{
map[i] = ft_calloc(15, sizeof(*map));
j = -1;
while(++j < 15)
map[i][j] = 0;
}
map[3][6] = 10;
// size map
fdf->map_size_x = --j * fdf->offset + 1;
fdf->map_size_y = --i * fdf->offset + 1;
return (map);
}
int shut_down(t_map *map)
int shut_down(t_fdf *fdf)
{
mlx_destroy_window(map->mlx_ptr, map->win_ptr);
mlx_destroy_window(fdf->mlx_ptr, fdf->win_ptr);
exit(0);
free(map);
free(fdf);
return (0);
}