1 2 // Copyright Marcelo S. N. Mancini(Hipreme) 2020. 3 // Distributed under the Boost Software License, Version 1.0. 4 // (See accompanying file LICENSE_1_0.txt or copy at 5 // http://www.boost.org/LICENSE_1_0.txt) 6 7 module bindbc.cimgui.additional; 8 import bindbc.cimgui.types : ImDrawCallback; 9 import bindbc.cimgui.funcs : igMemAlloc, igMemFree; 10 import core.stdc.string : memcpy; 11 //This file is non auto generated, it may be better creating one file which will contain every manually added change 12 13 extern(C) enum ImDrawCallback_ResetRenderState = cast(ImDrawCallback)(-1); 14 enum IM_OFFSETOF(alias member) = member.offsetof; 15 enum IM_FREE(T)(ref T* ptr){igMemFree(cast(void*)ptr);} 16 enum IM_ALLOC(size_t size){return igMemAlloc(size);} 17 int IM_ARRAYSIZE(T)(ref T var) 18 { 19 return cast(int)((var.sizeof)/((*(var).ptr).sizeof)); //Same as var.sizeof/(*var).sizeof 20 } 21 void IM_ASSERT()(auto ref bool exp, string msg = "") 22 { 23 assert(exp, msg); 24 } 25 void IM_DELETE(T)(ref T* p){if(p)destroy(p); igMemFree(p);} 26 27 @nogc T* IM_NEW(T)() 28 { 29 import core.lifetime : emplace, forward; 30 import core.stdc.string:memset; 31 T* ptr = cast(T*)igMemAlloc(T.sizeof); 32 //*ptr = T.init; 33 return cast(T*)ptr; 34 } 35 36 //Using viewport branch right now, don't want to bother master as it is already 900 commits behind viwport 37 enum CIMGUI_VIEWPORT_BRANCH = true; 38 39 //Now the more specifics one that should not be used extensively 40 /** 41 * ImVector_reserve dependency 42 */ 43 static int _grow_capacity(T)(ref T imvec, int size) 44 { 45 int new_capacity = imvec.Capacity ? (imvec.Capacity + imvec.Capacity / 2) : 8; 46 return new_capacity > size ? new_capacity : size; 47 } 48 49 /** 50 * ImVector_resize dependency 51 */ 52 static void _reserve(T)(ref T imvec, int new_capacity) 53 { 54 if (new_capacity <= imvec.Capacity) 55 return; 56 alias vecDataType = typeof(T.Data); 57 58 vecDataType new_data = cast(vecDataType)IM_ALLOC(cast(size_t)new_capacity * T.sizeof); 59 if (imvec.Data) 60 { 61 memcpy(new_data, imvec.Data, cast(size_t)imvec.Size * T.sizeof); 62 IM_FREE(imvec.Data); 63 } 64 imvec.Data = new_data; 65 imvec.Capacity = new_capacity; 66 } 67 68 /** 69 * Reserved for using on ImVector_ 70 * Required on Impl_SDL 71 */ 72 void ImVector_resize(T)(ref T imvec, int new_size) 73 { 74 if (new_size > imvec.Capacity) 75 { 76 _reserve!T(imvec, _grow_capacity!T(imvec, new_size)); 77 imvec.Size = new_size; 78 } 79 } 80 81 /** 82 * Reserver for using on ImVector_ 83 * Required on Impl_SDL 84 * //As the data is a C array, it should be the *T.Data 85 */ 86 void ImVector_push_back(T)(ref T vec, const ref typeof(*T.Data) v) 87 { 88 if (vec.Size == vec.Capacity) 89 _reserve!T(vec, _grow_capacity!T(vec, vec.Size + 1)); 90 memcpy(&vec.Data[vec.Size], &v, v.sizeof); 91 vec.Size++; 92 } 93 94 //NOT AUTO-GENERATED, CIMGUI SPECIFIC: 95 96 static if(CIMGUI_VIEWPORT_BRANCH) 97 { 98 // NOTE: Some function pointers in the ImGuiPlatformIO structure are not C-compatible because of their 99 // use of a complex return type. To work around this, we store a custom CimguiStorage object inside 100 // ImGuiIO::BackendLanguageUserData, which contains C-compatible function pointer variants for these 101 // functions. When a user function pointer is provided, we hook up the underlying ImGuiPlatformIO 102 // function pointer to a thunk which accesses the user function pointer through CimguiStorage. 103 import bindbc.cimgui.types; 104 105 extern(C) struct CimguiStorage 106 { 107 void function (ImGuiViewport* vp, ImVec2* out_pos) Platform_GetWindowPos; 108 void function (ImGuiViewport* vp, ImVec2* out_pos) Platform_GetWindowSize; 109 } 110 extern(C) @nogc nothrow 111 { 112 alias pImGuiPlatformIO_Set_Platform_GetWindowPos = void function(ImGuiPlatformIO* platform_io, void function (ImGuiViewport* vp, ImVec2* out_pos) user_callback); 113 alias pImGuiPlatformIO_Set_Platform_GetWindowSize = void function(ImGuiPlatformIO* platform_io, void function(ImGuiViewport* vp, ImVec2* out_size) user_callback); 114 } 115 __gshared 116 { 117 pImGuiPlatformIO_Set_Platform_GetWindowPos ImGuiPlatformIO_Set_Platform_GetWindowPos; 118 pImGuiPlatformIO_Set_Platform_GetWindowSize ImGuiPlatformIO_Set_Platform_GetWindowSize; 119 } 120 import bindbc.loader:SharedLib, bindSymbol; 121 package void bindCimguiStorage(SharedLib lib) 122 { 123 lib.bindSymbol(cast(void**)&ImGuiPlatformIO_Set_Platform_GetWindowPos, "ImGuiPlatformIO_Set_Platform_GetWindowPos"); 124 lib.bindSymbol(cast(void**)&ImGuiPlatformIO_Set_Platform_GetWindowSize, "ImGuiPlatformIO_Set_Platform_GetWindowSize"); 125 } 126 }