Browse Source

Multiple changes

World uses smart pointers, UI got calculate and shutdown,
Removed atexit handler
Thomas Buck 9 years ago
parent
commit
10a27174bb
12 changed files with 104 additions and 87 deletions
  1. 3
    0
      ChangeLog.md
  2. 0
    1
      TODO.md
  3. 2
    0
      include/Debug.h
  4. 1
    1
      include/RunTime.h
  5. 4
    0
      include/UI.h
  6. 6
    5
      include/World.h
  7. 25
    10
      src/Debug.cpp
  8. 2
    2
      src/RunTime.cpp
  9. 39
    25
      src/UI.cpp
  10. 3
    1
      src/WindowSDL.cpp
  11. 5
    19
      src/World.cpp
  12. 14
    23
      src/main.cpp

+ 3
- 0
ChangeLog.md View File

@@ -4,6 +4,9 @@
4 4
 
5 5
     [ 20140904 ]
6 6
     * Fixed imgui colors
7
+    * World now using smart pointers
8
+    * Removed atexit handler, now breaking out of main loop on quit
9
+    * Added calculate and shutdown calls to UI interface, properly shutting down imgui
7 10
 
8 11
     [ 20140903 ]
9 12
     * Finishing imgui integration, but now as UI layer and not on top of everything

+ 0
- 1
TODO.md View File

@@ -17,7 +17,6 @@
17 17
 * imgui integration still very much unfinished
18 18
     * toggling with ‘q’ means one can’t use input ‘q’ into a text field
19 19
     * clicking outside of the debug window does nothing
20
-    * it seems as if colors aren’t rendering properly
21 20
 
22 21
 ## Cmake
23 22
 

+ 2
- 0
include/Debug.h View File

@@ -18,6 +18,8 @@ public:
18 18
     virtual int initialize();
19 19
     virtual void eventsFinished();
20 20
     virtual void display();
21
+    virtual void calculate();
22
+    virtual void shutdown();
21 23
 
22 24
     virtual void handleKeyboard(KeyboardButton key, bool pressed);
23 25
     virtual void handleText(char *text, bool notFinished);

+ 1
- 1
include/RunTime.h View File

@@ -31,7 +31,7 @@ public:
31 31
     void setKeyBinding(ActionEvents event, KeyboardButton button);
32 32
 
33 33
     bool isRunning();
34
-    void start();
34
+    void setRunning(bool run);
35 35
 
36 36
     bool getFPS();
37 37
     void setFPS(bool fps);

+ 4
- 0
include/UI.h View File

@@ -21,6 +21,8 @@ public:
21 21
     virtual int initialize();
22 22
     virtual void eventsFinished();
23 23
     virtual void display();
24
+    virtual void calculate();
25
+    virtual void shutdown();
24 26
 
25 27
     virtual void handleKeyboard(KeyboardButton key, bool pressed);
26 28
     virtual void handleText(char *text, bool notFinished);
@@ -38,6 +40,8 @@ public:
38 40
     static int passInitialize();
39 41
     static void passEvents();
40 42
     static void passDisplay();
43
+    static void passCalculate();
44
+    static void passShutdown();
41 45
     static void passKeyboard(KeyboardButton key, bool pressed);
42 46
     static void passText(char *text, bool notFinished);
43 47
     static void passMouseClick(unsigned int x, unsigned int y, KeyboardButton button, bool released);

+ 6
- 5
include/World.h View File

@@ -9,6 +9,7 @@
9 9
 #ifndef _WORLD_H_
10 10
 #define _WORLD_H_
11 11
 
12
+#include <memory>
12 13
 #include <vector>
13 14
 
14 15
 #include "Entity.h"
@@ -77,11 +78,11 @@ public:
77 78
     long getRoomByLocation(float x, float y, float z);
78 79
 
79 80
 private:
80
-    std::vector<Room *> mRooms;
81
-    std::vector<SpriteSequence *> mSprites;
82
-    std::vector<Entity *> mEntities;
83
-    std::vector<SkeletalModel *> mModels;
84
-    std::vector<StaticMesh *> mMeshes;
81
+    std::vector<std::unique_ptr<Room>> mRooms;
82
+    std::vector<std::unique_ptr<SpriteSequence>> mSprites;
83
+    std::vector<std::unique_ptr<Entity>> mEntities;
84
+    std::vector<std::unique_ptr<SkeletalModel>> mModels;
85
+    std::vector<std::unique_ptr<StaticMesh>> mMeshes;
85 86
 };
