Bladeren bron

Multiple changes

World uses smart pointers, UI got calculate and shutdown,
Removed atexit handler
Thomas Buck 9 jaren geleden
bovenliggende
commit
10a27174bb
12 gewijzigde bestanden met toevoegingen van 104 en 87 verwijderingen
  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 Bestand weergeven

4
 
4
 
5
     [ 20140904 ]
5
     [ 20140904 ]
6
     * Fixed imgui colors
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
     [ 20140903 ]
11
     [ 20140903 ]
9
     * Finishing imgui integration, but now as UI layer and not on top of everything
12
     * Finishing imgui integration, but now as UI layer and not on top of everything

+ 0
- 1
TODO.md Bestand weergeven

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

+ 2
- 0
include/Debug.h Bestand weergeven

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

+ 1
- 1
include/RunTime.h Bestand weergeven

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

+ 4
- 0
include/UI.h Bestand weergeven

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

+ 6
- 5
include/World.h Bestand weergeven

9
 #ifndef _WORLD_H_
9
 #ifndef _WORLD_H_
10
 #define _WORLD_H_
10
 #define _WORLD_H_
11
 
11
 
12
+#include <memory>
12
 #include <vector>
13
 #include <vector>
13
 
14
 
14
 #include "Entity.h"
15
 #include "Entity.h"
77
     long getRoomByLocation(float x, float y, float z);
78
     long getRoomByLocation(float x, float y, float z);
78
 
79
 
79
 private:
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
 World &getWorld();
88
 World &getWorld();

+ 25
- 10
src/Debug.cpp Bestand weergeven

9
 #include "Debug.h"
9
 #include "Debug.h"
10
 #include "RunTime.h"
10
 #include "RunTime.h"
11
 #include "TextureManager.h"
11
 #include "TextureManager.h"
12
+#include "utils/time.h"
12
 #include "Window.h"
13
 #include "Window.h"
13
 
14
 
14
 #define STB_IMAGE_IMPLEMENTATION
15
 #define STB_IMAGE_IMPLEMENTATION
23
 }
24
 }
24
 
25
 
25
 Debug::~Debug() {
26
 Debug::~Debug() {
26
-    // TODO Segfaults...?
27
-    //ImGui::Shutdown();
28
-
29
     UI::removeWindow(this);
27
     UI::removeWindow(this);
30
 }
28
 }
31
 
29
 
35
 
33
 
36
     ImGuiIO& io = ImGui::GetIO();
34
     ImGuiIO& io = ImGui::GetIO();
37
     io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
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
     io.IniFilename = iniFilename.c_str();
38
     io.IniFilename = iniFilename.c_str();
41
     io.LogFilename = logFilename.c_str();
39
     io.LogFilename = logFilename.c_str();
61
     io.RenderDrawListsFn = Debug::renderImGui;
59
     io.RenderDrawListsFn = Debug::renderImGui;
62
 
60
 
63
     // Load font texture
61
     // Load font texture
62
+    //! \todo Use our own font subsystem instead of this?
64
     const void* png_data;
63
     const void* png_data;
65
     unsigned int png_size;
64
     unsigned int png_size;
66
     ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
65
     ImGui::GetDefaultFontData(NULL, NULL, &png_data, &png_size);
67
     int tex_x, tex_y, tex_comp;
66
     int tex_x, tex_y, tex_comp;
