zoom fonctionne sauf z

This commit is contained in:
hugogogo
2021-07-23 15:06:56 +02:00
parent 964c83478d
commit b9f02a20f3
11 changed files with 281 additions and 272 deletions

View File

@@ -1,18 +1,12 @@
# have the .c in a "srcs/" directory
# have the .h in a "includes/" directory
# - - - - - - - - - - - - - - # name = value \
# variables names # value
# - - - - - - - - - - - - - - # ! name is case sensitive
# - - - - - - - - - - - - - - #
# variables names #
# - - - - - - - - - - - - - - #
# name = value \
# value
# name is case sensitive
# VPATH where to look for rules preriquisites not found in "./"
# best to use only for ".c", not ".o" nor ".h"
NAME = fdf
CC = gcc
VPATH = srcs
IDIR = ./includes
@@ -23,7 +17,10 @@ LDIR = ./libft
_LIBS = libft.a
LIBS = $(_LIBS:lib%.a=%)
SRCS = fdf.c
SRCS = fdf.c \
draw.c \
parse.c \
modifs.c
ODIR = ./builds
OBJS = $(SRCS:%.c=$(ODIR)/%.o)
@@ -34,12 +31,10 @@ LFLAGS = -L$(LDIR) -l$(LIBS)
CFLAGS += -I./minilibx-linux-master
LFLAGS += -lm -lmlx -lXext -lX11 -L./minilibx-linux-master
# - - - - - - - - - - - - - - #
# rules to execute #
# - - - - - - - - - - - - - - #
# target: prerequisites ...
# recipe ...
# - - - - - - - - - - - - - - # target: prerequisites | $@ : target
# rules to execute # recipe | $< : 1st prerequisite
# - - - - - - - - - - - - - - # recipe | $^ : all prerequisites
all: $(NAME)
@@ -67,12 +62,16 @@ clean:
/bin/rm -f $(OBJS)
fclean: clean
make fclean -C $(LDIR)
/bin/rm -rf $(ODIR)
/bin/rm -f $(NAME)
/bin/rm -rf a.out a.out.dSYM
libfclean:
make fclean -C $(LDIR)
re: fclean all
relib: libfclean re
.PHONY: all clean fclean re gcc

BIN
builds/draw.o Normal file

Binary file not shown.

Binary file not shown.

BIN
builds/modifs.o Normal file

Binary file not shown.

BIN
builds/parse.o Normal file

Binary file not shown.

BIN
fdf

Binary file not shown.

View File

@@ -5,6 +5,7 @@
# include <mlx.h>
# include <unistd.h> // for sleep()
# include <math.h> // for M_PI
# include <stdio.h> // for printf()
typedef struct s_fdf
{
@@ -31,11 +32,25 @@ typedef struct s_fdf
int mov_x;
int mov_y;
int max_z;
int zoom;
int img_bpp;
int img_sizel;
int img_endian;
} t_fdf;
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);
void draw_lines(t_fdf *fdf, int *start, int *end);
void draw_grid(t_fdf *fdf, int i, int j);
# define ESCAPE 65307
# define UP 65362
# define DOWN 65364

94
srcs/draw.c Normal file
View File

@@ -0,0 +1,94 @@
#include "fdf.h"
void draw_image(t_fdf *fdf)
{
int i;
int j;
fdf->rad_x = fdf->rot_x * M_PI / 180;
fdf->rad_y = fdf->rot_y * M_PI / 180;
//fdf->altitude *= (fdf->offset + fdf->zoom) / fdf->offset;
i =-1;
while (++i < fdf->img_size_y * fdf->img_sizel)
*(unsigned int*)(fdf->img_addr + i) = 0;
j = -1;
while (++j < fdf->map_height)
{
i = -1;
while (++i < fdf->map_width)
draw_grid(fdf, i, j);
}
mlx_put_image_to_window(fdf->mlx_ptr, fdf->win_ptr, fdf->img_ptr, 0, 0);
rotation_state(fdf);
}
void draw_grid(t_fdf *fdf, int i, int j)
{
int *point_start;
int *point_end;
point_start = new_coordinates(fdf, i, j);
point_end = NULL;
if (i + 1 < fdf->map_width)
point_end = new_coordinates(fdf, i + 1, j);
draw_lines(fdf, point_start, point_end);
if (j + 1 < fdf->map_height)
point_end = new_coordinates(fdf, i, j + 1);
draw_lines(fdf, point_start, point_end);
}
void draw_lines(t_fdf *fdf, int *start, int *end)
{
int dx;
int dy;
int z;
int i;
int j;
if (end)
{
dx = end[0] - start[0];
dy = end[1] - start[1];
i = 0;
j = 0;
z = start[2];
while (ft_abs(i) <= ft_abs(dx) && ft_abs(j) <= ft_abs(dy))
{
if ((dx + dy) && (i + j))
z = start[2] + (end[2] - start[2]) * (i + j) / (dx + dy);
draw_color_pixel(fdf, start[0] + i, start[1] + j, z);
if (!ft_abs(dx) || ft_abs(j) < ft_abs(i * dy / dx))
j += ft_sign(dy);
else
i += ft_sign(dx);
}
}
}
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 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;
}

