Bläddra i källkod

Moved command logic into static Command methods

Thomas Buck 9 år sedan
förälder
incheckning
84c85a588c

+ 3
- 0
ChangeLog.md Visa fil

@@ -2,6 +2,9 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20140831 ]
6
+    * Moved command specific code from OpenRaider to static Command methods
7
+
5 8
     [ 20140829 ]
6 9
     * Moved key/action/string comparison functions into CommandBind
7 10
     * Added Renderflag command to toggle all flags in Render

+ 8
- 0
TODO.md Visa fil

@@ -8,6 +8,9 @@
8 8
 * Mesh has 2 different approaches of storing the same data (eg. mColors and mColorArray), but half of ‘em isn’t implemented. Unify this, probably even combining Mesh and StaticMesh...
9 9
 * Don’t use float everywhere just because (eg. float colors)
10 10
 * Add verbose command line flag for debug output also in release builds
11
+* Don’t depend on setup: no data or config files should be necessary
12
+* Be able to change configuration from within OpenRaider
13
+* Put log functionality outside of Console
11 14
 
12 15
 ## Bugs
13 16
 
@@ -21,9 +24,14 @@
21 24
 
22 25
 ## Future Features
23 26
 
27
+* Depend on physfs for easier file location management
24 28
 * Depend on libcdio, use it to read original CDs or CUE/TOC/ISO images
25 29
 * Depend on imgui for nicer GUIs?
26 30
 * Add ability to play the FMVs. Format? VLC can play them!
27 31
 * Cut TGA image reader, currently only used for menu background?!
28 32
     * Need useful, always available image writer alternative for screenshots then
29 33
 
34
+* Create abstract interface from Game/Menu/Console, use this to do real “windows” that can be layered independently and can receive keyboard/mouse input
35
+* Put keyboard/mouse/windowing logic into UI or UIManager class
36
+* Move small rest of OpenRaider into main or a config class or something, remove OpenRaider class
37
+

+ 0
- 7
include/OpenRaider.h Visa fil

@@ -8,11 +8,7 @@
8 8
 #ifndef _OPENRAIDER_H_
9 9
 #define _OPENRAIDER_H_
10 10
 
11
-#include <memory>
12 11
 #include <string>
13
-#include <vector>
14
-
15
-#include "commands/Command.h"
16 12
 
