Browse Source

Global objects instead of pointers. Initialization in OpenRaider

Thomas Buck 10 years ago
parent
commit
da7cda9f16
3 changed files with 69 additions and 102 deletions
  1. 2
    0
      include/OpenRaider.h
  2. 40
    0
      src/OpenRaider.cpp
  3. 27
    102
      src/main.cpp

+ 2
- 0
include/OpenRaider.h View File

@@ -33,6 +33,8 @@ public:
33 33
      */
34 34
     ~OpenRaider();
35 35
 
36
+    int initialize();
37
+
36 38
     /*!
37 39
      * \brief Load the configuration file
38 40
      * \returns 0 on success

+ 40
- 0
src/OpenRaider.cpp View File

@@ -56,6 +56,46 @@ OpenRaider::~OpenRaider() {
56 56
     }
57 57
 }
58 58
 
59
+int OpenRaider::initialize() {
60
+    // Initialize Windowing
61
+    int error = getWindow().initialize();
62
+    if (error != 0) {
63
+        printf("Could not initialize Window (%d)!\n", error);
64
+        return -1;
65
+    }
66
+
67
+    // Initialize OpenGL
68
+    error = getWindow().initializeGL();
69
+    if (error != 0) {
70
+        printf("Could not initialize OpenGL (%d)!\n", error);
71
+        return -2;
72
+    }
73
+
74
+    error = getWindow().initializeFont();
75
+    if (error != 0) {
76
+        printf("Could not initialize SDL-TTF (%d)!\n", error);
77
+        return -3;
78
+    }
79
+
80
+    error = getSound().initialize();
81
+    if (error != 0) {
82
+        printf("Could not initialize Sound (%d)!\n", error);
83
+        return -4;
84
+    }
85
+
86
+    // Initialize game engine
87
+    error = getGame().initialize();
88
+    if (error != 0) {
89
+        printf("Could not initialize Game Engine (%d)!\n", error);
90
+        return -5;
91
+    }
92
+
93
+    getMenu().setVisible(true);
94
+    systemTimerReset();
95
+
96
+    return 0;
97
+}
98
+
59 99
 int OpenRaider::loadConfig(const char *config) {
60 100
     assert(config != NULL);
61 101
     assert(config[0] != '\0');

+ 27
- 102
src/main.cpp View File

@@ -17,89 +17,54 @@
17 17
 #include "SoundAL.h"
18 18
 #include "WindowSDL.h"
19 19
 
20
-Camera *gCamera = NULL;
21
-Console *gConsole = NULL;
22
-Game *gGame = NULL;
23
-Menu *gMenu = NULL;
24
-OpenRaider *gOpenRaider = NULL;
25
-Render *gRender = NULL;
26
-Sound *gSound = NULL;
27
-Window *gWindow = NULL;
28
-World *gWorld = NULL;
20
+Camera gCamera;
21
+Console gConsole;
22
+Game gGame;
23
+Menu gMenu;
24
+OpenRaider gOpenRaider;
25
+Render gRender;
26
+World gWorld;
27
+
28
+SoundAL gSound;
29
+WindowSDL gWindow;
29 30
 
30 31
 Camera &getCamera() {
31
-    assert(gCamera != NULL);
32
-    return *gCamera;
32
+    return gCamera;
33 33
 }
34 34
 
35 35
 Console &getConsole() {
36
-    assert(gConsole != NULL);
37
-    return *gConsole;
36
+    return gConsole;
38 37
 }
39 38
 
40 39
 Game &getGame() {
41
-    assert(gGame != NULL);
42
-    return *gGame;
40
+    return gGame;
43 41
 }
44 42
 
45 43
 Menu &getMenu() {
46
-    assert(gMenu != NULL);
47
-    return *gMenu;
44
+    return gMenu;
48 45
 }
49 46
 
50 47
 OpenRaider &getOpenRaider() {
51
-    assert(gOpenRaider != NULL);
52
-    return *gOpenRaider;
48
+    return gOpenRaider;
53 49
 }
54 50
 
55 51
 Render &getRender() {
56
-    assert(gRender != NULL);
57
-    return *gRender;
52
+    return gRender;
58 53
 }
59 54
 
60 55
 Sound &getSound() {
61
-    assert(gSound != NULL);
62
-    return *gSound;
56
+    return gSound;
63 57
 }
64 58
 
65 59
 Window &getWindow() {
66
-    assert(gWindow != NULL);
67
-    return *gWindow;
60
+    return gWindow;
68 61
 }
69 62
 
70 63
 World &getWorld() {
71
-    assert(gWorld != NULL);
72
-    return *gWorld;
64
+    return gWorld;
73 65
 }
74 66
 
75 67
 void cleanupHandler(void) {
76
-    if (gConsole)
77
-        delete gConsole;
78
-
79
-    if (gGame)
80
-        delete gGame;
81
-
82
-    if (gMenu)
83
-        delete gMenu;
84
-
85
-    if (gWorld)
86
-        delete gWorld;
87
-
88
-    if (gRender)
89
-        delete gRender;
90
-
91
-    if (gCamera)
92
-        delete gCamera;
93
-
94
-    if (gOpenRaider)
95
-        delete gOpenRaider;
96
-
97
-    if (gSound)
98
-        delete gSound;
99
-
100
-    if (gWindow)
101
-        delete gWindow;
102
-
103 68
 #ifdef DEBUG
104 69
     printf("\nThanks for testing %s\n", VERSION);
105 70
     printf("Build date: %s @ %s\n", __DATE__, __TIME__);
@@ -146,68 +111,28 @@ int main(int argc, char *argv[]) {
146 111
     printf("Initializing %s\n", VERSION);
147 112
 #endif
148 113
 
149
-    // Create globals
150 114
     atexit(cleanupHandler);
151
-    gOpenRaider = new OpenRaider();
152
-    gWindow = new WindowSDL();
153
-    gSound = new SoundAL();
154
-    gWorld = new World();
155
-    gCamera = new Camera();
156
-    gRender = new Render();
157
-    gConsole = new Console();
158
-    gMenu = new Menu();
159
-    gGame = new Game();
160 115
 
161 116
     // Try to load a configuration
162
-    if (gOpenRaider->loadConfig(config) != 0) {
163
-        if (gOpenRaider->loadConfig(DEFAULT_CONFIG_PATH "/" DEFAULT_CONFIG_FILE) != 0) {
164
-            if (gOpenRaider->loadConfig(DEFAULT_CONFIG_FILE) != 0) {
117
+    if (gOpenRaider.loadConfig(config) != 0) {
118
+        if (gOpenRaider.loadConfig(DEFAULT_CONFIG_PATH "/" DEFAULT_CONFIG_FILE) != 0) {
119
+            if (gOpenRaider.loadConfig(DEFAULT_CONFIG_FILE) != 0) {
165 120
                 printf("Could not find a config file. Aborting...\n");
166 121
                 return 2;
167 122
             }
168 123
         }
169 124
     }
170 125
 
171
-    // Initialize Windowing
172
-    int error = gWindow->initialize();
126
+    // Initialize everything
127
+    int error = gOpenRaider.initialize();
173 128
     if (error != 0) {
174
-        printf("Could not initialize Window (%d)!\n", error);
129
+        printf("Could not initialize OpenRaider (%d)!\n", error);
175 130
         return 3;
176 131
     }
177 132
 
178
-    // Initialize OpenGL
179
-    error = gWindow->initializeGL();
180
-    if (error != 0) {
181
-        printf("Could not initialize OpenGL (%d)!\n", error);
182
-        return 4;
183
-    }
184
-
185
-    error = gWindow->initializeFont();
186
-    if (error != 0) {
187
-        printf("Could not initialize SDL-TTF (%d)!\n", error);
188
-        return 5;
189
-    }
190
-
191
-    error = gSound->initialize();
192
-    if (error != 0) {
193
-        printf("Could not initialize Sound (%d)!\n", error);
194
-        return 7;
195
-    }
196
-
197
-    // Initialize game engine
198
-    error = gGame->initialize();
199
-    if (error != 0) {
200
-        printf("Could not initialize Game Engine (%d)!\n", error);
201
-        return 8;
202
-    }
203
-
204
-    gMenu->setVisible(true);
205
-
206
-    systemTimerReset();
207
-
208 133
     // Enter Main loop
209
-    gConsole->print("Starting %s", VERSION);
210
-    gOpenRaider->run();
134
+    gConsole.print("Starting %s", VERSION);
135
+    gOpenRaider.run();
211 136
 
212 137
     return 0;
213 138
 }

Loading…
Cancel
Save