Browse Source

Started implementing command system.

Config file now ends with ini instead of init.
Thomas Buck 10 years ago
parent
commit
03bb3e01ed
10 changed files with 174 additions and 11 deletions
  1. 2
    2
      README.md
  2. 1
    1
      cmake/setup.sh
  3. 2
    2
      cmake/setup_mac.sh
  4. 0
    0
      data/OpenRaider.ini
  5. 1
    1
      include/Config.h.in
  6. 10
    0
      include/OpenRaider.h
  7. 3
    0
      include/utils/strings.h
  8. 1
    1
      src/CMakeLists.txt
  9. 144
    4
      src/OpenRaider.cpp
  10. 10
    0
      src/utils/strings.cpp

+ 2
- 2
README.md View File

13
 OpenRaider needs some configuration files, and level data and assets from custom levels or the Tomb Raider games.
13
 OpenRaider needs some configuration files, and level data and assets from custom levels or the Tomb Raider games.
14
 These are stored in `~/.OpenRaider`. Running `make setup` will create/copy the necessary files and directories.
14
 These are stored in `~/.OpenRaider`. Running `make setup` will create/copy the necessary files and directories.
15
 
15
 
16
-You still need to add level files in `~/.OpenRaider/paks/` and add them to `~/.OpenRaider/OpenRaider.init`.
16
+You still need to add level files in `~/.OpenRaider/paks/` and add them to `~/.OpenRaider/OpenRaider.ini`.
17
 Dust off your old Tomb Raider CDs or grab some [custom levels](http://www.aspidetr.com/levels/yvel-woods-v1-5/) from the interwebs.
17
 Dust off your old Tomb Raider CDs or grab some [custom levels](http://www.aspidetr.com/levels/yvel-woods-v1-5/) from the interwebs.
18
 
18
 
19
 ## Documentation
19
 ## Documentation
71
 
71
 
72
 ### Configuration file
72
 ### Configuration file
73
 
73
 
74
-OpenRaider will try to load `~/.OpenRaider/OpenRaider.init` or, if it doesn't exist, `OpenRaider.init` from the current directory.
74
+OpenRaider will try to load `~/.OpenRaider/OpenRaider.ini` or, if it doesn't exist, `OpenRaider.ini` from the current directory.
75
 Running `make setup` will create a minimal configuration in your home directory.
75
 Running `make setup` will create a minimal configuration in your home directory.
76
 
76
 
77
 The configuration file format is very simple:
77
 The configuration file format is very simple:

+ 1
- 1
cmake/setup.sh View File

2
 
2
 
3
 echo "Setting up OpenRaider for user $USER..."
3
 echo "Setting up OpenRaider for user $USER..."
4
 mkdir -p ~/.OpenRaider
4
 mkdir -p ~/.OpenRaider
5
-cp data/OpenRaider.init ~/.OpenRaider/
5
+cp data/OpenRaider.ini ~/.OpenRaider/
6
 mkdir -p ~/.OpenRaider/paks
6
 mkdir -p ~/.OpenRaider/paks
7
 mkdir -p ~/.OpenRaider/music
7
 mkdir -p ~/.OpenRaider/music
8
 mkdir -p ~/.OpenRaider/data
8
 mkdir -p ~/.OpenRaider/data

+ 2
- 2
cmake/setup_mac.sh View File

3
 if [ ! -d "${HOME}/.OpenRaider" ]; then
3
 if [ ! -d "${HOME}/.OpenRaider" ]; then
4
     echo "Setting up OpenRaider for user $USER..."
4
     echo "Setting up OpenRaider for user $USER..."
5
     mkdir -p ~/.OpenRaider
5
     mkdir -p ~/.OpenRaider
6
-    cp ../Resources/defaults/OpenRaider.init ~/.OpenRaider/
6
+    cp ../Resources/defaults/OpenRaider.ini ~/.OpenRaider/
7
     mkdir -p ~/.OpenRaider/paks
7
     mkdir -p ~/.OpenRaider/paks
8
     mkdir -p ~/.OpenRaider/music
8
     mkdir -p ~/.OpenRaider/music
9
     mkdir -p ~/.OpenRaider/data
9
     mkdir -p ~/.OpenRaider/data
11
     cp ../Resources/defaults/*.tga ~/.OpenRaider/data
11
     cp ../Resources/defaults/*.tga ~/.OpenRaider/data
12
     cp ../Resources/defaults/*.ttf ~/.OpenRaider/data
12
     cp ../Resources/defaults/*.ttf ~/.OpenRaider/data
13
     echo "DONE"
13
     echo "DONE"
14
-    osascript -e 'tell app "System Events" to display alert "Initial Configuration stored in ~/.OpenRaider\n\nView and edit OpenRaider.init to your needs..."'
14
+    osascript -e 'tell app "System Events" to display alert "Initial Configuration stored in ~/.OpenRaider\n\nView and edit OpenRaider.ini to your needs..."'
15
 fi
15
 fi
16
 if [[ ! -n `find "${HOME}/.OpenRaider/paks" -type f -exec echo Found {} \;` ]]; then
16
 if [[ ! -n `find "${HOME}/.OpenRaider/paks" -type f -exec echo Found {} \;` ]]; then
17
     echo "Missing level files!"
17
     echo "Missing level files!"

data/OpenRaider.init → data/OpenRaider.ini View File


+ 1
- 1
include/Config.h.in View File

2
 #define BUILD_HOST "@OpenRaider_BUILD_HOST@ @OpenRaider_HOSTNAME@"
2
 #define BUILD_HOST "@OpenRaider_BUILD_HOST@ @OpenRaider_HOSTNAME@"
3
 
3
 
4
 #define DEFAULT_CONFIG_PATH "~/.OpenRaider/"
4
 #define DEFAULT_CONFIG_PATH "~/.OpenRaider/"
5
-#define DEFAULT_CONFIG_FILE "OpenRaider.init"
5
+#define DEFAULT_CONFIG_FILE "OpenRaider.ini"
6
 #define DEFAULT_WIDTH 640
6
 #define DEFAULT_WIDTH 640
7
 #define DEFAULT_HEIGHT 480
7
 #define DEFAULT_HEIGHT 480
8
 
8
 

+ 10
- 0
include/OpenRaider.h View File

8
 #ifndef _OPENRAIDER_H_
8
 #ifndef _OPENRAIDER_H_
9
 #define _OPENRAIDER_H_
9
 #define _OPENRAIDER_H_
10
 
10
 
11
+#include <vector>
12
+
11
 #include "Window.h"
13
 #include "Window.h"
12
 
14
 
13
 /*!
15
 /*!
32
      */
34
      */
33
     int loadConfig(const char *config);
35
     int loadConfig(const char *config);
34
 
36
 
37
+    int command(const char *command);
38
+
39
+    int command(const char *command, std::vector<char *> *args);
40
+
41
+    int set(const char *var, const char *value);
42
+
43
+    int bind(const char *action, const char *key);
44
+
35
     int initialize();
45
     int initialize();
36
 
46
 
37
     void run();
47
     void run();

+ 3
- 0
include/utils/strings.h View File

10
 #define _UTILS_STRINGS_H_
10
 #define _UTILS_STRINGS_H_
11
 
11
 
12
 #include <cstdarg>
12
 #include <cstdarg>
13
+#include <vector>
14
+
15
+void printStringVector(std::vector<char *> *args);
13
 
16
 
14
 /*!
17
 /*!
15
  * \brief Check if a string ends with another string.
18
  * \brief Check if a string ends with another string.

+ 1
- 1
src/CMakeLists.txt View File

72
     set (SRCS ${SRCS} ${MAC_ICON})
72
     set (SRCS ${SRCS} ${MAC_ICON})
73
 
73
 
74
     # Copy Data
74
     # Copy Data
75
-    set (MAC_DATA "../data/OpenRaider.init" "../data/test.ttf")
75
+    set (MAC_DATA "../data/OpenRaider.ini" "../data/test.ttf")
76
     set (MAC_DATA ${MAC_DATA} "../data/snow.tga" "../data/snow2.tga" "../data/splash.tga")
76
     set (MAC_DATA ${MAC_DATA} "../data/snow.tga" "../data/snow2.tga" "../data/splash.tga")
77
     set_source_files_properties (${MAC_DATA} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/defaults)
77
     set_source_files_properties (${MAC_DATA} PROPERTIES MACOSX_PACKAGE_LOCATION Resources/defaults)
78
     set (SRCS ${SRCS} ${MAC_DATA})
78
     set (SRCS ${SRCS} ${MAC_DATA})

+ 144
- 4
src/OpenRaider.cpp View File

6
  */
6
  */
7
 
7
 
8
 #include <cstdio>
8
 #include <cstdio>
9
+#include <cstring>
9
 #include <assert.h>
10
 #include <assert.h>
10
 
11
 
11
 #include "WindowSDL.h"
12
 #include "WindowSDL.h"
20
 OpenRaider::OpenRaider() {
21
 OpenRaider::OpenRaider() {
21
     mInit = false;
22
     mInit = false;
22
     mRunning = false;
23
     mRunning = false;
23
-    mWindow = NULL;
24
+
25
+    mWindow = new WindowSDL();
24
 }
26
 }
25
 
27
 
26
 OpenRaider::~OpenRaider() {
28
 OpenRaider::~OpenRaider() {
30
 
32
 
31
 int OpenRaider::loadConfig(const char *config) {
33
 int OpenRaider::loadConfig(const char *config) {
32
     assert(config != NULL);
34
     assert(config != NULL);
35
+    assert(config[0] != '\0');
33
 
36
 
34
     char *configFile = fullPath(config, 0);
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
     return 0;
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
 int OpenRaider::initialize() {
182
 int OpenRaider::initialize() {
41
-    assert(mWindow == NULL);
42
     assert(mInit == false);
183
     assert(mInit == false);
43
     assert(mRunning == false);
184
     assert(mRunning == false);
44
 
185
 
45
     // Initialize Windowing
186
     // Initialize Windowing
46
-    mWindow = new WindowSDL();
47
     if (mWindow->initialize() != 0)
187
     if (mWindow->initialize() != 0)
48
         return -1;
188
         return -1;
49
 
189
 

+ 10
- 0
src/utils/strings.cpp View File

17
 
17
 
18
 #include "utils/strings.h"
18
 #include "utils/strings.h"
19
 
19
 
20
+void printStringVector(std::vector<char *> *args) {
21
+    printf("(");
22
+    for (std::vector<char *>::size_type i = 0; i < args->size(); i++) {
23
+        printf("%s", args->at(i));
24
+        if (i < (args->size() - 1))
25
+            printf(" ");
26
+    }
27
+    printf(")");
28
+}
29
+
20
 bool stringEndsWith(const char *str, const char *suffix) {
30
 bool stringEndsWith(const char *str, const char *suffix) {
21
     assert(str != NULL);
31
     assert(str != NULL);
22
     assert(suffix != NULL);
32
     assert(suffix != NULL);

Loading…
Cancel
Save