68
     void* tex_data = stbi_load_from_memory((const unsigned char*)png_data,
67
     void* tex_data = stbi_load_from_memory((const unsigned char*)png_data,
69
             (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
68
             (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
69
+
70
+     //! \fixme TODO use proper slot
70
     fontTex = getTextureManager().loadBufferSlot((unsigned char *)tex_data,
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
     stbi_image_free(tex_data);
74
     stbi_image_free(tex_data);
73
 
75
 
74
     return 0;
76
     return 0;
75
 }
77
 }
76
 
78
 
77
-void Debug::display() {
78
-    ImGui::Render();
79
-}
80
-
81
 void Debug::eventsFinished() {
79
 void Debug::eventsFinished() {
82
     ImGuiIO& io = ImGui::GetIO();
80
     ImGuiIO& io = ImGui::GetIO();
83
     io.DisplaySize = ImVec2((float)getWindow().getWidth(), (float)getWindow().getHeight());
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
     ImGui::NewFrame();
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
 void Debug::handleKeyboard(KeyboardButton key, bool pressed) {
104
 void Debug::handleKeyboard(KeyboardButton key, bool pressed) {
90
     ImGuiIO& io = ImGui::GetIO();
105
     ImGuiIO& io = ImGui::GetIO();
91
     io.KeysDown[key] = pressed;
106
     io.KeysDown[key] = pressed;

+ 2
- 2
src/RunTime.cpp Bestand weergeven

75
     return gameIsRunning;
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
 bool RunTime::getFPS() {
82
 bool RunTime::getFPS() {

+ 39
- 25
src/UI.cpp Bestand weergeven

23
 int UI::initialize() { return 0; }
23
 int UI::initialize() { return 0; }
24
 void UI::eventsFinished() { }
24
 void UI::eventsFinished() { }
25
 void UI::display() { }
25
 void UI::display() { }
26
+void UI::calculate() { }
27
+void UI::shutdown() { }
26
 void UI::handleKeyboard(KeyboardButton key, bool pressed) { }
28
 void UI::handleKeyboard(KeyboardButton key, bool pressed) { }
27
 void UI::handleText(char *text, bool notFinished) { }
29
 void UI::handleText(char *text, bool notFinished) { }
28
 void UI::handleAction(ActionEvents action, bool isFinished) { }
30
 void UI::handleAction(ActionEvents action, bool isFinished) { }
30
 void UI::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) { }
32
 void UI::handleMouseMotion(int xrel, int yrel, int xabs, int yabs) { }
31
 void UI::handleMouseScroll(int xrel, int yrel) { }
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
 void UI::passKeyboard(KeyboardButton key, bool pressed) {
72
 void UI::passKeyboard(KeyboardButton key, bool pressed) {
34
     if (pressed) {
73
     if (pressed) {
35
         if (getRunTime().getKeyBinding(menuAction) == key) {
74
         if (getRunTime().getKeyBinding(menuAction) == key) {
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 Bestand weergeven

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

+ 5
- 19
src/World.cpp Bestand weergeven

16
 }
16
 }
17
 
17
 
18
 void World::addRoom(Room &room) {
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
 unsigned long World::sizeRoom() {
22
 unsigned long World::sizeRoom() {
29
 }
29
 }
30
 
30
 
31
 void World::addSprite(SpriteSequence &sprite) {
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
 unsigned long World::sizeSprite() {
35
 unsigned long World::sizeSprite() {
42
 }
42
 }
43
 
43
 
44
 void World::addEntity(Entity &entity) {
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
 unsigned long World::sizeEntity() {
48
 unsigned long World::sizeEntity() {
55
 }
55
 }
56
 
56
 
57
 void World::addSkeletalModel(SkeletalModel &model) {
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
 unsigned long World::sizeSkeletalModel() {
61
 unsigned long World::sizeSkeletalModel() {
68
 }
68
 }
69
 
69
 
70
 void World::addStaticMesh(StaticMesh &model) {
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
 unsigned long World::sizeStaticMesh() {
74
 unsigned long World::sizeStaticMesh() {
111
 
111
 
112
 
112
 
113
 void World::destroy() {
113
 void World::destroy() {
114
-    for (unsigned long i = 0; i < mRooms.size(); i++)
115
-        delete mRooms[i];
116
     mRooms.clear();
114
     mRooms.clear();
117
-
118
-    for (unsigned long i = 0; i < mSprites.size(); i++)
119
-        delete mSprites[i];
120
     mSprites.clear();
115
     mSprites.clear();
121
-
122
-    for (unsigned long i = 0; i < mEntities.size(); i++)
123
-        delete mEntities[i];
124
     mEntities.clear();
116
     mEntities.clear();
125
-
126
-    for (unsigned long i = 0; i < mModels.size(); i++)
127
-        delete mModels[i];
128
     mModels.clear();
117
     mModels.clear();
129
-
130
-    for (unsigned long i = 0; i < mMeshes.size(); i++)
131
-        delete mMeshes[i];
132
     mMeshes.clear();
118
     mMeshes.clear();
133
 }
119
 }
134
 
120
 

+ 14
- 23
src/main.cpp Bestand weergeven

56
 static std::shared_ptr<Window> gWindow;
56
 static std::shared_ptr<Window> gWindow;
57
 static std::shared_ptr<World> gWorld;
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
 Camera &getCamera() {
59
 Camera &getCamera() {
71
     return *gCamera;
60
     return *gCamera;
72
 }
61
 }
146
     gWindow.reset(new WindowSDL());
135
     gWindow.reset(new WindowSDL());
147
 #endif
136
 #endif
148
 
137
 
149
-    atexit(cleanupHandler);
150
     Command::fillCommandList();
138
     Command::fillCommandList();
151
 
139
 
152
     if (configFileToUse == "") {
140
     if (configFileToUse == "") {
204
     getConsole() << "Starting " << VERSION << Console::endl;
192
     getConsole() << "Starting " << VERSION << Console::endl;
205
     getMenu().moveToTop();
193
     getMenu().moveToTop();
206
     systemTimerReset();
194
     systemTimerReset();
207
-    getRunTime().start();
195
+    getRunTime().setRunning(true);
208
 
196
 
209
     while (getRunTime().isRunning()) {
197
     while (getRunTime().isRunning()) {
198
+        getWindow().eventHandling();
199
+        UI::passCalculate();
210
         renderFrame();
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
     return 0;
214
     return 0;
214
 }
215
 }
215
 
216
 
216
 void renderFrame() {
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
     UI::passDisplay();
218
     UI::passDisplay();
226
-
227
-    // Put new frame on screen
228
     getWindow().swapBuffersGL();
219
     getWindow().swapBuffersGL();
229
 }
220
 }
230
 
221
 

Laden…
Annuleren
Opslaan