17 13
 /*!
18 14
  * \brief Main Game Singleton
@@ -57,9 +53,6 @@ public:
57 53
     KeyboardButton keyBindings[ActionEventCount];
58 54
     bool mRunning;
59 55
     bool mFPS;
60
-
61
-private:
62
-    std::vector<std::shared_ptr<Command>> commands;
63 56
 };
64 57
 
65 58
 OpenRaider &getOpenRaider();

+ 5
- 16
include/commands/Command.h Visa fil

@@ -20,24 +20,13 @@ public:
20 20
     virtual std::string brief() = 0;
21 21
     virtual void printHelp();
22 22
     virtual int execute(std::istream& args) = 0;
23
-};
24 23
 
25
-#define DECLARE_SIMPLE_CMD(x) \
26
-    class x : public Command { \
27
-    public: \
28
-        virtual std::string name(); \
29
-        virtual std::string brief(); \
30
-        virtual void printHelp(); \
31
-        virtual int execute(std::istream& args); \
32
-    }
24
+    static void fillCommandList();
25
+    static int command(std::string c);
33 26
 
34
-#define DECLARE_SIMPLE_CMD_NO_HELP(x) \
35
-    class x : public Command { \
36
-    public: \
37
-        virtual std::string name(); \
38
-        virtual std::string brief(); \
39
-        virtual int execute(std::istream& args); \
40
-    }
27
+private:
28
+    static std::vector<std::shared_ptr<Command>> commands;
29
+};
41 30
 
42 31
 #endif
43 32
 

+ 7
- 1
include/commands/CommandAnimate.h Visa fil

@@ -10,7 +10,13 @@
10 10
 
11 11
 #include "commands/Command.h"
12 12
 
13
-DECLARE_SIMPLE_CMD(CommandAnimate);
13
+class CommandAnimate : public Command {
14
+public:
15
+    virtual std::string name();
16
+    virtual std::string brief();
17
+    virtual void printHelp();
18
+    virtual int execute(std::istream& args);
19
+};
14 20
 
15 21
 #endif
16 22
 

+ 20
- 3
include/commands/CommandEngine.h Visa fil

@@ -10,11 +10,28 @@
10 10
 
11 11
 #include "commands/Command.h"
12 12
 
13
-DECLARE_SIMPLE_CMD(CommandLoad);
13
+class CommandLoad : public Command {
14
+public:
15
+    virtual std::string name();
16
+    virtual std::string brief();
17
+    virtual void printHelp();
18
+    virtual int execute(std::istream& args);
19
+};
14 20
 
15
-DECLARE_SIMPLE_CMD(CommandScreenshot);
21
+class CommandScreenshot : public Command {
22
+public:
23
+    virtual std::string name();
24
+    virtual std::string brief();
25
+    virtual void printHelp();
26
+    virtual int execute(std::istream& args);
27
+};
16 28
 
17
-DECLARE_SIMPLE_CMD_NO_HELP(CommandQuit);
29
+class CommandQuit : public Command {
30
+public:
31
+    virtual std::string name();
32
+    virtual std::string brief();
33
+    virtual int execute(std::istream& args);
34
+};
18 35
 
19 36
 #endif
20 37
 

+ 27
- 7
include/commands/CommandGame.h Visa fil

@@ -10,13 +10,33 @@
10 10
 
11 11
 #include "commands/Command.h"
12 12
 
13
-DECLARE_SIMPLE_CMD_NO_HELP(CommandPos);
14
-
15
-DECLARE_SIMPLE_CMD_NO_HELP(CommandViewmodel);
16
-
17
-DECLARE_SIMPLE_CMD_NO_HELP(CommandPigtail);
18
-
19
-DECLARE_SIMPLE_CMD_NO_HELP(CommandPonypos);
13
+class CommandPos : public Command {
14
+public:
15
+    virtual std::string name();
16
+    virtual std::string brief();
17
+    virtual int execute(std::istream& args);
18
+};
19
+
20
+class CommandViewmodel : public Command {
21
+public:
22
+    virtual std::string name();
23
+    virtual std::string brief();
24
+    virtual int execute(std::istream& args);
25
+};
26
+
27
+class CommandPigtail : public Command {
28
+public:
29
+    virtual std::string name();
30
+    virtual std::string brief();
31
+    virtual int execute(std::istream& args);
32
+};
33
+
34
+class CommandPonypos : public Command {
35
+public:
36
+    virtual std::string name();
37
+    virtual std::string brief();
38
+    virtual int execute(std::istream& args);
39
+};
20 40
 
21 41
 #endif
22 42
 

+ 7
- 1
include/commands/CommandMove.h Visa fil

@@ -10,7 +10,13 @@
10 10
 
11 11
 #include "commands/Command.h"
12 12
 
13
-DECLARE_SIMPLE_CMD(CommandMove);
13
+class CommandMove : public Command {
14
+public:
15
+    virtual std::string name();
16
+    virtual std::string brief();
17
+    virtual void printHelp();
18
+    virtual int execute(std::istream& args);
19
+};
14 20
 
15 21
 #endif
16 22
 

+ 7
- 1
include/commands/CommandRender.h Visa fil

@@ -10,7 +10,13 @@
10 10
 
11 11
 #include "commands/Command.h"
12 12
 
13
-DECLARE_SIMPLE_CMD(CommandMode);
13
+class CommandMode : public Command {
14
+public:
15
+    virtual std::string name();
16
+    virtual std::string brief();
17
+    virtual void printHelp();
18
+    virtual int execute(std::istream& args);
19
+};
14 20
 
15 21
 class CommandRenderflag : public Command {
16 22
 public:

+ 7
- 1
include/commands/CommandSet.h Visa fil

@@ -10,7 +10,13 @@
10 10
 
11 11
 #include "commands/Command.h"
12 12
 
13
-DECLARE_SIMPLE_CMD(CommandSet);
13
+class CommandSet : public Command {
14
+public:
15
+    virtual std::string name();
16
+    virtual std::string brief();
17
+    virtual void printHelp();
18
+    virtual int execute(std::istream& args);
19
+};
14 20
 
15 21
 #endif
16 22
 

+ 7
- 1
include/commands/CommandSound.h Visa fil

@@ -10,7 +10,13 @@
10 10
 
11 11
 #include "commands/Command.h"
12 12
 
13
-DECLARE_SIMPLE_CMD(CommandSound);
13
+class CommandSound : public Command {
14
+public:
15
+    virtual std::string name();
16
+    virtual std::string brief();
17
+    virtual void printHelp();
18
+    virtual int execute(std::istream& args);
19
+};
14 20
 
15 21
 #endif
16 22
 

+ 3
- 83
src/OpenRaider.cpp Visa fil

@@ -5,21 +5,11 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
-#include <cstdio>
9
-#include <cstring>
10 8
 #include <fstream>
11
-#include <iomanip>
12 9
 #include <sstream>
13 10
 
14 11
 #include "global.h"
15
-#include "commands/CommandAnimate.h"
16
-#include "commands/CommandBind.h"
17
-#include "commands/CommandEngine.h"
18
-#include "commands/CommandGame.h"
19
-#include "commands/CommandMove.h"
20
-#include "commands/CommandRender.h"
21
-#include "commands/CommandSet.h"
22
-#include "commands/CommandSound.h"
12
+#include "commands/Command.h"
23 13
 #include "Console.h"
24 14
 #include "Font.h"
25 15
 #include "Game.h"
@@ -44,20 +34,7 @@ OpenRaider::OpenRaider() {
44 34
     for (int i = 0; i < ActionEventCount; i++)
45 35
         keyBindings[i] = unknownKey;
46 36
 
47
-    commands.push_back(std::shared_ptr<Command>(new CommandLoad()));
48
-    commands.push_back(std::shared_ptr<Command>(new CommandBind()));
49
-    commands.push_back(std::shared_ptr<Command>(new CommandSet()));
50
-    commands.push_back(std::shared_ptr<Command>(new CommandScreenshot()));
51
-    commands.push_back(std::shared_ptr<Command>(new CommandAnimate()));
52
-    commands.push_back(std::shared_ptr<Command>(new CommandMove()));
53
-    commands.push_back(std::shared_ptr<Command>(new CommandMode()));
54
-    commands.push_back(std::shared_ptr<Command>(new CommandRenderflag()));
55
-    commands.push_back(std::shared_ptr<Command>(new CommandSound()));
56
-    commands.push_back(std::shared_ptr<Command>(new CommandPos()));
57
-    commands.push_back(std::shared_ptr<Command>(new CommandViewmodel()));
58
-    commands.push_back(std::shared_ptr<Command>(new CommandPigtail()));
59
-    commands.push_back(std::shared_ptr<Command>(new CommandPonypos()));
60
-    commands.push_back(std::shared_ptr<Command>(new CommandQuit()));
37
+    Command::fillCommandList();
61 38
 }
62 39
 
63 40
 OpenRaider::~OpenRaider() {
@@ -102,64 +79,7 @@ int OpenRaider::loadConfig(const char *config) {
102 79
 }
103 80
 
104 81
 int OpenRaider::command(std::string c) {
105
-    // Remove comment, if any
106
-    size_t comment = c.find_first_of('#');
107
-    if (comment != std::string::npos)
108
-        c.erase(comment);
109
-
110
-    // Execute command
111
-    std::stringstream command(c);
112
-    std::string cmd;
113
-    command >> cmd;
114
-    command >> std::boolalpha >> std::ws;
115
-
116
-    if (cmd.length() == 0)
117
-        return 0;
118
-
119
-    // Print help
120
-    if (cmd.compare("help") == 0) {
121
-        std::string arg;
122
-        command >> arg;
123
-        if (arg.length() == 0) {
124
-            // List all available commands
125
-            getConsole() << "Available commands:" << Console::endl;
126
-            getConsole() << std::right << std::setw(11);
127
-            getConsole() << "help" << " - print command help" << Console::endl;
128
-            for (auto &x : commands) {
129
-                if (x) {
130
-                    getConsole() << std::right << std::setw(11);
131
-                    getConsole() << x->name() << " - " << x->brief() << Console::endl;
132
-                }
133
-            }
134
-            getConsole() << "Use help COMMAND to get additional info" << Console::endl;
135
-            getConsole() << "Pass BOOLs as true or false" << Console::endl;
136
-            return 0;
137
-        } else {
138
-            // Show help for a specific command
139
-            for (auto &x : commands) {
140
-                if (x) {
141
-                    if (x->name().compare(arg) == 0) {
142
-                        x->printHelp();
143
-                        return 0;
144
-                    }
145
-                }
146
-            }
147
-            getConsole() << "Unknown command: \"" << arg << "\"" << Console::endl;
148
-            return -1;
149
-        }
150
-    }
151
-
152
-    // Execute command
153
-    for (auto &x : commands) {
154
-        if (x) {
155
-            if (x->name().compare(cmd) == 0) {
156
-                return x->execute(command);
157
-            }
158
-        }
159
-    }
160
-
161
-    getConsole() << "Unknown command: \"" << cmd << "\"" << Console::endl;
162
-    return -1;
82
+    return Command::command(c);
163 83
 }
164 84
 
165 85
 int OpenRaider::initialize() {

+ 93
- 1
src/commands/Command.cpp Visa fil

@@ -5,15 +5,107 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
+#include <iomanip>
9
+
8 10
 #include "global.h"
9 11
 #include "Console.h"
10 12
 #include "commands/Command.h"
13
+#include "commands/CommandAnimate.h"
14
+#include "commands/CommandBind.h"
15
+#include "commands/CommandEngine.h"
16
+#include "commands/CommandGame.h"
17
+#include "commands/CommandMove.h"
18
+#include "commands/CommandRender.h"
19
+#include "commands/CommandSet.h"
20
+#include "commands/CommandSound.h"
11 21
 
12
-Command::~Command() {
22
+std::vector<std::shared_ptr<Command>> Command::commands;
13 23
 
24
+Command::~Command() {
14 25
 }
15 26
 
16 27
 void Command::printHelp() {
17 28
     getConsole() << "No help available!" << Console::endl;
18 29
 }
19 30
 
31
+void Command::fillCommandList() {
32
+    commands.clear();
33
+    commands.push_back(std::shared_ptr<Command>(new CommandLoad()));
34
+    commands.push_back(std::shared_ptr<Command>(new CommandBind()));
35
+    commands.push_back(std::shared_ptr<Command>(new CommandSet()));
36
+    commands.push_back(std::shared_ptr<Command>(new CommandScreenshot()));
37
+    commands.push_back(std::shared_ptr<Command>(new CommandAnimate()));
38
+    commands.push_back(std::shared_ptr<Command>(new CommandMove()));
39
+    commands.push_back(std::shared_ptr<Command>(new CommandMode()));
40
+    commands.push_back(std::shared_ptr<Command>(new CommandRenderflag()));
41
+    commands.push_back(std::shared_ptr<Command>(new CommandSound()));
42
+    commands.push_back(std::shared_ptr<Command>(new CommandPos()));
43
+    commands.push_back(std::shared_ptr<Command>(new CommandViewmodel()));
44
+    commands.push_back(std::shared_ptr<Command>(new CommandPigtail()));
45
+    commands.push_back(std::shared_ptr<Command>(new CommandPonypos()));
46
+    commands.push_back(std::shared_ptr<Command>(new CommandQuit()));
47
+}
48
+
49
+int Command::command(std::string c) {
50
+    assert(commands.size() > 0);
51
+
52
+    // Remove comment, if any
53
+    size_t comment = c.find_first_of('#');
54
+    if (comment != std::string::npos)
55
+        c.erase(comment);
56
+
57
+    // Execute command
58
+    std::stringstream command(c);
59
+    std::string cmd;
60
+    command >> cmd;
61
+    command >> std::boolalpha >> std::ws;
62
+
63
+    if (cmd.length() == 0)
64
+        return 0;
65
+
66
+    // Print help
67
+    if (cmd.compare("help") == 0) {
68
+        std::string arg;
69
+        command >> arg;
70
+        if (arg.length() == 0) {
71
+            // List all available commands
72
+            getConsole() << "Available commands:" << Console::endl;
73
+            getConsole() << std::right << std::setw(11);
74
+            getConsole() << "help" << " - print command help" << Console::endl;
75
+            for (auto &x : commands) {
76
+                if (x) {
77
+                    getConsole() << std::right << std::setw(11);
78
+                    getConsole() << x->name() << " - " << x->brief() << Console::endl;
79
+                }
80
+            }
81
+            getConsole() << "Use help COMMAND to get additional info" << Console::endl;
82
+            getConsole() << "Pass BOOLs as true or false" << Console::endl;
83
+            return 0;
84
+        } else {
85
+            // Show help for a specific command
86
+            for (auto &x : commands) {
87
+                if (x) {
88
+                    if (x->name().compare(arg) == 0) {
89
+                        x->printHelp();
90
+                        return 0;
91
+                    }
92
+                }
93
+            }
94
+            getConsole() << "Unknown command: \"" << arg << "\"" << Console::endl;
95
+            return -1;
96
+        }
97
+    }
98
+
99
+    // Execute command
100
+    for (auto &x : commands) {
101
+        if (x) {
102
+            if (x->name().compare(cmd) == 0) {
103
+                return x->execute(command);
104
+            }
105
+        }
106
+    }
107
+
108
+    getConsole() << "Unknown command: \"" << cmd << "\"" << Console::endl;
109
+    return -1;
110
+}
111
+

+ 2
- 2
src/commands/CommandBind.cpp Visa fil

@@ -44,13 +44,13 @@ int CommandBind::execute(std::istream& args) {
44 44
         getConsole() << "Invalid use of bind-command" << Console::endl;
45 45
         return -1;
46 46
     } else {
47
-        ActionEvents e = stringToActionEvent(a.c_str());
47
+        ActionEvents e = stringToActionEvent(a);
48 48
         if (e == ActionEventCount) {
49 49
             getConsole() << "bind-Error: Unknown action (" << a << ")" << Console::endl;
50 50
             return -2;
51 51
         }
52 52
 
53
-        KeyboardButton c = stringToKeyboardButton(b.c_str());
53
+        KeyboardButton c = stringToKeyboardButton(b);
54 54
         if (c == unknownKey) {
55 55
             getConsole() << "bind-Error: Unknown key (" << b << ")" << Console::endl;
56 56
             return -3;

Laddar…
Avbryt
Spara