]> git.localhorst.tv Git - blank.git/blob - src/app/Runtime.cpp
some experiments with sound
[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 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 }