Advertisement
BlueBear

opstack.cpp

Mar 21st, 2018
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.52 KB | None | 0 0
  1. // ImGui - standalone example application for DirectX 11
  2. // If you are new to ImGui, see examples/README.txt and documentation at the top of imgui.cpp.
  3.  
  4. #include "imgui.h"
  5. #include "imgui_impl_dx11.h"
  6. #include <d3d11.h>
  7. #define DIRECTINPUT_VERSION 0x0800
  8. #include <dinput.h>
  9. #include <tchar.h>
  10.  
  11. // Data
  12. static ID3D11Device*            g_pd3dDevice = NULL;
  13. static ID3D11DeviceContext*     g_pd3dDeviceContext = NULL;
  14. static IDXGISwapChain*          g_pSwapChain = NULL;
  15. static ID3D11RenderTargetView*  g_mainRenderTargetView = NULL;
  16.  
  17. void CreateRenderTarget()
  18. {
  19.     ID3D11Texture2D* pBackBuffer;
  20.     g_pSwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
  21.     g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView);
  22.     pBackBuffer->Release();
  23. }
  24.  
  25. void CleanupRenderTarget()
  26. {
  27.     if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = NULL; }
  28. }
  29.  
  30. HRESULT CreateDeviceD3D(HWND hWnd)
  31. {
  32.     // Setup swap chain
  33.     DXGI_SWAP_CHAIN_DESC sd;
  34.     ZeroMemory(&sd, sizeof(sd));
  35.     sd.BufferCount = 2;
  36.     sd.BufferDesc.Width = 0;
  37.     sd.BufferDesc.Height = 0;
  38.     sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  39.     sd.BufferDesc.RefreshRate.Numerator = 60;
  40.     sd.BufferDesc.RefreshRate.Denominator = 1;
  41.     sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
  42.     sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
  43.     sd.OutputWindow = hWnd;
  44.     sd.SampleDesc.Count = 1;
  45.     sd.SampleDesc.Quality = 0;
  46.     sd.Windowed = TRUE;
  47.     sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  48.  
  49.     UINT createDeviceFlags = 0;
  50.     //createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
  51.     D3D_FEATURE_LEVEL featureLevel;
  52.     const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, };
  53.     if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext) != S_OK)
  54.         return E_FAIL;
  55.  
  56.     CreateRenderTarget();
  57.  
  58.     return S_OK;
  59. }
  60.  
  61. void CleanupDeviceD3D()
  62. {
  63.     CleanupRenderTarget();
  64.     if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = NULL; }
  65.     if (g_pd3dDeviceContext) { g_pd3dDeviceContext->Release(); g_pd3dDeviceContext = NULL; }
  66.     if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; }
  67. }
  68.  
  69. extern LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  70. LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
  71. {
  72.     if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam))
  73.         return true;
  74.  
  75.     switch (msg)
  76.     {
  77.     case WM_SIZE:
  78.         if (g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
  79.         {
  80.             ImGui_ImplDX11_InvalidateDeviceObjects();
  81.             CleanupRenderTarget();
  82.             g_pSwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0);
  83.             CreateRenderTarget();
  84.             ImGui_ImplDX11_CreateDeviceObjects();
  85.         }
  86.         return 0;
  87.     case WM_SYSCOMMAND:
  88.         if ((wParam & 0xfff0) == SC_KEYMENU) // Disable ALT application menu
  89.             return 0;
  90.         break;
  91.     case WM_DESTROY:
  92.         PostQuitMessage(0);
  93.         return 0;
  94.     }
  95.     return DefWindowProc(hWnd, msg, wParam, lParam);
  96. }
  97.  
  98.  
  99.  
  100. #include <cmath>
  101. #include <iostream>
  102. static inline ImVec2 operator+(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x + rhs.x, lhs.y + rhs.y); }
  103. static inline ImVec2 operator-(const ImVec2& lhs, const ImVec2& rhs) { return ImVec2(lhs.x - rhs.x, lhs.y - rhs.y); }
  104.  
  105. void OperatorStack(bool* p_open)
  106. {
  107.     ImGui::SetNextWindowSize(ImVec2(700, 600), ImGuiSetCond_FirstUseEver);
  108.     if (!ImGui::Begin("Operator Stack", p_open))
  109.     {
  110.         ImGui::End();
  111.         return;
  112.     }
  113.  
  114.     static ImVec2 scrolling = ImVec2(0.0f, 0.0f);
  115.  
  116.     const float NODE_SLOT_RADIUS = 4.0f;
  117.     const ImVec2 NODE_WINDOW_PADDING(8.0f, 8.0f);
  118.  
  119.     // Create our child canvas
  120.     ImGui::Text("Hold middle mouse button to scroll (%.2f,%.2f)", scrolling.x, scrolling.y);
  121.     ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(1, 1));
  122.     ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0, 0));
  123.     ImGui::PushStyleColor(ImGuiCol_ChildWindowBg, IM_COL32(60, 60, 70, 200));
  124.     ImGui::BeginChild("scrolling_region", ImVec2(0, 0), true, ImGuiWindowFlags_NoScrollbar | ImGuiWindowFlags_NoMove);
  125.     ImGui::PushItemWidth(120.0f);
  126.  
  127.     ImVec2 offset = ImGui::GetCursorScreenPos() + scrolling;
  128.     ImDrawList* draw_list = ImGui::GetWindowDrawList();
  129.  
  130.     ImU32 GRID_COLOR = IM_COL32(200, 200, 200, 40);
  131.     ImU32 OP_COLOR = IM_COL32(150, 150, 200, 255);
  132.     float zoom = 14.0f;
  133.     ImVec2 win_pos = ImGui::GetCursorScreenPos();
  134.     std::cout << offset.x << " " << offset.y << std::endl;
  135.     ImVec2 canvas_sz = ImGui::GetWindowSize();
  136.     //grid
  137.     for (float x = fmodf(scrolling.x, zoom); x < canvas_sz.x; x += zoom)
  138.     {
  139.         draw_list->AddLine(ImVec2(x, 0.0f) + win_pos, ImVec2(x, canvas_sz.y) + win_pos, GRID_COLOR);
  140.     }
  141.     for (float y = fmodf(scrolling.y, zoom); y < canvas_sz.y; y += zoom)
  142.     {
  143.         draw_list->AddLine(ImVec2(0.0f, y) + win_pos, ImVec2(canvas_sz.x, y) + win_pos, GRID_COLOR);
  144.     }
  145.  
  146.     draw_list->AddRectFilled(ImVec2(fmodf(scrolling.x, zoom), fmodf(scrolling.x, zoom)) + win_pos, ImVec2(zoom * 4, zoom) + win_pos, OP_COLOR);
  147.  
  148.     // Scrolling
  149.     if (ImGui::IsWindowHovered() && !ImGui::IsAnyItemActive() && ImGui::IsMouseDragging(2, 0.0f))
  150.     {
  151.         scrolling = scrolling + ImGui::GetIO().MouseDelta;
  152.     }
  153.     ImGui::PopItemWidth();
  154.     ImGui::EndChild();
  155.     ImGui::PopStyleColor();
  156.     ImGui::PopStyleVar(2);
  157.  
  158.     ImGui::End();
  159. }
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167. int main(int, char**)
  168. {
  169.     // Create application window
  170.     WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("ImGui Example"), NULL };
  171.     RegisterClassEx(&wc);
  172.     HWND hwnd = CreateWindow(_T("ImGui Example"), _T("ImGui DirectX11 Example"), WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800, NULL, NULL, wc.hInstance, NULL);
  173.  
  174.     // Initialize Direct3D
  175.     if (CreateDeviceD3D(hwnd) < 0)
  176.     {
  177.         CleanupDeviceD3D();
  178.         UnregisterClass(_T("ImGui Example"), wc.hInstance);
  179.         return 1;
  180.     }
  181.  
  182.     // Show the window
  183.     ShowWindow(hwnd, SW_SHOWDEFAULT);
  184.     UpdateWindow(hwnd);
  185.  
  186.     // Setup ImGui binding
  187.     ImGui::CreateContext();
  188.     ImGuiIO& io = ImGui::GetIO(); (void)io;
  189.     //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;  // Enable Keyboard Controls
  190.     ImGui_ImplDX11_Init(hwnd, g_pd3dDevice, g_pd3dDeviceContext);
  191.  
  192.     // Setup style
  193.     ImGui::StyleColorsDark();
  194.     //ImGui::StyleColorsClassic();
  195.  
  196.     // Load Fonts
  197.     // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
  198.     // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
  199.     // - 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).
  200.     // - 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.
  201.     // - Read 'misc/fonts/README.txt' for more instructions and details.
  202.     // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
  203.     //io.Fonts->AddFontDefault();
  204.     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
  205.     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
  206.     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
  207.     //io.Fonts->AddFontFromFileTTF("../../misc/fonts/ProggyTiny.ttf", 10.0f);
  208.     //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f, NULL, io.Fonts->GetGlyphRangesJapanese());
  209.     //IM_ASSERT(font != NULL);
  210.  
  211.     bool show_demo_window = true;
  212.     bool show_another_window = false;
  213.     ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
  214.  
  215.     // Main loop
  216.     MSG msg;
  217.     ZeroMemory(&msg, sizeof(msg));
  218.     while (msg.message != WM_QUIT)
  219.     {
  220.         // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
  221.         // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application.
  222.         // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application.
  223.         // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
  224.         if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
  225.         {
  226.             TranslateMessage(&msg);
  227.             DispatchMessage(&msg);
  228.             continue;
  229.         }
  230.         ImGui_ImplDX11_NewFrame();
  231.  
  232.         // 1. Show a simple window.
  233.         // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug".
  234.         {
  235.             static float f = 0.0f;
  236.             static int counter = 0;
  237.             ImGui::Text("Hello, world!");                           // Display some text (you can use a format string too)
  238.             ImGui::SliderFloat("float", &f, 0.0f, 1.0f);            // Edit 1 float using a slider from 0.0f to 1.0f    
  239.             ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
  240.  
  241.             ImGui::Checkbox("Demo Window", &show_demo_window);      // Edit bools storing our windows open/close state
  242.             ImGui::Checkbox("Another Window", &show_another_window);
  243.  
  244.             if (ImGui::Button("Button"))                            // Buttons return true when clicked (NB: most widgets return true when edited/activated)
  245.                 counter++;
  246.             ImGui::SameLine();
  247.             ImGui::Text("counter = %d", counter);
  248.  
  249.             ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  250.         }
  251.  
  252.         // 2. Show another simple window. In most cases you will use an explicit Begin/End pair to name your windows.
  253.         if (show_another_window)
  254.         {
  255.             ImGui::Begin("Another Window", &show_another_window);
  256.             ImGui::Text("Hello from another window!");
  257.             if (ImGui::Button("Close Me"))
  258.                 show_another_window = false;
  259.             ImGui::End();
  260.         }
  261.  
  262.         // 3. Show the ImGui demo window. Most of the sample code is in ImGui::ShowDemoWindow(). Read its code to learn more about Dear ImGui!
  263.         if (show_demo_window)
  264.         {
  265.             ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiCond_FirstUseEver); // Normally user code doesn't need/want to call this because positions are saved in .ini file anyway. Here we just want to make the demo initial state a bit more friendly!
  266.             OperatorStack(&show_demo_window);
  267.         }
  268.  
  269.         // Rendering
  270.         g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
  271.         g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, (float*)&clear_color);
  272.         ImGui::Render();
  273.         ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
  274.  
  275.         g_pSwapChain->Present(1, 0); // Present with vsync
  276.         //g_pSwapChain->Present(0, 0); // Present without vsync
  277.     }
  278.  
  279.     ImGui_ImplDX11_Shutdown();
  280.     ImGui::DestroyContext();
  281.  
  282.     CleanupDeviceD3D();
  283.     UnregisterClass(_T("ImGui Example"), wc.hInstance);
  284.  
  285.     return 0;
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement