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