diff --git a/CMakeLists.txt b/CMakeLists.txt index ce809a8..7745294 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,9 @@ set(SOURCE_FILES src/line.c src/map_init.c src/pnt_init.c - src/help_func.c src/render.c src/mat4.c) # sources + src/help_func.c + src/render.c + src/mat4.c) # sources add_executable(fdf ${SOURCE_FILES}) # compilation diff --git a/Makefile b/Makefile index 6779c06..3c1b08c 100644 --- a/Makefile +++ b/Makefile @@ -10,83 +10,93 @@ # # # **************************************************************************** # -NAME = fdf +# clor constants +SWITCH = \033[ -SRCDIR = ./src/ +NORMAL = $(SWITCH)0m +GREEN = $(SWITCH)32m +BLUE = $(SWITCH)34m +CYAN = $(SWITCH)36m -OBJDIR = ./obj/ +# final binary name +NAME = fdf -SRC_FILES = main.c \ - line.c \ - map_init.c \ - pnt_init.c \ - help_func.c \ - mat4.c \ - render.c +# static libraries -OBJ_FILES = $(SRC_FILES:.c=.o) +LIBFT = libft.a +MLX = libmlx.a +# directories -SRC = $(addprefix $(SRCDIR), $(SRC_FILES)) +SRC_DIR = ./src/ +OBJ_DIR = ./obj/ +LIBFT_DIR = ./libft/ +MLX_DIR = ./minilibx/ -OBJ = $(addprefix $(OBJDIR), $(OBJ_FILES)) +# src and obj files +SRC_FILES = $(shell ls $(SRC_DIR)) +OBJ_FILES = $(SRC_FILES:.c=.o) -INC = -I $(LIBFT_FOLDER)includes/ \ - -I $(MLX_FOLDER) \ - -I ./inc +SRC = $(addprefix $(SRC_DIR), $(SRC_FILES)) +OBJ = $(addprefix $(OBJ_DIR), $(OBJ_FILES)) +# header files -LIBFT = libft.a +INC = -I $(LIBFT_DIR)includes/ \ + -I $(MLX_DIR) \ + -I ./inc -MLX = libmlx.a +# compiler flags +LIBFT_FLAGS = -lft -L $(LIBFT_DIR) +MLX_FLAGS = -lmlx -lXext -lX11 -L $(MLX_DIR) +OTHER_FLAGS = -lm +CC_FLAGS = -Werror -Wextra -Wall -O3 +FLAGS := $(CC_FLAGS) $(LIBFT_FLAGS) $(MLX_FLAGS) $(OTHER_FLAGS) -LIBFT_FLAGS = -lft -L $(LIBFT_FOLDER) +# compiler -MLX_FLAGS = -lmlx -lXext -lX11 -L $(MLX_FOLDER) - -OTHER_FLAGS = -lm - -LIBFT_FOLDER = ./libft/ - -MLX_FOLDER = ./minilibx/ - - -FLAGS = -Werror -Wextra -Wall -O3 - -CC = gcc +CC = gcc +# rules all: $(NAME) -$(NAME): $(OBJ) $(LIBFT_FOLDER)$(LIBFT) $(MLX_FOLDER)$(MLX) - $(CC) $(OBJ) $(FLAGS) $(LIBFT_FLAGS) $(MLX_FLAGS) $(OTHER_FLAGS) -o $(NAME) +$(NAME): $(OBJ) $(LIBFT_DIR)$(LIBFT) $(MLX_DIR)$(MLX) + @echo "$(CYAN)Linking fdf binary...$(NORMAL)" + @$(CC) $(OBJ) $(FLAGS) -o $(NAME) + @echo "$(GREEN)Done!$(NORMAL)" -$(OBJDIR)%.o : $(SRCDIR)%.c - $(CC) $(FLAGS) $(INC) -c $< -o $@ +$(OBJ_DIR)%.o : $(SRC_DIR)%.c + @echo "$(CYAN)Compiling object files: $(BLUE)$@$(NORMAL)" + @$(CC) $(CC_FLAGS) $(INC) -c $< -o $@ -$(LIBFT_FOLDER)$(LIBFT): - make -C $(LIBFT_FOLDER) +$(LIBFT_DIR)$(LIBFT): + @echo "$(CYAN)Compiling libft library...$(NORMAL)" + @make -s -C $(LIBFT_DIR) -$(MLX_FOLDER)$(MLX): - make -C $(MLX_FOLDER) +$(MLX_DIR)$(MLX): + @echo "$(CYAN)Compiling libmlx library...$(NORMAL)" + @make -s -C $(MLX_DIR) clean: - rm -rf $(OBJ) + @rm -rf $(OBJ) fclean: clean - rm -rf $(NAME) + @rm -rf $(NAME) libclean: - make clean -C $(LIBFT_FOLDER) - make clean -C $(MLX_FOLDER) + @make clean -s -C $(LIBFT_DIR) + @make clean -s -C $(MLX_DIR) libfclean: - make fclean -C $(LIBFT_FOLDER) + @make fclean -s -C $(LIBFT_DIR) re: fclean libfclean all -pub: fclean libfclean libclean +pu: fclean libfclean libclean + @echo "$(CYAN)Cleaning for push...$(NORMAL)" + diff --git a/inc/fdf.h b/inc/fdf.h index 31566a3..b41f164 100644 --- a/inc/fdf.h +++ b/inc/fdf.h @@ -15,32 +15,38 @@ #include #include +# define HEIGHT 500 +# define WIDTH 500 + typedef struct s_vec { double x; double y; double z; + int color; } t_vec; typedef struct s_pnt { int x; int y; + int z; int color; } t_pnt; -typedef struct s_mlx +typedef struct s_mw { void *mlx; void *win; -} t_mlx; +} t_mw; typedef struct s_map { t_list *lst_map; t_pnt **arr_pnt; - int width; - int height; + int x_max; + int y_max; + int z_max; } t_map; typedef struct s_mat4 @@ -57,21 +63,22 @@ typedef struct s_trnsf t_mat4 *fin; } t_trnsf; +typedef struct s_FDF +{ + t_map *map; + t_mw *mw; + int w_heigth; + int w_width; +} t_FDF; + typedef struct s_swap { int swap_x; int swap_coord; } t_swap; -//delete -typedef struct s_edg -{ - int p1; - int p2; -} t_edg; - -void line(t_pnt p1, t_pnt p2, t_mlx *m); +void line(t_pnt p1, t_pnt p2, t_mw *mw); t_map *map_init(char *path); void pnt_init(t_map *map); @@ -87,11 +94,10 @@ void vec_mat_mult(t_mat4 *m, t_vec *v, t_pnt *result); void map_transform(t_map *map); void transform(t_mat4 *mat, t_map *map); -void perspective_transform(t_map *map, t_mlx *mlx); -void render(t_map *map, t_mlx *m, int x, int y); +void render(t_FDF *FDF); void free_tab(char **tab); -void quit(t_mlx *m); +void quit(t_FDF *FDF); #endif diff --git a/minilibx/Makefile b/minilibx/Makefile index 5c418c5..d334105 100644 --- a/minilibx/Makefile +++ b/minilibx/Makefile @@ -14,9 +14,9 @@ all : do_configure do_configure : - ./configure + ./configure > /dev/null 2>&1 clean : - ./configure clean + ./configure clean > /dev/null 2>&1 re : clean all diff --git a/src/help_func.c b/src/help_func.c index 0f5e445..c9ec840 100644 --- a/src/help_func.c +++ b/src/help_func.c @@ -7,8 +7,8 @@ void free_tab(char **tab) free(*tab); } -void quit(t_mlx *m) +void quit(t_FDF *FDF) { - mlx_destroy_window(m->mlx, m->win); + mlx_destroy_window(FDF->mw->mlx, FDF->mw->win); exit(0); } \ No newline at end of file diff --git a/src/line.c b/src/line.c index 4b62237..007cd07 100644 --- a/src/line.c +++ b/src/line.c @@ -23,7 +23,7 @@ static void swap_points(t_pnt *p1, t_pnt *p2) } -static void draw_line(t_pnt *p1, t_pnt *p2, t_mlx *m, t_swap *s) +static void draw_line(t_pnt *p1, t_pnt *p2, t_mw *mw, t_swap *s) { int dx = abs(p2->x - p1->x); int dy = abs(p2->y - p1->y); @@ -31,9 +31,9 @@ static void draw_line(t_pnt *p1, t_pnt *p2, t_mlx *m, t_swap *s) while (p1->x < p2->x) { if (s->swap_coord) - mlx_pixel_put(m->mlx, m->win, p1->y, p1->x, 0xFFFFFF); + mlx_pixel_put(mw->mlx, mw->win, p1->y, p1->x, 0xFFFFFF); else - mlx_pixel_put(m->mlx, m->win, p1->x, p1->y, 0xFFFFFF); + mlx_pixel_put(mw->mlx, mw->win, p1->x, p1->y, 0xFFFFFF); if (derr > 0) { p1->y += s->swap_x; @@ -44,7 +44,7 @@ static void draw_line(t_pnt *p1, t_pnt *p2, t_mlx *m, t_swap *s) } } -void line(t_pnt p1, t_pnt p2, t_mlx *m) +void line(t_pnt p1, t_pnt p2, t_mw *mw) { int dx; int dy; @@ -63,6 +63,6 @@ void line(t_pnt p1, t_pnt p2, t_mlx *m) swap_points(&p1, &p2); if (p1.y > p2.y) swap.swap_x = -1; - draw_line(&p1, &p2, m, &swap); + draw_line(&p1, &p2, mw, &swap); } diff --git a/src/main.c b/src/main.c index c241235..38a649c 100644 --- a/src/main.c +++ b/src/main.c @@ -3,83 +3,16 @@ // FILE: main.c // ROLE: to rule them all // CREATED: 2017-01-30 21:51:28 -// MODIFIED: 2017-02-13 20:30:48 +// MODIFIED: 2017-03-09 20:51:10 #include "fdf.h" #include -#include - - -void test_line(t_mlx mlx) -{ - t_pnt p1; - t_pnt p2; - int temp; - - p1.color = 0xC100E5; - p2.color = 0xC100E5; - for (p2.y = 0; p2.y <= 500; p2.y += 4) - { - p1.x = 0; - p1.y = 0; - p2.x = 500; - line(p1, p2, &mlx); - } - for (p2.x = 500; p2.x >= 0; p2.x -= 4) - { - p1.x = 0; - p1.y = 0; - p2.y = 500; - temp = p2.x; - line(p1, p2, &mlx); - p2.x = temp; - } - p1.color = 0x1EBF00; - p2.color = 0x1EBF00; - for (p2.y = 0; p2.y <= 500; p2.y += 4) - { - p1.x = 500; - p1.y = 0; - p2.x = 0; - temp = p2.y; - line(p1, p2, &mlx); - p2.y = temp; - } - for (p2.x = 0; p2.x <= 500; p2.x += 4) - { - p1.x = 500; - p1.y = 0; - p2.y = 500; - temp = p2.x; - line(p1, p2, &mlx); - p2.x = temp; - } -} - -void circle(t_mlx m) -{ - t_pnt p1; - t_pnt p2; - double angle; - - - for (int i = 0; i < 360; i += 4) - { - p1.x = 250; - p1.y = 250; - angle = i * M_PI / 180; - p2.x = p1.x + 200 * sin(angle); - p2.y = p1.y + 200 * cos(angle); - line(p1, p2, &m); - } -} - int key_hook(int keycode, void *m) { printf("%d\n", keycode); - if (keycode == 65307) + if (keycode == 53 || keycode == 65307) quit(m); // if (keycode == 123) // left(mlx); @@ -90,20 +23,67 @@ int key_hook(int keycode, void *m) return (0); } +t_mw *mlx_and_win_ptr_init(int x, int y) +{ + t_mw *mw; + + mw = NULL; + if ((mw = malloc(sizeof(t_mw)))) + { + mw->mlx = mlx_init(); + mw->win = mlx_new_window(mw->mlx, x, y, "FDF"); + } + return (mw); +} + +t_FDF *FDF_init(char *path) +{ + t_FDF *FDF; + + FDF = NULL; + if ((FDF = malloc(sizeof(t_FDF)))) + { + FDF->w_heigth = HEIGHT; + FDF->w_width = WIDTH; + FDF->mw = mlx_and_win_ptr_init(WIDTH, HEIGHT); + FDF->map = map_init(path); + pnt_init(FDF->map); + } + return (FDF); +} + int main(int argc, char **argv) { - t_mlx m; - t_map *map; + t_FDF *FDF; + void *image; + char *data; + int i; + int bpp; + int endian; + int j; + int line; if (argc != 2) return (0); - m.mlx = mlx_init(); - m.win = mlx_new_window(m.mlx, 500, 500, "fdf"); - map = map_init(argv[1]); - pnt_init(map); - //circle(m); - render(map, &m, 500, 500); - mlx_key_hook(m.win, key_hook, &m); - mlx_loop(m.mlx); + FDF = FDF_init(argv[1]); + i = 0; + bpp = 4; + endian = 1; + image = mlx_new_image(FDF->mw->mlx, FDF->w_heigth, FDF->w_heigth); + data = mlx_get_data_addr(image, &bpp, &line, &endian); + while (i < FDF->w_heigth) + { + j = 0; + while (j < FDF->w_width) + { + *((int *)data + j + i * FDF->w_width) = mlx_get_color_value(FDF->mw->mlx, 0x4286f4); + j++; + } + i++; + } + mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, image, 0, 0); + //render(FDF); + mlx_key_hook(FDF->mw->win, key_hook, FDF); + mlx_loop(FDF->mw->mlx); return (0); } diff --git a/src/map_init.c b/src/map_init.c index e90fc1b..e45e765 100644 --- a/src/map_init.c +++ b/src/map_init.c @@ -1,6 +1,6 @@ #include "fdf.h" -void fill_row(t_list **list, int i, char *row, t_map *map) +void fill_row(t_list **list, int i, char *row) { int j; char **row_numbers; @@ -16,11 +16,10 @@ void fill_row(t_list **list, int i, char *row, t_map *map) ft_lstadd_back(list, &vec3, sizeof(t_vec)); j++; } - free(row_numbers); - map->width = j; + free_tab(row_numbers); } -t_list *to_lst_map(char **tab_map, t_map *map) +t_list *to_lst_map(char **tab_map) { int i; t_list *lst_map; @@ -29,7 +28,7 @@ t_list *to_lst_map(char **tab_map, t_map *map) i = 0; while (tab_map[i]) { - fill_row(&lst_map, i, tab_map[i], map); + fill_row(&lst_map, i, tab_map[i]); i++; } return (lst_map); @@ -45,15 +44,30 @@ char *to_str_map(char *path) return (0); } -int map_heigth(char *str_map) +void map_dimensions(t_map *map) { - int h; + int x; + int y; + int z; + t_list *l; - h = 0; - while (*str_map++) - if (*str_map == '\n') - h++; - return (h); + l = map->lst_map; + x = 0; + y = 0; + z = 0; + while (l) + { + if (((t_vec *)(l->content))->x > x) + x = (int)((t_vec *)(l->content))->x; + if (((t_vec *)(l->content))->y > y) + y = (int)((t_vec *)(l->content))->y; + if (((t_vec *)(l->content))->z > z) + z = (int)((t_vec *)(l->content))->z; + l = l->next; + } + map->x_max = x; + map->y_max = y; + map->z_max = z; } t_map *map_init(char *path) @@ -64,10 +78,9 @@ t_map *map_init(char *path) map = malloc(sizeof(t_map)); str_map = to_str_map(path); - printf("%s\n", str_map); - map->height = map_heigth(str_map); tab_map = ft_strsplit(str_map, '\n'); - map->lst_map = to_lst_map(tab_map, map); + map->lst_map = to_lst_map(tab_map); + map_dimensions(map); free(str_map); free_tab(tab_map); return (map); diff --git a/src/pnt_init.c b/src/pnt_init.c index 91d316b..8edc3b3 100644 --- a/src/pnt_init.c +++ b/src/pnt_init.c @@ -5,11 +5,11 @@ void pnt_init(t_map *map) t_pnt **arr_pnt; int i; - arr_pnt = malloc(sizeof(t_pnt *) * map->height); + arr_pnt = malloc(sizeof(t_pnt *) * (map->y_max + 1)); i = 0; - while (i < map->height) + while (i <= map->y_max) { - arr_pnt[i] = malloc(sizeof(t_pnt) * (map->width)); + arr_pnt[i] = malloc(sizeof(t_pnt) * (map->x_max + 1)); i++; } map->arr_pnt = arr_pnt; diff --git a/src/render.c b/src/render.c index 42575fd..23b305e 100644 --- a/src/render.c +++ b/src/render.c @@ -16,24 +16,6 @@ t_trnsf *transfor_mat_init() return (trnsf); } -int max_z(t_map *map) -{ - int z; - t_list *tmp; - - z = 0; - tmp = map->lst_map; - while (tmp) - { - if (((t_vec *)(tmp->content))->z > z) - { - z = (int)((t_vec *)(tmp->content))->z; - } - tmp = tmp->next; - } - return (z); -} - void transform(t_mat4 *mat, t_map *map) { t_list *vec_list; @@ -42,10 +24,10 @@ void transform(t_mat4 *mat, t_map *map) vec_list = map->lst_map; i = 0; - while (i < map->height) + while (i <= map->y_max) { j = 0; - while (j < map->width) + while (j <= map->x_max) { vec_mat_mult(mat, vec_list->content, &map->arr_pnt[i][j]); vec_list = vec_list->next; @@ -61,20 +43,20 @@ t_mat4 *viewport_mat_init(int x, int y) if ((m = malloc(sizeof(t_mat4)))) { - m->mx[0][0] = (double)x / 2; + m->mx[0][0] = (double)x / 2.0f; m->mx[0][1] = 0; m->mx[0][2] = 0; m->mx[0][3] = 0; m->mx[1][0] = 0; - m->mx[1][1] = (double)y / 2; + m->mx[1][1] = (double)y / 2.0f; m->mx[1][2] = 0; m->mx[1][3] = 0; m->mx[2][0] = 0; m->mx[2][1] = 0; m->mx[2][2] = 1; m->mx[2][3] = 0; - m->mx[3][0] = ((double)x - 1) / 2; - m->mx[3][1] = ((double)y - 1) / 2; + m->mx[3][0] = ((double)x - 1) / 2.0f; + m->mx[3][1] = ((double)y - 1) / 2.0f; m->mx[3][2] = 0; m->mx[3][3] = 1; } @@ -87,17 +69,17 @@ t_mat4 *orth_mat_init(int l, int r, int b, int t, int n, int f) if ((orth = malloc(sizeof(t_mat4)))) { - orth->mx[0][0] = 2 / ((double)r - (double)l); + orth->mx[0][0] = 2.0f / ((double)r - (double)l); orth->mx[0][1] = 0; orth->mx[0][2] = 0; orth->mx[0][3] = 0; orth->mx[1][0] = 0; - orth->mx[1][1] = 2 / ((double)t - (double)b); + orth->mx[1][1] = 2.0f / ((double)t - (double)b); orth->mx[1][2] = 0; orth->mx[1][3] = 0; orth->mx[2][0] = 0; orth->mx[2][1] = 0; - orth->mx[2][2] = 2 / ((double)n - (double)f); + orth->mx[2][2] = 2.0f / ((double)n - (double)f); orth->mx[2][3] = 0; orth->mx[3][0] = - (((double)r + (double)l) / ((double)r - (double)l)); orth->mx[3][1] = - (((double)t + (double)b) / ((double)t - (double)b)); @@ -107,30 +89,25 @@ t_mat4 *orth_mat_init(int l, int r, int b, int t, int n, int f) return (orth); } -void draw(t_map *map, t_mlx *m) +void draw(t_map *map, t_mw *mw) { - int x; - int y; int i; int j; i = 0; - while (i < map->height) + while (i <= map->y_max) { j = 0; - while (j < map->width) + while (j <= map->x_max) { - x = map->arr_pnt[i][j].x; - y = map->arr_pnt[i][j].y; - mlx_pixel_put(m->mlx, m->win, x, y, 0xFFFFFF); - if (i == map->height - 1 && j < map->width - 1) - line(map->arr_pnt[i][j], map->arr_pnt[i][j + 1], m); - else if (i < map->height - 1 && j == map->width - 1) - line(map->arr_pnt[i][j], map->arr_pnt[i + 1][j], m); - else if (i < map->height - 1 && j < map->width - 1) + if (i == map->y_max && j < map->x_max) + line(map->arr_pnt[i][j], map->arr_pnt[i][j + 1], mw); + else if (i < map->y_max && j == map->x_max) + line(map->arr_pnt[i][j], map->arr_pnt[i + 1][j], mw); + else if (i < map->y_max && j < map->x_max) { - line(map->arr_pnt[i][j], map->arr_pnt[i + 1][j], m); - line(map->arr_pnt[i][j], map->arr_pnt[i][j + 1], m); + line(map->arr_pnt[i][j], map->arr_pnt[i + 1][j], mw); + line(map->arr_pnt[i][j], map->arr_pnt[i][j + 1], mw); } j++; } @@ -138,7 +115,24 @@ void draw(t_map *map, t_mlx *m) } } -void render(t_map *map, t_mlx *m, int x, int y) +void center(t_map *map) +{ + t_list *l; + double d_width; + double d_height; + + d_width = map->x_max / 2.0f; + d_height = map->y_max / 2.0f; + l = map->lst_map; + while (l) + { + ((t_vec *)(l->content))->x = ((t_vec *)(l->content))->x - d_width; + ((t_vec *)(l->content))->y = ((t_vec *)(l->content))->y - d_height; + l = l->next; + } +} + +void render(t_FDF *FDF) { t_mat4 *vp; t_mat4 *orth; @@ -154,19 +148,31 @@ void render(t_map *map, t_mlx *m, int x, int y) rot_z = mat4_init(); translate = mat4_init(); scale = mat4_init(); - vp = viewport_mat_init(x, y); - orth = orth_mat_init(0, map->width + 1, 0, map->height + 1, 0, 20); - mat4_x_rot(rot_x, 20); + vp = viewport_mat_init(FDF->w_width, FDF->w_heigth); + orth = orth_mat_init( + 0, + FDF->map->x_max + 2, + 0, + FDF->map->x_max + 2, + 0, + FDF->map->z_max + 2); + mat4_x_rot(rot_x, 45); mat4_z_rot(rot_z, 45); - mat4_y_rot(rot_y, 20); + mat4_y_rot(rot_y, 45); mat4_scale(scale, 1, 1, 1); - mat4_translate(translate, 0, 0, 0); + mat4_translate( + translate, + (((double)FDF->map->x_max) / 2.0f), + (((double)FDF->map->x_max) / 2.0f), + (((double)FDF->map->z_max) / 2.0f)); final = mat4_mult(vp, orth); final = mat4_mult(rot_x, final); - final = mat4_mult(translate, final); final = mat4_mult(rot_y ,final); - final = mat4_mult(scale, final); - transform(final, map); - draw(map, m); + final = mat4_mult(translate, final); +// final = mat4_mult(scale, final); + + center(FDF->map); + transform(final, FDF->map); + draw(FDF->map, FDF->mw); }