diff --git a/Makefile b/Makefile index 3c1b08c..5195306 100644 --- a/Makefile +++ b/Makefile @@ -55,7 +55,9 @@ 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) +DEBUG = -g +FLAGS := $(CC_FLAGS) $(LIBFT_FLAGS) $(MLX_FLAGS) $(OTHER_FLAGS) \ + $(DEBUG) # compiler @@ -72,7 +74,7 @@ $(NAME): $(OBJ) $(LIBFT_DIR)$(LIBFT) $(MLX_DIR)$(MLX) $(OBJ_DIR)%.o : $(SRC_DIR)%.c @echo "$(CYAN)Compiling object files: $(BLUE)$@$(NORMAL)" - @$(CC) $(CC_FLAGS) $(INC) -c $< -o $@ + @$(CC) $(CC_FLAGS) $(DEBUG) $(INC) -c $< -o $@ $(LIBFT_DIR)$(LIBFT): @echo "$(CYAN)Compiling libft library...$(NORMAL)" diff --git a/inc/fdf.h b/inc/fdf.h index 4666f76..d91f624 100644 --- a/inc/fdf.h +++ b/inc/fdf.h @@ -58,7 +58,7 @@ typedef struct s_mw typedef struct s_map { - t_list *lst_map; + t_vec **arr_vec; t_pnt **arr_pnt; int x_max; int y_max; @@ -123,6 +123,7 @@ void render(t_FDF *FDF); void free_tab(char **tab); void quit(t_FDF *FDF); void swap_init(t_swap *s); +void print_help(void); t_color color_init(int red, int green, int blue); t_color color_lerp(t_color c1, t_color c2, float step); diff --git a/src/help_func.c b/src/help_func.c index b2ae163..b118376 100644 --- a/src/help_func.c +++ b/src/help_func.c @@ -19,4 +19,19 @@ void swap_init(t_swap *s) s->swap_coord = 0; s->swap_x = 1; s->swap_z = 0; +} + +void print_help(void) +{ + ft_putstr( + "Usage: " + "fdf [OPTIONS] MAP_PATH" + "\n\n" + "Opitons:\n" + "--heatmap \"R,G,B - R,G,B\"" + "\toptional parameter for specifying color range of\n" + "\t\t\t\theatmap in RGB format. Each color component\n" + "\t\t\t\tshould be between 0 and 255.\n" + "\t\t\t\tPlease, put range in double quotes, for example:\n\n" + "\t\t\t\tfdf --heatmap \"125,255,6 - 250,41,1\" ~/maps/42.fdf\n\n"); } \ No newline at end of file diff --git a/src/main.c b/src/main.c index f28067e..c2eb7d6 100644 --- a/src/main.c +++ b/src/main.c @@ -7,154 +7,16 @@ #include "fdf.h" -void circle(t_FDF *FDF) -{ - t_pnt p1; - t_pnt p2; - double angle; - - - for (int i = 0; i < 360; i += 1) - { - p1.x = 250; - p1.y = 250; - angle = i * M_PI / 180; - p2.x = p1.x + 250 * sin(angle); - p2.y = p1.y + 250 * cos(angle); - line(p1, p2, FDF); - } -} - -void test_line(t_FDF *FDF) -{ - t_pnt p1; - t_pnt p2; - - p1.x = 0; - p1.y = 0; - p2.x = 500; - for (p2.y = 0; p2.y <= 500; p2.y += 4) - { - line(p1, p2, FDF); - mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0); - } - for (p2.x = 500; p2.x >= 0; p2.x -= 4) - { - line(p1, p2, FDF); - mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0); - } - p1.x = 500; - p1.y = 0; - p2.x = 0; - for (p2.y = 0; p2.y <= 500; p2.y += 4) - { - line(p1, p2, FDF); - mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0); - } - for (p2.x = 0; p2.x <= 500; p2.x += 4) - { - line(p1, p2, FDF); - mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0); - } - p1.x = 500; - p1.y = 500; - p2.x = 500; - p2.y = 0; - for (p2.x = 500; p2.x >= 0; p2.x -= 4) - { - line(p1, p2, FDF); - mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0); - } - for (p2.y = 0; p2.y <= 500; p2.y += 4) - { - line(p1, p2, FDF); - mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0); - } - p1.x = 0; - p1.y = 500; - p2.x = 500; - p2.y = 500; - for (p2.y = 500; p2.y >= 0; p2.y -= 4) - { - line(p1, p2, FDF); - mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0); - } - for (p2.x = 500; p2.x >= 0; p2.x -= 4) - { - line(p1, p2, FDF); - mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0); - } - mlx_clear_window(FDF->mw->mlx, FDF->mw->win); - p1.x = 0; - p1.y = 0; - p2.y = 500; - for (p2.x = 0; p2.x <= 500; p2.x += 1) - { - line(p1, p2, FDF); - mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0); - p1.x += 1; - } - mlx_clear_window(FDF->mw->mlx, FDF->mw->win); - p1.x = 0; - p1.y = 0; - p2.x = 500; - for (p2.y = 0; p2.y <= 500; p2.y += 1) - { - line(p1, p2, FDF); - mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0); - p1.y += 1; - } - mlx_clear_window(FDF->mw->mlx, FDF->mw->win); - p1.x = 500; - p1.y = 0; - p2.x = 500; - for (; p2.x >= 0; p2.x -= 1) - { - line(p1, p2, FDF); - mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0); - p1.x -= 1; - } - mlx_clear_window(FDF->mw->mlx, FDF->mw->win); - p1.x = 0; - p1.y = 500; - p2.x = 500; - for (p2.y = 500; p2.y >= 0; p2.y -= 1) - { - line(p1, p2, FDF); - mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0); - p1.y -= 1; - } -} - int main(int argc, char **argv) { t_FDF *FDF; -// int i; -// int j; -// int rgb; -// t_color c1; -// t_color c2; -// t_color c3; - if (argc != 2) + + if (argc < 2) + { + print_help(); return (0); + } FDF = FDF_init(argv[1]); -// i = 0; -// c1 = color_init(225, 13, 14); -// c2 = color_init(20, 177, 4); -// c3 = c1; -// while (i < FDF->w_height) -// { -// j = 0; -// rgb = color_to_int(c3); -// while (j < FDF->w_width) -// { -// *((int *)FDF->image_data + j + i * FDF->w_width) = rgb; -// j++; -// } -// c3 = color_lerp(c1, c2, (float)i / FDF->w_height); -// i++; -// } -// mlx_put_image_to_window(FDF->mw->mlx, FDF->mw->win, FDF->image, 0, 0); render(FDF); mlx_key_hook(FDF->mw->win, key_hook, FDF); mlx_loop(FDF->mw->mlx); diff --git a/src/map_init.c b/src/map_init.c index d013e7d..900f529 100644 --- a/src/map_init.c +++ b/src/map_init.c @@ -44,35 +44,66 @@ char *to_str_map(char *path) return (0); } -void map_dimensions(t_map *map) +int tab_length(char **tab) { - int x; - int y; - int max_z; - int min_z; - t_list *l; + int len; - l = map->lst_map; - x = 0; - y = 0; - max_z = 0; - min_z = 0; - while (l) + len = 0; + while (*tab) { - 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 > max_z) - max_z = (int)((t_vec *)(l->content))->z; - if (((t_vec *)(l->content))->z < min_z) - min_z = (int)((t_vec *)(l->content))->z; - l = l->next; + tab++; + if (*tab) + len++; + } + return (len); +} + +int map_width(char **tab) +{ + int width; + char **line_tab; + + line_tab = ft_strsplit(*tab, ' '); + width = tab_length(line_tab); + free_tab(line_tab); + return (width); +} + + +void min_max_z(t_map *map, int i, int j) +{ + if (map->arr_vec[i][j].z > map->z_max) + map->z_max = map->arr_vec[i][j].z; + if (map->arr_vec[i][j].z < map->z_max) + map->z_min = map->arr_vec[i][j].z; +} + +void to_vec_arr(char **tab, t_map *map) +{ + char **line; + int i; + int j; + + map->y_max = tab_length(tab); + map->x_max = map_width(tab); + map->arr_vec = malloc(sizeof(t_vec *) * (map->y_max + 1)); + i = 0; + while (i <= map->y_max) + { + j = 0; + line = ft_strsplit(tab[i], ' '); + map->arr_vec[i] = malloc(sizeof(t_vec) * (map->x_max + 1)); + while (j <= map->x_max) + { + map->arr_vec[i][j].z = ft_atoi(line[j]); + map->arr_vec[i][j].x = j; + map->arr_vec[i][j].y = i; + min_max_z(map, i, j); + j++; + } + free_tab(line); + i++; } - map->x_max = x; - map->y_max = y; - map->z_max = max_z; - map->z_min = min_z; } t_map *map_init(char *path) @@ -82,13 +113,15 @@ t_map *map_init(char *path) t_map *map; map = malloc(sizeof(t_map)); + map->z_min = 0; + map->x_max = 0; if (!(str_map = to_str_map(path))) { + ft_putstr("Wrong path to a map! For help help put \"fdf --help\""); quit(0); } tab_map = ft_strsplit(str_map, '\n'); - map->lst_map = to_lst_map(tab_map); - map_dimensions(map); + to_vec_arr(tab_map, map); free(str_map); free_tab(tab_map); return (map); diff --git a/src/pnt_init.c b/src/pnt_init.c index f779249..b525783 100644 --- a/src/pnt_init.c +++ b/src/pnt_init.c @@ -2,24 +2,21 @@ void pnt_init(t_map *map) { - t_list *lst_map; int i; int j; float z; - lst_map = map->lst_map; map->arr_pnt = malloc(sizeof(t_pnt *) * (map->y_max + 1)); i = 0; - while (i <= map->y_max && lst_map) + while (i <= map->y_max) { map->arr_pnt[i] = malloc(sizeof(t_pnt) * (map->x_max + 1)); j = 0; while (j <= map->x_max) { - z = ((t_vec *)(lst_map->content))->z - map->z_min; + z = map->arr_vec[i][j].z - map->z_min; map->arr_pnt[i][j].z = z; first_color(&map->arr_pnt[i][j], z, map); - lst_map = lst_map->next; j++; } i++; diff --git a/src/render.c b/src/render.c index dfa237d..779a0f9 100644 --- a/src/render.c +++ b/src/render.c @@ -18,19 +18,16 @@ t_trnsf *transfor_mat_init() void transform(t_mat4 *mat, t_map *map) { - t_list *vec_list; int i; int j; - vec_list = map->lst_map; i = 0; while (i <= map->y_max) { j = 0; while (j <= map->x_max) { - vec_mat_mult(mat, vec_list->content, &map->arr_pnt[i][j]); - vec_list = vec_list->next; + vec_mat_mult(mat, &map->arr_vec[i][j], &map->arr_pnt[i][j]); j++; } i++; @@ -120,22 +117,27 @@ void draw(t_FDF *FDF) void center(t_map *map) { - t_list *l; double d_x; double d_y; double d_z; - + int i; + int j; d_x = map->x_max / 2.0f; d_y = map->y_max / 2.0f; d_z = map->z_max / 2.0f; - l = map->lst_map; - while (l) + i = 0; + while (i <= map->y_max) { - ((t_vec *)(l->content))->x = ((t_vec *)(l->content))->x - d_x; - ((t_vec *)(l->content))->y = ((t_vec *)(l->content))->y - d_y; - ((t_vec *)(l->content))->z = ((t_vec *)(l->content))->z - d_z; - l = l->next; + j = 0; + while (j <= map->x_max) + { + map->arr_vec[i][j].z -= d_z; + map->arr_vec[i][j].x -= d_x; + map->arr_vec[i][j].y -= d_y; + j++; + } + i++; } }