| | |
| | |
| | |
| | |
| |
|
| | #include "imgui.h" |
| | #include "imgui_impl_sdl.h" |
| | #include "imgui_impl_dx11.h" |
| | #include <d3d11.h> |
| | #include <stdio.h> |
| | #include <SDL.h> |
| | #include <SDL_syswm.h> |
| |
|
| | |
| | static ID3D11Device* g_pd3dDevice = NULL; |
| | static ID3D11DeviceContext* g_pd3dDeviceContext = NULL; |
| | static IDXGISwapChain* g_pSwapChain = NULL; |
| | static ID3D11RenderTargetView* g_mainRenderTargetView = NULL; |
| |
|
| | |
| | bool CreateDeviceD3D(HWND hWnd); |
| | void CleanupDeviceD3D(); |
| | void CreateRenderTarget(); |
| | void CleanupRenderTarget(); |
| |
|
| | |
| | int main(int, char**) |
| | { |
| | |
| | |
| | |
| | if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) |
| | { |
| | printf("Error: %s\n", SDL_GetError()); |
| | return -1; |
| | } |
| |
|
| | |
| | SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI); |
| | SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+DirectX11 example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags); |
| | SDL_SysWMinfo wmInfo; |
| | SDL_VERSION(&wmInfo.version); |
| | SDL_GetWindowWMInfo(window, &wmInfo); |
| | HWND hwnd = (HWND)wmInfo.info.win.window; |
| |
|
| | |
| | if (!CreateDeviceD3D(hwnd)) |
| | { |
| | CleanupDeviceD3D(); |
| | return 1; |
| | } |
| |
|
| | |
| | IMGUI_CHECKVERSION(); |
| | ImGui::CreateContext(); |
| | ImGuiIO& io = ImGui::GetIO(); (void)io; |
| | |
| | |
| |
|
| | |
| | ImGui::StyleColorsDark(); |
| | |
| |
|
| | |
| | ImGui_ImplSDL2_InitForD3D(window); |
| | ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext); |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | bool show_demo_window = true; |
| | bool show_another_window = false; |
| | ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
| |
|
| | |
| | bool done = false; |
| | while (!done) |
| | { |
| | |
| | |
| | |
| | |
| | |
| | SDL_Event event; |
| | while (SDL_PollEvent(&event)) |
| | { |
| | ImGui_ImplSDL2_ProcessEvent(&event); |
| | if (event.type == SDL_QUIT) |
| | done = true; |
| | if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window)) |
| | done = true; |
| | if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED && event.window.windowID == SDL_GetWindowID(window)) |
| | { |
| | |
| | CleanupRenderTarget(); |
| | g_pSwapChain->ResizeBuffers(0, 0, 0, DXGI_FORMAT_UNKNOWN, 0); |
| | CreateRenderTarget(); |
| | } |
| | } |
| |
|
| | |
| | ImGui_ImplDX11_NewFrame(); |
| | ImGui_ImplSDL2_NewFrame(); |
| | ImGui::NewFrame(); |
| |
|
| | |
| | if (show_demo_window) |
| | ImGui::ShowDemoWindow(&show_demo_window); |
| |
|
| | |
| | { |
| | static float f = 0.0f; |
| | static int counter = 0; |
| |
|
| | ImGui::Begin("Hello, world!"); |
| |
|
| | ImGui::Text("This is some useful text."); |
| | ImGui::Checkbox("Demo Window", &show_demo_window); |
| | ImGui::Checkbox("Another Window", &show_another_window); |
| |
|
| | ImGui::SliderFloat("float", &f, 0.0f, 1.0f); |
| | ImGui::ColorEdit3("clear color", (float*)&clear_color); |
| |
|
| | if (ImGui::Button("Button")) |
| | counter++; |
| | ImGui::SameLine(); |
| | ImGui::Text("counter = %d", counter); |
| |
|
| | ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); |
| | ImGui::End(); |
| | } |
| |
|
| | |
| | if (show_another_window) |
| | { |
| | ImGui::Begin("Another Window", &show_another_window); |
| | ImGui::Text("Hello from another window!"); |
| | if (ImGui::Button("Close Me")) |
| | show_another_window = false; |
| | ImGui::End(); |
| | } |
| |
|
| | |
| | ImGui::Render(); |
| | const float clear_color_with_alpha[4] = { clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w }; |
| | g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL); |
| | g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha); |
| | ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData()); |
| |
|
| | g_pSwapChain->Present(1, 0); |
| | |
| | } |
| |
|
| | |
| | ImGui_ImplDX11_Shutdown(); |
| | ImGui_ImplSDL2_Shutdown(); |
| | ImGui::DestroyContext(); |
| |
|
| | CleanupDeviceD3D(); |
| | SDL_DestroyWindow(window); |
| | SDL_Quit(); |
| |
|
| | return 0; |
| | } |
| |
|
| | |
| |
|
| | bool CreateDeviceD3D(HWND hWnd) |
| | { |
| | |
| | DXGI_SWAP_CHAIN_DESC sd; |
| | ZeroMemory(&sd, sizeof(sd)); |
| | sd.BufferCount = 2; |
| | sd.BufferDesc.Width = 0; |
| | sd.BufferDesc.Height = 0; |
| | sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; |
| | sd.BufferDesc.RefreshRate.Numerator = 60; |
| | sd.BufferDesc.RefreshRate.Denominator = 1; |
| | sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; |
| | sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; |
| | sd.OutputWindow = hWnd; |
| | sd.SampleDesc.Count = 1; |
| | sd.SampleDesc.Quality = 0; |
| | sd.Windowed = TRUE; |
| | sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; |
| |
|
| | UINT createDeviceFlags = 0; |
| | |
| | D3D_FEATURE_LEVEL featureLevel; |
| | const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, }; |
| | if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_pd3dDevice, &featureLevel, &g_pd3dDeviceContext) != S_OK) |
| | return false; |
| |
|
| | CreateRenderTarget(); |
| | return true; |
| | } |
| |
|
| | void CleanupDeviceD3D() |
| | { |
| | CleanupRenderTarget(); |
| | if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = NULL; } |
| | if (g_pd3dDeviceContext) { g_pd3dDeviceContext->Release(); g_pd3dDeviceContext = NULL; } |
| | if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = NULL; } |
| | } |
| |
|
| | void CreateRenderTarget() |
| | { |
| | ID3D11Texture2D* pBackBuffer; |
| | g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer)); |
| | g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView); |
| | pBackBuffer->Release(); |
| | } |
| |
|
| | void CleanupRenderTarget() |
| | { |
| | if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = NULL; } |
| | } |
| |
|