affichage pas ouf car une seule image et erreur dans la size de l'image
This commit is contained in:
238
:w:w
Normal file
238
:w:w
Normal file
@@ -0,0 +1,238 @@
|
||||
#include "fdf.h"
|
||||
|
||||
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);
|
||||
|
||||
// to be exact, use :
|
||||
// fdf->img_sizel / (fdf->img_bpp / 8)
|
||||
// instead of :
|
||||
// fdf->img_size_x
|
||||
void draw_image(t_fdf *fdf)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int x;
|
||||
int y;
|
||||
int z;
|
||||
int new_x;
|
||||
int new_y;
|
||||
|
||||
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];
|
||||
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);
|
||||
draw_pixel(fdf, new_x, new_y, 0xfff);
|
||||
}
|
||||
}
|
||||
// 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
|
||||
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;
|
||||
|
||||
if (x < fdf->map_size_x && y < fdf->map_size_y)
|
||||
{
|
||||
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), " | ");
|
||||
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;
|
||||
// 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
|
||||
*/
|
||||
BIN
builds/fdf.o
BIN
builds/fdf.o
Binary file not shown.
@@ -4,6 +4,7 @@
|
||||
# include "../libft/includes/libft.h"
|
||||
# include <mlx.h>
|
||||
# include <unistd.h> // for sleep()
|
||||
# include <math.h> // for M_PI
|
||||
|
||||
# define ESCAPE 65307
|
||||
# define UP 65362
|
||||
@@ -11,28 +12,30 @@
|
||||
# define LEFT 65361
|
||||
# define RIGHT 65363
|
||||
|
||||
typedef struct s_fdf
|
||||
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;
|
||||
} t_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;
|
||||
double rad_x;
|
||||
double rad_y;
|
||||
int img_bpp;
|
||||
int img_sizel;
|
||||
int img_endian;
|
||||
} t_fdf;
|
||||
|
||||
#endif
|
||||
|
||||
43
srcs/fdf.c
43
srcs/fdf.c
@@ -1,4 +1,5 @@
|
||||
#include "fdf.h"
|
||||
#include <stdio.h>
|
||||
|
||||
t_fdf *init_fdf(void);
|
||||
int print_keycode(int keycode);
|
||||
@@ -10,19 +11,36 @@ int **parse_map(t_fdf *fdf);
|
||||
|
||||
void draw_image(t_fdf *fdf)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
int i;
|
||||
int j;
|
||||
int x;
|
||||
int y;
|
||||
int z;
|
||||
int new_x;
|
||||
int new_y;
|
||||
|
||||
y = 0;
|
||||
while (y < fdf->img_size_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)
|
||||
{
|
||||
x = 0;
|
||||
while ((x * fdf->img_bpp / 8) < fdf->img_sizel)
|
||||
i = -1;
|
||||
while (++i < fdf->map_size_x)
|
||||
{
|
||||
draw_pixel(fdf, x, y, 0xffffff);
|
||||
x += fdf->offset;
|
||||
|
||||
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 += 100;
|
||||
draw_pixel(fdf, new_x, new_y, 0xffffff);
|
||||
}
|
||||
y += fdf->offset;
|
||||
}
|
||||
// put image on screen
|
||||
mlx_put_image_to_window(fdf->mlx_ptr, fdf->win_ptr, fdf->img_ptr, 0, 0);
|
||||
@@ -61,6 +79,9 @@ int keypress(int keycode, t_fdf *fdf)
|
||||
(fdf->rot_y) -= 1;
|
||||
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);
|
||||
@@ -77,6 +98,8 @@ void draw_pixel(t_fdf *fdf, int x, int y, int color)
|
||||
{
|
||||
int position;
|
||||
|
||||
if (x < 0 || y < 0 || x > fdf->img_sizel || y > fdf->map_size_y)
|
||||
return ;
|
||||
position = y * fdf->img_sizel + x * fdf->img_bpp / 8;
|
||||
*(unsigned int*)(fdf->img_addr + position) = color;
|
||||
}
|
||||
@@ -89,7 +112,7 @@ 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), " | ");
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user