View File

@@ -1,79 +1,40 @@
#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);
void draw_lines(t_fdf *fdf, int *start, int *end)
int shut_down(t_fdf *fdf)
{
int dx;
int dy;
int z;
int i;
int j;
if (end)
{
dx = end[0] - start[0];
dy = end[1] - start[1];
i = 0;
j = 0;
z = start[2];
while (ft_abs(i) <= ft_abs(dx) && ft_abs(j) <= ft_abs(dy))
{
if ((dx + dy) && (i + j))
z = start[2] + (end[2] - start[2]) * (i + j) / (dx + dy);
draw_color_pixel(fdf, start[0] + i, start[1] + j, z);
if (!ft_abs(dx) || ft_abs(j) < ft_abs(i * dy / dx))
j += ft_sign(dy);
else
i += ft_sign(dx);
}
}
mlx_destroy_window(fdf->mlx_ptr, fdf->win_ptr);
exit(0);
free(fdf);
return (0);
}
// point[0], point[1], point[2] -> new x, new y, new z
void draw_point_n_lines(t_fdf *fdf, int i, int j)
t_fdf *init_fdf(void)
{
int *point_start;
int *point_end;
t_fdf *fdf;
point_start = new_coordinates(fdf, i, j);
point_end = NULL;
if (i + 1 < fdf->map_width)
point_end = new_coordinates(fdf, i + 1, j);
draw_lines(fdf, point_start, point_end);
}
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_point_n_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);
fdf = malloc(sizeof(t_fdf));
fdf->offset = 50;
fdf->margin = 50;
fdf->altitude = 3;
fdf->map = parse_map(fdf);
fdf->win_size_x = fdf->map_size_x + 2 * fdf->margin;
fdf->win_size_y = fdf->map_size_y + 2 * fdf->margin;
fdf->img_size_x = fdf->win_size_x;
fdf->img_size_y = fdf->win_size_y;
fdf->rot_x = 0;
fdf->rot_y = 0;
fdf->mov_x = 0;
fdf->mov_y = 0;
fdf->zoom = 0;
fdf->mlx_ptr = mlx_init();
fdf->win_ptr = mlx_new_window(fdf->mlx_ptr, fdf->win_size_x,
fdf->win_size_y, "test");
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(fdf);
return (fdf);
}
int main(int ac, char **av)
@@ -84,197 +45,12 @@ int main(int ac, char **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);
}
// 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);
}
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[9][0] = 1;
map[9][1] = 2;
map[9][2] = 3;
map[9][3] = 4;
map[9][4] = 5;
map[9][5] = 6;
map[9][6] = 7;
map[9][7] = 8;
map[9][8] = 9;
map[9][9] = 10;
map[9][10] = 2;
map[9][11] = 5;
map[9][12] = 8;
map[9][13] = 3;
map[9][14] = 6;
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

85
srcs/modifs.c Normal file
View File

@@ -0,0 +1,85 @@
#include "fdf.h"
// add "print_keycode(keycode);" at begining to print keycode
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 if (keycode == A)
(fdf->zoom) += 6;
else if (keycode == W)
(fdf->zoom) -= 6;
draw_image(fdf);
return (0);
}
// 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));
// pre zoom
x = i * (fdf->offset + fdf->zoom);
y = j * (fdf->offset + fdf->zoom);
// pre center
x -= (fdf->map_size_x + fdf->zoom * fdf->map_width) / 2;
y -= (fdf->map_size_y + fdf->zoom * fdf->map_height) / 2;
// rotation
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);
// deplacements
point[0] += fdf->margin + fdf->mov_x;
point[1] += fdf->margin + fdf->mov_y;
// post zoom
point[0] -= ((fdf->map_size_x / 2) / fdf->offset) * fdf->zoom;
point[1] -= ((fdf->map_size_y / 2) / fdf->offset) * fdf->zoom;
// post center
point[0] += (fdf->map_size_x + fdf->zoom * fdf->map_width) / 2;
point[1] += (fdf->map_size_y + fdf->zoom * fdf->map_height) / 2;
point[2] = z;
return (point);
}
int print_keycode(int keycode)
{
ft_putnbr(keycode);
ft_putchar(' ');
return(0);
}
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);
}

40
srcs/parse.c Normal file
View File

@@ -0,0 +1,40 @@
#include "fdf.h"
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[9][0] = 1;
map[9][1] = 2;
map[9][2] = 3;
map[9][3] = 4;
map[9][4] = 5;
map[9][5] = 6;
map[9][6] = 7;
map[9][7] = 8;
map[9][8] = 9;
map[9][9] = 10;
map[9][10] = 2;
map[9][11] = 5;
map[9][12] = 8;
map[9][13] = 3;
map[9][14] = 6;
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);
}