Преглед на файлове

Updated imgui, now with own TTF support.

Thomas Buck преди 9 години
родител
ревизия
6d26fdc0ea
променени са 10 файла, в които са добавени 1303 реда и са изтрити 688 реда
  1. 3
    1
      ChangeLog.md
  2. 1
    1
      include/system/Shader.h
  3. 2
    0
      src/Camera.cpp
  4. 6
    14
      src/UI.cpp
  5. 0
    67
      src/deps/imgui/README.md
  6. 1140
    475
      src/deps/imgui/imgui.cpp
  7. 130
    118
      src/deps/imgui/imgui.h
  8. 4
    3
      src/deps/stb/stb.cpp
  9. 12
    6
      src/system/FontImGui.cpp
  10. 5
    3
      src/system/Shader.cpp

+ 3
- 1
ChangeLog.md Целия файл

@@ -3,7 +3,9 @@
3 3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4 4
 
5 5
     [ 20140118 ]
6
-    * Added ability to visualize font _outline_
6
+    * Added ability to visualize font _outline_.
7
+    * Updated imgui to newest version, now with its own TTF support.
8
+    * Fixed drawing of camera view frustum.
7 9
 
8 10
     [ 20140117 ]
9 11
     * Updated imgui, fix for Logging to Clipboard included.

+ 1
- 1
include/system/Shader.h Целия файл

