]> git.localhorst.tv Git - blank.git/blob - src/app/Runtime.cpp
move RandomWalk into new "ai" module
[blank.git] / src / app / Runtime.cpp
1 #include "Runtime.hpp"
2
3 #include "init.hpp"
4
5 #include <cctype>
6 #include <cstdlib>
7 #include <iostream>
8
9 using namespace std;
10
11
12 namespace blank {
13
14 Runtime::Runtime() noexcept
15 : name("blank")
16 , mode(NORMAL)
17 , n(0)
18 , t(0)
19 , config() {
20
21 }
22
23
24 void Runtime::ReadArgs(int argc, const char *const *argv) {
25         if (argc <= 0) return;
26         name = argv[0];
27
28         bool options = true;
29         bool error = false;
30
31         for (int i = 1; i < argc; ++i) {
32                 const char *arg = argv[i];
33                 if (!arg || arg[0] == '\0') {
34                         cerr << "warning: found empty argument at position " << i << endl;
35                         continue;
36                 }
37                 if (options && arg[0] == '-') {
38                         if (arg[1] == '\0') {
39                                 cerr << "warning: incomplete option list at position " << i << endl;
40                         } else if (arg[1] == '-') {
41                                 if (arg[2] == '\0') {
42                                         // stopper
43                                         options = false;
44                                 } else {
45                                         // long option
46                                         if (strcmp(arg + 2, "no-vsync") == 0) {
47                                                 config.vsync = false;
48                                         } else if (strcmp(arg + 2, "no-keyboard") == 0) {
49                                                 config.interface.keyboard_disabled = true;
50                                         } else if (strcmp(arg + 2, "no-mouse") == 0) {
51                                                 config.interface.mouse_disabled = true;
52                                         } else if (strcmp(arg + 2, "no-hud") == 0) {
53                                                 config.interface.visual_disabled = true;
54                                         } else if (strcmp(arg + 2, "no-audio") == 0) {
55                                                 config.interface.audio_disabled = true;
56                                         } else {
57                                                 cerr << "unknown option " << arg << endl;
58                                                 error = true;
59                                         }
60                                 }
61                         } else {
62                                 // short options
63                                 for (int j = 1; arg[j] != '\0'; ++j) {
64                                         switch (arg[j]) {
65                                                 case 'd':
66                                                         config.doublebuf = false;
67                                                         break;
68                                                 case 'm':
69                                                         ++i;
70                                                         if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
71                                                                 cerr << "missing argument to -m" << endl;
72                                                                 error = true;
73                                                         } else {
74                                                                 config.multisampling = strtoul(argv[i], nullptr, 10);
75                                                         }
76                                                         break;
77                                                 case 'n':
78                                                         ++i;
79                                                         if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
80                                                                 cerr << "missing argument to -n" << endl;
81                                                                 error = true;
82                                                         } else {
83                                                                 n = strtoul(argv[i], nullptr, 10);
84                                                         }
85                                                         break;
86                                                 case 's':
87                                                         ++i;
88                                                         if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
89                                                                 cerr << "missing argument to -s" << endl;
90                                                                 error = true;
91                                                         } else {
92                                                                 config.world.gen.solid_seed = strtoul(argv[i], nullptr, 10);
93                                                                 config.world.gen.type_seed = config.world.gen.solid_seed;
94                                                         }
95                                                         break;
96                                                 case 't':
97                                                         ++i;
98                                                         if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
99                                                                 cerr << "missing argument to -t" << endl;
100                                                                 error = true;
101                                                         } else {
102                                                                 t = strtoul(argv[i], nullptr, 10);
103                                                         }
104                                                         break;
105                                                 case '-':
106                                                         // stopper
107                                                         options = false;
108                                                         break;
109                                                 default:
110                                                         cerr << "unknown option " << arg[j] << endl;
111                                                         error = true;
112                                                         break;
113                                         }
114                                 }
115                         }
116                 } else {
117                         cerr << "unable to interpret argument "
118                                 << i << " (" << arg << ")" << endl;
119                         error = true;
120                 }
121         }
122
123         if (error) {
124                 mode = ERROR;
125                 return;
126         }
127
128         if (n > 0) {
129                 if (t > 0) {
130                         mode = FIXED_FRAME_LIMIT;
131                 } else {
132                         mode = FRAME_LIMIT;
133                 }
134         } else if (t > 0) {
135                 mode = TIME_LIMIT;
136         } else {
137                 mode = NORMAL;
138         }
139 }
140
141 int Runtime::Execute() {
142         if (mode == ERROR) {
143                 return 1;
144         }
145
146         Init init(config.doublebuf, config.multisampling);
147         Application app(init.window, config);
148         switch (mode) {
149                 default:
150                 case NORMAL:
151                         app.Run();
152                         break;
153                 case FRAME_LIMIT:
154                         app.RunN(n);
155                         break;
156                 case TIME_LIMIT:
157                         app.RunT(t);
158                         break;
159                 case FIXED_FRAME_LIMIT:
160                         app.RunS(n, t);
161                         break;
162         }
163         return 0;
164 }
165
166 }