瀏覽代碼

Default Font using ImGUI

Thomas Buck 9 年之前
父節點
當前提交
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,6 +2,9 @@
2 2
 
3 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 8
     [ 20141015 ]
6 9
     * Added rudimentary command history support for Console
7 10
 

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

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

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

@@ -0,0 +1,28 @@
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,9 +31,9 @@ public:
31 31
     static void handleMouseMotion(int xrel, int yrel, int xabs, int yabs);
32 32
     static void handleMouseScroll(int xrel, int yrel);
33 33
 
34
-private:
35 34
     static void renderImGui(ImDrawList** const draw_lists, int count);
36 35
 
36
+private:
37 37
     static bool visible;
38 38
     static unsigned int fontTex;
39 39
     static std::string iniFilename;

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

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

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

@@ -10,6 +10,7 @@
10 10
 #include "utils/strings.h"
11 11
 #include "Window.h"
12 12
 #include "Font.h"
13
+#include "FontImGui.h"
13 14
 #include "FontTRLE.h"
14 15
 
15 16
 #ifdef USING_SDL_FONT
@@ -36,8 +37,12 @@ int Font::initialize(std::string font) {
36 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 48
 unsigned int Font::widthText(float scale, std::string s) {
@@ -47,9 +52,9 @@ unsigned int Font::widthText(float scale, std::string s) {
47 52
     } else if (stringEndsWith(fontName, ".ttf")) {
48 53
         return FontSDL::widthText(scale, s);
49 54
 #endif
55
+    } else {
56
+        return FontImGui::widthText(scale, s);
50 57
     }
51
-
52
-    return 0;
53 58
 }
54 59
 
55 60
 unsigned int Font::heightText(float scale, unsigned int maxWidth, std::string s) {
@@ -59,9 +64,9 @@ unsigned int Font::heightText(float scale, unsigned int maxWidth, std::string s)
59 64
     } else if (stringEndsWith(fontName, ".ttf")) {
60 65
         return FontSDL::heightText(scale, maxWidth, s);
61 66
 #endif
67
+    } else {
68
+        return FontImGui::heightText(scale, maxWidth, s);
62 69
     }
63
-
64
-    return 0;
65 70
 }
66 71
 
67 72
 void Font::drawText(unsigned int x, unsigned int y, float scale,
@@ -72,6 +77,8 @@ void Font::drawText(unsigned int x, unsigned int y, float scale,
72 77
     } else if (stringEndsWith(fontName, ".ttf")) {
73 78
         FontSDL::drawText(x, y, scale, color, s);
74 79
 #endif
80
+    } else {
81
+        FontImGui::drawText(x, y, scale, color, s);
75 82
     }
76 83
 }
77 84
 
@@ -83,6 +90,8 @@ void Font::drawTextWrapped(unsigned int x, unsigned int y, float scale,
83 90
     } else if (stringEndsWith(fontName, ".ttf")) {
84 91
         FontSDL::drawTextWrapped(x, y, scale, color, maxWidth, s);
85 92
 #endif
93
+    } else {
94
+        FontImGui::drawTextWrapped(x, y, scale, color, maxWidth, s);
86 95
     }
87 96
 }
88 97
 

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

@@ -0,0 +1,45 @@
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,8 +167,6 @@ void UI::eventsFinished() {
167 167
                 || ((!io.WantCaptureMouse) && clicked)
168 168
             )) {
169 169
         visible = false;
170
-
171
-        getLog() << io.WantCaptureKeyboard << io.WantCaptureMouse << io.KeysDown[escapeKey] << Log::endl;
172 170
     }
173 171
 
174 172
     if (getWindow().getTextInput() != visible)

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

@@ -147,8 +147,13 @@ int main(int argc, char* argv[]) {
147 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 157
     if (configFileToUse == "") {
153 158
         if (Command::executeFile(DEFAULT_CONFIG_FILE) != 0) {
154 159
             if (Command::executeFile(std::string(DEFAULT_CONFIG_PATH) + "/" + DEFAULT_CONFIG_FILE) != 0) {
@@ -163,17 +168,6 @@ int main(int argc, char* argv[]) {
163 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 171
     // Initialize Sound
178 172
     error = getSound().initialize();
179 173
     if (error != 0) {

Loading…
取消
儲存