Files
42_INT_04_fdf/srcs/fdf.c
2021-07-18 15:31:44 +02:00

191 lines
4.0 KiB
C

#include "fdf.h"
t_map *init_map(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);
void draw_image(t_map *map)
{
int x;
int y;
y = 0;
while (y < map->img_size_y)
{
x = 0;
while ((x * map->img_bpp / 8) < map->img_sizel)
{
draw_pixel(map, x, y, 0xffffff);
x += 4;
}
y += 4;
}
// put image on screen
mlx_put_image_to_window(map->mlx_ptr, map->win_ptr, map->img_ptr, 0, 0);
// put rotation on screen
rotation_state(map);
}
int main(int ac, char **av)
{
t_map *map;
(void)av;
(void)ac;
map = init_map();
// receive a keypress event
mlx_hook(map->win_ptr, 2, 1L << 0, keypress, map);
// receive event when clicking the button to close the window
mlx_hook(map->win_ptr, 17, 1L << 17, shut_down, map);
// run window when no events occurs
mlx_loop(map->mlx_ptr);
return (0);
}
int keypress(int keycode, t_map *map)
{
if (keycode == ESCAPE)
shut_down(map);
else if (keycode == LEFT)
(map->rot_x) -= 1;
else if (keycode == RIGHT)
(map->rot_x) += 1;
else if (keycode == UP)
(map->rot_y) += 1;
else if (keycode == DOWN)
(map->rot_y) -= 1;
else
print_keycode(keycode);
// draw image
draw_image(map);
return (0);
}
int print_keycode(int keycode)
{
ft_putnbr(keycode);
ft_putchar(' ');
return(0);
}
void draw_pixel(t_map *map, 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;
}
void rotation_state(t_map *map)
{
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 -= ft_strlen(position) * 6;
mlx_string_put(map->mlx_ptr, map->win_ptr, x, y, 0xffffff, position);
}
t_map *init_map(void)
{
t_map *map;
map = malloc(sizeof(t_map));
// size window
map->win_size_x = 500;
map->win_size_y = 500;
// size image
map->img_size_x = map->win_size_x;
map->img_size_y = map->win_size_y;
// view rotation
map->rot_x = 0;
map->rot_y = 0;
// init connexion to server
map->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");
// 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));
// draw image
draw_image(map);
return (map);
}
int shut_down(t_map *map)
{
mlx_destroy_window(map->mlx_ptr, map->win_ptr);
exit(0);
free(map);
return (0);
}
/*
** w forward 119
** a left 97
** s backward 115
** d right 100
** < 65361
** > 65363
** v 65364
** ^ 65362
** esc 65307
**
**
**
**
**
**
**
**
**
** // x_event | x_mask | action
** // 2 | 1L << 0 | key press
** // 3 | 1L << 1 | key release
** // 4 | | mouse press
** // 5 | | mouse release
** // 6 | | mouse move
** // 12 | | expose event
** // 17 | 1L << 17 | x button press (red button)
** // | |
**
**
**
** FONCTIONS EXTERNES AUTORISEES :
** . open
** . close
** . read
** . write
** . malloc
** . free
** . perror
** . strerror
** . exit
** . math lib :
** -lm // needed at compilation to link the lib :
** gcc foo.c -o foo -lm
** man
** man 3 math
** . minilibx :
** minilibx_opengl.tgz
** minilibx_mms_20200219_beta.tgz
** // to open an archive.tgz :
** gzip -d archive.tgz --> turn it into archive.tar
** tar -xf archive.tar --> un-archive it
** // how to add a man directory to the manual :
** . cp man/man1 /usr/local/share/man/man1
** (create man1 if necessary)
** . mandb
** // i didn't use any of both library above but the one for linux :
** https://github.com/42Paris/minilibx-linux
** there are pbm with their man pages
*/