-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_01.c
139 lines (132 loc) · 3.72 KB
/
extract_01.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* extract_01.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aviholai <aviholai@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 15:50:39 by aviholai #+# #+# */
/* Updated: 2022/10/19 18:02:49 by aviholai ### ########.fr */
/* */
/* ************************************************************************** */
#include "filsdefer.h"
static int open_read_projection(t_vars *v)
{
v->fd = open(v->file, O_RDONLY);
if (v->fd == -1)
return (error(OPEN_FAIL));
v->ret = read(v->fd, v->buf, MAX_READ);
if (v->ret == -1)
return (error(READ_FAIL));
if (v->parallel_mode == PARALLEL_TRUE)
{
v->x_pos = v->x_start_pos;
mlx_string_put(v->mlx, v->win, 60, 20, DAWN,
"[ P a r a l l e l ] Projection Mode.");
}
if (v->parallel_mode == PARALLEL_FALSE)
{
v->x_pos = v->x_start_pos;
mlx_string_put(v->mlx, v->win, 60, 20, DAWN,
"[ I s o m e t r i c ] Projection Mode.");
}
mlx_string_put(v->mlx, v->win, 60, 40, NIGHT,
"| TAB to change Mode. | ^ < v > to move. | + - to zoom. | * / to adjust altitude. | ESC to quit. |");
return (0);
}
char *write_coordinate(t_vars *v)
{
char *coordinate;
int i2;
coordinate = (char *)malloc(sizeof(char) * (ft_strlen(v->buf) + 1));
if (coordinate == NULL)
return (NULL);
i2 = 0;
while (v->buf[v->i] != ' ' && v->buf[v->i] != '\n' && v->buf[v->i])
{
coordinate[i2] = v->buf[v->i];
v->i++;
i2++;
}
while (coordinate[i2])
{
coordinate[i2] = '\0';
i2++;
}
return (coordinate);
}
static int draw_pixel(t_vars *v)
{
v->coordinate = write_coordinate(v);
if (v->coordinate == NULL)
return (-1);
v->depth = depth_parser(v->coordinate);
v->color = color_parser(v->coordinate, v->depth, v);
free(v->coordinate);
if (v->parallel_mode == PARALLEL_FALSE)
v->y_pos += ((v->depth * v->altitude) * -1);
mlx_pixel_put(v->mlx, v->win, v->x_pos, v->y_pos, v->color);
draw_line(v);
v->log_x[v->cl] = v->x_pos;
v->log_y[v->cl] = v->y_pos;
v->prev_x = v->x_pos;
v->prev_y = v->y_pos;
if (v->parallel_mode == PARALLEL_TRUE)
v->x_pos += v->increment;
else if (v->parallel_mode == PARALLEL_FALSE)
{
v->x_pos += v->isometric_increment;
v->y_pos += (v->isometric_increment + (v->depth * v->altitude));
}
v->cl++;
return (0);
}
static int graphic_loop(t_vars *v)
{
while (v->i < MAX_READ)
{
if ((v->buf[v->i] != ' ' && v->buf[v->i] != '\n') && v->buf[v->i])
draw_pixel(v);
if (v->buf[v->i] == '\n')
{
v->nl++;
v->cl = 0;
v->prev_x = NEW_LINE;
v->prev_y = NEW_LINE;
if (v->parallel_mode == PARALLEL_TRUE)
{
v->x_pos = v->x_start_pos;
v->y_pos += v->increment;
}
else if (v->parallel_mode == PARALLEL_FALSE)
{
v->x_pos = (int)(v->x_start_pos - ((v->isometric_increment) * v->nl));
v->y_pos = (int)(v->y_start_pos + ((v->isometric_increment) * v->nl));
}
}
v->i++;
}
return (0);
}
int projection(t_vars *v)
{
v->i = 0;
v->nl = 0;
v->cl = 0;
v->y_pos = v->y_start_pos;
v->prev_x = NEW_LINE;
v->prev_y = NEW_LINE;
open_read_projection(v);
while (v->ret)
{
v->buf[v->ret] = '\0';
if (graphic_loop(v) == -1)
return (-1);
v->ret = read(v->fd, v->buf, MAX_READ);
if (v->ret < 0)
return (READ_FAIL);
}
if (close(v->fd) == -1)
return (error(CLOSE_FAIL));
return (0);
}