-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
148 lines (139 loc) · 3.76 KB
/
main.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
140
141
142
143
144
145
146
147
148
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jgourdin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/09/13 01:43:09 by jgourdin #+# #+# */
/* Updated: 2017/10/04 01:39:25 by jgourdin ### ########.fr */
/* */
/* ************************************************************************** */
#include "fdf.h"
int k_hook(int keycode, t_fdf *env)
{
if (keycode == ESC)
exit(42);
else if (keycode == LEFT || keycode == RIGHT
|| keycode == UP || keycode == DOWN)
{
if (keycode <= 124)
keycode == LEFT ? BASEX -= 10 : (BASEX += 10);
else
keycode == UP ? BASEY -= 10 : (BASEY += 10);
}
else if (keycode == PLUS || keycode == MINUS)
keycode == PLUS ? ZOOM += 5 : (ZOOM -= 5);
else if (keycode == AP || keycode == AM)
keycode == AP ? ANGLE += 10 : (ANGLE -= 10);
else if (keycode == 8)
{
COLORTMP = COLORTMP + 1100000;
COLORTMP2 = COLORTMP2 - 1100000;
}
else
return (0);
mlx_destroy_image(INIT, IMAGE);
lauchfactory(env);
return (0);
}
void init(t_fdf **env)
{
(*env)->mlx.init = mlx_init();
(*env)->options.zoom = 5;
(*env)->options.colortmp = 0xFFFFFF;
(*env)->largeur = 2300;
(*env)->hauteur = 1300;
(*env)->options.angle = 20;
(*env)->basex = 1;
(*env)->basey = 1;
(*env)->options.zed = 2;
(*env)->mlx.window = mlx_new_window((*env)->mlx.init, (*env)->largeur,
(*env)->hauteur, "Fdf");
while ((*env)->xmax * (*env)->ymax * (*env)->options.zoom <=
((*env)->largeur * (*env)->hauteur) / (*env)->options.zoom / 4)
(*env)->options.zoom = (*env)->options.zoom + 1;
(*env)->basex = ((*env)->largeur - ((*env)->xmax * (*env)->options.zoom));
(*env)->basey = ((*env)->hauteur - ((*env)->ymax *
(*env)->options.zoom)) / 2;
}
int *atoi_feed(char **line, t_fdf **env)
{
int *out;
int y;
y = 0;
while (line[y])
y++;
if ((*env)->xmax && y < (*env)->xmax)
{
return (NULL);
}
(*env)->xmax = y;
if (!(out = (int *)malloc(sizeof(int) * (y + 1))))
return (NULL);
y = 0;
while (line[y])
{
ft_atoi(line[y]) > (*env)->zmax ? (*env)->zmax = ft_atoi(line[y]) : 0;
ft_atoi(line[y]) > (*env)->zmin ? (*env)->zmin = ft_atoi(line[y]) : 0;
*out++ = ft_atoi(line[y]);
y++;
}
*out = '\0';
out = out - y;
return (out);
}
int count(t_fdf **env, char *file)
{
int i;
char buff[BUFF_SIZE + 1];
i = 0;
(*env)->rd = 0;
if (((*env)->fd = open(file, O_RDONLY)) < 0)
{
ft_putstr("Not valid Argument");
exit(42);
}
while (((*env)->rd = read((*env)->fd, buff, BUFF_SIZE)))
{
buff[(*env)->rd] = '\0';
(*env)->c = 0;
while (buff[(*env)->c])
{
if (buff[(*env)->c] == '\n')
i++;
(*env)->c++;
}
}
(*env)->ymax = i;
if (!((*env)->map.map = (int **)malloc(sizeof(int *) * (i + 1))))
return (0);
return (i);
}
int main(int argc, char **argv)
{
t_fdf *env;
char *line;
int i;
if (!(env = (t_fdf *)malloc(sizeof(t_fdf))))
return (0);
if (argc == 2)
{
count(&env, argv[1]);
env->zmax = 0;
env->zmin = 0;
env->map.fd = open(argv[1], O_RDONLY);
while ((get_next_line(env->map.fd, &line)) > 0)
{
if (!(*(MAP)++ = atoi_feed(ft_strsplit(line, ' '), &env)))
break ;
}
MAP = MAP - env->ymax;
init(&env);
lauchfactory(env);
mlx_hook(WIN, 2, 0, k_hook, env);
mlx_loop(INIT);
}
else
ft_putstr("Error : Need One Argument");
}