Browse Source

Using stb_image, stb_image_write. New imgui version.

Thomas Buck 9 years ago
parent
commit
39b0ffac3b

+ 7
- 0
ChangeLog.md View File

@@ -2,6 +2,13 @@
2 2
 
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5
+    [ 20141229 ]
6
+    * Removed utils/png, utils/tga image readers
7
+    * Included current stb_image, stb_image_write and stb_textedit
8
+    * Now using stb_image for all image loading (except pcx format)
9
+    * Now storing screenshots in PNG format
10
+    * Updated imgui
11
+
5 12
     [ 20141228 ]
6 13
     * Room Bounding Boxes are now visualized in Wireframe mode (again).
7 14
     * Window/WindowSDL/WindowGLFW are now completely static.

+ 0
- 1
README.md View File

@@ -46,7 +46,6 @@ Basically, OpenRaider depends on the following:
46 46
 * SDL2 or GLFW
47 47
 * SDL2-TTF (optional, doesn’t work with GLFW, needed if you want to use TTF fonts)
48 48
 * OpenAL & ALUT (optional, needed for sound output)
49
-* libpng (optional)
50 49
 
51 50
 On Mac OS X 10.9 / 10.10 with [XCode](https://developer.apple.com/xcode/) and [MacPorts](http://www.macports.org) installed, the following should be enough to get all dependencies that are available as port:
52 51
 

+ 1
- 5
TODO.md View File

@@ -8,10 +8,8 @@
8 8
 
9 9
 ## Bugs
10 10
 
11
-* Screenshots are sometimes not written, sometimes distorted?
11
+* Screenshots are sometimes distorted?
12 12
     * Seems to happen if OpenRaider.ini sets a resolution larger than the window can be
13
-* When the freeGLUT windowing interface is used, the first pressed button seems to be repeated
14
-    * Replace freeGLUT with glfw?
15 13
 
16 14
 ## Cmake
17 15
 
@@ -24,6 +22,4 @@
24 22
 * Depend on physfs for easier file location management
25 23
 * Depend on libcdio, use it to read original CDs or CUE/TOC/ISO images
26 24
 * Add ability to play the FMVs. Format? VLC can play them!
27
-* Cut TGA image reader, currently only used for menu background?!
28
-    * Need useful, always available image writer alternative for screenshots then
29 25
 

+ 1
- 0
include/Render.h View File

@@ -29,6 +29,7 @@ class Render {
29 29
     static void setMode(RenderMode m);
30 30
 
31 31
     static void display();
32
+    static void displayUI();
32 33
 
33 34
     static void clearRoomList();
34 35
 

+ 0
- 3
include/TextureManager.h View File

@@ -107,10 +107,7 @@ class TextureManager {
107 107
 
108 108
   private:
109 109
     std::vector<unsigned int>& getIds(TextureStorage s);
110
-
111
-    int loadTGA(const char* filename, TextureStorage s, int slot);
112 110
     int loadPCX(const char* filename, TextureStorage s, int slot);
113
-    int loadPNG(const char* filename, TextureStorage s, int slot);
114 111
 
115 112
     std::vector<unsigned int> mTextureIdsGame;
116 113
     std::vector<unsigned int> mTextureIdsSystem;

+ 1
- 1
include/UI.h View File

@@ -13,7 +13,7 @@
13 13
 #include <memory>
14 14
 #include <tuple>
15 15
 
16
-#include "imgui/imgui.h"
16
+class ImDrawList;
17 17
 
18 18
 class UI {
19 19
   public:

+ 0
- 1
include/config.h.in View File

@@ -19,7 +19,6 @@ extern const char* DEFAULT_CONFIG_FILE;
19 19
 extern const char* INSTALL_PREFIX;
20 20
 
21 21
 #cmakedefine USING_AL
22
-#cmakedefine USING_PNG
23 22
 #cmakedefine USING_SDL
24 23
 #cmakedefine USING_SDL_FONT
25 24
 #cmakedefine USING_GLFW

+ 0
- 49
include/utils/png.h View File

@@ -1,49 +0,0 @@
1
-/*!
2
- * \file include/utils/png.h
3
- * \brief PNG image reader
4
- *
5
- * \author xythobuz
6
- */
7
-
8
-#ifndef _UTILS_PNG_H_
9
-#define _UTILS_PNG_H_
10
-
11
-#include "TextureManager.h"
12
-
13
-/*!
14
- * \brief Check if a file is a valid PNG image
15
- * \param filename path of file to read
16
- * \returns 0 on success
17
- */
18
-int pngCheck(const char* filename);
19
-
20
-/*!
21
- * \brief Load a PNG image file into an RGBA buffer
22
- * \param filename path of file to read
23
- * \param image place where allocated buffer of size (width * height * 4) will be allocated
24
- * \param width place where image width will be stored
25
- * \param height place where image height will be stored
26
- * \param mode place where Color Mode of image will be stored
27
- * \param bpp place where pixel width will be stored (8, 24, 32)
28
- * \returns 0 on success
29
- */
30
-int pngLoad(const char* filename, unsigned char** image,
31
-            unsigned int* width, unsigned int* height,
32
-            TextureManager::ColorMode* mode, unsigned int* bpp);
33
-
34
-/*!
35
- * \brief Create a PNG image file from an RGBA buffer
36
- * \param filename path of file to create
37
- * \param image buffer of size (width * height * 4)
38
- * \param width image width
39
- * \param height image height
40
- * \param mode color mode
41
- * \param bpp bits per pixel (8, 24, 32)
42
- * \returns 0 on success
43
- */
44
-int pngSave(const char* filename, unsigned char* image,
45
-            unsigned int width, unsigned int height,
46
-            TextureManager::ColorMode mode, unsigned int bpp);
47
-
48
-#endif
49
-

+ 0
- 43
include/utils/tga.h View File

@@ -1,43 +0,0 @@
1
-/*!
2
- * \file include/utils/tga.h
3
- * \brief TGA image reader/writer
4
- *
5
- * \author Mongoose
6
- * \author xythobuz
7
- */
8
-#ifndef _UTILS_TGA_H
9
-#define _UTILS_TGA_H
10
-
11
-/*!
12
- * \brief Check if a file is a valid TGA image
13
- * \param filename path to file
14
- * \returns 0 if valid, else error condition
15
- */
16
-int tgaCheck(const char* filename);
17
-
18
-/*!
19
- * \brief Load a TGA image from file
20
- * \param filename path to file
21
- * \param image Where the pixmap will be stored (or nullptr)
22
- * \param width where the width will be stored
23
- * \param height where the height will be stored
24
- * \param type where the type will be stored (tga_type_t)
25
- * \returns 0 on success, else error condition
26
- */
27
-int tgaLoad(const char* filename, unsigned char** image,
28
-            unsigned int* width, unsigned int* height, char* type);
29
-
30
-/*!
31
- * \brief Save a pixel buffer into a file on disk
32
- * \param filename path to file
33
- * \param image pixmap to be stored
34
- * \param width width of pixmap/image
35
- * \param height height of pixmap/image
36
- * \param type tga type to use
37
- * \returns 0 on success, else error condition
38
- */
39
-int tgaSave(const char* filename, unsigned char* image,
40
-            unsigned int width, unsigned int height, char type);
41
-
42
-#endif
43
-

+ 0
- 14
src/CMakeLists.txt View File

@@ -46,14 +46,6 @@ if (OPENAL_FOUND)
46 46
     endif (ALUT_FOUND)
47 47
 endif (OPENAL_FOUND)
48 48
 
49
-# Add PNG Library
50
-find_package (PNG)
51
-if (PNG_FOUND)
52
-    include_directories (SYSTEM ${PNG_INCLUDE_DIRS})
53
-    set (LIBS ${LIBS} ${PNG_LIBRARIES})
54
-    add_definitions (${PNG_DEFINITIONS})
55
-endif (PNG_FOUND)
56
-
57 49
 # Add GLM Library
58 50
 find_package (GLM REQUIRED)
59 51
 if (GLM_FOUND)
@@ -115,12 +107,6 @@ else (SDL2_FOUND AND NOT FORCE_GLFW)
115 107
     endif (GLFW_FOUND)
116 108
 endif (SDL2_FOUND AND NOT FORCE_GLFW)
117 109
 
118
-if (PNG_FOUND)
119
-    set (USING_PNG TRUE)
120
-else (PNG_FOUND)
121
-    set (USING_PNG FALSE)
122
-endif (PNG_FOUND)
123
-
124 110
 #################################################################
125 111
 
126 112
 # Check for header/function presence

+ 2
- 0
src/Console.cpp View File

@@ -7,6 +7,8 @@
7 7
 
8 8
 #include <cstring>
9 9
 
10
+#include "imgui/imgui.h"
11
+
10 12
 #include "global.h"
11 13
 #include "Log.h"
12 14
 #include "UI.h"

+ 71
- 23
src/Render.cpp View File

@@ -10,17 +10,17 @@
10 10
 #include <sstream>
11 11
 #include <glm/gtc/matrix_transform.hpp>
12 12
 
13
-#include <stdlib.h>
14
-#include <math.h>
15
-#include <string.h>
13
+#include "imgui/imgui.h"
14
+#include "stb/stb_image_write.h"
16 15
 
17 16
 #include "global.h"
18 17
 #include "Camera.h"
19 18
 #include "Game.h"
20
-#include "utils/strings.h"
21
-#include "utils/tga.h"
19
+#include "Log.h"
20
+#include "UI.h"
22 21
 #include "World.h"
23 22
 #include "system/Window.h"
23
+#include "utils/strings.h"
24 24
 #include "Render.h"
25 25
 
26 26
 RenderMode Render::mode = RenderMode::Disabled;
@@ -131,32 +131,42 @@ void Render::buildRoomList(int room) {
131 131
 }
132 132
 
133 133
 void Render::screenShot(const char* filenameBase) {
134
-    /*
135
-    int sz = Window::getSize().x * Window::getSize().y;
136
-    unsigned char* image = new unsigned char[sz * 3];
137
-    static int count = 0;
138
-    bool done = false;
139
-
140 134
     assert(filenameBase != nullptr);
141 135
 
136
+    int w = Window::getSize().x;
137
+    int h = Window::getSize().y;
138
+    int sz = w * h;
139
+    unsigned char* image = new unsigned char[sz * 3];
140
+    glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_BYTE, image);
141
+
142
+    unsigned char* buffer = new unsigned char[sz * 3];
143
+    for (int x = 0; x < w; x++) {
144
+        for (int y = 0; y < h; y++) {
145
+            buffer[3 * (x + (y * w))] = image[3 * (x + ((h - y - 1) * w))];
146
+            buffer[(3 * (x + (y * w))) + 1] = image[(3 * (x + ((h - y - 1) * w))) + 1];
147
+            buffer[(3 * (x + (y * w))) + 2] = image[(3 * (x + ((h - y - 1) * w))) + 2];
148
+        }
149
+    }
150
+
142 151
     // Don't overwrite files
143
-    std::ostringstream filename;
152
+    static int count = 0;
153
+    bool done = false;
154
+    std::string f;
144 155
     while (!done) {
145
-        filename << filenameBase << "-" << count++ << ".tga";
156
+        std::ostringstream filename;
157
+        filename << filenameBase << "-" << count++ << ".png";
158
+        f = filename.str();
159
+        std::ifstream fs(f);
160
+        done = !fs.good();
161
+        fs.close();
162
+    }
146 163
 
147
-        FILE* f = fopen(filename.str().c_str(), "rb");
148
-        if (f) {
149
-            fclose(f);
150
-        } else {
151
-            done = true;
152
-        }
164
+    if (!stbi_write_png(f.c_str(), w, h, 3, buffer, 0)) {
165
+        getLog() << "Error saving image \"" << f << "\"!" << Log::endl;
153 166
     }
154 167
 
155
-    glReadPixels(0, 0, Window::getSize().x, Window::getSize().y, GL_BGR_EXT, GL_UNSIGNED_BYTE,
156
-                 image);
157
-    tgaSave(filename.str().c_str(), image, Window::getSize().x, Window::getSize().y, 0);
158 168
     delete [] image;
159
-    */
169
+    delete [] buffer;
160 170
 }
161 171
 
162 172
 void Render::drawTexture(float x, float y, float w, float h, glm::vec4 color,
@@ -186,3 +196,41 @@ void Render::drawTexture(float x, float y, float w, float h, glm::vec4 color,
186 196
     Window::drawTextGL(vertices, uvs, color, texture);
187 197
 }
188 198
 
199
+void Render::displayUI() {
200
+    static const int modeStringCount = 5;
201
+    static const char* modeStrings[modeStringCount] = {
202
+        "Disable", "Splash", "Texture", "Wireframe", "Solid"
203
+    };
204
+
205
+    if (ImGui::CollapsingHeader("Render Settings")) {
206
+        int item = 0;
207
+        if (mode == RenderMode::LoadScreen)
208
+            item = 1;
209
+        else if (mode == RenderMode::Texture)
210
+            item = 2;
211
+        else if (mode == RenderMode::Wireframe)
212
+            item = 3;
213
+        else if (mode == RenderMode::Solid)
214
+            item = 4;
215
+        if (ImGui::Combo("Mode", &item, modeStrings, modeStringCount)) {
216
+            if (item == 0)
217
+                mode = RenderMode::Disabled;
218
+            else if (item == 1)
219
+                mode = RenderMode::LoadScreen;
220
+            else if (item == 2)
221
+                mode = RenderMode::Texture;
222
+            else if (item == 3)
223
+                mode = RenderMode::Wireframe;
224
+            else if (item == 4)
225
+                mode = RenderMode::Solid;
226
+        }
227
+
228
+        bool updateViewFrustum = Camera::getUpdateViewFrustum();
229
+        if (ImGui::Checkbox("Update Frustum##runtime", &updateViewFrustum)) {
230
+            Camera::setUpdateViewFrustum(updateViewFrustum);
231
+        }
232
+        ImGui::SameLine();
233
+        ImGui::Checkbox("Show Frustum##runtime", &displayViewFrustum);
234
+    }
235
+}
236
+

+ 15
- 28
src/RunTime.cpp View File

@@ -5,9 +5,10 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
+#include "imgui/imgui.h"
9
+
8 10
 #include "global.h"
9 11
 #include "Camera.h"
10
-#include "Render.h"
11 12
 #include "UI.h"
12 13
 #include "system/Sound.h"
13 14
 #include "system/Window.h"
@@ -65,10 +66,13 @@ void RunTime::setKeyBinding(ActionEvents event, KeyboardButton button) {
65 66
 
66 67
 void RunTime::updateFPS() {
67 68
     frameCount++;
69
+    frameCount2++;
70
+
68 71
     lastFrameTime = systemTimerGet() - lastTime;
69 72
     frameTimeSum += lastFrameTime;
70 73
     frameTimeSum2 += lastFrameTime;
71 74
     lastTime = systemTimerGet();
75
+
72 76
     if (frameTimeSum >= 200) {
73 77
         fps = frameCount * (1000 / frameTimeSum);
74 78
         frameCount = frameTimeSum = 0;
@@ -78,20 +82,13 @@ void RunTime::updateFPS() {
78 82
         history.push_back(frameCount2);
79 83
         frameCount2 = frameTimeSum2 = 0;
80 84
     }
81
-    frameCount2++;
82 85
 }
83 86
 
84 87
 void RunTime::display() {
85 88
     if (ImGui::CollapsingHeader("RunTime Settings")) {
86
-        bool showFPS = getShowFPS();
87
-        if (ImGui::Checkbox("Show FPS##runtime", &showFPS)) {
88
-            setShowFPS(showFPS);
89
-        }
89
+        ImGui::Checkbox("Show FPS##runtime", &showFPS);
90 90
         ImGui::SameLine();
91
-        bool running = isRunning();
92
-        if (ImGui::Checkbox("Running (!)##runtime", &running)) {
93
-            setRunning(running);
94
-        }
91
+        ImGui::Checkbox("Running (!)##runtime", &gameIsRunning);
95 92
         ImGui::SameLine();
96 93
         bool sound = Sound::getEnabled();
97 94
         if (ImGui::Checkbox("Sound##runtime", &sound)) {
@@ -103,16 +100,6 @@ void RunTime::display() {
103 100
             Window::setFullscreen(fullscreen);
104 101
         }
105 102
 
106
-        bool updateViewFrustum = Camera::getUpdateViewFrustum();
107
-        if (ImGui::Checkbox("Update Frustum##runtime", &updateViewFrustum)) {
108
-            Camera::setUpdateViewFrustum(updateViewFrustum);
109
-        }
110
-        ImGui::SameLine();
111
-        bool displayViewFrustum = Render::getDisplayViewFrustum();
112
-        if (ImGui::Checkbox("Show Frustum##runtime", &displayViewFrustum)) {
113
-            Render::setDisplayViewFrustum(displayViewFrustum);
114
-        }
115
-
116 103
         float vol = Sound::getVolume();
117 104
         if (ImGui::InputFloat("Volume##runtime", &vol, 0.0f, 0.0f, 3,
118 105
                               ImGuiInputTextFlags_EnterReturnsTrue)) {
@@ -191,19 +178,19 @@ void RunTime::display() {
191 178
 
192 179
     if (ImGui::CollapsingHeader("Performance")) {
193 180
         ImGui::Text("Uptime: %lums", systemTimerGet());
194
-        ImGui::Text("Frames per Second: %luFPS", getFPS());
195
-        if (getHistoryFPS().size() > 1) {
181
+        ImGui::Text("Frames per Second: %luFPS", fps);
182
+        if (history.size() > 1) {
196 183
             static bool scroll = true;
197 184
             if (scroll) {
198
-                int offset = getHistoryFPS().size() - 1;
185
+                int offset = history.size() - 1;
199 186
                 if (offset > 10)
200 187
                     offset = 10;
201
-                ImGui::PlotLines("FPS", &getHistoryFPS()[1],
202
-                                 getHistoryFPS().size() - 1,
203
-                                 getHistoryFPS().size() - offset - 1);
188
+                ImGui::PlotLines("FPS", &history[1],
189
+                                 history.size() - 1,
190
+                                 history.size() - offset - 1);
204 191
             } else {
205
-                ImGui::PlotLines("FPS", &getHistoryFPS()[1],
206
-                                 getHistoryFPS().size() - 1);
192
+                ImGui::PlotLines("FPS", &history[1],
193
+                                 history.size() - 1);
207 194
             }
208 195
             ImGui::SameLine();
209 196
             ImGui::Checkbox("Scroll##fpsscroll", &scroll);

+ 2
- 0
src/SoundManager.cpp View File

@@ -5,6 +5,8 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
+#include "imgui/imgui.h"
9
+
8 10
 #include "global.h"
9 11
 #include "UI.h"
10 12
 #include "system/Sound.h"

+ 40
- 87
src/TextureManager.cpp View File

@@ -11,19 +11,16 @@
11 11
 #include <stdio.h>
12 12
 #include <stdarg.h>
13 13
 
14
+#include "stb/stb_image.h"
15
+
14 16
 #include "global.h"
15 17
 #include "Log.h"
16 18
 #include "RunTime.h"
17 19
 #include "utils/pcx.h"
18 20
 #include "utils/pixel.h"
19 21
 #include "utils/strings.h"
20
-#include "utils/tga.h"
21 22
 #include "TextureManager.h"
22 23
 
23
-#ifdef USING_PNG
24
-#include "utils/png.h"
25
-#endif
26
-
27 24
 glm::vec2 TextureTile::getUV(unsigned int i) {
28 25
     glm::vec2 uv(vertices.at(i).xPixel,
29 26
                  vertices.at(i).yPixel);
@@ -150,9 +147,9 @@ int TextureManager::initializeSplash() {
150 147
 
151 148
     //! \fixme Temporary?
152 149
     std::string filename = RunTime::getPakDir() + "/tr2/TITLE.PCX";
153
-    if (loadPCX(filename.c_str(), TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
150
+    if (loadImage(filename.c_str(), TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
154 151
         filename = RunTime::getDataDir() + "/splash.tga";
155
-        if (loadTGA(filename.c_str(), TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
152
+        if (loadImage(filename.c_str(), TextureStorage::SYSTEM, TEXTURE_SPLASH) < 0) {
156 153
             return -2;
157 154
         }
158 155
     }
@@ -241,100 +238,56 @@ void TextureManager::bindTextureId(unsigned int n, TextureStorage s, unsigned in
241 238
 }
242 239
 
243 240
 int TextureManager::loadImage(const char* filename, TextureStorage s, int slot) {
241
+    //! \todo case insensitive compare
242
+
244 243
     if (stringEndsWith(filename, ".pcx") || stringEndsWith(filename, ".PCX")) {
245 244
         return loadPCX(filename, s, slot);
246
-    } else if (stringEndsWith(filename, ".png") || stringEndsWith(filename, ".PNG")) {
247
-        return loadPNG(filename, s, slot);
248
-    } else if (stringEndsWith(filename, ".tga") || stringEndsWith(filename, ".TGA")) {
249
-        return loadTGA(filename, s, slot);
250 245
     } else {
251
-        getLog() << "No known image file type? (" << filename << ")" << Log::endl;
252
-    }
253
-
254
-    return -1;
255
-}
256
-
257
-int TextureManager::loadPCX(const char* filename, TextureStorage s, int slot) {
258
-    assert(filename != nullptr);
259
-    assert(filename[0] != '\0');
260
-
261
-    unsigned char* image;
262
-    unsigned int w, h, bpp;
263
-    ColorMode c;
264
-    int id = -1;
265
-    int error = pcxLoad(filename, &image, &w, &h, &c, &bpp);
266
-
267
-    if (error == 0) {
268
-        unsigned char* image2 = scaleBuffer(image, &w, &h, bpp);
269
-        if (image2) {
270
-            delete [] image;
271
-            image = image2;
246
+        int x, y, n;
247
+        unsigned char* data = stbi_load(filename, &x, &y, &n, 0);
248
+        if (data) {
249
+            if ((n < 3) || (n > 4)) {
250
+                getLog() << "Image \"" << filename << "\" has unsupported format ("
251
+                         << n << ")!" << Log::endl;
252
+                stbi_image_free(data);
253
+                return -2;
254
+            }
255
+            int id = loadBufferSlot(data, x, y, (n == 3) ? ColorMode::RGB : ColorMode::RGBA,
256
+                                    (n == 3) ? 24 : 32, s, slot);
257
+            stbi_image_free(data);
258
+            return id;
259
+        } else {
260
+            getLog() << "Can't load image \"" << filename << "\"!" << Log::endl;
261
+            return -1;
272 262
         }
273
-        id = loadBufferSlot(image, w, h, c, bpp, s, slot);
274
-        delete [] image;
275 263
     }
276
-
277
-    return id;
278 264
 }
279 265
 
280
-int TextureManager::loadPNG(const char* filename, TextureStorage s, int slot) {
281
-#ifdef USING_PNG
266
+int TextureManager::loadPCX(const char* filename, TextureStorage s, int slot) {
282 267
     assert(filename != nullptr);
283 268
     assert(filename[0] != '\0');
284 269
 
285
-    if (pngCheck(filename) != 0) {
286
-        return -1;
287
-    }
288
-
289
-    unsigned char* image;
290
-    unsigned int w, h, bpp;
291
-    ColorMode c;
292
-    int id = -1;
293
-    int error = pngLoad(filename, &image, &w, &h, &c, &bpp);
294
-
295
-    if (error == 0) {
296
-        unsigned char* image2 = scaleBuffer(image, &w, &h, bpp);
297
-        if (image2) {
270
+    int error = pcxCheck(filename);
271
+    if (!error) {
272
+        unsigned char* image;
273
+        unsigned int w, h, bpp;
274
+        ColorMode c;
275
+
276
+        error = pcxLoad(filename, &image, &w, &h, &c, &bpp);
277
+        if (!error) {
278
+            unsigned char* image2 = scaleBuffer(image, &w, &h, bpp);
279
+            if (image2) {
280
+                delete [] image;
281
+                image = image2;
282
+            }
283
+            int id = loadBufferSlot(image, w, h, c, bpp, s, slot);
298 284
             delete [] image;
299
-            image = image2;
285
+            return id;
300 286
         }
301
-        id = loadBufferSlot(image, w, h, c, bpp, s, slot);
302
-        delete [] image;
303
-    }
304 287
 
305
-    return id;
306
-#else
307
-    getLog() << "No PNG support available (" << filename << ")" << Log::endl;
308
-    return -1;
309
-#endif
310
-}
311
-
312
-int TextureManager::loadTGA(const char* filename, TextureStorage s, int slot) {
313
-    assert(filename != nullptr);
314
-    assert(filename[0] != '\0');
315
-
316
-    unsigned char* image;
317
-    unsigned int w, h;
318
-    char type;
319
-    int id = -1;
320
-
321
-    if (!tgaCheck(filename)) {
322
-        tgaLoad(filename, &image, &w, &h, &type);
323
-
324
-        unsigned char* image2 = scaleBuffer(image, &w, &h, (type == 2) ? 32 : 24);
325
-        if (image2) {
326
-            delete [] image;
327
-            image = image2;
328
-        }
329
-        if (image) {
330
-            id = loadBufferSlot(image, w, h,
331
-                                (type == 2) ? ColorMode::RGBA : ColorMode::RGB,
332
-                                (type == 2) ? 32 : 24,
333
-                                s, slot);
334
-            delete [] image;
335
-        }
288
+        return -5;
336 289
     }
337 290
 
338
-    return id;
291
+    return -4;
339 292
 }
340 293
 

+ 12
- 3
src/UI.cpp View File

@@ -8,6 +8,9 @@
8 8
 #include <algorithm>
9 9
 #include <cstring>
10 10
 
11
+#include "imgui/imgui.h"
12
+#include "stb/stb_image.h"
13
+
11 14
 #include "global.h"
12 15
 #include "Camera.h"
13 16
 #include "Console.h"
@@ -25,9 +28,6 @@
25 28
 #include "utils/time.h"
26 29
 #include "UI.h"
27 30
 
28
-#define STB_IMAGE_IMPLEMENTATION
29
-#include "imgui/stb_image.h"
30
-
31 31
 bool UI::visible = false;
32 32
 unsigned int UI::fontTex;
33 33
 std::string UI::iniFilename;
@@ -191,7 +191,9 @@ void UI::display() {
191 191
 
192 192
     Console::display();
193 193
 
194
+    static bool showTestWindow = false;
194 195
     if (ImGui::Begin("Engine")) {
196
+        Render::displayUI();
195 197
         RunTime::display();
196 198
         SoundManager::display();
197 199
 
@@ -422,10 +424,17 @@ void UI::display() {
422 424
             //                   " the left side and scale with the size of this window!");
423 425
             //ImGui::Separator();
424 426
             ImGui::ShowUserGuide();
427
+            ImGui::Separator();
428
+            if (ImGui::Button("Show/Hide Test Window")) {
429
+                showTestWindow = !showTestWindow;
430
+            }
425 431
         }
426 432
     }
427 433
     ImGui::End();
428 434
 
435
+    if (showTestWindow)
436
+        ImGui::ShowTestWindow();
437
+
429 438
     ImGui::Render();
430 439
 }
431 440
 

+ 3
- 26
src/commands/CommandEngine.cpp View File

@@ -51,8 +51,8 @@ std::string CommandScreenshot::brief() {
51 51
 
52 52
 void CommandScreenshot::printHelp() {
53 53
     getLog() << "sshot-Command Usage:" << Log::endl;
54
-    getLog() << "  sshot [debug|menu] [debug|menu]" << Log::endl;
55
-    getLog() << "Add console/menu to capture them too" << Log::endl;
54
+    getLog() << "  sshot" << Log::endl;
55
+    getLog() << "You wont be able to capture imgui..." << Log::endl;
56 56
 }
57 57
 
58 58
 int CommandScreenshot::execute(std::istream& args) {
@@ -65,30 +65,7 @@ int CommandScreenshot::execute(std::istream& args) {
65 65
     filename += "/sshots/";
66 66
     filename += VERSION_SHORT;
67 67
 
68
-    std::string temp, temp2;
69
-    args >> temp >> temp2;
70
-
71
-    UI::setVisible(false);
72
-    getMenu().setVisible(false);
73
-
74
-    if (temp == "debug")
75
-        UI::setVisible(true);
76
-    else if (temp == "menu")
77
-        getMenu().setVisible(true);
78
-
79
-    if (temp2 == "debug")
80
-        UI::setVisible(true);
81
-    else if (temp2 == "menu")
82
-        getMenu().setVisible(true);
83
-
84
-    renderFrame();
85
-    renderFrame(); // Double buffered
86 68
     Render::screenShot(filename.c_str());
87
-
88
-    getMenu().setVisible(false);
89
-    UI::setVisible(true);
90
-
91
-    getLog() << "Screenshot stored..." << Log::endl;
92 69
     return 0;
93 70
 }
94 71
 
@@ -103,7 +80,7 @@ std::string CommandQuit::brief() {
103 80
 }
104 81
 
105 82
 int CommandQuit::execute(std::istream& args) {
106
-    exit(0);
83
+    RunTime::setRunning(false);
107 84
     return 0;
108 85
 }
109 86
 

+ 2
- 1
src/deps/CMakeLists.txt View File

@@ -1,6 +1,7 @@
1 1
 # Source files
2 2
 set (DEPS_SRCS ${DEPS_SRCS} "commander/commander.c" "commander/commander.h")
3
-set (DEPS_SRCS ${DEPS_SRCS} "imgui/imgui.cpp" "imgui/imgui.h" "imgui/imconfig.h")
3
+set (DEPS_SRCS ${DEPS_SRCS} "imgui/imgui.cpp" "imgui/imgui.h" "imgui/imconfig.h" "imgui/stb_textedit.h")
4
+set (DEPS_SRCS ${DEPS_SRCS} "stb/stb.cpp" "stb/stb_image.h" "stb/stb_image_write.h")
4 5
 
5 6
 # Add library
6 7
 add_library (OpenRaider_deps OBJECT ${DEPS_SRCS})

+ 5
- 1
src/deps/imgui/imconfig.h View File

@@ -12,7 +12,8 @@
12 12
 //#define ImVector  MyVector
13 13
 
14 14
 //---- Define assertion handler. Defaults to calling assert().
15
-//#define IM_ASSERT(_EXPR)  MyAssert(_EXPR)
15
+#include "global.h"
16
+#define IM_ASSERT(_EXPR)  assert(_EXPR)
16 17
 
17 18
 //---- Don't implement default clipboard handlers for Windows (so as not to link with OpenClipboard() and others Win32 functions)
18 19
 //#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCS
@@ -20,6 +21,9 @@
20 21
 //---- Include imgui_user.inl at the end of imgui.cpp so you can include code that extends ImGui using its private data/functions.
21 22
 //#define IMGUI_INCLUDE_IMGUI_USER_INL
22 23
 
24
+//---- Include imgui_user.h at the end of imgui.h
25
+//#define IMGUI_INCLUDE_IMGUI_USER_H
26
+
23 27
 //---- Define implicit cast operators to convert back<>forth from your math types and ImVec2/ImVec4.
24 28
 /*
25 29
 #define IM_VEC2_CLASS_EXTRA                                                 \

+ 421
- 234
src/deps/imgui/imgui.cpp
File diff suppressed because it is too large
View File


+ 81
- 39
src/deps/imgui/imgui.h View File

@@ -1,4 +1,4 @@
1
-// ImGui library v1.18 wip
1
+// ImGui library v1.19 wip
2 2
 // See .cpp file for commentary.
3 3
 // See ImGui::ShowTestWindow() for sample code.
4 4
 // Read 'Programmer guide' in .cpp for notes on how to setup ImGui in your codebase.
@@ -37,6 +37,7 @@ typedef int ImGuiStyleVar;          // enum ImGuiStyleVar_
37 37
 typedef int ImGuiKey;               // enum ImGuiKey_
38 38
 typedef int ImGuiColorEditMode;     // enum ImGuiColorEditMode_
39 39
 typedef int ImGuiWindowFlags;       // enum ImGuiWindowFlags_
40
+typedef int ImGuiSetCondition;      // enum ImGuiSetCondition_
40 41
 typedef int ImGuiInputTextFlags;    // enum ImGuiInputTextFlags_
41 42
 struct ImGuiTextEditCallbackData;
42 43
 
@@ -117,7 +118,7 @@ public:
117 118
     inline void                 pop_back()                      { IM_ASSERT(Size > 0); Size--; }
118 119
 
119 120
     inline iterator             erase(const_iterator it)        { IM_ASSERT(it >= begin() && it < end()); const ptrdiff_t off = it - begin(); memmove(Data + off, Data + off + 1, (Size - (size_t)off - 1) * sizeof(value_type)); Size--; return Data + off; }
120
-    inline void                 insert(const_iterator it, const value_type& v)  { IM_ASSERT(it >= begin() && it <= end()); const ptrdiff_t off = it - begin(); if (Size == Capacity) reserve(Capacity ? Capacity * 2 : 4); if (off < (int)Size) memmove(Data + off + 1, Data + off, (Size - (size_t)off) * sizeof(value_type)); Data[off] = v; Size++; }
121
+    inline iterator             insert(const_iterator it, const value_type& v)  { IM_ASSERT(it >= begin() && it <= end()); const ptrdiff_t off = it - begin(); if (Size == Capacity) reserve(Capacity ? Capacity * 2 : 4); if (off < (int)Size) memmove(Data + off + 1, Data + off, (Size - (size_t)off) * sizeof(value_type)); Data[off] = v; Size++; return Data + off; }
121 122
 };
122 123
 #endif // #ifndef ImVector
123 124
 
@@ -127,7 +128,7 @@ public:
127 128
 // - struct ImGuiTextBuffer             // Text buffer for logging/accumulating text
128 129
 // - struct ImGuiStorage                // Custom key value storage (if you need to alter open/close states manually)
129 130
 // - struct ImDrawList                  // Draw command list
130
-// - struct ImBitmapFont                // Bitmap font loader
131
+// - struct ImFont                      // Bitmap font loader
131 132
 
132 133
 // ImGui End-user API
133 134
 // In a namespace so that user can add extra functions (e.g. Value() helpers for your vector or common types)
@@ -144,16 +145,11 @@ namespace ImGui
144 145
     IMGUI_API void          ShowTestWindow(bool* open = NULL);
145 146
 
146 147
     // Window
147
-    IMGUI_API bool          Begin(const char* name = "Debug", bool* open = NULL, ImVec2 size = ImVec2(0,0), float fill_alpha = -1.0f, ImGuiWindowFlags flags = 0);    // return false when window is collapsed, so you can early out in your code.
148
+    IMGUI_API bool          Begin(const char* name = "Debug", bool* p_opened = NULL, ImVec2 size = ImVec2(0,0), float fill_alpha = -1.0f, ImGuiWindowFlags flags = 0);// return false when window is collapsed, so you can early out in your code. passing 'bool* p_opened' displays a Close button on the upper-right corner of the window, the pointed value will be set to false when the button is pressed.
148 149
     IMGUI_API void          End();
149 150
     IMGUI_API void          BeginChild(const char* str_id, ImVec2 size = ImVec2(0,0), bool border = false, ImGuiWindowFlags extra_flags = 0);                         // size==0.0f: use remaining window size, size<0.0f: use remaining window size minus abs(size). on each axis.
150 151
     IMGUI_API void          EndChild();
151 152
     IMGUI_API bool          GetWindowIsFocused();
152
-    IMGUI_API ImVec2        GetWindowSize();
153
-    IMGUI_API float         GetWindowWidth();
154
-    IMGUI_API void          SetWindowSize(const ImVec2& size);                                  // set to ImVec2(0,0) to force an auto-fit
155
-    IMGUI_API ImVec2        GetWindowPos();                                                     // you should rarely need/care about the window position, but it can be useful if you want to use your own drawing.
156
-    IMGUI_API void          SetWindowPos(const ImVec2& pos);                                    // set current window pos.
157 153
     IMGUI_API ImVec2        GetContentRegionMax();                                              // window or current column boundaries
158 154
     IMGUI_API ImVec2        GetWindowContentRegionMin();                                        // window boundaries
159 155
     IMGUI_API ImVec2        GetWindowContentRegionMax();
@@ -161,11 +157,22 @@ namespace ImGui
161 157
     IMGUI_API ImFont*       GetWindowFont();
162 158
     IMGUI_API float         GetWindowFontSize();
163 159
     IMGUI_API void          SetWindowFontScale(float scale);                                    // per-window font scale. Adjust IO.FontGlobalScale if you want to scale all windows.
160
+    IMGUI_API ImVec2        GetWindowPos();                                                     // you should rarely need/care about the window position, but it can be useful if you want to do your own drawing.
161
+    IMGUI_API ImVec2        GetWindowSize();                                                    // get current window position.
162
+    IMGUI_API float         GetWindowWidth();
163
+    IMGUI_API bool          GetWindowCollapsed();
164
+    IMGUI_API void          SetWindowPos(const ImVec2& pos, ImGuiSetCondition cond = 0);        // set current window position - call within Begin()/End().
165
+    IMGUI_API void          SetWindowSize(const ImVec2& size, ImGuiSetCondition cond = 0);      // set current window size. set to ImVec2(0,0) to force an auto-fit
166
+    IMGUI_API void          SetWindowCollapsed(bool collapsed, ImGuiSetCondition cond = 0);     // set current window collapsed state.
167
+    IMGUI_API void          SetNextWindowPos(const ImVec2& pos, ImGuiSetCondition cond = 0);    // set next window position - call before Begin().
168
+    IMGUI_API void          SetNextWindowSize(const ImVec2& size, ImGuiSetCondition cond = 0);  // set next window size. set to ImVec2(0,0) to force an auto-fit
169
+    IMGUI_API void          SetNextWindowCollapsed(bool collapsed, ImGuiSetCondition cond = 0); // set next window collapsed state.
170
+
164 171
     IMGUI_API void          SetScrollPosHere();                                                 // adjust scrolling position to center into the current cursor position.
165
-    IMGUI_API void          SetKeyboardFocusHere(int offset = 0);                               // focus keyboard on the next widget. Use 'offset' to access sub components of a multiple component widget.
172
+    IMGUI_API void          SetKeyboardFocusHere(int offset = 0);                               // focus keyboard on the next widget. Use positive 'offset' to access sub components of a multiple component widget.
166 173
     IMGUI_API void          SetTreeStateStorage(ImGuiStorage* tree);                            // replace tree state storage with our own (if you want to manipulate it yourself, typically clear subsection of it).
167 174
     IMGUI_API ImGuiStorage* GetTreeStateStorage();
168
-    
175
+
169 176
     IMGUI_API void          PushItemWidth(float item_width);                                    // width of items for the common item+label case. default to ~2/3 of windows width.
170 177
     IMGUI_API void          PopItemWidth();
171 178
     IMGUI_API float         GetItemWidth();
@@ -204,10 +211,13 @@ namespace ImGui
204 211
     IMGUI_API float         GetTextLineHeight();
205 212
 
206 213
     // ID scopes
207
-    IMGUI_API void          PushID(const char* str_id);
214
+    // If you are creating repeated widgets in a loop you most likely want to push a unique identifier so ImGui can differentiate them.
215
+    IMGUI_API void          PushID(const char* str_id);                                         // push identifier into the ID stack. IDs are hash of the *entire* stack!
208 216
     IMGUI_API void          PushID(const void* ptr_id);
209 217
     IMGUI_API void          PushID(const int int_id);
210 218
     IMGUI_API void          PopID();
219
+    IMGUI_API ImGuiID       GetID(const char* str_id);                                          // calculate unique ID (hash of whole ID stack + given parameter). useful if you want to query into ImGuiStorage yourself. otherwise rarely needed.
220
+    IMGUI_API ImGuiID       GetID(const void* ptr_id);
211 221
 
212 222
     // Widgets
213 223
     IMGUI_API void          Text(const char* fmt, ...);
@@ -261,8 +271,7 @@ namespace ImGui
261 271
     IMGUI_API void          TreePop();
262 272
     IMGUI_API void          OpenNextNode(bool open);                                            // force open/close the next TreeNode or CollapsingHeader
263 273
 
264
-    // Value helper output "name: value"
265
-    // Freely declare your own in the ImGui namespace.
274
+    // Value helper output "name: value". tip: freely declare your own within the ImGui namespace!
266 275
     IMGUI_API void          Value(const char* prefix, bool b);
267 276
     IMGUI_API void          Value(const char* prefix, int v);
268 277
     IMGUI_API void          Value(const char* prefix, unsigned int v);
@@ -277,7 +286,6 @@ namespace ImGui
277 286
     IMGUI_API void          LogToClipboard(int max_depth = -1);
278 287
 
279 288
     // Utilities
280
-    IMGUI_API void          SetNewWindowDefaultPos(const ImVec2& pos);                          // set position of window that do
281 289
     IMGUI_API bool          IsItemHovered();                                                    // was the last item active area hovered by mouse?
282 290
     IMGUI_API bool          IsItemFocused();                                                    // was the last item focused for keyboard input?
283 291
     IMGUI_API ImVec2        GetItemBoxMin();                                                    // get bounding box of last item
@@ -295,7 +303,7 @@ namespace ImGui
295 303
     IMGUI_API int           GetFrameCount();
296 304
     IMGUI_API const char*   GetStyleColorName(ImGuiCol idx);
297 305
     IMGUI_API void          GetDefaultFontData(const void** fnt_data, unsigned int* fnt_size, const void** png_data, unsigned int* png_size);
298
-    IMGUI_API ImVec2        CalcTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_double_hash = true, float wrap_width = -1.0f);
306
+    IMGUI_API ImVec2        CalcTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_double_hash = false, float wrap_width = -1.0f);
299 307
 
300 308
 } // namespace ImGui
301 309
 
@@ -310,11 +318,12 @@ enum ImGuiWindowFlags_
310 318
     ImGuiWindowFlags_NoScrollbar            = 1 << 4,
311 319
     ImGuiWindowFlags_NoScrollWithMouse      = 1 << 5,
312 320
     ImGuiWindowFlags_AlwaysAutoResize       = 1 << 6,
313
-    ImGuiWindowFlags_ChildWindow            = 1 << 7,   // For internal use by BeginChild()
314
-    ImGuiWindowFlags_ChildWindowAutoFitX    = 1 << 8,   // For internal use by BeginChild()
315
-    ImGuiWindowFlags_ChildWindowAutoFitY    = 1 << 9,   // For internal use by BeginChild()
316
-    ImGuiWindowFlags_ComboBox               = 1 << 10,  // For internal use by ComboBox()
317
-    ImGuiWindowFlags_Tooltip                = 1 << 11   // For internal use by Render() when using Tooltip
321
+    ImGuiWindowFlags_NoSavedSettings        = 1 << 7,   // Never load/save settings in .ini file
322
+    ImGuiWindowFlags_ChildWindow            = 1 << 8,   // For internal use by BeginChild()
323
+    ImGuiWindowFlags_ChildWindowAutoFitX    = 1 << 9,   // For internal use by BeginChild()
324
+    ImGuiWindowFlags_ChildWindowAutoFitY    = 1 << 10,  // For internal use by BeginChild()
325
+    ImGuiWindowFlags_ComboBox               = 1 << 11,  // For internal use by ComboBox()
326
+    ImGuiWindowFlags_Tooltip                = 1 << 12   // For internal use by Render() when using Tooltip
318 327
 };
319 328
 
320 329
 // Flags for ImGui::InputText()
@@ -327,7 +336,7 @@ enum ImGuiInputTextFlags_
327 336
     ImGuiInputTextFlags_EnterReturnsTrue    = 1 << 3,   // Return 'true' when Enter is pressed (as opposed to when the value was modified)
328 337
     ImGuiInputTextFlags_CallbackCompletion  = 1 << 4,   // Call user function on pressing TAB (for completion handling)
329 338
     ImGuiInputTextFlags_CallbackHistory     = 1 << 5,   // Call user function on pressing Up/Down arrows (for history handling)
330
-    ImGuiInputTextFlags_CallbackAlways      = 1 << 6    // Call user function every frame
339
+    ImGuiInputTextFlags_CallbackAlways      = 1 << 6    // Call user function every time
331 340
     //ImGuiInputTextFlags_AlignCenter       = 1 << 6,
332 341
 };
333 342
 
@@ -403,6 +412,7 @@ enum ImGuiStyleVar_
403 412
 {
404 413
     ImGuiStyleVar_Alpha,             // float
405 414
     ImGuiStyleVar_WindowPadding,     // ImVec2
415
+    ImGuiStyleVar_WindowRounding,    // float
406 416
     ImGuiStyleVar_FramePadding,      // ImVec2
407 417
     ImGuiStyleVar_ItemSpacing,       // ImVec2
408 418
     ImGuiStyleVar_ItemInnerSpacing,  // ImVec2
@@ -419,6 +429,15 @@ enum ImGuiColorEditMode_
419 429
     ImGuiColorEditMode_HEX = 2
420 430
 };
421 431
 
432
+// Condition flags for ImGui::SetWindow***() and SetNextWindow***() functions
433
+// Those functions treat 0 as a shortcut to ImGuiSetCondition_Always
434
+enum ImGuiSetCondition_
435
+{
436
+    ImGuiSetCondition_Always              = 1 << 0, // Set the variable
437
+    ImGuiSetCondition_FirstUseThisSession = 1 << 1, // Only set the variable on the first call for this window (once per session)
438
+    ImGuiSetCondition_FirstUseEver        = 1 << 2, // Only set the variable if the window doesn't exist in the .ini file
439
+};
440
+
422 441
 struct ImGuiStyle
423 442
 {
424 443
     float       Alpha;                      // Global alpha applies to everything in ImGui
@@ -449,9 +468,9 @@ struct ImGuiIO
449 468
 
450 469
     ImVec2      DisplaySize;                // <unset>                  // Display size, in pixels. For clamping windows positions.
451 470
     float       DeltaTime;                  // = 1.0f/60.0f             // Time elapsed since last frame, in seconds.
452
-    float       IniSavingRate;              // = 5.0f                   // Maximum time between saving .ini file, in seconds. Set to a negative value to disable .ini saving.
453
-    const char* IniFilename;                // = "imgui.ini"            // Absolute path to .ini file.
454
-    const char* LogFilename;                // = "imgui_log.txt"        // Absolute path to .log file.
471
+    float       IniSavingRate;              // = 5.0f                   // Maximum time between saving .ini file, in seconds.
472
+    const char* IniFilename;                // = "imgui.ini"            // Path to .ini file. NULL to disable .ini saving.
473
+    const char* LogFilename;                // = "imgui_log.txt"        // Path to .log file (default parameter to ImGui::LogToFile when no file is specified).
455 474
     float       MouseDoubleClickTime;       // = 0.30f                  // Time for a double-click, in seconds.
456 475
     float       MouseDoubleClickMaxDist;    // = 6.0f                   // Distance threshold to stay in to validate a double-click, in pixels.
457 476
     int         KeyMap[ImGuiKey_COUNT];     // <unset>                  // Map of indices into the KeysDown[512] entries array
@@ -480,7 +499,7 @@ struct ImGuiIO
480 499
     void*       (*MemReallocFn)(void* ptr, size_t sz);
481 500
     void        (*MemFreeFn)(void* ptr);
482 501
 
483
-    // Optional: notify OS Input Method Editor of text input position (e.g. when using Japanese/Chinese inputs, otherwise this isn't needed)
502
+    // Optional: notify OS Input Method Editor of the screen position of your cursor for text input position (e.g. when using Japanese/Chinese inputs in Windows)
484 503
     void        (*ImeSetInputScreenPosFn)(int x, int y);
485 504
 
486 505
     //------------------------------------------------------------------
@@ -588,22 +607,38 @@ struct ImGuiTextBuffer
588 607
 };
589 608
 
590 609
 // Helper: Key->value storage
591
-// - Store collapse state for a tree
592
-// - Store color edit options, etc.
610
+// - Store collapse state for a tree (Int 0/1)
611
+// - Store color edit options (Int using values in ImGuiColorEditMode enum).
612
+// - Custom user storage for temporary values.
593 613
 // Typically you don't have to worry about this since a storage is held within each Window.
594
-// Declare your own storage if you want to manipulate the open/close state of a particular sub-tree in your interface.
614
+// Declare your own storage if:
615
+// - You want to manipulate the open/close state of a particular sub-tree in your interface (tree node uses Int 0/1 to store their state).
616
+// - You want to store custom debug data easily without adding or editing structures in your code.
595 617
 struct ImGuiStorage
596 618
 {
597
-    struct Pair { ImU32 key; int val; };
619
+    struct Pair 
620
+    { 
621
+        ImGuiID key; 
622
+        union { int val_i; float val_f; };        
623
+        Pair(ImGuiID _key, int _val_i) { key = _key; val_i = _val_i; } 
624
+        Pair(ImGuiID _key, float _val_f) { key = _key; val_f = _val_f; } 
625
+    };
598 626
     ImVector<Pair>    Data;
599 627
 
628
+    // - Get***() functions find pair, never add/allocate. Pairs are sorted so a query is O(log N)
629
+    // - Set***() functions find pair, insertion on demand if missing.
630
+    // - Get***Ptr() functions find pair, insertion on demand if missing, return pointer. Useful if you intend to do Get+Set. 
631
+    //   A typical use case where this is very convenient:
632
+    //      float* pvar = ImGui::GetIntPtr(key); ImGui::SliderInt("var", pvar, 0, 100); some_var += *pvar;
633
+    // - Sorted insertion is costly but should amortize. A typical frame shouldn't need to insert any new pair.
600 634
     IMGUI_API void    Clear();
601
-    IMGUI_API int     GetInt(ImU32 key, int default_val = 0);
602
-    IMGUI_API void    SetInt(ImU32 key, int val);
603
-    IMGUI_API void    SetAllInt(int val);
604
-
605
-    IMGUI_API int*    Find(ImU32 key);
606
-    IMGUI_API void    Insert(ImU32 key, int val);
635
+    IMGUI_API int     GetInt(ImGuiID key, int default_val = 0) const;
636
+    IMGUI_API void    SetInt(ImGuiID key, int val);
637
+    IMGUI_API int*    GetIntPtr(ImGuiID key, int default_val = 0);
638
+    IMGUI_API float   GetFloat(ImGuiID key, float default_val = 0.0f) const;
639
+    IMGUI_API void    SetFloat(ImGuiID key, float val);
640
+    IMGUI_API float*  GetFloatPtr(ImGuiID key, float default_val = 0);
641
+    IMGUI_API void    SetAllInt(int val);    // Use on your own storage if you know only integer are being stored.
607 642
 };
608 643
 
609 644
 // Shared state of InputText(), passed to callback when a ImGuiInputTextFlags_Callback* flag is used.
@@ -651,8 +686,8 @@ IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT;
651 686
 #endif
652 687
 
653 688
 // Draw command list
654
-// This is the low-level list of polygon that ImGui:: functions are filling. At the end of the frame, all command lists are passed to your ImGuiIO::RenderDrawListFn function for rendering.
655
-// Each ImGui window contains its own ImDrawList.
689
+// This is the low-level list of polygon that ImGui:: functions are creating. At the end of the frame, all command lists are passed to your ImGuiIO::RenderDrawListFn function for rendering.
690
+// At the moment, each ImGui window contains its own ImDrawList but they could potentially be merged.
656 691
 // If you want to add custom rendering within a window, you can use ImGui::GetWindowDrawList() to access the current draw list and add your own primitives.
657 692
 // You can interleave normal ImGui:: calls and adding primitives to the current draw list.
658 693
 // Note that this only gives you access to rendering polygons. If your intent is to create custom widgets and the publicly exposed functions/data aren't sufficient, you can add code in imgui_user.inl
@@ -686,7 +721,7 @@ struct ImDrawList
686 721
     IMGUI_API void  AddText(ImFont* font, float font_size, const ImVec2& pos, ImU32 col, const char* text_begin, const char* text_end = NULL, float wrap_width = 0.0f);
687 722
 };
688 723
 
689
-// Optional bitmap font data loader & renderer into vertices
724
+// Bitmap font data loader & renderer into vertices
690 725
 // Using the .fnt format exported by BMFont
691 726
 //  - tool: http://www.angelcode.com/products/bmfont
692 727
 //  - file-format: http://www.angelcode.com/products/bmfont/doc/file_format.html
@@ -778,3 +813,10 @@ struct ImFont
778 813
     };
779 814
 #pragma pack(pop)
780 815
 };
816
+
817
+//---- Include imgui_user.h at the end of imgui.h
818
+//---- So you can include code that extends ImGui using any of the types declared above.
819
+//---- (also convenient for user to only explicitly include vanilla imgui.h)
820
+#ifdef IMGUI_INCLUDE_IMGUI_USER_H
821
+#include "imgui_user.h"
822
+#endif

+ 31
- 1
src/deps/imgui/stb_textedit.h View File

@@ -1,4 +1,4 @@
1
-// stb_textedit.h - v1.4  - public domain - Sean Barrett
1
+// stb_textedit.h - v1.5  - public domain - Sean Barrett
2 2
 // Development of this library was sponsored by RAD Game Tools
3 3
 //
4 4
 // This C header file implements the guts of a multi-line text-editing
@@ -30,6 +30,7 @@
30 30
 //
31 31
 // VERSION HISTORY
32 32
 //
33
+//   1.5  (2014-09-10) add support for secondary keys for OS X
33 34
 //   1.4  (2014-08-17) fix signed/unsigned warnings
34 35
 //   1.3  (2014-06-19) fix mouse clicking to round to nearest char boundary
35 36
 //   1.2  (2014-05-27) fix some RAD types that had crept into the new code
@@ -43,6 +44,7 @@
43 44
 //
44 45
 //   Ulf Winklemann: move-by-word in 1.1
45 46
 //   Scott Graham: mouse selection bugfix in 1.3
47
+//   Fabian Giesen: secondary key inputs in 1.5
46 48
 //
47 49
 // USAGE
48 50
 //
@@ -142,6 +144,10 @@
142 144
 //                                 required for WORDLEFT/WORDRIGHT
143 145
 //    STB_TEXTEDIT_K_WORDLEFT    keyboard input to move cursor left one word // e.g. ctrl-LEFT
144 146
 //    STB_TEXTEDIT_K_WORDRIGHT   keyboard input to move cursor right one word // e.g. ctrl-RIGHT
147
+//    STB_TEXTEDIT_K_LINESTART2  secondary keyboard input to move cursor to start of line
148
+//    STB_TEXTEDIT_K_LINEEND2    secondary keyboard input to move cursor to end of line
149
+//    STB_TEXTEDIT_K_TEXTSTART2  secondary keyboard input to move cursor to start of text
150
+//    STB_TEXTEDIT_K_TEXTEND2    secondary keyboard input to move cursor to end of text
145 151
 //
146 152
 // Todo:
147 153
 //    STB_TEXTEDIT_K_PGUP        keyboard input to move cursor up a page
@@ -917,23 +923,35 @@ retry:
917 923
          state->has_preferred_x = 0;
918 924
          break;
919 925
          
926
+#ifdef STB_TEXTEDIT_K_TEXTSTART2
927
+      case STB_TEXTEDIT_K_TEXTSTART2:
928
+#endif
920 929
       case STB_TEXTEDIT_K_TEXTSTART:
921 930
          state->cursor = state->select_start = state->select_end = 0;
922 931
          state->has_preferred_x = 0;
923 932
          break;
924 933
 
934
+#ifdef STB_TEXTEDIT_K_TEXTEND2
935
+      case STB_TEXTEDIT_K_TEXTEND2:
936
+#endif
925 937
       case STB_TEXTEDIT_K_TEXTEND:
926 938
          state->cursor = STB_TEXTEDIT_STRINGLEN(str);
927 939
          state->select_start = state->select_end = 0;
928 940
          state->has_preferred_x = 0;
929 941
          break;
930 942
         
943
+#ifdef STB_TEXTEDIT_K_TEXTSTART2
944
+      case STB_TEXTEDIT_K_TEXTSTART2 | STB_TEXTEDIT_K_SHIFT:
945
+#endif
931 946
       case STB_TEXTEDIT_K_TEXTSTART | STB_TEXTEDIT_K_SHIFT:
932 947
          stb_textedit_prep_selection_at_cursor(state);
933 948
          state->cursor = state->select_end = 0;
934 949
          state->has_preferred_x = 0;
935 950
          break;
936 951
 
952
+#ifdef STB_TEXTEDIT_K_TEXTEND2
953
+      case STB_TEXTEDIT_K_TEXTEND2 | STB_TEXTEDIT_K_SHIFT:
954
+#endif
937 955
       case STB_TEXTEDIT_K_TEXTEND | STB_TEXTEDIT_K_SHIFT:
938 956
          stb_textedit_prep_selection_at_cursor(state);
939 957
          state->cursor = state->select_end = STB_TEXTEDIT_STRINGLEN(str);
@@ -941,6 +959,9 @@ retry:
941 959
          break;
942 960
 
943 961
 
962
+#ifdef STB_TEXTEDIT_K_LINESTART2
963
+      case STB_TEXTEDIT_K_LINESTART2:
964
+#endif
944 965
       case STB_TEXTEDIT_K_LINESTART: {
945 966
          StbFindState find;
946 967
          stb_textedit_clamp(str, state);
@@ -951,6 +972,9 @@ retry:
951 972
          break;
952 973
       }
953 974
 
975
+#ifdef STB_TEXTEDIT_K_LINEEND2
976
+      case STB_TEXTEDIT_K_LINEEND2:
977
+#endif
954 978
       case STB_TEXTEDIT_K_LINEEND: {
955 979
          StbFindState find;
956 980
          stb_textedit_clamp(str, state);
@@ -961,6 +985,9 @@ retry:
961 985
          break;
962 986
       }
963 987
 
988
+#ifdef STB_TEXTEDIT_K_LINESTART2
989
+      case STB_TEXTEDIT_K_LINESTART2 | STB_TEXTEDIT_K_SHIFT:
990
+#endif
964 991
       case STB_TEXTEDIT_K_LINESTART | STB_TEXTEDIT_K_SHIFT: {
965 992
          StbFindState find;
966 993
          stb_textedit_clamp(str, state);
@@ -971,6 +998,9 @@ retry:
971 998
          break;
972 999
       }
973 1000
 
1001
+#ifdef STB_TEXTEDIT_K_LINEEND2
1002
+      case STB_TEXTEDIT_K_LINEEND2 | STB_TEXTEDIT_K_SHIFT:
1003
+#endif
974 1004
       case STB_TEXTEDIT_K_LINEEND | STB_TEXTEDIT_K_SHIFT: {
975 1005
          StbFindState find;
976 1006
          stb_textedit_clamp(str, state);

+ 16
- 0
src/deps/stb/stb.cpp View File

@@ -0,0 +1,16 @@
1
+/*!
2
+ * \file src/deps/stb/stb.cpp
3
+ * \brief stb implementation
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include "global.h"
9
+
10
+#define STB_IMAGE_IMPLEMENTATION
11
+#define STBI_ASSERT(x) assert(x)
12
+#include "stb/stb_image.h"
13
+
14
+#define STB_IMAGE_WRITE_IMPLEMENTATION
15
+#include "stb/stb_image_write.h"
16
+

src/deps/stb/stb_image.h
File diff suppressed because it is too large
View File


+ 525
- 0
src/deps/stb/stb_image_write.h View File

@@ -0,0 +1,525 @@
1
+/* stb_image_write - v0.95 - public domain - http://nothings.org/stb/stb_image_write.h
2
+   writes out PNG/BMP/TGA images to C stdio - Sean Barrett 2010
3
+                            no warranty implied; use at your own risk
4
+
5
+
6
+Before including,
7
+
8
+    #define STB_IMAGE_WRITE_IMPLEMENTATION
9
+
10
+in the file that you want to have the implementation.
11
+
12
+Will probably not work correctly with strict-aliasing optimizations.
13
+
14
+
15
+ABOUT:
16
+
17
+   This header file is a library for writing images to C stdio. It could be
18
+   adapted to write to memory or a general streaming interface; let me know.
19
+
20
+   The PNG output is not optimal; it is 20-50% larger than the file
21
+   written by a decent optimizing implementation. This library is designed
22
+   for source code compactness and simplicitly, not optimal image file size
23
+   or run-time performance.
24
+
25
+USAGE:
26
+
27
+   There are three functions, one for each image file format:
28
+
29
+     int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
30
+     int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
31
+     int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
32
+
33
+   Each function returns 0 on failure and non-0 on success.
34
+
35
+   The functions create an image file defined by the parameters. The image
36
+   is a rectangle of pixels stored from left-to-right, top-to-bottom.
37
+   Each pixel contains 'comp' channels of data stored interleaved with 8-bits
38
+   per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is
39
+   monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall.
40
+   The *data pointer points to the first byte of the top-left-most pixel.
41
+   For PNG, "stride_in_bytes" is the distance in bytes from the first byte of
42
+   a row of pixels to the first byte of the next row of pixels.
43
+
44
+   PNG creates output files with the same number of components as the input.
45
+   The BMP format expands Y to RGB in the file format and does not
46
+   output alpha.
47
+
48
+   PNG supports writing rectangles of data even when the bytes storing rows of
49
+   data are not consecutive in memory (e.g. sub-rectangles of a larger image),
50
+   by supplying the stride between the beginning of adjacent rows. The other
51
+   formats do not. (Thus you cannot write a native-format BMP through the BMP
52
+   writer, both because it is in BGR order and because it may have padding
53
+   at the end of the line.)
54
+*/
55
+
56
+#ifndef INCLUDE_STB_IMAGE_WRITE_H
57
+#define INCLUDE_STB_IMAGE_WRITE_H
58
+
59
+#ifdef __cplusplus
60
+extern "C" {
61
+#endif
62
+
63
+extern int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
64
+extern int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
65
+extern int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
66
+
67
+#ifdef __cplusplus
68
+}
69
+#endif
70
+
71
+#endif//INCLUDE_STB_IMAGE_WRITE_H
72
+
73
+#ifdef STB_IMAGE_WRITE_IMPLEMENTATION
74
+
75
+#include <stdarg.h>
76
+#include <stdlib.h>
77
+#include <stdio.h>
78
+#include <string.h>
79
+
80
+#ifndef STBI_ASSERT
81
+#include <assert.h>
82
+#define STBI_ASSERT(x) assert(x)
83
+#endif
84
+
85
+typedef unsigned int stbiw_uint32;
86
+typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1];
87
+
88
+static void writefv(FILE *f, const char *fmt, va_list v)
89
+{
90
+   while (*fmt) {
91
+      switch (*fmt++) {
92
+         case ' ': break;
93
+         case '1': { unsigned char x = (unsigned char) va_arg(v, int); fputc(x,f); break; }
94
+         case '2': { int x = va_arg(v,int); unsigned char b[2];
95
+                     b[0] = (unsigned char) x; b[1] = (unsigned char) (x>>8);
96
+                     fwrite(b,2,1,f); break; }
97
+         case '4': { stbiw_uint32 x = va_arg(v,int); unsigned char b[4];
98
+                     b[0]=(unsigned char)x; b[1]=(unsigned char)(x>>8);
99
+                     b[2]=(unsigned char)(x>>16); b[3]=(unsigned char)(x>>24);
100
+                     fwrite(b,4,1,f); break; }
101
+         default:
102
+            STBI_ASSERT(0);
103
+            return;
104
+      }
105
+   }
106
+}
107
+
108
+static void write3(FILE *f, unsigned char a, unsigned char b, unsigned char c)
109
+{
110
+   unsigned char arr[3];
111
+   arr[0] = a, arr[1] = b, arr[2] = c;
112
+   fwrite(arr, 3, 1, f);
113
+}
114
+
115
+static void write_pixels(FILE *f, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad)
116
+{
117
+   unsigned char bg[3] = { 255, 0, 255}, px[3];
118
+   stbiw_uint32 zero = 0;
119
+   int i,j,k, j_end;
120
+
121
+   if (y <= 0)
122
+      return;
123
+
124
+   if (vdir < 0)
125
+      j_end = -1, j = y-1;
126
+   else
127
+      j_end =  y, j = 0;
128
+
129
+   for (; j != j_end; j += vdir) {
130
+      for (i=0; i < x; ++i) {
131
+         unsigned char *d = (unsigned char *) data + (j*x+i)*comp;
132
+         if (write_alpha < 0)
133
+            fwrite(&d[comp-1], 1, 1, f);
134
+         switch (comp) {
135
+            case 1:
136
+            case 2: fwrite(d, 1, 1, f);
137
+                    break;
138
+            case 4:
139
+               if (!write_alpha) {
140
+                  // composite against pink background
141
+                  for (k=0; k < 3; ++k)
142
+                     px[k] = bg[k] + ((d[k] - bg[k]) * d[3])/255;
143
+                  write3(f, px[1-rgb_dir],px[1],px[1+rgb_dir]);
144
+                  break;
145
+               }
146
+               /* FALLTHROUGH */
147
+            case 3:
148
+               write3(f, d[1-rgb_dir],d[1],d[1+rgb_dir]);
149
+               break;
150
+         }
151
+         if (write_alpha > 0)
152
+            fwrite(&d[comp-1], 1, 1, f);
153
+      }
154
+      fwrite(&zero,scanline_pad,1,f);
155
+   }
156
+}
157
+
158
+static int outfile(char const *filename, int rgb_dir, int vdir, int x, int y, int comp, void *data, int alpha, int pad, const char *fmt, ...)
159
+{
160
+   FILE *f;
161
+   if (y < 0 || x < 0) return 0;
162
+   f = fopen(filename, "wb");
163
+   if (f) {
164
+      va_list v;
165
+      va_start(v, fmt);
166
+      writefv(f, fmt, v);
167
+      va_end(v);
168
+      write_pixels(f,rgb_dir,vdir,x,y,comp,data,alpha,pad);
169
+      fclose(f);
170
+   }
171
+   return f != NULL;
172
+}
173
+
174
+int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data)
175
+{
176
+   int pad = (-x*3) & 3;
177
+   return outfile(filename,-1,-1,x,y,comp,(void *) data,0,pad,
178
+           "11 4 22 4" "4 44 22 444444",
179
+           'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40,  // file header
180
+            40, x,y, 1,24, 0,0,0,0,0,0);             // bitmap header
181
+}
182
+
183
+int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data)
184
+{
185
+   int has_alpha = (comp == 2 || comp == 4);
186
+   int colorbytes = has_alpha ? comp-1 : comp;
187
+   int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3
188
+   return outfile(filename, -1,-1, x, y, comp, (void *) data, has_alpha, 0,
189
+                  "111 221 2222 11", 0,0,format, 0,0,0, 0,0,x,y, (colorbytes+has_alpha)*8, has_alpha*8);
190
+}
191
+
192
+// stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size()
193
+#define stbiw__sbraw(a) ((int *) (a) - 2)
194
+#define stbiw__sbm(a)   stbiw__sbraw(a)[0]
195
+#define stbiw__sbn(a)   stbiw__sbraw(a)[1]
196
+
197
+#define stbiw__sbneedgrow(a,n)  ((a)==0 || stbiw__sbn(a)+n >= stbiw__sbm(a))
198
+#define stbiw__sbmaybegrow(a,n) (stbiw__sbneedgrow(a,(n)) ? stbiw__sbgrow(a,n) : 0)
199
+#define stbiw__sbgrow(a,n)  stbiw__sbgrowf((void **) &(a), (n), sizeof(*(a)))
200
+
201
+#define stbiw__sbpush(a, v)      (stbiw__sbmaybegrow(a,1), (a)[stbiw__sbn(a)++] = (v))
202
+#define stbiw__sbcount(a)        ((a) ? stbiw__sbn(a) : 0)
203
+#define stbiw__sbfree(a)         ((a) ? free(stbiw__sbraw(a)),0 : 0)
204
+
205
+static void *stbiw__sbgrowf(void **arr, int increment, int itemsize)
206
+{
207
+   int m = *arr ? 2*stbiw__sbm(*arr)+increment : increment+1;
208
+   void *p = realloc(*arr ? stbiw__sbraw(*arr) : 0, itemsize * m + sizeof(int)*2);
209
+   STBI_ASSERT(p);
210
+   if (p) {
211
+      if (!*arr) ((int *) p)[1] = 0;
212
+      *arr = (void *) ((int *) p + 2);
213
+      stbiw__sbm(*arr) = m;
214
+   }
215
+   return *arr;
216
+}
217
+
218
+static unsigned char *stbiw__zlib_flushf(unsigned char *data, unsigned int *bitbuffer, int *bitcount)
219
+{
220
+   while (*bitcount >= 8) {
221
+      stbiw__sbpush(data, (unsigned char) *bitbuffer);
222
+      *bitbuffer >>= 8;
223
+      *bitcount -= 8;
224
+   }
225
+   return data;
226
+}
227
+
228
+static int stbiw__zlib_bitrev(int code, int codebits)
229
+{
230
+   int res=0;
231
+   while (codebits--) {
232
+      res = (res << 1) | (code & 1);
233
+      code >>= 1;
234
+   }
235
+   return res;
236
+}
237
+
238
+static unsigned int stbiw__zlib_countm(unsigned char *a, unsigned char *b, int limit)
239
+{
240
+   int i;
241
+   for (i=0; i < limit && i < 258; ++i)
242
+      if (a[i] != b[i]) break;
243
+   return i;
244
+}
245
+
246
+static unsigned int stbiw__zhash(unsigned char *data)
247
+{
248
+   stbiw_uint32 hash = data[0] + (data[1] << 8) + (data[2] << 16);
249
+   hash ^= hash << 3;
250
+   hash += hash >> 5;
251
+   hash ^= hash << 4;
252
+   hash += hash >> 17;
253
+   hash ^= hash << 25;
254
+   hash += hash >> 6;
255
+   return hash;
256
+}
257
+
258
+#define stbiw__zlib_flush() (out = stbiw__zlib_flushf(out, &bitbuf, &bitcount))
259
+#define stbiw__zlib_add(code,codebits) \
260
+      (bitbuf |= (code) << bitcount, bitcount += (codebits), stbiw__zlib_flush())
261
+#define stbiw__zlib_huffa(b,c)  stbiw__zlib_add(stbiw__zlib_bitrev(b,c),c)
262
+// default huffman tables
263
+#define stbiw__zlib_huff1(n)  stbiw__zlib_huffa(0x30 + (n), 8)
264
+#define stbiw__zlib_huff2(n)  stbiw__zlib_huffa(0x190 + (n)-144, 9)
265
+#define stbiw__zlib_huff3(n)  stbiw__zlib_huffa(0 + (n)-256,7)
266
+#define stbiw__zlib_huff4(n)  stbiw__zlib_huffa(0xc0 + (n)-280,8)
267
+#define stbiw__zlib_huff(n)  ((n) <= 143 ? stbiw__zlib_huff1(n) : (n) <= 255 ? stbiw__zlib_huff2(n) : (n) <= 279 ? stbiw__zlib_huff3(n) : stbiw__zlib_huff4(n))
268
+#define stbiw__zlib_huffb(n) ((n) <= 143 ? stbiw__zlib_huff1(n) : stbiw__zlib_huff2(n))
269
+
270
+#define stbiw__ZHASH   16384
271
+
272
+unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality)
273
+{
274
+   static unsigned short lengthc[] = { 3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258, 259 };
275
+   static unsigned char  lengtheb[]= { 0,0,0,0,0,0,0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4,  4,  5,  5,  5,  5,  0 };
276
+   static unsigned short distc[]   = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577, 32768 };
277
+   static unsigned char  disteb[]  = { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13 };
278
+   unsigned int bitbuf=0;
279
+   int i,j, bitcount=0;
280
+   unsigned char *out = NULL;
281
+   unsigned char **hash_table[stbiw__ZHASH]; // 64KB on the stack!
282
+   if (quality < 5) quality = 5;
283
+
284
+   stbiw__sbpush(out, 0x78);   // DEFLATE 32K window
285
+   stbiw__sbpush(out, 0x5e);   // FLEVEL = 1
286
+   stbiw__zlib_add(1,1);  // BFINAL = 1
287
+   stbiw__zlib_add(1,2);  // BTYPE = 1 -- fixed huffman
288
+
289
+   for (i=0; i < stbiw__ZHASH; ++i)
290
+      hash_table[i] = NULL;
291
+
292
+   i=0;
293
+   while (i < data_len-3) {
294
+      // hash next 3 bytes of data to be compressed
295
+      int h = stbiw__zhash(data+i)&(stbiw__ZHASH-1), best=3;
296
+      unsigned char *bestloc = 0;
297
+      unsigned char **hlist = hash_table[h];
298
+      int n = stbiw__sbcount(hlist);
299
+      for (j=0; j < n; ++j) {
300
+         if (hlist[j]-data > i-32768) { // if entry lies within window
301
+            int d = stbiw__zlib_countm(hlist[j], data+i, data_len-i);
302
+            if (d >= best) best=d,bestloc=hlist[j];
303
+         }
304
+      }
305
+      // when hash table entry is too long, delete half the entries
306
+      if (hash_table[h] && stbiw__sbn(hash_table[h]) == 2*quality) {
307
+         memcpy(hash_table[h], hash_table[h]+quality, sizeof(hash_table[h][0])*quality);
308
+         stbiw__sbn(hash_table[h]) = quality;
309
+      }
310
+      stbiw__sbpush(hash_table[h],data+i);
311
+
312
+      if (bestloc) {
313
+         // "lazy matching" - check match at *next* byte, and if it's better, do cur byte as literal
314
+         h = stbiw__zhash(data+i+1)&(stbiw__ZHASH-1);
315
+         hlist = hash_table[h];
316
+         n = stbiw__sbcount(hlist);
317
+         for (j=0; j < n; ++j) {
318
+            if (hlist[j]-data > i-32767) {
319
+               int e = stbiw__zlib_countm(hlist[j], data+i+1, data_len-i-1);
320
+               if (e > best) { // if next match is better, bail on current match
321
+                  bestloc = NULL;
322
+                  break;
323
+               }
324
+            }
325
+         }
326
+      }
327
+
328
+      if (bestloc) {
329
+         int d = (int) (data+i - bestloc); // distance back
330
+         STBI_ASSERT(d <= 32767 && best <= 258);
331
+         for (j=0; best > lengthc[j+1]-1; ++j);
332
+         stbiw__zlib_huff(j+257);
333
+         if (lengtheb[j]) stbiw__zlib_add(best - lengthc[j], lengtheb[j]);
334
+         for (j=0; d > distc[j+1]-1; ++j);
335
+         stbiw__zlib_add(stbiw__zlib_bitrev(j,5),5);
336
+         if (disteb[j]) stbiw__zlib_add(d - distc[j], disteb[j]);
337
+         i += best;
338
+      } else {
339
+         stbiw__zlib_huffb(data[i]);
340
+         ++i;
341
+      }
342
+   }
343
+   // write out final bytes
344
+   for (;i < data_len; ++i)
345
+      stbiw__zlib_huffb(data[i]);
346
+   stbiw__zlib_huff(256); // end of block
347
+   // pad with 0 bits to byte boundary
348
+   while (bitcount)
349
+      stbiw__zlib_add(0,1);
350
+
351
+   for (i=0; i < stbiw__ZHASH; ++i)
352
+      (void) stbiw__sbfree(hash_table[i]);
353
+
354
+   {
355
+      // compute adler32 on input
356
+      unsigned int i=0, s1=1, s2=0, blocklen = data_len % 5552;
357
+      int j=0;
358
+      while (j < data_len) {
359
+         for (i=0; i < blocklen; ++i) s1 += data[j+i], s2 += s1;
360
+         s1 %= 65521, s2 %= 65521;
361
+         j += blocklen;
362
+         blocklen = 5552;
363
+      }
364
+      stbiw__sbpush(out, (unsigned char) (s2 >> 8));
365
+      stbiw__sbpush(out, (unsigned char) s2);
366
+      stbiw__sbpush(out, (unsigned char) (s1 >> 8));
367
+      stbiw__sbpush(out, (unsigned char) s1);
368
+   }
369
+   *out_len = stbiw__sbn(out);
370
+   // make returned pointer freeable
371
+   memmove(stbiw__sbraw(out), out, *out_len);
372
+   return (unsigned char *) stbiw__sbraw(out);
373
+}
374
+
375
+unsigned int stbiw__crc32(unsigned char *buffer, int len)
376
+{
377
+   static unsigned int crc_table[256];
378
+   unsigned int crc = ~0u;
379
+   int i,j;
380
+   if (crc_table[1] == 0)
381
+      for(i=0; i < 256; i++)
382
+         for (crc_table[i]=i, j=0; j < 8; ++j)
383
+            crc_table[i] = (crc_table[i] >> 1) ^ (crc_table[i] & 1 ? 0xedb88320 : 0);
384
+   for (i=0; i < len; ++i)
385
+      crc = (crc >> 8) ^ crc_table[buffer[i] ^ (crc & 0xff)];
386
+   return ~crc;
387
+}
388
+
389
+#define stbiw__wpng4(o,a,b,c,d) ((o)[0]=(unsigned char)(a),(o)[1]=(unsigned char)(b),(o)[2]=(unsigned char)(c),(o)[3]=(unsigned char)(d),(o)+=4)
390
+#define stbiw__wp32(data,v) stbiw__wpng4(data, (v)>>24,(v)>>16,(v)>>8,(v));
391
+#define stbiw__wptag(data,s) stbiw__wpng4(data, s[0],s[1],s[2],s[3])
392
+
393
+static void stbiw__wpcrc(unsigned char **data, int len)
394
+{
395
+   unsigned int crc = stbiw__crc32(*data - len - 4, len+4);
396
+   stbiw__wp32(*data, crc);
397
+}
398
+
399
+static unsigned char stbiw__paeth(int a, int b, int c)
400
+{
401
+   int p = a + b - c, pa = abs(p-a), pb = abs(p-b), pc = abs(p-c);
402
+   if (pa <= pb && pa <= pc) return (unsigned char) a;
403
+   if (pb <= pc) return (unsigned char) b;
404
+   return (unsigned char) c;
405
+}
406
+
407
+unsigned char *stbi_write_png_to_mem(unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len)
408
+{
409
+   int ctype[5] = { -1, 0, 4, 2, 6 };
410
+   unsigned char sig[8] = { 137,80,78,71,13,10,26,10 };
411
+   unsigned char *out,*o, *filt, *zlib;
412
+   signed char *line_buffer;
413
+   int i,j,k,p,zlen;
414
+
415
+   if (stride_bytes == 0)
416
+      stride_bytes = x * n;
417
+
418
+   filt = (unsigned char *) malloc((x*n+1) * y); if (!filt) return 0;
419
+   line_buffer = (signed char *) malloc(x * n); if (!line_buffer) { free(filt); return 0; }
420
+   for (j=0; j < y; ++j) {
421
+      static int mapping[] = { 0,1,2,3,4 };
422
+      static int firstmap[] = { 0,1,0,5,6 };
423
+      int *mymap = j ? mapping : firstmap;
424
+      int best = 0, bestval = 0x7fffffff;
425
+      for (p=0; p < 2; ++p) {
426
+         for (k= p?best:0; k < 5; ++k) {
427
+            int type = mymap[k],est=0;
428
+            unsigned char *z = pixels + stride_bytes*j;
429
+            for (i=0; i < n; ++i)
430
+               switch (type) {
431
+                  case 0: line_buffer[i] = z[i]; break;
432
+                  case 1: line_buffer[i] = z[i]; break;
433
+                  case 2: line_buffer[i] = z[i] - z[i-stride_bytes]; break;
434
+                  case 3: line_buffer[i] = z[i] - (z[i-stride_bytes]>>1); break;
435
+                  case 4: line_buffer[i] = (signed char) (z[i] - stbiw__paeth(0,z[i-stride_bytes],0)); break;
436
+                  case 5: line_buffer[i] = z[i]; break;
437
+                  case 6: line_buffer[i] = z[i]; break;
438
+               }
439
+            for (i=n; i < x*n; ++i) {
440
+               switch (type) {
441
+                  case 0: line_buffer[i] = z[i]; break;
442
+                  case 1: line_buffer[i] = z[i] - z[i-n]; break;
443
+                  case 2: line_buffer[i] = z[i] - z[i-stride_bytes]; break;
444
+                  case 3: line_buffer[i] = z[i] - ((z[i-n] + z[i-stride_bytes])>>1); break;
445
+                  case 4: line_buffer[i] = z[i] - stbiw__paeth(z[i-n], z[i-stride_bytes], z[i-stride_bytes-n]); break;
446
+                  case 5: line_buffer[i] = z[i] - (z[i-n]>>1); break;
447
+                  case 6: line_buffer[i] = z[i] - stbiw__paeth(z[i-n], 0,0); break;
448
+               }
449
+            }
450
+            if (p) break;
451
+            for (i=0; i < x*n; ++i)
452
+               est += abs((signed char) line_buffer[i]);
453
+            if (est < bestval) { bestval = est; best = k; }
454
+         }
455
+      }
456
+      // when we get here, best contains the filter type, and line_buffer contains the data
457
+      filt[j*(x*n+1)] = (unsigned char) best;
458
+      memcpy(filt+j*(x*n+1)+1, line_buffer, x*n);
459
+   }
460
+   free(line_buffer);
461
+   zlib = stbi_zlib_compress(filt, y*( x*n+1), &zlen, 8); // increase 8 to get smaller but use more memory
462
+   free(filt);
463
+   if (!zlib) return 0;
464
+
465
+   // each tag requires 12 bytes of overhead
466
+   out = (unsigned char *) malloc(8 + 12+13 + 12+zlen + 12);
467
+   if (!out) return 0;
468
+   *out_len = 8 + 12+13 + 12+zlen + 12;
469
+
470
+   o=out;
471
+   memcpy(o,sig,8); o+= 8;
472
+   stbiw__wp32(o, 13); // header length
473
+   stbiw__wptag(o, "IHDR");
474
+   stbiw__wp32(o, x);
475
+   stbiw__wp32(o, y);
476
+   *o++ = 8;
477
+   *o++ = (unsigned char) ctype[n];
478
+   *o++ = 0;
479
+   *o++ = 0;
480
+   *o++ = 0;
481
+   stbiw__wpcrc(&o,13);
482
+
483
+   stbiw__wp32(o, zlen);
484
+   stbiw__wptag(o, "IDAT");
485
+   memcpy(o, zlib, zlen); o += zlen; free(zlib);
486
+   stbiw__wpcrc(&o, zlen);
487
+
488
+   stbiw__wp32(o,0);
489
+   stbiw__wptag(o, "IEND");
490
+   stbiw__wpcrc(&o,0);
491
+
492
+   STBI_ASSERT(o == out + *out_len);
493
+
494
+   return out;
495
+}
496
+
497
+int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, int stride_bytes)
498
+{
499
+   FILE *f;
500
+   int len;
501
+   unsigned char *png = stbi_write_png_to_mem((unsigned char *) data, stride_bytes, x, y, comp, &len);
502
+   if (!png) return 0;
503
+   f = fopen(filename, "wb");
504
+   if (!f) { free(png); return 0; }
505
+   fwrite(png, 1, len, f);
506
+   fclose(f);
507
+   free(png);
508
+   return 1;
509
+}
510
+#endif // STB_IMAGE_WRITE_IMPLEMENTATION
511
+
512
+/* Revision history
513
+
514
+      0.95 (2014-08-17)
515
+		       add monochrome TGA output
516
+      0.94 (2014-05-31)
517
+             rename private functions to avoid conflicts with stb_image.h
518
+      0.93 (2014-05-27)
519
+             warning fixes
520
+      0.92 (2010-08-01)
521
+             casts to unsigned char to fix warnings
522
+      0.91 (2010-07-17)
523
+             first public release
524
+      0.90   first internal release
525
+*/

+ 2
- 0
src/system/FontImGui.cpp View File

@@ -5,6 +5,8 @@
5 5
  * \author xythobuz
6 6
  */
7 7
 
8
+#include "imgui/imgui.h"
9
+
8 10
 #include "global.h"
9 11
 #include "UI.h"
10 12
 #include "system/FontImGui.h"

+ 0
- 5
src/utils/CMakeLists.txt View File

@@ -7,13 +7,8 @@ set (UTIL_SRCS ${UTIL_SRCS} "FolderRecursive.cpp")
7 7
 set (UTIL_SRCS ${UTIL_SRCS} "pcx.cpp" "../../include/utils/pcx.h")
8 8
 set (UTIL_SRCS ${UTIL_SRCS} "pixel.cpp" "../../include/utils/pixel.h")
9 9
 set (UTIL_SRCS ${UTIL_SRCS} "strings.cpp" "../../include/utils/strings.h")
10
-set (UTIL_SRCS ${UTIL_SRCS} "tga.cpp" "../../include/utils/tga.h")
11 10
 set (UTIL_SRCS ${UTIL_SRCS} "time.cpp" "../../include/utils/time.h")
12 11
 
13
-if (USING_PNG)
14
-    set (UTIL_SRCS ${UTIL_SRCS} "png.cpp" "../../include/utils/png.h")
15
-endif (USING_PNG)
16
-
17 12
 # Add library
18 13
 add_library (OpenRaider_utils OBJECT ${UTIL_SRCS})
19 14
 

+ 0
- 261
src/utils/png.cpp View File

@@ -1,261 +0,0 @@
1
-/*!
2
- * \file src/utils/png.cpp
3
- * \brief PNG image reader
4
- *
5
- * https://github.com/DavidEGrayson/ahrs-visualizer/blob/master/png_texture.cpp
6
- * http://zarb.org/~gc/html/libpng.html
7
- *
8
- * \author xythobuz
9
- */
10
-
11
-#include <png.h>
12
-#include <cstdio>
13
-#include <iostream>
14
-
15
-#include "global.h"
16
-#include "utils/pixel.h"
17
-#include "utils/png.h"
18
-
19
-int pngCheck(const char* filename) {
20
-    png_byte header[8];
21
-
22
-    assert(filename != nullptr);
23
-    assert(filename[0] != '\0');
24
-
25
-    FILE* fp = fopen(filename, "rb");
26
-    if (fp == nullptr) {
27
-        std::cout << "Could not open " << filename << std::endl;
28
-        return -1;
29
-    }
30
-
31
-    fread(header, 1, 8, fp);
32
-    fclose(fp);
33
-
34
-    if (png_sig_cmp(header, 0, 8)) {
35
-        std::cout << "File " << filename << " is not a PNG." << std::endl;
36
-        return -2;
37
-    }
38
-
39
-    return 0;
40
-}
41
-
42
-int pngLoad(const char* filename, unsigned char** image,
43
-            unsigned int* width, unsigned int* height,
44
-            TextureManager::ColorMode* mode, unsigned int* bpp) {
45
-    png_byte header[8];
46
-
47
-    assert(filename != nullptr);
48
-    assert(filename[0] != '\0');
49
-    assert(image != nullptr);
50
-    assert(width != nullptr);
51
-    assert(height != nullptr);
52
-    assert(mode != nullptr);
53
-    assert(bpp != nullptr);
54
-
55
-    FILE* fp = fopen(filename, "rb");
56
-    if (fp == nullptr) {
57
-        std::cout << "Could not open " << filename << std::endl;
58
-        return -1;
59
-    }
60
-
61
-    fread(header, 1, 8, fp);
62
-
63
-    if (png_sig_cmp(header, 0, 8)) {
64
-        std::cout << "File " << filename << " is not a PNG." << std::endl;
65
-        fclose(fp);
66
-        return -2;
67
-    }
68
-
69
-    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
70
-    if (!png_ptr) {
71
-        std::cout << "png_create_read_struct returned 0." << std::endl;
72
-        fclose(fp);
73
-        return -3;
74
-    }
75
-
76
-    png_infop info_ptr = png_create_info_struct(png_ptr);
77
-    if (!info_ptr) {
78
-        std::cout << "png_create_info_struct returned 0." << std::endl;
79
-        png_destroy_read_struct(&png_ptr, (png_infopp)nullptr, (png_infopp)nullptr);
80
-        fclose(fp);
81
-        return -4;
82
-    }
83
-
84
-    png_infop end_info = png_create_info_struct(png_ptr);
85
-    if (!end_info) {
86
-        std::cout << "png_create_info_struct returned 0." << std::endl;
87
-        png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) nullptr);
88
-        fclose(fp);
89
-        return -5;
90
-    }
91
-
92
-    if (setjmp(png_jmpbuf(png_ptr))) {
93
-        std::cout << "Error from libpng" << std::endl;
94
-        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
95
-        fclose(fp);
96
-        return -6;
97
-    }
98
-
99
-    png_init_io(png_ptr, fp);
100
-    png_set_sig_bytes(png_ptr, 8);
101
-    png_read_info(png_ptr, info_ptr);
102
-
103
-    int bit_depth, color_type;
104
-    png_uint_32 tmpWidth, tmpHeight;
105
-
106
-    png_get_IHDR(png_ptr, info_ptr, &tmpWidth, &tmpHeight, &bit_depth, &color_type,
107
-                 nullptr, nullptr, nullptr);
108
-
109
-    *width = tmpWidth;
110
-    *height = tmpHeight;
111
-
112
-    if (bit_depth != 8) {
113
-        std::cout << filename << ": Unsupported bit depth " << bit_depth << ".  Must be 8." << std::endl;
114
-        return -7;
115
-    }
116
-
117
-    png_read_update_info(png_ptr, info_ptr);
118
-
119
-    png_size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);
120
-
121
-    png_byte* image_data = new png_byte[(rowbytes** height) + 15];
122
-    png_bytep* row_pointers = new png_bytep[*height];
123
-
124
-    for (unsigned int i = 0; i < *height; i++)
125
-        row_pointers[*height - 1 - i] = image_data + i * rowbytes;
126
-
127
-    png_read_image(png_ptr, row_pointers);
128
-
129
-    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
130
-    delete [] row_pointers;
131
-    fclose(fp);
132
-
133
-    if (color_type == PNG_COLOR_TYPE_RGB) {
134
-        *mode = TextureManager::ColorMode::RGB;
135
-        *bpp = 24;
136
-    } else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
137
-        *mode = TextureManager::ColorMode::RGBA;
138
-        *bpp = 32;
139
-    } else {
140
-        std::cout << filename << ": Unknown libpng color type " << color_type << std::endl;
141
-        delete [] image_data;
142
-        return -8;
143
-    }
144
-
145
-    // Flip
146
-    *image = new unsigned char[*width** height * (*bpp / 8)];
147
-    for (unsigned int y = 0; y < (*height); y++) {
148
-        for (unsigned int x = 0; x < (*width); x++) {
149
-            (*image)[((y** width) + x) * (*bpp / 8)]
150
-                = image_data[(((*height - y - 1)** width) + x) * (*bpp / 8)];
151
-            (*image)[(((y** width) + x) * (*bpp / 8)) + 1]
152
-                = image_data[((((*height - y - 1)** width) + x) * (*bpp / 8)) + 1];
153
-            (*image)[(((y** width) + x) * (*bpp / 8)) + 2]
154
-                = image_data[((((*height - y - 1)** width) + x) * (*bpp / 8)) + 2];
155
-            (*image)[(((y** width) + x) * (*bpp / 8)) + 3]
156
-                = image_data[((((*height - y - 1)** width) + x) * (*bpp / 8)) + 3];
157
-        }
158
-    }
159
-
160
-    delete [] image_data;
161
-    return 0;
162
-}
163
-
164
-int pngSave(const char* filename, unsigned char* image,
165
-            unsigned int width, unsigned int height,
166
-            TextureManager::ColorMode mode, unsigned int bpp) {
167
-    assert(filename != nullptr);
168
-    assert(filename[0] != '\0');
169
-    assert(image != nullptr);
170
-    assert(width > 0);
171
-    assert(height > 0);
172
-
173
-    FILE* fp = fopen(filename, "wb");
174
-    if (!fp) {
175
-        std::cout << "File " << filename << " could not be opened for writing" << std::endl;
176
-        return -1;
177
-    }
178
-
179
-    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
180
-
181
-    if (!png_ptr) {
182
-        std::cout << "png_create_write_struct failed" << std::endl;
183
-        fclose(fp);
184
-        return -2;
185
-    }
186
-
187
-    png_infop info_ptr = png_create_info_struct(png_ptr);
188
-    if (!info_ptr) {
189
-        std::cout << "png_create_info_struct failed" << std::endl;
190
-        fclose(fp);
191
-        return -3;
192
-    }
193
-
194
-    if (setjmp(png_jmpbuf(png_ptr))) {
195
-        std::cout << "Error during init_io" << std::endl;
196
-        fclose(fp);
197
-        return -4;
198
-    }
199
-
200
-    png_init_io(png_ptr, fp);
201
-
202
-    if (setjmp(png_jmpbuf(png_ptr))) {
203
-        std::cout << "Error during writing header" << std::endl;
204
-        fclose(fp);
205
-        return -5;
206
-    }
207
-
208
-    int color_type;
209
-    if (((mode == TextureManager::ColorMode::RGB)
210
-         || (mode == TextureManager::ColorMode::BGR)) && (bpp == 24)) {
211
-        if (mode == TextureManager::ColorMode::BGR) {
212
-            bgr2rgb24(image, width, height);
213
-        }
214
-        color_type = PNG_COLOR_TYPE_RGB;
215
-    } else if (((mode == TextureManager::ColorMode::RGBA)
216
-                || (mode == TextureManager::ColorMode::BGRA)) && (bpp == 32)) {
217
-        if (mode == TextureManager::ColorMode::BGRA) {
218
-            bgra2rgba32(image, width, height);
219
-        }
220
-        color_type = PNG_COLOR_TYPE_RGB_ALPHA;
221
-    } else {
222
-        std::cout << "Error invalid color mode" << std::endl;
223
-        fclose(fp);
224
-        return -6;
225
-    }
226
-
227
-    png_set_IHDR(png_ptr, info_ptr, width, height,
228
-                 8, color_type, PNG_INTERLACE_NONE,
229
-                 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
230
-
231
-    png_write_info(png_ptr, info_ptr);
232
-
233
-    png_bytep* row_pointers = new png_bytep[height];
234
-
235
-    for (unsigned int i = 0; i < height; i++)
236
-        row_pointers[height - 1 - i] = image + (i * width * 4);
237
-
238
-    if (setjmp(png_jmpbuf(png_ptr))) {
239
-        std::cout << "Error during writing bytes" << std::endl;
240
-        delete [] row_pointers;
241
-        fclose(fp);
242
-        return -7;
243
-    }
244
-
245
-    png_write_image(png_ptr, row_pointers);
246
-
247
-    if (setjmp(png_jmpbuf(png_ptr))) {
248
-        std::cout << "Error during end of write" << std::endl;
249
-        delete [] row_pointers;
250
-        fclose(fp);
251
-        return -8;
252
-    }
253
-
254
-    png_write_end(png_ptr, nullptr);
255
-
256
-    delete [] row_pointers;
257
-    fclose(fp);
258
-
259
-    return 0;
260
-}
261
-

+ 0
- 415
src/utils/tga.cpp View File

@@ -1,415 +0,0 @@
1
-/*!
2
- * \file src/utils/tga.cpp
3
- * \brief TGA image reader/writer
4
- *
5
- * \todo type should pass more info (2 bits for RGBA|RGB|GREY; val for depth)
6
- *
7
- * \author Mongoose
8
- * \author xythobuz
9
- */
10
-
11
-#include <stdio.h>
12
-#include <string.h>
13
-#include <stdarg.h>
14
-
15
-#include "global.h"
16
-#include "utils/tga.h"
17
-
18
-typedef enum {
19
-    //TGA_TYPE__NO_DATA    = 0,
20
-    //TGA_TYPE__MAPPED     = 1,
21
-    TGA_TYPE__COLOR      = 2,
22
-    TGA_TYPE__GREYSCALE  = 3,
23
-    //TGA_TYPE__MAPPED_RLE = 9,
24
-    TGA_TYPE__COLOR_RLE  = 10
25
-} tga_type_t;
26
-
27
-typedef struct {
28
-    unsigned char comment_lenght;   //!< Number of bytes in comment
29
-    unsigned char colormap_type;    //!< 0 No colormap; 1 Has colormap
30
-    unsigned char image_type;       //!< 1 Colormapped; 2 Unmapped; 9 Colormapped RLE; 10 Unmapped RLE
31
-    unsigned short colormap_index;  //!< Index of first color map entry
32
-    unsigned short colormap_lenght; //!< Number of color map entries
33
-    unsigned char colormap_bbp;     //!< 16, 24, or 32 bits per pixel format
34
-    unsigned short origin_x;        //!< X coor of lower-left corner
35
-    unsigned short origin_y;        //!< Y coor of lower-left corner
36
-    unsigned short width;           //!< Width in pixels
37
-    unsigned short height;          //!< Height in pixels
38
-    unsigned char bpp;              //!< Number of bits in a pixel index
39
-    unsigned char desc_flags;       //!< Various magic bits
40
-} tga_t;
41
-
42
-int tgaCheck(const char* filename) {
43
-    char buffer[10];
44
-
45
-    assert(filename != nullptr);
46
-    assert(filename[0] != '\0');
47
-
48
-    FILE* f = fopen(filename, "rb");
49
-    if (!f) {
50
-        printf("tgaCheck> File not found\n");
51
-        return -1;
52
-    }
53
-
54
-    /* Read the header */
55
-    fseek(f, 0, SEEK_SET);
56
-    fread(buffer, 8, 1, f);
57
-
58
-    // buffer[1] = 0 - Means not color mapped (1 would mean mapped)
59
-    if (!(buffer[1] == 0 && (buffer[2] == TGA_TYPE__COLOR ||
60
-                             //buffer[2] == TGA_TYPE__GREYSCALE ||
61
-                             buffer[2] == TGA_TYPE__COLOR_RLE))) {
62
-        printf("tgaCheck> Inavlid or unknown TGA format.\n");
63
-        fclose(f);
64
-        return -2;
65
-    }
66
-
67
-    fclose(f);
68
-    return 0;
69
-}
70
-
71
-int tgaLoad(const char* filename, unsigned char** image, unsigned int* width, unsigned int* height,
72
-            char* type) {
73
-    tga_t header;
74
-    char comment[256];
75
-    unsigned char pixel[4];
76
-    unsigned char* swap_row = nullptr;
77
-    unsigned char tmp, packet;
78
-    bool must_flip = 0;
79
-    unsigned int size;
80
-    unsigned int i, j;
81
-
82
-    assert(filename != nullptr);
83
-    assert(filename[0] != '\0');
84
-    assert(image != nullptr);
85
-    assert(width != nullptr);
86
-    assert(height != nullptr);
87
-    assert(type != nullptr);
88
-
89
-    FILE* f = fopen(filename, "rb");
90
-    if (!f) {
91
-        printf("tgaLoad> File not found\n");
92
-        return -1;
93
-    }
94
-
95
-    fseek(f, 0, SEEK_SET);
96
-
97
-    // Read TGA header
98
-    fread(&header.comment_lenght, 1, 1, f);
99
-    fread(&header.colormap_type, 1, 1, f);
100
-    fread(&header.image_type, 1, 1, f);
101
-    fread(&header.colormap_index, 2, 1, f);
102
-    fread(&header.colormap_lenght, 2, 1, f);
103
-    fread(&header.colormap_bbp, 1, 1, f);
104
-
105
-    fread(&tmp, 1, 1, f);
106
-    header.origin_x = tmp;
107
-    fread(&tmp, 1, 1, f);
108
-    header.origin_x += tmp * 256;
109
-
110
-    fread(&tmp, 1, 1, f);
111
-    header.origin_y = tmp;
112
-    fread(&tmp, 1, 1, f);
113
-    header.origin_y += tmp * 256;
114
-
115
-    fread(&tmp, 1, 1, f);
116
-    header.width = tmp;
117
-    fread(&tmp, 1, 1, f);
118
-    header.width += tmp * 256;
119
-
120
-    fread(&tmp, 1, 1, f);
121
-    header.height = tmp;
122
-    fread(&tmp, 1, 1, f);
123
-    header.height += tmp * 256;
124
-
125
-    fread(&header.bpp, 1, 1, f);
126
-    fread(&header.desc_flags, 1, 1, f);
127
-
128
-    *width = header.width;
129
-    *height = header.height;
130
-
131
-    switch (header.bpp) {
132
-        case 32:
133
-            *type = 2; //32;
134
-            break;
135
-        case 24:
136
-            *type = 1; //24;
137
-            break;
138
-        case 8:
139
-        default:
140
-            *type = 0; //8;
141
-            break;
142
-    }
143
-
144
-#ifdef DEBUG_TGA
145
-    printf("TGA [%ix%i@%ibpp, %it, %ix, %iy, %uf]\n",
146
-           header.width, header.height, header.bpp, header.image_type,
147
-           header.origin_x, header.origin_y,
148
-           header.desc_flags);
149
-#endif
150
-
151
-    // Comments can be 0 - 255
152
-    if (header.comment_lenght) {
153
-        fread(&comment, 1, header.comment_lenght, f);
154
-        for (i = 0; i < 255; ++i) {
155
-            if (!(comment[i] > 32 && comment[i] < 127))
156
-                comment[i] = '.'; // print a dot for invalid text
157
-        }
158
-        comment[255] = 0;
159
-        printf("Comment: '%s'\n", comment);
160
-    }
161
-
162
-    *image = nullptr;
163
-    size = header.width * header.height;
164
-
165
-    if (!size || (!(header.colormap_type == 0 && (header.image_type == 2
166
-                    || header.image_type == 10)))) {
167
-        fprintf(stderr, "tgaLoad> Unknown image format.\n");
168
-        fclose(f);
169
-        return -2;
170
-    }
171
-
172
-    // Mongoose: Added 'screen origin bit' support back here
173
-    if (!(header.desc_flags & 32)) {
174
-        must_flip = true;
175
-    }
176
-
177
-    switch (header.bpp) {
178
-        case 32:
179
-            size *= 4;
180
-            *image = new unsigned char [size];
181
-            switch (header.image_type) {
182
-                case TGA_TYPE__COLOR_RLE:
183
-                    for (i = 0; i < size;) {
184
-                        fread(&packet, 1, 1, f);
185
-                        if (packet & 0x80) { // Run Length
186
-                            packet = (packet & 0x7F) + 1;
187
-                            fread(&pixel, 4, 1, f);
188
-                            for (j = 0; j < packet; j++) {
189
-                                (*image)[i++] = pixel[2];
190
-                                (*image)[i++] = pixel[1];
191
-                                (*image)[i++] = pixel[0];
192
-                                (*image)[i++] = pixel[3];
193
-                            }
194
-                        } else { // RAW
195
-                            packet = (packet & 0x7F) + 1;
196
-                            for (j = 0; j < packet; j++) {
197
-                                fread(&pixel, 4, 1, f);
198
-                                (*image)[i++] = pixel[2];
199
-                                (*image)[i++] = pixel[1];
200
-                                (*image)[i++] = pixel[0];
201
-                                (*image)[i++] = pixel[3];
202
-                            }
203
-                        }
204
-                    }
205
-                    break;
206
-                case TGA_TYPE__COLOR:
207
-                    if (fread((*image), size, 1, f) < 1) {
208
-                        fprintf(stderr, "tgaLoad> Image fread failed.\n");
209
-                        delete [] *image;
210
-                        fclose(f);
211
-                        return -4;
212
-                    }
213
-                    for (i = 0; i < size; i += 4) {
214
-                        tmp = (*image)[i];
215
-                        (*image)[i] = (*image)[i + 2];
216
-                        (*image)[i + 2] = tmp;
217
-                    }
218
-                    break;
219
-                default:
220
-                    ;
221
-            }
222
-            if (must_flip) {
223
-                swap_row = new unsigned char [header.width * 4];
224
-                for (i = 0, j = header.height - 1; (int)i < header.height / 2; i++, j--) {
225
-                    memcpy(swap_row, &(*image)[i * header.width * 4], header.width * 4);
226
-                    memcpy(&(*image)[i * header.width * 4], &(*image)[j * header.width * 4],
227
-                           header.width * 4);
228
-                    memcpy(&(*image)[j * header.width * 4], swap_row, header.width * 4);
229
-                }
230
-                delete [] swap_row;
231
-            }
232
-            break;
233
-        case 24:
234
-            size *= 3;
235
-            *image = new unsigned char [size];
236
-            switch (header.image_type) {
237
-                case TGA_TYPE__COLOR_RLE:
238
-                    for (i = 0; i < size;) {
239
-                        fread(&packet, 1, 1, f);
240
-                        if (packet & 0x80) { // Run Length
241
-                            packet = (packet & 0x7F) + 1;
242
-                            fread(&pixel, 3, 1, f);
243
-                            for (j = 0; j < packet; j++) {
244
-                                (*image)[i++] = pixel[2];
245
-                                (*image)[i++] = pixel[1];
246
-                                (*image)[i++] = pixel[0];
247
-                            }
248
-                        } else { // RAW
249
-                            packet = (packet & 0x7F) + 1;
250
-                            for (j = 0; j < packet; j++) {
251
-                                fread(&pixel, 3, 1, f);
252
-                                (*image)[i++] = pixel[2];
253
-                                (*image)[i++] = pixel[1];
254
-                                (*image)[i++] = pixel[0];
255
-                            }
256
-                        }
257
-                    }
258
-                    break;
259
-                case TGA_TYPE__COLOR:
260
-                    if (fread((*image), size, 1, f) < 1) {
261
-                        fprintf(stderr, "tgaLoad> Image fread failed.\n");
262
-                        delete [] *image;
263
-                        fclose(f);
264
-                        return -4;
265
-                    }
266
-                    for (i = 0; i < size; i += 3) {
267
-                        tmp = (*image)[i];
268
-                        (*image)[i] = (*image)[i + 2];
269
-                        (*image)[i + 2] = tmp;
270
-                    }
271
-                    break;
272
-                default:
273
-                    ;
274
-            }
275
-            if (must_flip) {
276
-                swap_row = new unsigned char [header.width * 3];
277
-                for (i = 0, j = header.height - 1; (int)i < header.height / 2; i++, j--) {
278
-                    memcpy(swap_row, &(*image)[i * header.width * 3], header.width * 3);
279
-                    memcpy(&(*image)[i * header.width * 3], &(*image)[j * header.width * 3],
280
-                           header.width * 3);
281
-                    memcpy(&(*image)[j * header.width * 3], swap_row, header.width * 3);
282
-                }
283
-                delete [] swap_row;
284
-            }
285
-            break;
286
-        case 8:
287
-            printf("tgaLoad> 8bpp Not implemented\n");
288
-            break;
289
-        default:
290
-            ;
291
-    }
292
-
293
-#ifdef DEBUG_TGA
294
-    char c;
295
-    printf("TGA Comment: ");
296
-    while (fread(&c, 1, 1, f) == 1) {
297
-        printf("%c", c);
298
-    }
299
-    printf("\n");
300
-#endif
301
-
302
-    fclose(f);
303
-    return 0;
304
-}
305
-
306
-int tgaSave(const char* filename, unsigned char* image, unsigned int width, unsigned int height,
307
-            char type) {
308
-    tga_t header;
309
-    unsigned int size;
310
-    char comment[64];
311
-    //unsigned int i;
312
-    //unsigned char tmp;
313
-
314
-    assert(filename != nullptr);
315
-    assert(filename[0] != '\0');
316
-    assert(image != nullptr);
317
-    assert(width > 0);
318
-    assert(height > 0);
319
-
320
-    FILE* f = fopen(filename, "wb");
321
-    if (!f) {
322
-        printf("tgaSave> File not found\n");
323
-        return -1;
324
-    }
325
-
326
-    strncpy(comment, "OpenRaider TGA", 63);
327
-    comment[63] = 0;
328
-
329
-    header.comment_lenght = (unsigned char)strlen(comment);
330
-    header.colormap_type = 0;
331
-
332
-    // No colormaps
333
-    header.colormap_index = 0;
334
-    header.colormap_lenght = 0;
335
-    header.colormap_bbp = 0;
336
-
337
-    header.origin_x = header.origin_y = 0;
338
-    header.width = (unsigned short)width;
339
-    header.height = (unsigned short)height;
340
-
341
-    header.desc_flags = 0;
342
-
343
-    switch (type) {
344
-        case 4:
345
-            header.image_type = TGA_TYPE__COLOR;
346
-            header.desc_flags = 32;
347
-            header.bpp = 32;
348
-            break;
349
-        case 2:
350
-            header.bpp = 32;
351
-            break;
352
-        case 1:
353
-            header.image_type = TGA_TYPE__GREYSCALE;
354
-            header.bpp = 8;
355
-            break;
356
-        default:
357
-            header.image_type = TGA_TYPE__COLOR;
358
-            header.bpp = 24;
359
-    }
360
-
361
-    // Write TGA header
362
-    fwrite(&header.comment_lenght, 1, 1, f);
363
-    fwrite(&header.colormap_type, 1, 1, f);
364
-    fwrite(&header.image_type, 1, 1, f);
365
-    fwrite(&header.colormap_index, 2, 1, f);
366
-    fwrite(&header.colormap_lenght, 2, 1, f);
367
-    fwrite(&header.colormap_bbp, 1, 1, f);
368
-    fwrite(&header.origin_x, 2, 1, f);
369
-    fwrite(&header.origin_y, 2, 1, f);
370
-    fwrite(&header.width, 2, 1, f);
371
-    fwrite(&header.height, 2, 1, f);
372
-    fwrite(&header.bpp, 1, 1, f);
373
-    fwrite(&header.desc_flags, 1, 1, f);
374
-
375
-    // Write comment
376
-    fwrite(&comment, 1, header.comment_lenght, f);
377
-
378
-    switch (header.bpp) {
379
-        case 32:
380
-            size = header.width * header.height * 4;
381
-
382
-            //for (i = 0; i < size; i += 4)
383
-            //{
384
-            //  tmp = image[i];
385
-            //  image[i] = image[i + 2];
386
-            //  image[i + 2] = tmp;
387
-            //}
388
-            break;
389
-        case 24:
390
-            size = header.width * header.height * 3;
391
-
392
-            //for (i = 0; i < size; i += 3)
393
-            //{
394
-            //  tmp = image[i];
395
-            //  image[i] = image[i + 2];
396
-            //  image[i + 2] = tmp;
397
-            //}
398
-            break;
399
-        case 8:
400
-        default:
401
-            size = header.width * header.height;
402
-            break;
403
-    }
404
-
405
-    // Write image data
406
-    if (fwrite(image, size, 1, f) < 1) {
407
-        perror("tgaSave> Disk write failed.\n");
408
-        fclose(f);
409
-        return -2;
410
-    }
411
-
412
-    fclose(f);
413
-    return 0;
414
-}
415
-

Loading…
Cancel
Save