1 2 module app; 3 4 import std.stdio; 5 static import std.format; 6 import bindbc.sdl, 7 bindbc.sdl.dynload, 8 bindbc.imgui.dynload, 9 bindbc.imgui.bind.imgui, 10 bindbc.opengl; 11 12 import bindbc.imgui.ogl; 13 14 void main() 15 { 16 loadSDL(); 17 loadImGui(); 18 19 const char* glsl_version = "#version 130"; 20 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0); 21 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); 22 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); 23 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0); 24 25 // Create window with graphics context 26 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); 27 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); 28 SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); 29 SDL_WindowFlags window_flags = cast(SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE /*| SDL_WINDOW_ALLOW_HIGHDPI*/); 30 SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+OpenGL3 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags); 31 SDL_GLContext gl_context = SDL_GL_CreateContext(window); 32 SDL_GL_MakeCurrent(window, gl_context); 33 SDL_GL_SetSwapInterval(1); // Enable vsync 34 35 loadOpenGL(); 36 //InitOpenGLForImGui(); 37 38 39 40 41 // Setup Dear ImGui context 42 //IMGUI_CHECKVERSION(); 43 44 45 igCreateContext(null); 46 ImGuiIO* io = igGetIO(); 47 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls 48 //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls 49 io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking 50 //io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows 51 52 // Setup Dear ImGui style 53 igStyleColorsDark(null); 54 //igStyleColorsClassic(); 55 56 // Setup Platform/Renderer backends 57 ImGui_ImplSDL2_InitForOpenGL(window, gl_context); 58 ImGuiOpenGLBackend.init(glsl_version); 59 60 // Load Fonts 61 // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use igPushFont()/PopFont() to select them. 62 // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple. 63 // - If the file cannot be loaded, the function will return null. Please handle those errors in your application (e.g. use an assertion, or display an error and quit). 64 // - The fonts will be rasterized at a given size (w/ oversampling) and stored into a texture when calling ImFontAtlas::Build()/GetTexDataAsXXXX(), which ImGui_ImplXXXX_NewFrame below will call. 65 // - Read 'docs/FONTS.md' for more instructions and details. 66 // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ ! 67 //io.Fonts.AddFontDefault(); 68 //io.Fonts.AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f); 69 //io.Fonts.AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f); 70 //io.Fonts.AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f); 71 //io.Fonts.AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f); 72 //ImFont* font = io.Fonts.AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, null, io.Fonts.GetGlyphRangesJapanese()); 73 //IM_ASSERT(font != null); 74 75 // Our state 76 bool show_demo_window = true; 77 bool show_another_window = false; 78 ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); 79 80 // Main loop 81 bool done = false; 82 while (!done) 83 { 84 // Poll and handle events (inputs, window resize, etc.) 85 // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs. 86 // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application. 87 // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application. 88 // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags. 89 SDL_Event event; 90 while (SDL_PollEvent(&event)) 91 { 92 ImGui_ImplSDL2_ProcessEvent(&event); 93 if (event.type == SDL_QUIT) 94 done = true; 95 if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) 96 done = true; 97 } 98 99 // Start the Dear ImGui frame 100 ImGuiOpenGLBackend.new_frame(); 101 ImGui_ImplSDL2_NewFrame(window); 102 igNewFrame(); 103 104 igDockSpaceOverViewport(null, cast(ImGuiDockNodeFlags)0, null); 105 106 // 1. Show the big demo window (Most of the sample code is in igShowDemoWindow()! You can browse its code to learn more about Dear ImGui!). 107 if (show_demo_window) 108 igShowDemoWindow(&show_demo_window); 109 110 // 2. Show a simple window that we create ourselves. We use a Begin/End pair to created a named window. 111 { 112 static float f = 0.0f; 113 static int counter = 0; 114 115 igBegin("Hello, world!", null, cast(ImGuiWindowFlags)0); // Create a window called "Hello, world!" and append into it. 116 117 igText("This is some useful text."); // Display some text (you can use a format strings too) 118 igCheckbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state 119 igCheckbox("Another Window", &show_another_window); 120 121 igSliderFloat("float", &f, 0.0f, 1.0f, null, 0); // Edit 1 float using a slider from 0.0f to 1.0f 122 //igColorEdit3("clear color", cast(float*)&clear_color.x); // Edit 3 floats representing a color 123 124 if (igButton("Button", ImVec2(0,0))) // Buttons return true when clicked (most widgets return true when edited/activated) 125 counter++; 126 igSameLine(0,0); 127 igText("counter = %d", counter); 128 129 igText("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / igGetIO().Framerate, igGetIO().Framerate); 130 igEnd(); 131 } 132 133 // 3. Show another simple window. 134 if (show_another_window) 135 { 136 igBegin("Another Window", &show_another_window, 0); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked) 137 igText("Hello from another window!"); 138 if (igButton("Close Me", ImVec2(0,0))) 139 show_another_window = false; 140 igEnd(); 141 } 142 143 // Rendering 144 igRender(); 145 glViewport(0, 0, cast(int)io.DisplaySize.x, cast(int)io.DisplaySize.y); 146 glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w); 147 glClear(GL_COLOR_BUFFER_BIT); 148 ImGuiOpenGLBackend.render_draw_data(igGetDrawData()); 149 SDL_GL_SwapWindow(window); 150 igUpdatePlatformWindows(); 151 } 152 153 // Cleanup 154 ImGuiOpenGLBackend.shutdown(); 155 ImGui_ImplSDL2_Shutdown(); 156 igDestroyContext(null); 157 158 SDL_GL_DeleteContext(gl_context); 159 SDL_DestroyWindow(window); 160 SDL_Quit(); 161 162 writeln("Hello, world without explicit compilations!"); 163 }