瀏覽代碼

Default Font using ImGUI

Thomas Buck 10 年之前
父節點
當前提交
c9f13c0d29
共有 9 個文件被更改,包括 101 次插入23 次删除
  1. 3
    0
      ChangeLog.md
  2. 1
    1
      include/Font.h
  3. 28
    0
      include/FontImGui.h
  4. 1
    1
      include/UI.h
  5. 1
    0
      src/CMakeLists.txt
  6. 15
    6
      src/Font.cpp
  7. 45
    0
      src/FontImGui.cpp
  8. 0
    2
      src/UI.cpp
  9. 7
    13
      src/main.cpp

+ 3
- 0
ChangeLog.md 查看文件

2
 
2
 
3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4
 
4
 
5
+    [ 20141016 ]
6
+    * ImGuis Font can now be used if no other is available
7
+
5
     [ 20141015 ]
8
     [ 20141015 ]
6
     * Added rudimentary command history support for Console
9
     * Added rudimentary command history support for Console
7
 
10
 

+ 1
- 1
include/Font.h 查看文件

17
 public:
17
 public:
18
     static void shutdown();
18
     static void shutdown();
19
 
19
 
20
-    static int initialize(std::string font);
20
+    static int initialize(std::string font = "");
21
 
21
 
22
     static unsigned int widthText(float scale, std::string s);
22
     static unsigned int widthText(float scale, std::string s);
23
 
23
 

+ 28
- 0
include/FontImGui.h 查看文件

1
+/*!
2
+ * \file include/FontImGui.h
3
+ * \brief Default Font implementation
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _FONT_IMGUI_H_
9
+#define _FONT_IMGUI_H_
10
+
11
+/*!
12
+ * \brief Default Font implementation
13
+ */
14
+class FontImGui {
15
+public:
16
+    static unsigned int widthText(float scale, std::string s);
17
+
18
+    static unsigned int heightText(float scale, unsigned int maxWidth, std::string s);
19
+
20
+    static void drawText(unsigned int x, unsigned int y, float scale,
21
+            const unsigned char color[4], std::string s);
22
+
23
+    static void drawTextWrapped(unsigned int x, unsigned int y, float scale,
24
+            const unsigned char color[4], unsigned int maxWidth, std::string s);
25
+};
26
+
27
+#endif
28
+

+ 1
- 1
include/UI.h 查看文件

31
     static void handleMouseMotion(int xrel, int yrel, int xabs, int yabs);
31
     static void handleMouseMotion(int xrel, int yrel, int xabs, int yabs);
32
     static void handleMouseScroll(int xrel, int yrel);
32
     static void handleMouseScroll(int xrel, int yrel);
33
 
33
 
34
-private:
35
     static void renderImGui(ImDrawList** const draw_lists, int count);
34
     static void renderImGui(ImDrawList** const draw_lists, int count);
36
 
35
 
36
+private:
37
     static bool visible;
37
     static bool visible;
38
     static unsigned int fontTex;
38
     static unsigned int fontTex;
39
     static std::string iniFilename;
39
     static std::string iniFilename;

+ 1
- 0
src/CMakeLists.txt 查看文件

63
 set (SRCS ${SRCS} "Entity.cpp")
63
 set (SRCS ${SRCS} "Entity.cpp")
64
 set (SRCS ${SRCS} "Exception.cpp")
64
 set (SRCS ${SRCS} "Exception.cpp")
65
 set (SRCS ${SRCS} "Font.cpp")
65
 set (SRCS ${SRCS} "Font.cpp")
66
+set (SRCS ${SRCS} "FontImGui.cpp")
66
 set (SRCS ${SRCS} "FontTRLE.cpp")
67
 set (SRCS ${SRCS} "FontTRLE.cpp")
67
 set (SRCS ${SRCS} "Game.cpp")
68
 set (SRCS ${SRCS} "Game.cpp")
68
 set (SRCS ${SRCS} "Log.cpp")
69
 set (SRCS ${SRCS} "Log.cpp")

