-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
113 lines (95 loc) · 3.08 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
/* Copyright (c) 2008-2021 Alexey Ozeritskiy
* All rights reserved.
*/
#include <stdio.h>
#include <stdlib.h>
#include "lsystem.h"
#include "lsystem_scanner.h"
#include "lsystem_parser.h"
void usage(const char * n)
{
printf("usage:\n");
printf("%s [-f filename] [-t type] [-l order] [-w width] [-h height] \n", n);
printf("-f -- read lsystem from file (stdin - default)\n");
printf("-t -- points save type: \n");
printf("\t\t png -- PNG\n");
printf("\t\t txt -- txt\n");
printf("-l -- default order \n");
exit(0);
}
static void log_function(void* unused, const char* text) {
fprintf(stderr, "%s", text);
}
int main(int argc, char** argv) {
struct Parser* p = parser_new(log_function, NULL);
struct Group* g = NULL;
FILE * f = 0;
int level = 0;
int type = 2;
struct Lines lines;
memset(&lines, 0, sizeof(lines));
int w = 1024;
int h = 1024;
for (int i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "-f") && i < argc - 1) {
f = fopen(argv[i + 1], "r");
} else if (!strcmp(argv[i], "-l") && i < argc - 1) {
level = atoi(argv[i + 1]);
} else if (!strcmp(argv[i], "-w") && i < argc - 1) {
w = atoi(argv[i+1]);
} else if (!strcmp(argv[i], "-h") && i < argc - 1) {
h = atoi(argv[i+1]);
} else if (!strcmp(argv[i], "-t") && i < argc - 1) {
if (!strcmp(argv[i + 1], "txt")) {
type = 0;
} else if (!strcmp(argv[i + 1], "png")) {
type = 2;
}
} else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
usage(argv[0]);
}
}
if (f) yyrestart(f);
while (yyparse(p) && !parser_has_error(p));
if (parser_has_error(p)) {
parser_free(p);
return -1;
}
for (g = parser_group_start(p); g != NULL; g = group_next(g))
{
double min_x, max_x, min_y, max_y;
int l;
char* W;
if (!group_check(g)) continue;
l = level;
if (group_get_order(g) != 0 && level == 0) l = group_get_order(g);
fprintf(stderr, "building %s\n", group_get_name(g));
fprintf(stderr, " using order %d\n", l);
W = lsystem(g, l);
fprintf(stderr, " lsystem done\n");
//fprintf(stderr, " '%s'\n", W);
lines.n = 0;
turtle(&lines, g, W);
free(W);
fprintf(stderr, " turtle done\n");
lines_normilize(lines.lines, lines.n, &min_x, &max_x, &min_y, &max_y);
fprintf(stderr, " normilize done\n");
switch (type) {
case 0:
lines_save_txt(group_get_name(g), lines.lines, lines.n, min_x, max_x, min_y, max_y);
break;
case 2:
lines_save_png(group_get_name(g), lines.lines, lines.n, min_x, max_x, min_y, max_y, w, h);
break;
default:
fprintf(stderr, "Unknown save type: %d\n", type);
return -1;
break;
}
fprintf(stderr, " saving done\n");
}
free(lines.lines);
parser_free(p);
if (f) fclose(f);
return 0;
}