|
@@ -6,6 +6,7 @@
|
6
|
6
|
*/
|
7
|
7
|
|
8
|
8
|
#include <cstdio>
|
|
9
|
+#include <cstring>
|
9
|
10
|
#include <assert.h>
|
10
|
11
|
|
11
|
12
|
#include "WindowSDL.h"
|
|
@@ -20,7 +21,8 @@
|
20
|
21
|
OpenRaider::OpenRaider() {
|
21
|
22
|
mInit = false;
|
22
|
23
|
mRunning = false;
|
23
|
|
- mWindow = NULL;
|
|
24
|
+
|
|
25
|
+ mWindow = new WindowSDL();
|
24
|
26
|
}
|
25
|
27
|
|
26
|
28
|
OpenRaider::~OpenRaider() {
|
|
@@ -30,20 +32,158 @@ OpenRaider::~OpenRaider() {
|
30
|
32
|
|
31
|
33
|
int OpenRaider::loadConfig(const char *config) {
|
32
|
34
|
assert(config != NULL);
|
|
35
|
+ assert(config[0] != '\0');
|
33
|
36
|
|
34
|
37
|
char *configFile = fullPath(config, 0);
|
35
|
|
- printf("Trying to load \"%s\"...\n", configFile);
|
|
38
|
+ printf("Loading config from \"%s\"...\n", configFile);
|
|
39
|
+
|
|
40
|
+ FILE *f = fopen(configFile, "r");
|
|
41
|
+ if (f == NULL) {
|
|
42
|
+ printf("Could not open file!\n");
|
|
43
|
+ return -1;
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ char buffer[256];
|
|
47
|
+ while (fgets(buffer, 256, f) != NULL) {
|
|
48
|
+ command(buffer);
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ fclose(f);
|
|
52
|
+
|
|
53
|
+ return 0;
|
|
54
|
+}
|
|
55
|
+
|
|
56
|
+int OpenRaider::command(const char *command) {
|
|
57
|
+ assert(command != NULL);
|
|
58
|
+ assert(command[0] != '\0');
|
|
59
|
+
|
|
60
|
+ int returnValue = 0;
|
|
61
|
+ char *cmd = bufferString("%s", command);
|
|
62
|
+ size_t length = strlen(cmd);
|
|
63
|
+
|
|
64
|
+ // Command ends at '\n' or # when a comment begins
|
|
65
|
+ for (size_t i = 0; i < length; i++) {
|
|
66
|
+ if (cmd[i] == '\n' || cmd[i] == '#') {
|
|
67
|
+ cmd[i] = '\0';
|
|
68
|
+ break;
|
|
69
|
+ }
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ char *token = strtok(cmd, " \t");
|
|
73
|
+ if (token != NULL) {
|
|
74
|
+ // token is the command to execute
|
|
75
|
+ // get arguments
|
|
76
|
+ std::vector<char *> args;
|
|
77
|
+ char *next;
|
|
78
|
+ while ((next = strtok(NULL, " \t")) != NULL) {
|
|
79
|
+ args.push_back(next);
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ // Execute
|
|
83
|
+ returnValue = this->command(token, &args);
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+ free(cmd);
|
|
87
|
+ return returnValue;
|
|
88
|
+}
|
|
89
|
+
|
|
90
|
+int OpenRaider::command(const char *command, std::vector<char *> *args) {
|
|
91
|
+ assert(command != NULL);
|
|
92
|
+ assert(command[0] != '\0');
|
|
93
|
+ assert(args != NULL);
|
|
94
|
+
|
|
95
|
+ if (strcmp(command, "set") == 0) {
|
|
96
|
+ if (args->size() != 2) {
|
|
97
|
+ printf("Invalid use of set-command ");
|
|
98
|
+ printStringVector(args);
|
|
99
|
+ printf("\n");
|
|
100
|
+ return -2;
|
|
101
|
+ } else {
|
|
102
|
+ return set(args->at(0), args->at(1));
|
|
103
|
+ }
|
|
104
|
+ } else if (strcmp(command, "bind") == 0) {
|
|
105
|
+ if (args->size() != 2) {
|
|
106
|
+ printf("Invalid use of bind-command ");
|
|
107
|
+ printStringVector(args);
|
|
108
|
+ printf("\n");
|
|
109
|
+ return -3;
|
|
110
|
+ } else {
|
|
111
|
+ return bind(args->at(0), args->at(1));
|
|
112
|
+ }
|
|
113
|
+ } else {
|
|
114
|
+ printf("Unknown command: %s ", command);
|
|
115
|
+ printStringVector(args);
|
|
116
|
+ printf("\n");
|
|
117
|
+ return -1;
|
|
118
|
+ }
|
|
119
|
+}
|
|
120
|
+
|
|
121
|
+int OpenRaider::set(const char *var, const char *value) {
|
|
122
|
+ if (strcmp(var, "size") == 0) {
|
|
123
|
+ // value has format like "\"1024x768\""
|
|
124
|
+ unsigned int w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT;
|
|
125
|
+ if (sscanf(value, "\"%dx%d\"", &w, &h) != 2) {
|
|
126
|
+ printf("set-size-Error: Invalid value (%s)\n", value);
|
|
127
|
+ return -2;
|
|
128
|
+ }
|
|
129
|
+ mWindow->setSize(w, h);
|
|
130
|
+ } else if (strcmp(var, "fullscreen") == 0) {
|
|
131
|
+
|
|
132
|
+ } else if (strcmp(var, "gldriver") == 0) {
|
|
133
|
+
|
|
134
|
+ } else if (strcmp(var, "audio") == 0) {
|
|
135
|
+
|
|
136
|
+ } else if (strcmp(var, "volume") == 0) {
|
|
137
|
+
|
|
138
|
+ } else if (strcmp(var, "mouse_x") == 0) {
|
|
139
|
+
|
|
140
|
+ } else if (strcmp(var, "mouse_y") == 0) {
|
|
141
|
+
|
|
142
|
+ } else if (strcmp(var, "basedir") == 0) {
|
|
143
|
+
|
|
144
|
+ } else if (strcmp(var, "pakdir") == 0) {
|
|
145
|
+
|
|
146
|
+ } else if (strcmp(var, "audiodir") == 0) {
|
|
147
|
+
|
|
148
|
+ } else if (strcmp(var, "datadir") == 0) {
|
|
149
|
+
|
|
150
|
+ } else if (strcmp(var, "font") == 0) {
|
|
151
|
+
|
|
152
|
+ } else {
|
|
153
|
+ printf("set-Error: Unknown variable (%s = %s)\n", var, value);
|
|
154
|
+ return -1;
|
|
155
|
+ }
|
36
|
156
|
|
37
|
157
|
return 0;
|
38
|
158
|
}
|
39
|
159
|
|
|
160
|
+int OpenRaider::bind(const char *action, const char *key) {
|
|
161
|
+ if (strcmp(action, "console") == 0) {
|
|
162
|
+
|
|
163
|
+ } else if (strcmp(action, "forward") == 0) {
|
|
164
|
+
|
|
165
|
+ } else if (strcmp(action, "backward") == 0) {
|
|
166
|
+
|
|
167
|
+ } else if (strcmp(action, "jump") == 0) {
|
|
168
|
+
|
|
169
|
+ } else if (strcmp(action, "crouch") == 0) {
|
|
170
|
+
|
|
171
|
+ } else if (strcmp(action, "left") == 0) {
|
|
172
|
+
|
|
173
|
+ } else if (strcmp(action, "right") == 0) {
|
|
174
|
+
|
|
175
|
+ } else {
|
|
176
|
+ printf("bind-Error: Unknown action (%s --> %s)\n", key, action);
|
|
177
|
+ return -1;
|
|
178
|
+ }
|
|
179
|
+ return 0;
|
|
180
|
+}
|
|
181
|
+
|
40
|
182
|
int OpenRaider::initialize() {
|
41
|
|
- assert(mWindow == NULL);
|
42
|
183
|
assert(mInit == false);
|
43
|
184
|
assert(mRunning == false);
|
44
|
185
|
|
45
|
186
|
// Initialize Windowing
|
46
|
|
- mWindow = new WindowSDL();
|
47
|
187
|
if (mWindow->initialize() != 0)
|
48
|
188
|
return -1;
|
49
|
189
|
|