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