diff --git a/Makefile b/Makefile index d2053d6..83d773e 100644 --- a/Makefile +++ b/Makefile @@ -18,13 +18,20 @@ _LIBS = libft.a LIBS = $(_LIBS:lib%.a=%) SRCS = fdf.c \ - draw.c \ parse.c \ modifs.c \ + draw.c \ keypress.c +SRCS_B = fdf.c \ + parse.c \ + modifs.c \ + keypress_bonus.c \ + draw_bonus.c + ODIR = ./builds OBJS = $(SRCS:%.c=$(ODIR)/%.o) +OBJS_B = $(SRCS_B:%.c=$(ODIR)/%.o) # flag -g generates debug informations, g3 is maximal version CFLAGS = -I$(IDIR) -g3 -Wall -Wextra -Werror @@ -39,17 +46,17 @@ LFLAGS += -lm -lmlx -lXext -lX11 -L./minilibx-linux-master all: $(NAME) -# it verify if anything has changed in .c and .h files -# then it makes LDIR library, then it compiles NAME $(NAME): $(ODIR) $(OBJS) $(DEPS) make -C $(LDIR) $(CC) $(CFLAGS) -o $@ $(OBJS) $(LFLAGS) -# create "builds/" if doesn't exist +bonus: fclean $(ODIR) $(OBJS_B) $(DEPS) + make -C $(LDIR) + $(CC) $(CFLAGS) -o fdf $(OBJS_B) $(LFLAGS) + $(ODIR): mkdir -p $@ -# if a file.c has been modified, it's recompiled in object file.o $(ODIR)/%.o: %.c $(CC) $(CFLAGS) -c -o $@ $< @@ -61,7 +68,7 @@ leaks: $(NAME) # valgrind --leak-check=full --leak-resolution=low --show-reachable=yes ./$(NAME) maps/42_color.fdf clean: - /bin/rm -f $(OBJS) + /bin/rm -f $(OBJS) $(OBJS_B) fclean: clean /bin/rm -rf $(ODIR) diff --git a/bonus b/bonus new file mode 100755 index 0000000..b1be388 Binary files /dev/null and b/bonus differ diff --git a/builds/draw.o b/builds/draw.o deleted file mode 100644 index 11ce548..0000000 Binary files a/builds/draw.o and /dev/null differ diff --git a/builds/draw_bonus.o b/builds/draw_bonus.o new file mode 100644 index 0000000..06d9aae Binary files /dev/null and b/builds/draw_bonus.o differ diff --git a/builds/keypress.o b/builds/keypress.o deleted file mode 100644 index e979b5f..0000000 Binary files a/builds/keypress.o and /dev/null differ diff --git a/builds/keypress_bonus.o b/builds/keypress_bonus.o new file mode 100644 index 0000000..98c6a49 Binary files /dev/null and b/builds/keypress_bonus.o differ diff --git a/fdf b/fdf index fe826b0..b1be388 100755 Binary files a/fdf and b/fdf differ diff --git a/srcs/draw.c b/srcs/draw.c index 4d1e969..b63e863 100644 --- a/srcs/draw.c +++ b/srcs/draw.c @@ -18,7 +18,6 @@ void draw_image(t_fdf *fdf) draw_grid(fdf, i, j); } mlx_put_image_to_window(fdf->mlx_ptr, fdf->win_ptr, fdf->img_ptr, 0, 0); - position_state(fdf); } void draw_grid(t_fdf *fdf, int i, int j) diff --git a/srcs/draw_bonus.c b/srcs/draw_bonus.c new file mode 100644 index 0000000..4d1e969 --- /dev/null +++ b/srcs/draw_bonus.c @@ -0,0 +1,99 @@ +#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; + 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); + position_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) + { + free(point_end); + point_end = new_coordinates(fdf, i, j + 1); + } + draw_lines(fdf, point_start, point_end); + free(point_start); + free(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->zoom) / fdf->offset; + z /= fdf->altitude; + if (z > fdf->min_z && fdf->z_amplitude) + { + color = color ^ (((0xff / fdf->z_amplitude) * (z - fdf->min_z)) << 16); + color = color ^ (((0xff / fdf->z_amplitude) * (z - fdf->min_z)) << 0); + } + 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; +} diff --git a/srcs/keypress.c b/srcs/keypress.c index b0b0801..7e872e4 100644 --- a/srcs/keypress.c +++ b/srcs/keypress.c @@ -1,69 +1,9 @@ #include "fdf.h" -/* -** U -> up wiew -** I -> isometric original view -** O -> increase altitude -** P -> decrease altitude -** F -> fit to window -** C -> center -*/ -void keypress_more(int keycode, t_fdf *fdf) -{ - if (keycode == A) - fdf->zoom += 1; - else if (keycode == W) - fdf->zoom -= 1; - else if (keycode == U) - { - fdf->rot_x = 0; - fdf->rot_y = 0; - } - else if (keycode == I) - { - fdf->rot_x = -45; - fdf->rot_y = -35; - fdf->mov_x = (fdf->win_size_x - fdf->map_size_x) / 2; - fdf->mov_y = (fdf->win_size_y - fdf->map_size_y) / 2; - fdf->zoom = 0; - } - else if (keycode == O) - fdf->altitude++; - else if (keycode == P) - if (fdf->altitude > 1) - fdf->altitude--; -} - -/* -** Q -> move left -** D -> move right -** Z -> move up -** S -> move down -** A -> zoom -** W -> unzoom -*/ 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 - keypress_more(keycode, fdf); draw_image(fdf); return (0); } diff --git a/srcs/keypress_bonus.c b/srcs/keypress_bonus.c new file mode 100644 index 0000000..b0b0801 --- /dev/null +++ b/srcs/keypress_bonus.c @@ -0,0 +1,69 @@ +#include "fdf.h" + +/* +** U -> up wiew +** I -> isometric original view +** O -> increase altitude +** P -> decrease altitude +** F -> fit to window +** C -> center +*/ +void keypress_more(int keycode, t_fdf *fdf) +{ + if (keycode == A) + fdf->zoom += 1; + else if (keycode == W) + fdf->zoom -= 1; + else if (keycode == U) + { + fdf->rot_x = 0; + fdf->rot_y = 0; + } + else if (keycode == I) + { + fdf->rot_x = -45; + fdf->rot_y = -35; + fdf->mov_x = (fdf->win_size_x - fdf->map_size_x) / 2; + fdf->mov_y = (fdf->win_size_y - fdf->map_size_y) / 2; + fdf->zoom = 0; + } + else if (keycode == O) + fdf->altitude++; + else if (keycode == P) + if (fdf->altitude > 1) + fdf->altitude--; +} + +/* +** Q -> move left +** D -> move right +** Z -> move up +** S -> move down +** A -> zoom +** W -> unzoom +*/ +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 + keypress_more(keycode, fdf); + draw_image(fdf); + return (0); +}