+ 15
- 6
src/Font.cpp 查看文件

10
 #include "utils/strings.h"
10
 #include "utils/strings.h"
11
 #include "Window.h"
11
 #include "Window.h"
12
 #include "Font.h"
12
 #include "Font.h"
13
+#include "FontImGui.h"
13
 #include "FontTRLE.h"
14
 #include "FontTRLE.h"
14
 
15
 
15
 #ifdef USING_SDL_FONT
16
 #ifdef USING_SDL_FONT
36
 #endif
37
 #endif
37
     }
38
     }
38
 
39
 
39
-    getLog() << "Unknown font file format: " << font << Log::endl;
40
-    return -1;
40
+    if (font != "") {
41
+        getLog() << "Unknown font file format: " << font << Log::endl;
42
+        return -1;
43
+    } else {
44
+        return 0;
45
+    }
41
 }
46
 }
42
 
47
 
43
 unsigned int Font::widthText(float scale, std::string s) {
48
 unsigned int Font::widthText(float scale, std::string s) {
47
     } else if (stringEndsWith(fontName, ".ttf")) {
52
     } else if (stringEndsWith(fontName, ".ttf")) {
48
         return FontSDL::widthText(scale, s);
53
         return FontSDL::widthText(scale, s);
49
 #endif
54
 #endif
55
+    } else {
56
+        return FontImGui::widthText(scale, s);
50
     }
57
     }
51
-
52
-    return 0;
53
 }
58
 }
54
 
59
 
55
 unsigned int Font::heightText(float scale, unsigned int maxWidth, std::string s) {
60
 unsigned int Font::heightText(float scale, unsigned int maxWidth, std::string s) {
59
     } else if (stringEndsWith(fontName, ".ttf")) {
64
     } else if (stringEndsWith(fontName, ".ttf")) {
60
         return FontSDL::heightText(scale, maxWidth, s);
65
         return FontSDL::heightText(scale, maxWidth, s);
61
 #endif
66
 #endif
67
+    } else {
68
+        return FontImGui::heightText(scale, maxWidth, s);
62
     }
69
     }
63
-
64
-    return 0;
65
 }
70
 }
66
 
71
 
67
 void Font::drawText(unsigned int x, unsigned int y, float scale,
72
 void Font::drawText(unsigned int x, unsigned int y, float scale,
72
     } else if (stringEndsWith(fontName, ".ttf")) {
77
     } else if (stringEndsWith(fontName, ".ttf")) {
73
         FontSDL::drawText(x, y, scale, color, s);
78
         FontSDL::drawText(x, y, scale, color, s);
74
 #endif
79
 #endif
80
+    } else {
81
+        FontImGui::drawText(x, y, scale, color, s);
75
     }
82
     }
76
 }
83
 }
77
 
84
 
83
     } else if (stringEndsWith(fontName, ".ttf")) {
90
     } else if (stringEndsWith(fontName, ".ttf")) {
84
         FontSDL::drawTextWrapped(x, y, scale, color, maxWidth, s);
91
         FontSDL::drawTextWrapped(x, y, scale, color, maxWidth, s);
85
 #endif
92
 #endif
93
+    } else {
94
+        FontImGui::drawTextWrapped(x, y, scale, color, maxWidth, s);
86
     }
95
     }
87
 }
96
 }
88
 
97
 

+ 45
- 0
src/FontImGui.cpp 查看文件

