]> git.localhorst.tv Git - blank.git/blob - src/app/Runtime.cpp
e5f74b459ec14d38a7b589b81623d470056d1c8f
[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 if (strcmp(arg + 2, "no-audio") == 0) {
53                                                 config.interface.audio_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 {
115                         cerr << "unable to interpret argument "
116                                 << i << " (" << arg << ")" << endl;
117                         error = true;
118                 }
119         }
120
121         if (error) {
122                 mode = ERROR;
123                 return;
124         }
125
126         if (n > 0) {
127                 if (t > 0) {
128                         mode = FIXED_FRAME_LIMIT;
129                 } else {
130                         mode = FRAME_LIMIT;
131                 }
132         } else if (t > 0) {
133                 mode = TIME_LIMIT;
134         } else {
135                 mode = NORMAL;
136         }
137 }
138
139 int Runtime::Execute() {
140         if (mode == ERROR) {
141                 return 1;
142         }
143
144         Application app(config);
145         switch (mode) {
146                 default:
147                 case NORMAL:
148                         app.Run();
149                         break;
150                 case FRAME_LIMIT:
151                         app.RunN(n);
152                         break;
153                 case TIME_LIMIT:
154                         app.RunT(t);
155                         break;
156                 case FIXED_FRAME_LIMIT:
157                         app.RunS(n, t);
158                         break;
159         }
160         return 0;
161 }
162
163 }