@@ -54,7 +54,7 @@ class Shader {
54 54
     static int initialize();
55 55
     static void shutdown();
56 56
 
57
-    static void set2DState(bool on);
57
+    static void set2DState(bool on, bool depth = true);
58 58
 
59 59
     static void drawGL(ShaderBuffer& vertices, ShaderBuffer& uvs, glm::vec4 color, unsigned int texture,
60 60
                        TextureStorage store = TextureStorage::SYSTEM, unsigned int mode = GL_TRIANGLES,

+ 2
- 0
src/Camera.cpp Целия файл

@@ -366,7 +366,9 @@ bool Camera::boxInFrustum(BoundingBox b) {
366 366
 }
367 367
 
368 368
 void Camera::displayFrustum(glm::mat4 MVP) {
369
+    Shader::set2DState(true, false);
369 370
     Shader::drawGL(vertexBuffer, colorBuffer, indexBuffer, MVP);
370 371
     Shader::drawGL(vertexPointBuffer, colorPointBuffer, MVP, GL_POINTS);
372
+    Shader::set2DState(false, false);
371 373
 }
372 374
 

+ 6
- 14
src/UI.cpp Целия файл

@@ -6,7 +6,6 @@
6 6
  */
7 7
 
8 8
 #include "imgui/imgui.h"
9
-#include "stb/stb_image.h"
10 9
 
11 10
 #include "global.h"
12 11
 #include "Camera.h"
@@ -83,19 +82,12 @@ int UI::initialize() {
83 82
     io.SetClipboardTextFn = Window::setClipboard;
84 83
 
85 84
     // Load font texture
86
-    //! \todo Use our own font subsystem instead of this?
87
-    const void* png_data;
88
-    unsigned int png_size;
89
-    ImGui::GetDefaultFontData(nullptr, nullptr, &png_data, &png_size);
90
-    int tex_x, tex_y, tex_comp;
91
-    void* tex_data = stbi_load_from_memory((const unsigned char*)png_data,
92
-                                           (int)png_size, &tex_x, &tex_y, &tex_comp, 0);
93
-
94
-    fontTex = TextureManager::loadBufferSlot((unsigned char*)tex_data,
95
-              tex_x, tex_y, ColorMode::RGBA, 32,
96
-              TextureStorage::SYSTEM, -1, false);
97
-
98
-    stbi_image_free(tex_data);
85
+    //! \TODO allow loading other TTF fonts
86
+    unsigned char* pixels;
87
+    int width, height;
88
+    io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
89
+    fontTex = TextureManager::loadBufferSlot(pixels, width, height, ColorMode::RGBA, 32,
90
+                                             TextureStorage::SYSTEM, -1, false);
99 91
 
100 92
     // Set up OpenRaider style
101 93
     ImGuiStyle& style = ImGui::GetStyle();

+ 0
- 67
src/deps/imgui/README.md Целия файл

@@ -1,67 +0,0 @@
1
-ImGui
2
-=====
3
-
4
-ImGui is a bloat-free graphical user interface library for C++. It outputs vertex buffers that you can render in your 3D-pipeline enabled application. It is portable, renderer agnostic and carries minimal amount of dependencies (only 3 files are needed). It is based on an "immediate" graphical user interface paradigm which allows you to build simple user interfaces with ease.
5
-
6
-ImGui is designed to enable fast iteration and allow programmers to create "content creation" or "debug" tools (as opposed to tools for the average end-user). It favors simplicity and thus lacks certain features normally found in more high-level libraries, such as string localisation.
7
-
8
-ImGui is particularly suited to integration in 3D applications, fullscreen applications, embedded applications, games, or any applications on consoles platforms where operating system features are non-standard. 
9
-
10
-After ImGui is setup in your engine, you can use it like in this example:
11
-
12
-![screenshot of sample code alongside its output with ImGui](/web/code_sample_01.png?raw=true)
13
-
14
-ImGui outputs vertex buffers and simple command-lists that you can render in your application. Because it doesn't know or touch graphics state directly, you can call ImGui commands anywhere in your code (e.g. in the middle of a running algorithm, or in the middle of your own rendering process). Refer to the sample applications in the examples/ folder for instructions on how to integrate ImGui with your existing codebase. 
15
-
16
-Gallery
17
--------
18
-
19
-![screenshot 1](/web/test_window_01.png?raw=true)
20
-![screenshot 2](/web/test_window_02.png?raw=true)
21
-![screenshot 3](/web/test_window_03.png?raw=true)
22
-![screenshot 4](/web/test_window_04.png?raw=true)
23
-
24
-References
25
-----------
26
-
27
-The Immediate Mode GUI paradigm may at first appear unusual to some users. This is mainly because "Retained Mode" GUIs have been so widespread and predominant. The following links can give you a better understanding about how Immediate Mode GUIs works. 
28
-- [Johannes 'johno' Norneby's article](http://www.johno.se/book/imgui.html).
29
-- [A presentation by Rickard Gustafsson and Johannes Algelind](http://www.cse.chalmers.se/edu/year/2011/course/TDA361/Advanced%20Computer%20Graphics/IMGUI.pdf).
30
-- [Jari Komppa's tutorial on building an ImGui library](http://iki.fi/sol/imgui/).
31
-- [Casey Muratori's original video that popularized the concept](https://mollyrocket.com/861).
32
-
33
-Frequently Asked Question
34
--------------------------
35
-<b>How do you use ImGui on a platform that may not have a mouse and keyboard?</b>
36
-
37
-I recommend using [Synergy](http://synergy-project.org) and the uSynergy.c micro client to share your mouse and keyboard. This way you can seemingly use your PC input devices on a video game console or a tablet. ImGui was also designed to function with touch inputs if you increase the padding of widgets to compensate for the lack of precision of touch devices, but it is recommended you use a mouse to allow optimising for screen real-estate.
38
-
39
-<b>I integrated ImGui in my engine and the text or lines are blurry..</b>
40
-
41
-- Try adjusting ImGui::GetIO().PixelCenterOffset to 0.0f or 0.5f.
42
-- In your Render function, try translating your projection matrix by (0.5f,0.5f) or (0.375f,0.375f).
43
-
44
-<b>Can you create elaborate/serious tools with ImGui?</b>
45
-
46
-Yes. I have written data browsers, debuggers, profilers and all sort of non-trivial tools with the library. There's no reason you cannot, and in my experience the simplicity of the API is very empowering. However note that ImGui is programmer centric and the immediate-mode GUI paradigm might requires a bit of adaptation before you can realize its full potential. 
47
-
48
-<b>Can you reskin the look of ImGui?</b>
49
-
50
-Yes, you can alter the look of the interface to some degree: changing colors, sizes and padding, font. However, as ImGui is designed and optimised to create debug tools, the amount of skinning you can apply is limited. There is only so much you can stray away from the default look and feel of the interface. The example below uses modified settings to create a more compact UI with different colors:
51
-
52
-![skinning screenshot 1](/web/skinning_sample_01.png?raw=true)
53
-
54
-
55
-Credits
56
--------
57
-
58
-Developed by [Omar Cornut](http://www.miracleworld.net). The library was developed with the support of [Media Molecule](http://www.mediamolecule.com) and first used internally on the game [Tearaway](http://tearaway.mediamolecule.com). 
59
-
60
-Embeds [proggy_clean](http://upperbounds.net) font by Tristan Grimmer (also MIT license).
61
-
62
-Inspiration, feedback, and testing: Casey Muratori, Atman Binstock, Mikko Mononen, Emmanuel Briney, Stefan Kamoda, Anton Mikhailov, Matt Willis. Thanks!
63
-
64
-License
65
--------
66
-
67
-ImGui is licensed under the MIT License, see LICENSE for more information.

+ 1140
- 475
src/deps/imgui/imgui.cpp
Файловите разлики са ограничени, защото са твърде много
Целия файл


+ 130
- 118
src/deps/imgui/imgui.h Целия файл

@@ -1,4 +1,4 @@
1
-// ImGui library v1.21 wip
1
+// ImGui library v1.30 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.
@@ -8,6 +8,7 @@
8 8
 
9 9
 struct ImDrawList;
10 10
 struct ImFont;
11
+struct ImFontAtlas;
11 12
 struct ImGuiAabb;
12 13
 struct ImGuiIO;
13 14
 struct ImGuiStorage;
@@ -31,8 +32,9 @@ struct ImGuiWindow;
31 32
 #endif
32 33
 
33 34
 typedef unsigned int ImU32;
34
-typedef unsigned short ImWchar;     // hold a character for display
35
-typedef ImU32 ImGuiID;              // hold widget unique ID
35
+typedef unsigned short ImWchar;     // character for display
36
+typedef void* ImTextureID;          // user data to refer to a texture (e.g. store your texture handle/id)
37
+typedef ImU32 ImGuiID;              // unique ID used by widgets (typically hashed from a stack of string)
36 38
 typedef int ImGuiCol;               // enum ImGuiCol_
37 39
 typedef int ImGuiStyleVar;          // enum ImGuiStyleVar_
38 40
 typedef int ImGuiKey;               // enum ImGuiKey_
@@ -40,7 +42,7 @@ typedef int ImGuiColorEditMode;     // enum ImGuiColorEditMode_
40 42
 typedef int ImGuiWindowFlags;       // enum ImGuiWindowFlags_
41 43
 typedef int ImGuiSetCondition;      // enum ImGuiSetCondition_
42 44
 typedef int ImGuiInputTextFlags;    // enum ImGuiInputTextFlags_
43
-struct ImGuiTextEditCallbackData;
45
+struct ImGuiTextEditCallbackData;   // for advanced uses of InputText() 
44 46
 
45 47
 struct ImVec2
46 48
 {
@@ -106,10 +108,10 @@ public:
106 108
     inline const_iterator       begin() const                   { return Data; }
107 109
     inline iterator             end()                           { return Data + Size; }
108 110
     inline const_iterator       end() const                     { return Data + Size; }
109
-    inline value_type&          front()                         { return at(0); }
110
-    inline const value_type&    front() const                   { return at(0); }
111
-    inline value_type&          back()                          { IM_ASSERT(Size > 0); return at(Size-1); }
112
-    inline const value_type&    back() const                    { IM_ASSERT(Size > 0); return at(Size-1); }
111
+    inline value_type&          front()                         { IM_ASSERT(Size > 0); return Data[0]; }
112
+    inline const value_type&    front() const                   { IM_ASSERT(Size > 0); return Data[0]; }
113
+    inline value_type&          back()                          { IM_ASSERT(Size > 0); return Data[Size-1]; }
114
+    inline const value_type&    back() const                    { IM_ASSERT(Size > 0); return Data[Size-1]; }
113 115
     inline void                 swap(ImVector<T>& rhs)          { const size_t rhs_size = rhs.Size; rhs.Size = Size; Size = rhs_size; const size_t rhs_cap = rhs.Capacity; rhs.Capacity = Capacity; Capacity = rhs_cap; value_type* rhs_data = rhs.Data; rhs.Data = Data; Data = rhs_data; }
114 116
 
115 117
     inline void                 reserve(size_t new_capacity)    { Data = (value_type*)ImGui::MemRealloc(Data, new_capacity * sizeof(value_type)); Capacity = new_capacity; }
@@ -129,7 +131,7 @@ public:
129 131
 // - struct ImGuiTextBuffer             // Text buffer for logging/accumulating text
130 132
 // - struct ImGuiStorage                // Custom key value storage (if you need to alter open/close states manually)
131 133
 // - struct ImDrawList                  // Draw command list
132
-// - struct ImFont                      // Bitmap font loader
134
+// - struct ImFont                      // TTF font loader, bake glyphs into bitmap
133 135
 
134 136
 // ImGui End-user API
135 137
 // In a namespace so that user can add extra functions (e.g. Value() helpers for your vector or common types)
@@ -174,16 +176,21 @@ namespace ImGui
174 176
     IMGUI_API void          SetStateStorage(ImGuiStorage* tree);                                // replace tree state storage with our own (if you want to manipulate it yourself, typically clear subsection of it).
175 177
     IMGUI_API ImGuiStorage* GetStateStorage();
176 178
 
177
-    IMGUI_API void          PushItemWidth(float item_width);                                    // width of items for the common item+label case. default to ~2/3 of windows width.
178
-    IMGUI_API void          PopItemWidth();
179
-    IMGUI_API float         GetItemWidth();
180
-    IMGUI_API void          PushAllowKeyboardFocus(bool v);                                     // allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets.
181
-    IMGUI_API void          PopAllowKeyboardFocus();
179
+    // Parameters stacks (shared)
180
+    IMGUI_API void          PushFont(ImFont* font);                                             // use NULL as a shortcut to push default font
181
+    IMGUI_API void          PopFont();
182 182
     IMGUI_API void          PushStyleColor(ImGuiCol idx, const ImVec4& col);
183 183
     IMGUI_API void          PopStyleColor(int count = 1);
184 184
     IMGUI_API void          PushStyleVar(ImGuiStyleVar idx, float val);
185 185
     IMGUI_API void          PushStyleVar(ImGuiStyleVar idx, const ImVec2& val);
186 186
     IMGUI_API void          PopStyleVar(int count = 1);
187
+
188
+    // Parameters stacks (current window)
189
+    IMGUI_API void          PushItemWidth(float item_width);                                    // width of items for the common item+label case. default to ~2/3 of windows width.
190
+    IMGUI_API void          PopItemWidth();
191
+    IMGUI_API float         GetItemWidth();
192
+    IMGUI_API void          PushAllowKeyboardFocus(bool v);                                     // allow focusing using TAB/Shift-TAB, enabled by default but you can disable it for certain widgets.
193
+    IMGUI_API void          PopAllowKeyboardFocus();
187 194
     IMGUI_API void          PushTextWrapPos(float wrap_pos_x = 0.0f);                           // word-wrapping for Text*() commands. < 0.0f: no wrapping; 0.0f: wrap to end of window (or column); > 0.0f: wrap at 'wrap_pos_x' position in window local space.
188 195
     IMGUI_API void          PopTextWrapPos();
189 196
 
@@ -234,8 +241,9 @@ namespace ImGui
234 241
     IMGUI_API void          LabelTextV(const char* label, const char* fmt, va_list args);
235 242
     IMGUI_API void          BulletText(const char* fmt, ...);
236 243
     IMGUI_API void          BulletTextV(const char* fmt, va_list args);
237
-    IMGUI_API bool          Button(const char* label, ImVec2 size = ImVec2(0,0), bool repeat_when_held = false);
244
+    IMGUI_API bool          Button(const char* label, const ImVec2& size = ImVec2(0,0), bool repeat_when_held = false);
238 245
     IMGUI_API bool          SmallButton(const char* label);
246
+    IMGUI_API void          Image(ImTextureID user_texture_id, const ImVec2& size, const ImVec2& uv0 = ImVec2(0,0), const ImVec2& uv1 = ImVec2(1,1), ImU32 tint_col = 0xFFFFFFFF, ImU32 border_col = 0x00000000);
239 247
     IMGUI_API bool          CollapsingHeader(const char* label, const char* str_id = NULL, const bool display_frame = true, const bool default_open = false);
240 248
     IMGUI_API bool          SliderFloat(const char* label, float* v, float v_min, float v_max, const char* display_format = "%.3f", float power = 1.0f);     // adjust display_format to decorate the value with a prefix or a suffix. Use power!=1.0 for logarithmic sliders.
241 249
     IMGUI_API bool          SliderFloat2(const char* label, float v[2], float v_min, float v_max, const char* display_format = "%.3f", float power = 1.0f);
@@ -310,7 +318,6 @@ namespace ImGui
310 318
     IMGUI_API float         GetTime();
311 319
     IMGUI_API int           GetFrameCount();
312 320
     IMGUI_API const char*   GetStyleColName(ImGuiCol idx);
313
-    IMGUI_API void          GetDefaultFontData(const void** fnt_data, unsigned int* fnt_size, const void** png_data, unsigned int* png_size);
314 321
     IMGUI_API ImVec2        CalcTextSize(const char* text, const char* text_end = NULL, bool hide_text_after_double_hash = false, float wrap_width = -1.0f);
315 322
 
316 323
 } // namespace ImGui
@@ -426,7 +433,7 @@ enum ImGuiStyleVar_
426 433
     ImGuiStyleVar_FrameRounding,     // float
427 434
     ImGuiStyleVar_ItemSpacing,       // ImVec2
428 435
     ImGuiStyleVar_ItemInnerSpacing,  // ImVec2
429
-    ImGuiStyleVar_TreeNodeSpacing,   // float
436
+    ImGuiStyleVar_TreeNodeSpacing    // float
430 437
 };
431 438
 
432 439
 // Enumeration for ColorEditMode()
@@ -445,7 +452,7 @@ enum ImGuiSetCondition_
445 452
 {
446 453
     ImGuiSetCondition_Always              = 1 << 0, // Set the variable
447 454
     ImGuiSetCondition_FirstUseThisSession = 1 << 1, // Only set the variable on the first call for this window (once per session)
448
-    ImGuiSetCondition_FirstUseEver        = 1 << 2, // Only set the variable if the window doesn't exist in the .ini file
455
+    ImGuiSetCondition_FirstUseEver        = 1 << 2  // Only set the variable if the window doesn't exist in the .ini file
449 456
 };
450 457
 
451 458
 struct ImGuiStyle
@@ -477,20 +484,19 @@ struct ImGuiIO
477 484
     // Settings (fill once)                 // Default value:
478 485
     //------------------------------------------------------------------
479 486
 
480
-    ImVec2      DisplaySize;                // <unset>                  // Display size, in pixels. For clamping windows positions.
481
-    float       DeltaTime;                  // = 1.0f/60.0f             // Time elapsed since last frame, in seconds.
482
-    float       IniSavingRate;              // = 5.0f                   // Maximum time between saving .ini file, in seconds.
483
-    const char* IniFilename;                // = "imgui.ini"            // Path to .ini file. NULL to disable .ini saving.
484
-    const char* LogFilename;                // = "imgui_log.txt"        // Path to .log file (default parameter to ImGui::LogToFile when no file is specified).
485
-    float       MouseDoubleClickTime;       // = 0.30f                  // Time for a double-click, in seconds.
486
-    float       MouseDoubleClickMaxDist;    // = 6.0f                   // Distance threshold to stay in to validate a double-click, in pixels.
487
-    int         KeyMap[ImGuiKey_COUNT];     // <unset>                  // Map of indices into the KeysDown[512] entries array
488
-    ImFont*     Font;                       // <auto>                   // Font (also see 'Settings' fields inside ImFont structure for details)
489
-    float       FontGlobalScale;            // = 1.0f                   // Global scale all fonts
490
-    bool        FontAllowUserScaling;       // = false                  // Allow user scaling text of individual window with CTRL+Wheel.
491
-    float       PixelCenterOffset;          // = 0.0f                   // Try to set to 0.5f or 0.375f if rendering is blurry
492
-
493
-    void*       UserData;                   // = NULL                   // Store your own data for retrieval by callbacks.
487
+    ImVec2        DisplaySize;              // <unset>              // Display size, in pixels. For clamping windows positions.
488
+    float         DeltaTime;                // = 1.0f/60.0f         // Time elapsed since last frame, in seconds.
489
+    float         IniSavingRate;            // = 5.0f               // Maximum time between saving .ini file, in seconds.
490
+    const char*   IniFilename;              // = "imgui.ini"        // Path to .ini file. NULL to disable .ini saving.
491
+    const char*   LogFilename;              // = "imgui_log.txt"    // Path to .log file (default parameter to ImGui::LogToFile when no file is specified).
492
+    float         MouseDoubleClickTime;     // = 0.30f              // Time for a double-click, in seconds.
493
+    float         MouseDoubleClickMaxDist;  // = 6.0f               // Distance threshold to stay in to validate a double-click, in pixels.
494
+    int           KeyMap[ImGuiKey_COUNT];   // <unset>              // Map of indices into the KeysDown[512] entries array
495
+    void*         UserData;                 // = NULL               // Store your own data for retrieval by callbacks.
496
+
497
+    ImFontAtlas*  Fonts;                    // <auto>               // Load and assemble one or more fonts into a single tightly packed texture. Output to Fonts array.
498
+    float         FontGlobalScale;          // = 1.0f               // Global scale all fonts
499
+    bool          FontAllowUserScaling;     // = false              // Allow user scaling text of individual window with CTRL+Wheel.
494 500
 
495 501
     //------------------------------------------------------------------
496 502
     // User Functions
@@ -548,7 +554,7 @@ struct ImGuiIO
548 554
     float       MouseDownTime[5];
549 555
     float       KeysDownTime[512];
550 556
 
551
-    IMGUI_API ImGuiIO();
557
+    IMGUI_API   ImGuiIO();
552 558
 };
553 559
 
554 560
 //-----------------------------------------------------------------------------
@@ -673,13 +679,14 @@ struct ImGuiTextEditCallbackData
673 679
 
674 680
 //-----------------------------------------------------------------------------
675 681
 // Draw List
676
-// Hold a series of drawing commands. The user provide a renderer for ImDrawList
682
+// Hold a series of drawing commands. The user provides a renderer for ImDrawList
677 683
 //-----------------------------------------------------------------------------
678 684
 
679 685
 struct ImDrawCmd
680 686
 {
681 687
     unsigned int    vtx_count;
682 688
     ImVec4          clip_rect;
689
+    ImTextureID     texture_id;     // Copy of user-provided 'TexID' from ImFont or passed to Image*() functions. Ignore if not using images or multiple fonts.
683 690
 };
684 691
 
685 692
 #ifndef IMGUI_OVERRIDE_DRAWVERT_STRUCT_LAYOUT
@@ -710,16 +717,22 @@ struct ImDrawList
710 717
     ImVector<ImDrawVert>    vtx_buffer;         // each command consume ImDrawCmd::vtx_count of those
711 718
 
712 719
     // [Internal to ImGui]
713
-    ImVector<ImVec4>        clip_rect_stack;    // [internal] clip rect stack while building the command-list (so text command can perform clipping early on)
720
+    ImVector<ImVec4>        clip_rect_stack;    // [internal]
721
+    ImVector<ImTextureID>   texture_id_stack;   // [internal] 
714 722
     ImDrawVert*             vtx_write;          // [internal] point within vtx_buffer after each add command (to avoid using the ImVector<> operators too much)
715 723
 
716 724
     ImDrawList() { Clear(); }
717 725
 
718 726
     IMGUI_API void  Clear();
727
+    IMGUI_API void  SetClipRect(const ImVec4& clip_rect);
719 728
     IMGUI_API void  PushClipRect(const ImVec4& clip_rect);
720 729
     IMGUI_API void  PopClipRect();
730
+    IMGUI_API void  SetTextureID(const ImTextureID& texture_id);
731
+    IMGUI_API void  PushTextureID(const ImTextureID& texture_id);
732
+    IMGUI_API void  PopTextureID();
721 733
     IMGUI_API void  ReserveVertices(unsigned int vtx_count);
722 734
     IMGUI_API void  AddVtx(const ImVec2& pos, ImU32 col);
735
+    IMGUI_API void  AddVtxUV(const ImVec2& pos, ImU32 col, const ImVec2& uv);
723 736
     IMGUI_API void  AddVtxLine(const ImVec2& a, const ImVec2& b, ImU32 col);
724 737
 
725 738
     // Primitives   
@@ -731,99 +744,98 @@ struct ImDrawList
731 744
     IMGUI_API void  AddCircleFilled(const ImVec2& centre, float radius, ImU32 col, int num_segments = 12);
732 745
     IMGUI_API void  AddArc(const ImVec2& center, float rad, ImU32 col, int a_min, int a_max, bool tris = false, const ImVec2& third_point_offset = ImVec2(0,0));
733 746
     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);
747
+    IMGUI_API void  AddImage(ImTextureID user_texture_id, const ImVec2& a, const ImVec2& b, const ImVec2& uv0, const ImVec2& uv1, ImU32 col);
734 748
 };
735 749
 
736
-// Bitmap font data loader & renderer into vertices
737
-// Using the .fnt format exported by BMFont
738
-//  - tool: http://www.angelcode.com/products/bmfont
739
-//  - file-format: http://www.angelcode.com/products/bmfont/doc/file_format.html
740
-// Assume valid file data (won't handle invalid/malicious data)
741
-// Handle a subset of the options, namely:
742
-//  - kerning pair are not supported (because some ImGui code does per-character CalcTextSize calls, need to turn it into something more state-ful to allow for kerning)
743
-struct ImFont
750
+// Load and rasterize multiple TTF fonts into a same texture.
751
+// Sharing a texture for multiple fonts allows us to reduce the number of draw calls during rendering.
752
+// We also add custom graphic data into the texture that serves for ImGui.
753
+//  1. (Optional) Call AddFont*** functions. If you don't call any, the default font will be loaded for you.
754
+//  2. Call GetTexDataAsAlpha8() or GetTexDataAsRGBA32() to build and retrieve pixels data.
755
+//  3. Upload the pixels data into a texture within your graphics system.
756
+//  4. Call SetTexID(my_tex_id); and pass the pointer/identifier to your texture. This value will be passed back to you during rendering to identify the texture.
757
+//  5. Call ClearPixelsData() to free textures memory on the heap.
758
+struct ImFontAtlas
744 759
 {
745
-    struct FntInfo;
746
-    struct FntCommon;
747
-    struct FntGlyph;
748
-    struct FntKerning;
749
-
750
-    // Settings
751
-    float                       Scale;              // = 1.0f          // Base font scale, multiplied by the per-window font scale which you can adjust with SetFontScale()
752
-    ImVec2                      DisplayOffset;      // = (0.0f,0.0f)   // Offset font rendering by xx pixels
753
-    ImVec2                      TexUvForWhite;      // = (0.0f,0.0f)   // Font texture must have a white pixel at this UV coordinate. Adjust if you are using custom texture.
754
-    ImWchar                     FallbackChar;       // = '?'           // Replacement glyph is one isn't found.
755
-
756
-    // Data
757
-    unsigned char*              Data;               // Raw data, content of .fnt file
758
-    size_t                      DataSize;           //
759
-    bool                        DataOwned;          // 
760
-    const FntInfo*              Info;               // (point into raw data)
761
-    const FntCommon*            Common;             // (point into raw data)
762
-    const FntGlyph*             Glyphs;             // (point into raw data)
763
-    size_t                      GlyphsCount;        //
764
-    const FntKerning*           Kerning;            // (point into raw data) - NB: kerning is unsupported
765
-    size_t                      KerningCount;       //
766
-    ImVector<const char*>       Filenames;          // (point into raw data)
767
-    ImVector<int>               IndexLookup;        // (built)
768
-    const FntGlyph*             FallbackGlyph;      // == FindGlyph(FontFallbackChar)
769
-
770
-    IMGUI_API ImFont();
771
-    IMGUI_API ~ImFont()         { Clear(); }
772
-
773
-    IMGUI_API bool              LoadFromMemory(const void* data, size_t data_size);
774
-    IMGUI_API bool              LoadFromFile(const char* filename);
760
+    IMGUI_API ImFontAtlas();
761
+    IMGUI_API ~ImFontAtlas();
762
+    IMGUI_API ImFont*           AddFontDefault();
763
+    IMGUI_API ImFont*           AddFontFromFileTTF(const char* filename, float size_pixels, const ImWchar* glyph_ranges = NULL, int font_no = 0);
764
+    IMGUI_API ImFont*           AddFontFromMemoryTTF(void* in_ttf_data, size_t in_ttf_data_size, float size_pixels, const ImWchar* glyph_ranges = NULL, int font_no = 0); // Pass ownership of 'in_ttf_data' memory.
765
+    IMGUI_API void              ClearTexData();             // Saves RAM once the texture has been copied to graphics memory.
775 766
     IMGUI_API void              Clear();
776
-    IMGUI_API void              BuildLookupTable();
777
-    IMGUI_API const FntGlyph*   FindGlyph(unsigned short c) const;
778
-    IMGUI_API bool              IsLoaded() const { return Info != NULL && Common != NULL && Glyphs != NULL; }
779
-
780
-    // 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable.
781
-    // 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable.
782
-    IMGUI_API ImVec2            CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end = NULL, const char** remaining = NULL) const; // utf8
783
-    IMGUI_API ImVec2            CalcTextSizeW(float size, float max_width, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL) const;                 // wchar
784
-    IMGUI_API void              RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, ImDrawVert*& out_vertices, float wrap_width = 0.0f) const;
785
-
786
-    IMGUI_API const char*       CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const;
787
-
788
-#pragma pack(push, 1)
789
-    struct FntInfo
790
-    {
791
-        signed short    FontSize;
792
-        unsigned char   BitField;       // bit 0: smooth, bit 1: unicode, bit 2: italic, bit 3: bold, bit 4: fixedHeight, bits 5-7: reserved
793
-        unsigned char   CharSet;
794
-        unsigned short  StretchH;
795
-        unsigned char   AA;
796
-        unsigned char   PaddingUp, PaddingRight, PaddingDown, PaddingLeft;
797
-        unsigned char   SpacingHoriz, SpacingVert, Outline;
798
-        //char          FontName[];
799
-    };
800 767
 
801
-    struct FntCommon
802
-    {
803
-        unsigned short  LineHeight, Base;
804
-        unsigned short  ScaleW, ScaleH;
805
-        unsigned short  Pages;
806
-        unsigned char   BitField;
807
-        unsigned char   Channels[4];
808
-    };
768
+    // Retrieve texture data
769
+    // User is in charge of copying the pixels into graphics memory, then call SetTextureUserID()
770
+    // After loading the texture into your graphic system, store your texture handle in 'TexID' (ignore if you aren't using multiple fonts nor images)
771
+    // RGBA32 format is provided for convenience and high compatibility, but note that all RGB pixels are white, so 75% of the memory is wasted.
772
+    IMGUI_API void              GetTexDataAsAlpha8(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL);  // 1 byte per-pixel
773
+    IMGUI_API void              GetTexDataAsRGBA32(unsigned char** out_pixels, int* out_width, int* out_height, int* out_bytes_per_pixel = NULL);  // 4 bytes-per-pixel
774
+    IMGUI_API void              SetTexID(void* id)  { TexID = id; }
775
+
776
+    // Helpers to retrieve list of common Unicode ranges (2 value per range, values are inclusive, zero-terminated list)
777
+    // (Those functions could be static, aren't so simple use case doesn't have to refer to the ImFontAtlas:: type ever if in their code)
778
+    IMGUI_API const ImWchar*    GetGlyphRangesDefault();    // Basic Latin, Extended Latin
779
+    IMGUI_API const ImWchar*    GetGlyphRangesJapanese();   // Default + Hiragana, Katakana, Half-Width, Selection of 1946 Ideographs
780
+    IMGUI_API const ImWchar*    GetGlyphRangesChinese();    // Japanese + full set of about 21000 CJK Unified Ideographs
781
+
782
+    // Members
783
+    // (Access texture data via GetTexData*() calls which will setup a default font for you.)
784
+    void*                       TexID;              // User data to refer to the texture once it has been uploaded to user's graphic systems. It ia passed back to you during rendering.
785
+    unsigned char*              TexPixelsAlpha8;    // 1 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight
786
+    unsigned int*               TexPixelsRGBA32;    // 4 component per pixel, each component is unsigned 8-bit. Total size = TexWidth * TexHeight * 4
787
+    int                         TexWidth;
788
+    int                         TexHeight;
789
+    ImVec2                      TexExtraDataPos;    // Position of our rectangle where we draw non-font graphics
790
+    ImVec2                      TexUvWhitePixel;    // Texture coordinates to a white pixel (part of the TexExtraData block)
791
+    ImVector<ImFont*>           Fonts;
792
+
793
+    // Private
794
+    struct ImFontAtlasData;
795
+    ImVector<ImFontAtlasData*>  InputData;          // Internal data
796
+    IMGUI_API bool              Build();            // Build pixels data. This is automatically for you by the GetTexData*** functions.
797
+    IMGUI_API void              ClearInputData();   // Clear the input TTF data.
798
+};
809 799
 
810
-    struct FntGlyph
800
+// TTF font loading and rendering
801
+// ImFontAtlas automatically loads a default embedded font for you when you call GetTexDataAsAlpha8() or GetTexDataAsRGBA32().
802
+// Kerning isn't supported. At the moment some ImGui code does per-character CalcTextSize calls, need something more state-ful.
803
+struct ImFont
804
+{
805
+    // Members: Settings
806
+    float               FontSize;           // <user set>      // Height of characters, set during loading (don't change after loading)
807
+    float               Scale;              // = 1.0f          // Base font scale, multiplied by the per-window font scale which you can adjust with SetFontScale()
808
+    ImVec2              DisplayOffset;      // = (0.0f,0.0f)   // Offset font rendering by xx pixels
809
+    ImWchar             FallbackChar;       // = '?'           // Replacement glyph if one isn't found.
810
+
811
+    // Members: Runtime data
812
+    struct Glyph
811 813
     {
812
-        unsigned int    Id;
813
-        unsigned short  X, Y, Width, Height;
814
-        signed short    XOffset, YOffset;
814
+        ImWchar         Codepoint;
815 815
         signed short    XAdvance;
816
-        unsigned char   Page;
817
-        unsigned char   Channel;
816
+        signed short    Width, Height;
817
+        signed short    XOffset, YOffset;
818
+        float           U0, V0, U1, V1;     // Texture coordinates
818 819
     };
820
+    ImFontAtlas*        ContainerAtlas;     // What we has been loaded into
821
+    ImVector<Glyph>     Glyphs;
822
+    ImVector<int>       IndexLookup;        // Index glyphs by Unicode code-point
823
+    const Glyph*        FallbackGlyph;      // == FindGlyph(FontFallbackChar)
819 824
 
820
-    struct FntKerning
821
-    {
822
-        unsigned int    IdFirst;
823
-        unsigned int    IdSecond;
824
-        signed short    Amount;
825
-    };
826
-#pragma pack(pop)
825
+    // Methods
826
+    IMGUI_API ImFont();
827
+    IMGUI_API ~ImFont() { Clear(); }
828
+    IMGUI_API void                  Clear();
829
+    IMGUI_API void                  BuildLookupTable();
830
+    IMGUI_API const Glyph*          FindGlyph(unsigned short c) const;
831
+    IMGUI_API bool                  IsLoaded() const        { return ContainerAtlas != NULL; }
832
+
833
+    // 'max_width' stops rendering after a certain width (could be turned into a 2d size). FLT_MAX to disable.
834
+    // 'wrap_width' enable automatic word-wrapping across multiple lines to fit into given width. 0.0f to disable.
835
+    IMGUI_API ImVec2                CalcTextSizeA(float size, float max_width, float wrap_width, const char* text_begin, const char* text_end = NULL, const char** remaining = NULL) const; // utf8
836
+    IMGUI_API ImVec2                CalcTextSizeW(float size, float max_width, const ImWchar* text_begin, const ImWchar* text_end, const ImWchar** remaining = NULL) const;                 // wchar
837
+    IMGUI_API void                  RenderText(float size, ImVec2 pos, ImU32 col, const ImVec4& clip_rect, const char* text_begin, const char* text_end, ImDrawVert*& out_vertices, float wrap_width = 0.0f) const;
838
+    IMGUI_API const char*           CalcWordWrapPositionA(float scale, const char* text, const char* text_end, float wrap_width) const;
827 839
 };
828 840
 
829 841
 //---- Include imgui_user.h at the end of imgui.h

+ 4
- 3
src/deps/stb/stb.cpp Целия файл

@@ -18,7 +18,8 @@
18 18
 #define STBRP_ASSERT assert
19 19
 #include "stb/stb_rect_pack.h"
20 20
 
21
-#define STB_TRUETYPE_IMPLEMENTATION
22
-#define STBTT_assert(x) assert(x)
23
-#include "stb/stb_truetype.h"
21
+//! \TODO bring back when imgui allows it
22
+//#define STB_TRUETYPE_IMPLEMENTATION
23
+//#define STBTT_assert(x) assert(x)
24
+//#include "stb/stb_truetype.h"
24 25
 

+ 12
- 6
src/system/FontImGui.cpp Целия файл

@@ -16,20 +16,23 @@
16 16
 
17 17
 unsigned int FontImGui::widthText(float scale, std::string s) {
18 18
     ImGuiIO& io = ImGui::GetIO();
19
-    ImVec2 size = io.Font->CalcTextSizeA(scale * SCALE_CALC, FLT_MAX, io.DisplaySize.y, s.c_str(),
20
-                                         s.c_str() + s.length());
19
+    ImFont* font = io.Fonts->Fonts.at(0);
20
+    ImVec2 size = font->CalcTextSizeA(scale * SCALE_CALC, FLT_MAX, io.DisplaySize.y, s.c_str(),
21
+                                                    s.c_str() + s.length());
21 22
     return size.x;
22 23
 }
23 24
 
24 25
 void FontImGui::drawText(unsigned int x, unsigned int y, float scale,
25 26
                          const unsigned char color[4], std::string s) {
26 27
     ImGuiIO& io = ImGui::GetIO();
28
+    ImFont* font = io.Fonts->Fonts.at(0);
27 29
     ImVec2 pos = ImVec2(x, y);
28 30
     ImU32 col = color[0] | (color[1] << 8) | (color[2] << 16) | (color[3] << 24);
29 31
 
30 32
     ImDrawList dl;
33
+    dl.PushTextureID(font->ContainerAtlas->TexID);
31 34
     dl.PushClipRect(ImVec4(0.0f, 0.0f, io.DisplaySize.x, io.DisplaySize.y));
32
-    dl.AddText(io.Font, scale * SCALE_DRAW, pos, col, s.c_str(), s.c_str() + s.length());
35
+    dl.AddText(font, scale * SCALE_DRAW, pos, col, s.c_str(), s.c_str() + s.length());
33 36
 
34 37
     ImDrawList* dlp = &dl;
35 38
     UI::renderImGui(&dlp, 1);
@@ -37,20 +40,23 @@ void FontImGui::drawText(unsigned int x, unsigned int y, float scale,
37 40
 
38 41
 unsigned int FontImGui::heightText(float scale, unsigned int maxWidth, std::string s) {
39 42
     ImGuiIO& io = ImGui::GetIO();
40
-    ImVec2 size = io.Font->CalcTextSizeA(scale * SCALE_CALC, FLT_MAX, maxWidth, s.c_str(),
41
-                                         s.c_str() + s.length());
43
+    ImFont* font = io.Fonts->Fonts.at(0);
44
+    ImVec2 size = font->CalcTextSizeA(scale * SCALE_CALC, FLT_MAX, maxWidth, s.c_str(),
45
+                                                    s.c_str() + s.length());
42 46
     return size.y;
43 47
 }
44 48
 
45 49
 void FontImGui::drawTextWrapped(unsigned int x, unsigned int y, float scale,
46 50
                                 const unsigned char color[4], unsigned int maxWidth, std::string s) {
47 51
     ImGuiIO& io = ImGui::GetIO();
52
+    ImFont* font = io.Fonts->Fonts.at(0);
48 53
     ImVec2 pos = ImVec2(x, y);
49 54
     ImU32 col = color[0] | (color[1] << 8) | (color[2] << 16) | (color[3] << 24);
50 55
 
51 56
     ImDrawList dl;
57
+    dl.PushTextureID(font->ContainerAtlas->TexID);
52 58
     dl.PushClipRect(ImVec4(0.0f, 0.0f, io.DisplaySize.x, io.DisplaySize.y));
53
-    dl.AddText(io.Font, scale * SCALE_DRAW, pos, col, s.c_str(), s.c_str() + s.length(), maxWidth);
59
+    dl.AddText(font, scale * SCALE_DRAW, pos, col, s.c_str(), s.c_str() + s.length(), maxWidth);
54 60
 
55 61
     ImDrawList* dlp = &dl;
56 62
     UI::renderImGui(&dlp, 1);

+ 5
- 3
src/system/Shader.cpp Целия файл

@@ -224,13 +224,15 @@ void Shader::shutdown() {
224 224
     glDeleteVertexArrays(1, &vertexArrayID);
225 225
 }
226 226
 
227
-void Shader::set2DState(bool on) {
227
+void Shader::set2DState(bool on, bool depth) {
228 228
     if (on) {
229 229
         glDisable(GL_CULL_FACE);
230
-        glDisable(GL_DEPTH_TEST);
230
+        if (depth)
231
+            glDisable(GL_DEPTH_TEST);
231 232
     } else {
232
-        glEnable(GL_DEPTH_TEST);
233 233
         glEnable(GL_CULL_FACE);
234
+        if (depth)
235
+            glEnable(GL_DEPTH_TEST);
234 236
     }
235 237
 }
236 238
 

Loading…
Отказ
Запис