]> git.localhorst.tv Git - blank.git/blob - src/runtime.cpp
minor optimizations in chunk
[blank.git] / src / runtime.cpp
1 #include "runtime.hpp"
2
3 #include "app.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 {
55                                                 cerr << "unknown option " << arg << endl;
56                                                 error = true;
57                                         }
58                                 }
59                         } else {
60                                 // short options
61                                 for (int j = 1; arg[j] != '\0'; ++j) {
62                                         switch (arg[j]) {
63                                                 case 'd':
64                                                         config.doublebuf = false;
65                                                         break;
66                                                 case 'm':
67                                                         ++i;
68                                                         if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
69                                                                 cerr << "missing argument to -m" << endl;
70                                                                 error = true;
71                                                         } else {
72                                                                 config.multisampling = strtoul(argv[i], nullptr, 10);
73                                                         }
74                                                         break;
75                                                 case 'n':
76                                                         ++i;
77                                                         if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
78                                                                 cerr << "missing argument to -n" << endl;
79                                                                 error = true;
80                                                         } else {
81                                                                 n = strtoul(argv[i], nullptr, 10);
82                                                         }
83                                                         break;
84                                                 case 's':
85                                                         ++i;
86                                                         if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
87                                                                 cerr << "missing argument to -s" << endl;
88                                                                 error = true;
89                                                         } else {
90                                                                 config.world.gen.solid_seed = strtoul(argv[i], nullptr, 10);
91                                                                 config.world.gen.type_seed = config.world.gen.solid_seed;
92                                                         }
93                                                         break;
94                                                 case 't':
95                                                         ++i;
96                                                         if (i >= argc || argv[i] == nullptr || argv[i][0] == '\0') {
97                                                                 cerr << "missing argument to -t" << endl;
98                                                                 error = true;
99                                                         } else {
100                                                                 t = strtoul(argv[i], nullptr, 10);
101                                                         }
102                                                         break;
103                                                 case '-':
104                                                         // stopper
105                                                         options = false;
106                                                         break;
107                                                 default:
108                                                         cerr << "unknown option " << arg[j] << endl;
109                                                         error = true;
110                                                         break;
111                                         }
112                                 }
113                         }
114                 } else if (isdigit(arg[0])) {
115                         // positional number interpreted as -n
116                         n = strtoul(arg, nullptr, 10);
117                 } else {
118                         cerr << "unable to interpret argument "
119                                 << i << " (" << arg << ")" << endl;
120                         error = true;
121                 }
122         }
123
124         if (error) {
125                 mode = ERROR;
126                 return;
127         }
128
129         if (n > 0) {
130                 if (t > 0) {
131                         mode = FIXED_FRAME_LIMIT;
132                 } else {
133                         mode = FRAME_LIMIT;
134                 }
135         } else if (t > 0) {
136                 mode = TIME_LIMIT;
137         } else {
138                 mode = NORMAL;
139         }
140 }
141
142 int Runtime::Execute() {
143         if (mode == ERROR) {
144                 return 1;
145         }
146
147         Application app(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 }