#include "fdf.h" #include t_fdf *init_fdf(void); int print_keycode(int keycode); 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_fdf *fdf) { int i; int j; int x; int y; int z; int new_x; int new_y; // init image with 0 i =-1; while (++i < fdf->img_size_y * fdf->img_sizel) *(unsigned int*)(fdf->img_addr + i) = 0; // draw image j = -1; while (++j < fdf->map_size_y) { i = -1; while (++i < fdf->map_size_x) { x = i * fdf->offset; y = j * fdf->offset; // z = fdf->map[j][i]; z = 1; new_x = x * cos(fdf->rad_y) + y * sin(fdf->rad_y); new_y = y * cos(fdf->rad_y) - x * sin(fdf->rad_y); new_y = new_y * cos(fdf->rad_x) - z * sin(fdf->rad_x); new_x += fdf->margin + fdf->mov_x; new_y += fdf->margin + fdf->mov_y; draw_pixel(fdf, new_x, new_y, 0xffffff); } } // put image on screen mlx_put_image_to_window(fdf->mlx_ptr, fdf->win_ptr, fdf->img_ptr, 0, 0); // put rotation on screen rotation_state(fdf); } int main(int ac, char **av) { t_fdf *fdf; (void)av; (void)ac; fdf = init_fdf(); // receive a keypress event mlx_hook(fdf->win_ptr, 2, 1L << 0, keypress, fdf); // receive event when clicking the button to close the window mlx_hook(fdf->win_ptr, 17, 1L << 17, shut_down, fdf); // run window when no events occurs mlx_loop(fdf->mlx_ptr); return (0); } 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 print_keycode(keycode); // calculate radians fdf->rad_x = fdf->rot_x * M_PI / 180; fdf->rad_y = fdf->rot_y * M_PI / 180; // draw image draw_image(fdf); return (0); } int print_keycode(int keycode) { ft_putnbr(keycode); ft_putchar(' '); return(0); } void draw_pixel(t_fdf *fdf, int x, int y, int color) { int position; int xmax; int ymax; xmax = fdf->img_sizel / (fdf->img_bpp / 8); ymax = fdf->map_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; } void rotation_state(t_fdf *fdf) { char *position; int x; int y; 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)); x -= ft_strlen(position) * 6; mlx_string_put(fdf->mlx_ptr, fdf->win_ptr, x, y, 0xffffff, position); } t_fdf *init_fdf(void) { t_fdf *fdf; fdf = malloc(sizeof(t_fdf)); // map offset and margin fdf->offset = 10; fdf->margin = 50; // parse map fdf->map = parse_map(fdf); // size window fdf->win_size_x = fdf->map_size_x + 2 * fdf->margin; fdf->win_size_y = fdf->map_size_y + 2 * fdf->margin; // size image fdf->img_size_x = fdf->win_size_x; fdf->img_size_y = fdf->win_size_y; // view rotation fdf->rot_x = 0; fdf->rot_y = 0; // x and y deplacements fdf->mov_x = 0; fdf->mov_y = 0; // init connexion to server fdf->mlx_ptr = mlx_init(); // create the window fdf->win_ptr = mlx_new_window(fdf->mlx_ptr, fdf->win_size_x, fdf->win_size_y, "test"); // create image 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(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_fdf *fdf) { mlx_destroy_window(fdf->mlx_ptr, fdf->win_ptr); exit(0); free(fdf); 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 */