86 87
 
87 88
 World &getWorld();

+ 25
- 10
src/Debug.cpp View File

@@ -9,6 +9,7 @@
9 9
 #include "Debug.h"
10 10
 #include "RunTime.h"
11 11
 #include "TextureManager.h"
12
+#include "utils/time.h"
12 13
 #include "Window.h"
13 14
 
14 15
 #define STB_IMAGE_IMPLEMENTATION
@@ -23,9 +24,6 @@ Debug::Debug() {
23 24
 }
24 25
 
25 26
 Debug::~Debug() {
26
-    // TODO Segfaults...?
27
-    //ImGui::Shutdown();
28
-
29 27
     UI::removeWindow(this);
30 28
 }
31 29
 
@@ -35,7 +33,7 @@ int Debug::initialize() {
35 33
 
36 34
     ImGuiIO& io = ImGui::GetIO();
37 35
     io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
38
-    io.DeltaTime = 1.0f/60.0f;
36
+    io.DeltaTime = 1.0f / 60.0f;
39 37
 
40 38
     io.IniFilename = iniFilename.c_str();
41 39
     io.LogFilename = logFilename.c_str();
@@ -61,31 +59,48 @@ int Debug::initialize() {
61 59
     io.RenderDrawListsFn = Debug::renderImGui;
62 60
 
63 61
     // Load font texture
62
+    //! \todo Use our own font subsystem instead of this?
64 63
     const void* png_data;
65 64
     unsigned int png_size;
66 65
     ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
67 66
     int tex_x, tex_y, tex_comp;
68 67
     void* tex_data = stbi_load_from_memory((const unsigned char*)png_data,
69 68
             (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
69
+
70
+     //! \fixme TODO use proper slot
70 71
     fontTex = getTextureManager().loadBufferSlot((unsigned char *)tex_data,
71
-            tex_x, tex_y, RGBA, 32, 0, false); // TODO use proper slot!
72
+            tex_x, tex_y, RGBA, 32, 0, false);
73
+
72 74
     stbi_image_free(tex_data);
73 75
 
74 76
     return 0;
75 77
 }
76 78
 
77
-void Debug::display() {
78
-    ImGui::Render();
79
-}
80
-
81 79
 void Debug::eventsFinished() {
82 80
     ImGuiIO& io = ImGui::GetIO();
83 81
     io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
84
-    io.DeltaTime = 1.0f / 60.0f; // TODO proper timing
82
+
83
+    static long lastTime = 0;
84
+    io.DeltaTime = ((float)(systemTimerGet() - lastTime)) / 1000.0f;
85
+    lastTime = systemTimerGet();
85 86
 
86 87
     ImGui::NewFrame();
87 88
 }
88 89
 
90
+void Debug::display() {
91
+    ImGui::Render();
92
+}
93
+
94
+void Debug::calculate() {
95
+    //ImGui::ShowTestWindow();
96
+    //ImGui::ShowStyleEditor();
97
+    ImGui::ShowUserGuide();
98
+}
99
+
100
+void Debug::shutdown() {
101
+    ImGui::Shutdown();
102
+}
103
+
89 104
 void Debug::handleKeyboard(KeyboardButton key, bool pressed) {
90 105
     ImGuiIO& io = ImGui::GetIO();
91 106
     io.KeysDown[key] = pressed;

+ 2
- 2
src/RunTime.cpp View File

@@ -75,8 +75,8 @@ bool RunTime::isRunning() {
75 75
     return gameIsRunning;
76 76
 }
77 77
 
78
-void RunTime::start() {
79
-    gameIsRunning = true;
78
+void RunTime::setRunning(bool run) {
79
+    gameIsRunning = run;
80 80
 }
81 81
 
82 82
 bool RunTime::getFPS() {

+ 39
- 25
src/UI.cpp View File

@@ -23,6 +23,8 @@ UI::~UI() {
23 23
 int UI::initialize() { return 0; }
24 24
 void UI::eventsFinished() { }
25 25
 void UI::display() { }
26
+void UI::calculate() { }
27
+void UI::shutdown() { }
26 28
 void UI::handleKeyboard(KeyboardButton key, bool pressed) { }
27 29
 void UI::handleText(char *text, bool notFinished) { }
28 30
 void UI::handleAction(ActionEvents action, bool isFinished) { }
@@ -30,6 +32,43 @@ void UI::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton button,
30 32
 void UI::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) { }
31 33
 void UI::handleMouseScroll(int xrel, int yrel) { }
32 34
 
35
+int UI::passInitialize() {
36
+    for (auto &x : windows) {
37
+        int error = x->initialize();
38
+        if (error != 0)
39
+            return error;
40
+    }
41
+
42
+    return 0;
43
+}
44
+
45
+void UI::passEvents() {
46
+    for (auto &x : windows) {
47
+        x->eventsFinished();
48
+    }
49
+}
50
+
51
+void UI::passDisplay() {
52
+    std::sort(windows.begin(), windows.end(), compareUIs);
53
+    for (auto &x : windows) {
54
+        if (x->zPos >= 0) {
55
+            x->display();
56
+        }
57
+    }
58
+}
59
+
60
+void UI::passCalculate() {
61
+    for (auto &x : windows) {
62
+        x->calculate();
63
+    }
64
+}
65
+
66
+void UI::passShutdown() {
67
+    for (auto &x : windows) {
68
+        x->shutdown();
69
+    }
70
+}
71
+
33 72
 void UI::passKeyboard(KeyboardButton key, bool pressed) {
34 73
     if (pressed) {
35 74
         if (getRunTime().getKeyBinding(menuAction) == key) {
@@ -168,28 +207,3 @@ void UI::makeInvisible() {
168 207
     });
169 208
 }
170 209
 
171
-int UI::passInitialize() {
172
-    for (auto &x : windows) {
173
-        int error = x->initialize();
174
-        if (error != 0)
175
-            return error;
176
-    }
177
-
178
-    return 0;
179
-}
180
-
181
-void UI::passEvents() {
182
-    for (auto &x : windows) {
183
-        x->eventsFinished();
184
-    }
185
-}
186
-
187
-void UI::passDisplay() {
188
-    std::sort(windows.begin(), windows.end(), compareUIs);
189
-    for (auto &x : windows) {
190
-        if (x->zPos >= 0) {
191
-            x->display();
192
-        }
193
-    }
194
-}
195
-

+ 3
- 1
src/WindowSDL.cpp View File

@@ -9,6 +9,7 @@
9 9
 #include <ctime>
10 10
 
11 11
 #include "global.h"
12
+#include "RunTime.h"
12 13
 #include "UI.h"
13 14
 #include "utils/strings.h"
14 15
 #include "WindowSDL.h"
@@ -436,7 +437,8 @@ void WindowSDL::eventHandling() {
436 437
                 break;
437 438
 
438 439
             case SDL_QUIT:
439
-                exit(0);
440
+                getRunTime().setRunning(false);
441
+                break;
440 442
         }
441 443
     }
442 444
 

+ 5
- 19
src/World.cpp View File

@@ -16,7 +16,7 @@ World::~World() {
16 16
 }
17 17
 
18 18
 void World::addRoom(Room &room) {
19
-    mRooms.push_back(&room);
19
+    mRooms.push_back(std::unique_ptr<Room>(&room));
20 20
 }
21 21
 
22 22
 unsigned long World::sizeRoom() {
@@ -29,7 +29,7 @@ Room &World::getRoom(unsigned long index) {
29 29
 }
30 30
 
31 31
 void World::addSprite(SpriteSequence &sprite) {
32
-    mSprites.push_back(&sprite);
32
+    mSprites.push_back(std::unique_ptr<SpriteSequence>(&sprite));
33 33
 }
34 34
 
35 35
 unsigned long World::sizeSprite() {
@@ -42,7 +42,7 @@ SpriteSequence &World::getSprite(unsigned long index) {
42 42
 }
43 43
 
44 44
 void World::addEntity(Entity &entity) {
45
-    mEntities.push_back(&entity);
45
+    mEntities.push_back(std::unique_ptr<Entity>(&entity));
46 46
 }
47 47
 
48 48
 unsigned long World::sizeEntity() {
@@ -55,7 +55,7 @@ Entity &World::getEntity(unsigned long index) {
55 55
 }
56 56
 
57 57
 void World::addSkeletalModel(SkeletalModel &model) {
58
-    mModels.push_back(&model);
58
+    mModels.push_back(std::unique_ptr<SkeletalModel>(&model));
59 59
 }
60 60
 
61 61
 unsigned long World::sizeSkeletalModel() {
@@ -68,7 +68,7 @@ SkeletalModel &World::getSkeletalModel(unsigned long index) {
68 68
 }
69 69
 
70 70
 void World::addStaticMesh(StaticMesh &model) {
71
-    mMeshes.push_back(&model);
71
+    mMeshes.push_back(std::unique_ptr<StaticMesh>(&model));
72 72
 }
73 73
 
74 74
 unsigned long World::sizeStaticMesh() {
@@ -111,24 +111,10 @@ long World::getRoomByLocation(float x, float y, float z) {
111 111
 
112 112
 
113 113
 void World::destroy() {
114
-    for (unsigned long i = 0; i < mRooms.size(); i++)
115
-        delete mRooms[i];
116 114
     mRooms.clear();
117
-
118
-    for (unsigned long i = 0; i < mSprites.size(); i++)
119
-        delete mSprites[i];
120 115
     mSprites.clear();
121
-
122
-    for (unsigned long i = 0; i < mEntities.size(); i++)
123
-        delete mEntities[i];
124 116
     mEntities.clear();
125
-
126
-    for (unsigned long i = 0; i < mModels.size(); i++)
127
-        delete mModels[i];
128 117
     mModels.clear();
129
-
130
-    for (unsigned long i = 0; i < mMeshes.size(); i++)
131
-        delete mMeshes[i];
132 118
     mMeshes.clear();
133 119
 }
134 120
 

+ 14
- 23
src/main.cpp View File

@@ -56,17 +56,6 @@ static std::shared_ptr<TextureManager> gTextureManager;
56 56
 static std::shared_ptr<Window> gWindow;
57 57
 static std::shared_ptr<World> gWorld;
58 58
 
59
-static void cleanupHandler(void) {
60
-#ifdef DEBUG
61
-    std::cout << std::endl;
62
-    std::cout << "Thanks for testing " << VERSION << std::endl;
63
-    std::cout << "Build date: " << __DATE__ << " @ " << __TIME__ << std::endl;
64
-    std::cout << "Build host: " << BUILD_HOST << std::endl;
65
-    std::cout << "Web site  : http://github.com/xythobuz/OpenRaider" << std::endl;
66
-    std::cout << "Contact   : xythobuz@xythobuz.de" << std::endl;
67
-#endif
68
-}
69
-
70 59
 Camera &getCamera() {
71 60
     return *gCamera;
72 61
 }
@@ -146,7 +135,6 @@ int main(int argc, char* argv[]) {
146 135
     gWindow.reset(new WindowSDL());
147 136
 #endif
148 137
 
149
-    atexit(cleanupHandler);
150 138
     Command::fillCommandList();
151 139
 
152 140
     if (configFileToUse == "") {
@@ -204,27 +192,30 @@ int main(int argc, char* argv[]) {
204 192
     getConsole() << "Starting " << VERSION << Console::endl;
205 193
     getMenu().moveToTop();
206 194
     systemTimerReset();
207
-    getRunTime().start();
195
+    getRunTime().setRunning(true);
208 196
 
209 197
     while (getRunTime().isRunning()) {
198
+        getWindow().eventHandling();
199
+        UI::passCalculate();
210 200
         renderFrame();
211 201
     }
212 202
 
203
+    UI::passShutdown();
204
+
205
+#ifdef DEBUG
206
+    std::cout << std::endl;
207
+    std::cout << "Thanks for testing " << VERSION << std::endl;
208
+    std::cout << "Build date: " << __DATE__ << " @ " << __TIME__ << std::endl;
209
+    std::cout << "Build host: " << BUILD_HOST << std::endl;
210
+    std::cout << "Web site  : http://github.com/xythobuz/OpenRaider" << std::endl;
211
+    std::cout << "Contact   : xythobuz@xythobuz.de" << std::endl;
212
+#endif
213
+
213 214
     return 0;
214 215
 }
215 216
 
216 217
 void renderFrame() {
217
-    // Get keyboard and mouse input
218
-    getWindow().eventHandling();
219
-
220
-    ImGui::ShowTestWindow();
221
-    ImGui::ShowStyleEditor();
222
-    //ImGui::ShowUserGuide();
223
-
224
-    // Render everything
225 218
     UI::passDisplay();
226
-
227
-    // Put new frame on screen
228 219
     getWindow().swapBuffersGL();
229 220
 }
230 221
 

Loading…
Cancel
Save