Files
42_INT_04_fdf/srcs/fdf.c

303 lines
6.7 KiB
C

#include "fdf.h"
#include <stdio.h>
t_fdf *init_fdf(void);
int print_keycode(int keycode);
int shut_down(t_fdf *fdf);
void draw_image(t_fdf *fdf);
void draw_pixel(t_fdf *fdf, int x, int y, int color);
void draw_color_pixel(t_fdf *fdf, int new_x, int new_y, int z);
int keypress(int keycode, t_fdf *fdf);
void rotation_state(t_fdf *fdf);
int **parse_map(t_fdf *fdf);
int *new_coordinates(t_fdf *fdf, int i, int j);
// return an int[3] : int[0] = x, int[1] = y, int[2] = z
int *new_coordinates(t_fdf *fdf, int i, int j)
{
int x;
int y;
int z;
int *point;
point = ft_calloc(3, sizeof(int));
x = i * fdf->offset - (fdf->map_size_x / 2);
y = j * fdf->offset - (fdf->map_size_y / 2);
z = fdf->map[j][i] * fdf->altitude;
point[0] = x * cos(fdf->rad_x) + y * sin(fdf->rad_x);
point[1] = y * cos(fdf->rad_x) - x * sin(fdf->rad_x);
point[1] = point[1] * cos(fdf->rad_y) - -z * sin(fdf->rad_y);
point[0] += fdf->margin + fdf->mov_x + (fdf->map_size_x / 2);
point[1] += fdf->margin + fdf->mov_y + (fdf->map_size_y / 2);
point[2] = z;
return (point);
}
// point[0], point[1], point[2] -> new x, new y, new z
void draw_lines(t_fdf *fdf, int i, int j)
{
int *point_start;
int *point_end;
point_start = new_coordinates(fdf, i, j);
point_end = new_coordinates(fdf, i, j);
draw_color_pixel(fdf, point_start[0], point_start[1], point_start[2]);
}
void draw_image(t_fdf *fdf)
{
int i;
int j;
// 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_height)
{
i = -1;
while (++i < fdf->map_width)
draw_lines(fdf, i, j);
}
// 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->img_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 draw_color_pixel(t_fdf *fdf, int new_x, int new_y, int z)
{
int color;
color = 0xffffff;
z = z / fdf->altitude;
if (z)
{
color = color ^ (((0xff / fdf->max_z) * z) << 8 );
color = color ^ ((0xff / fdf->max_z) * z);
}
draw_pixel(fdf, new_x, new_y, 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, margin, and altitude factor
fdf->offset = 50;
fdf->margin = 50;
fdf->altitude = 3;
// 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[0][0] = 1;
map[1][0] = 2;
map[2][0] = 3;
map[3][0] = 4;
map[4][0] = 5;
map[5][0] = 6;
map[6][0] = 7;
map[7][0] = 8;
map[8][0] = 9;
map[9][0] = 10;
fdf->max_z = 10;
// size map
fdf->map_width = j;
fdf->map_height = i;
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
*/