1
+/*!
2
+ * \file src/FontImGui.cpp
3
+ * \brief Default Font implementation
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#include "global.h"
9
+#include "UI.h"
10
+#include "FontImGui.h"
11
+
12
+#define SCALE_CALC 1.0f
13
+#define SCALE_DRAW 20.0f
14
+
15
+unsigned int FontImGui::widthText(float scale, std::string s) {
16
+    ImGuiIO& io = ImGui::GetIO();
17
+    ImVec2 size = io.Font->CalcTextSizeA(scale * SCALE_CALC, io.DisplaySize.y, s.c_str(), s.c_str() + s.length());
18
+    return size.y;
19
+}
20
+
21
+void FontImGui::drawText(unsigned int x, unsigned int y, float scale,
22
+        const unsigned char color[4], std::string s) {
23
+    ImGuiIO& io = ImGui::GetIO();
24
+    ImVec2 pos = ImVec2(x, y);
25
+    ImU32 col = color[0] | (color[1] << 8) | (color[2] << 16) | (color[3] << 24);
26
+
27
+    ImDrawList dl;
28
+    dl.PushClipRect(ImVec4(0.0f, 0.0f, io.DisplaySize.x, io.DisplaySize.y));
29
+    dl.AddText(io.Font, scale * SCALE_DRAW, pos, col, s.c_str(), s.c_str() + s.length());
30
+
31
+    ImDrawList* dlp = &dl;
32
+    UI::renderImGui(&dlp, 1);
33
+}
34
+
35
+unsigned int FontImGui::heightText(float scale, unsigned int maxWidth, std::string s) {
36
+    ImGuiIO& io = ImGui::GetIO();
37
+    ImVec2 size = io.Font->CalcTextSizeA(scale * SCALE_CALC, maxWidth, s.c_str(), s.c_str() + s.length());
38
+    return size.x;
39
+}
40
+
41
+void FontImGui::drawTextWrapped(unsigned int x, unsigned int y, float scale,
42
+        const unsigned char color[4], unsigned int maxWidth, std::string s) {
43
+    drawText(x, y, scale, color, s);
44
+}
45
+

+ 0
- 2
src/UI.cpp 查看文件

167
                 || ((!io.WantCaptureMouse) && clicked)
167
                 || ((!io.WantCaptureMouse) && clicked)
168
             )) {
168
             )) {
169
         visible = false;
169
         visible = false;
170
-
171
-        getLog() << io.WantCaptureKeyboard << io.WantCaptureMouse << io.KeysDown[escapeKey] << Log::endl;
172
     }
170
     }
173
 
171
 
174
     if (getWindow().getTextInput() != visible)
172
     if (getWindow().getTextInput() != visible)

+ 7
- 13
src/main.cpp 查看文件

147
         return -2;
147
         return -2;
148
     }
148
     }
149
 
149
 
150
-    // Font initialization requires GL context, but is called from config file
151
-    // So we need to initialize some things before executing the config
150
+    // Initialize Font
151
+    error = Font::initialize();
152
+    if (error != 0) {
153
+        std::cout << "Could not initialize Font (" << error << ")!" << std::endl;
154
+        return -3;
155
+    }
156
+
152
     if (configFileToUse == "") {
157
     if (configFileToUse == "") {
153
         if (Command::executeFile(DEFAULT_CONFIG_FILE) != 0) {
158
         if (Command::executeFile(DEFAULT_CONFIG_FILE) != 0) {
154
             if (Command::executeFile(std::string(DEFAULT_CONFIG_PATH) + "/" + DEFAULT_CONFIG_FILE) != 0) {
159
             if (Command::executeFile(std::string(DEFAULT_CONFIG_PATH) + "/" + DEFAULT_CONFIG_FILE) != 0) {
163
         Command::executeFile(configFileToUse);
168
         Command::executeFile(configFileToUse);
164
     }
169
     }
165
 
170
 
166
-    // Initialize Font
167
-#ifdef USING_SDL_FONT
168
-    error = Font::initialize(getRunTime().getDataDir() + "/test.ttf");
169
-#else
170
-    error = Font::initialize(getRunTime().getDataDir() + "/font.pc");
171
-#endif
172
-    if (error != 0) {
173
-        std::cout << "Could not initialize Font (" << error << ")!" << std::endl;
174
-        return -3;
175
-    }
176
-
177
     // Initialize Sound
171
     // Initialize Sound
178
     error = getSound().initialize();
172
     error = getSound().initialize();
179
     if (error != 0) {
173
     if (error != 0) {

Loading…
取消
儲存