drawpixel fonctionne pour la limite horizontale mais pas verticale, et implementation macro lettres + mouvements
This commit is contained in:
238
:w:w
238
:w:w
@@ -1,238 +0,0 @@
|
|||||||
#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.
@@ -11,6 +11,32 @@
|
|||||||
# define DOWN 65364
|
# define DOWN 65364
|
||||||
# define LEFT 65361
|
# define LEFT 65361
|
||||||
# define RIGHT 65363
|
# define RIGHT 65363
|
||||||
|
# define A 97
|
||||||
|
# define B 98
|
||||||
|
# define C 99
|
||||||
|
# define D 100
|
||||||
|
# define E 101
|
||||||
|
# define F 102
|
||||||
|
# define G 103
|
||||||
|
# define H 104
|
||||||
|
# define I 105
|
||||||
|
# define J 106
|
||||||
|
# define K 107
|
||||||
|
# define L 108
|
||||||
|
# define M 109
|
||||||
|
# define N 110
|
||||||
|
# define O 111
|
||||||
|
# define P 112
|
||||||
|
# define Q 113
|
||||||
|
# define R 114
|
||||||
|
# define S 115
|
||||||
|
# define T 116
|
||||||
|
# define U 117
|
||||||
|
# define V 118
|
||||||
|
# define W 119
|
||||||
|
# define X 120
|
||||||
|
# define Y 121
|
||||||
|
# define Z 122
|
||||||
|
|
||||||
typedef struct s_fdf
|
typedef struct s_fdf
|
||||||
{
|
{
|
||||||
@@ -33,6 +59,8 @@ typedef struct s_fdf
|
|||||||
int rot_y;
|
int rot_y;
|
||||||
double rad_x;
|
double rad_x;
|
||||||
double rad_y;
|
double rad_y;
|
||||||
|
int mov_x;
|
||||||
|
int mov_y;
|
||||||
int img_bpp;
|
int img_bpp;
|
||||||
int img_sizel;
|
int img_sizel;
|
||||||
int img_endian;
|
int img_endian;
|
||||||
|
|||||||
20
srcs/fdf.c
20
srcs/fdf.c
@@ -38,7 +38,8 @@ void draw_image(t_fdf *fdf)
|
|||||||
new_x = x * cos(fdf->rad_y) + y * sin(fdf->rad_y);
|
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 = y * cos(fdf->rad_y) - x * sin(fdf->rad_y);
|
||||||
new_y = new_y * cos(fdf->rad_x) - z * sin(fdf->rad_x);
|
new_y = new_y * cos(fdf->rad_x) - z * sin(fdf->rad_x);
|
||||||
new_x += 100;
|
new_x += fdf->margin + fdf->mov_x;
|
||||||
|
new_y += fdf->margin + fdf->mov_y;
|
||||||
draw_pixel(fdf, new_x, new_y, 0xffffff);
|
draw_pixel(fdf, new_x, new_y, 0xffffff);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -77,6 +78,14 @@ int keypress(int keycode, t_fdf *fdf)
|
|||||||
(fdf->rot_y) += 1;
|
(fdf->rot_y) += 1;
|
||||||
else if (keycode == DOWN)
|
else if (keycode == DOWN)
|
||||||
(fdf->rot_y) -= 1;
|
(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
|
else
|
||||||
print_keycode(keycode);
|
print_keycode(keycode);
|
||||||
// calculate radians
|
// calculate radians
|
||||||
@@ -97,8 +106,12 @@ int print_keycode(int keycode)
|
|||||||
void draw_pixel(t_fdf *fdf, int x, int y, int color)
|
void draw_pixel(t_fdf *fdf, int x, int y, int color)
|
||||||
{
|
{
|
||||||
int position;
|
int position;
|
||||||
|
int xmax;
|
||||||
|
int ymax;
|
||||||
|
|
||||||
if (x < 0 || y < 0 || x > fdf->img_sizel || y > fdf->map_size_y)
|
xmax = fdf->img_sizel / (fdf->img_bpp / 8);
|
||||||
|
ymax = fdf->map_size_y;
|
||||||
|
if (x < 0 || y < 0 || x > xmax || y > ymax)
|
||||||
return ;
|
return ;
|
||||||
position = y * fdf->img_sizel + x * fdf->img_bpp / 8;
|
position = y * fdf->img_sizel + x * fdf->img_bpp / 8;
|
||||||
*(unsigned int*)(fdf->img_addr + position) = color;
|
*(unsigned int*)(fdf->img_addr + position) = color;
|
||||||
@@ -137,6 +150,9 @@ t_fdf *init_fdf(void)
|
|||||||
// view rotation
|
// view rotation
|
||||||
fdf->rot_x = 0;
|
fdf->rot_x = 0;
|
||||||
fdf->rot_y = 0;
|
fdf->rot_y = 0;
|
||||||
|
// x and y deplacements
|
||||||
|
fdf->mov_x = 0;
|
||||||
|
fdf->mov_y = 0;
|
||||||
// init connexion to server
|
// init connexion to server
|
||||||
fdf->mlx_ptr = mlx_init();
|
fdf->mlx_ptr = mlx_init();
|
||||||
// create the window
|
// create the window
|
||||||
|
|||||||
Reference in New Issue
Block a user