Skip to content

Commit

Permalink
glfw_dx11_example: add main.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
sonoro1234 committed Jun 12, 2024
1 parent 7c9687d commit b68f772
Show file tree
Hide file tree
Showing 2 changed files with 244 additions and 4 deletions.
8 changes: 4 additions & 4 deletions backend_test/example_glfw_dx11/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Project(cimgui_glfw)
Project(cimgui_glfwdx11)
cmake_minimum_required(VERSION 3.11)
if(WIN32) # to mingw work as all the others
set(CMAKE_SHARED_LIBRARY_PREFIX "")
Expand Down Expand Up @@ -92,11 +92,11 @@ target_link_libraries(cimgui ${IMGUI_LIBRARIES} glfw)

# using library
include_directories(../../generator/output/)
#add_executable(${PROJECT_NAME} main.c)
#target_compile_definitions(${PROJECT_NAME} PUBLIC -DCIMGUI_USE_DX11 -DCIMGUI_USE_GLFW)
add_executable(${PROJECT_NAME} main.cpp)
target_compile_definitions(${PROJECT_NAME} PUBLIC -DCIMGUI_USE_DX11 -DCIMGUI_USE_GLFW)
if (MINGW)
#target_link_options(${PROJECT_NAME} PRIVATE "-mconsole")
endif()
#target_link_libraries(${PROJECT_NAME} ${IMGUI_SDL_LIBRARY} cimgui)
target_link_libraries(${PROJECT_NAME} d3d11 d3dcompiler.lib cimgui)


240 changes: 240 additions & 0 deletions backend_test/example_glfw_dx11/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
#define CIMGUI_DEFINE_ENUMS_AND_STRUCTS
#include "cimgui.h"
#include "cimgui_impl.h"
#include <d3d11.h>
#include <GLFW/glfw3.h>
#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3native.h>
#include <stdio.h>
#ifdef _MSC_VER
#include <windows.h>
#endif



#ifdef IMGUI_HAS_IMSTR
#define igBegin igBegin_Str
#define igSliderFloat igSliderFloat_Str
#define igCheckbox igCheckbox_Str
#define igColorEdit3 igColorEdit3_Str
#define igButton igButton_Str
#endif

GLFWwindow *window;

// Data
static ID3D11Device* g_pd3dDevice = NULL;
static ID3D11DeviceContext* g_pd3dDeviceContext = NULL;
static IDXGISwapChain* g_pSwapChain = NULL;
static ID3D11RenderTargetView* g_mainRenderTargetView = NULL;

// Forward declarations of helper functions
bool CreateDeviceD3D(HWND hWnd);
void CleanupDeviceD3D();
void CreateRenderTarget();
void CleanupRenderTarget();

void window_size_callback(GLFWwindow* window, int width, int height)
{
CleanupRenderTarget();
g_pSwapChain->ResizeBuffers(0, width, height, DXGI_FORMAT_UNKNOWN, 0);
CreateRenderTarget();
}

int main(int argc, char *argv[])
{

if (!glfwInit())
return -1;

// Decide GL+GLSL versions
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);



// just an extra window hint for resize
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

window = glfwCreateWindow(1024, 768, "Hello World!", NULL, NULL);
if (!window)
{
printf("Failed to create window! Terminating!\n");
glfwTerminate();
return -1;
}

//
HWND hwnd = glfwGetWin32Window(window);
if (hwnd == NULL)
{
printf("Failed to get win32 window! Terminating!\n");
glfwTerminate();
return -1;
}
// Initialize Direct3D
if (!CreateDeviceD3D(hwnd))
{
CleanupDeviceD3D();
return 1;
}

glfwSetWindowSizeCallback(window, window_size_callback);

// setup imgui
igCreateContext(NULL);

// set docking
ImGuiIO *ioptr = igGetIO();
ioptr->ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
//ioptr->ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
#ifdef IMGUI_HAS_DOCK
ioptr->ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
ioptr->ConfigFlags |= ImGuiConfigFlags_ViewportsEnable; // Enable Multi-Viewport / Platform Windows
#endif

ImGui_ImplGlfw_InitForOther(window, true);
ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);

igStyleColorsDark(NULL);
// ImFontAtlas_AddFontDefault(io.Fonts, NULL);

bool showDemoWindow = true;
bool showAnotherWindow = false;
ImVec4 clearColor;
clearColor.x = 0.45f;
clearColor.y = 0.55f;
clearColor.z = 0.60f;
clearColor.w = 1.00f;

// main event loop
bool quit = false;
while (!glfwWindowShouldClose(window))
{

glfwPollEvents();

// start imgui frame
ImGui_ImplDX11_NewFrame();
ImGui_ImplGlfw_NewFrame();
igNewFrame();

if (showDemoWindow)
igShowDemoWindow(&showDemoWindow);

// show a simple window that we created ourselves.
{
static float f = 0.0f;
static int counter = 0;

igBegin("Hello, world!", NULL, 0);
igText("This is some useful text");
igCheckbox("Demo window", &showDemoWindow);
igCheckbox("Another window", &showAnotherWindow);

igSliderFloat("Float", &f, 0.0f, 1.0f, "%.3f", 0);
igColorEdit3("clear color", (float *)&clearColor, 0);

ImVec2 buttonSize;
buttonSize.x = 0;
buttonSize.y = 0;
if (igButton("Button", buttonSize))
counter++;
igSameLine(0.0f, -1.0f);
igText("counter = %d", counter);

igText("Application average %.3f ms/frame (%.1f FPS)",
1000.0f / igGetIO()->Framerate, igGetIO()->Framerate);
igEnd();
}

if (showAnotherWindow)
{
igBegin("imgui Another Window", &showAnotherWindow, 0);
igText("Hello from imgui");
ImVec2 buttonSize;
buttonSize.x = 0;
buttonSize.y = 0;
if (igButton("Close me", buttonSize)) {
showAnotherWindow = false;
}
igEnd();
}

// render
igRender();
ImGui_ImplDX11_RenderDrawData(igGetDrawData());
#ifdef IMGUI_HAS_DOCK
if (ioptr->ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
{
igUpdatePlatformWindows();
igRenderPlatformWindowsDefault(NULL, NULL);
}
#endif
g_pSwapChain->Present(1, 0); // Present with vsync
//g_pSwapChain->Present(0, 0); // Present without vsync
}

// clean up
ImGui_ImplDX11_Shutdown();
ImGui_ImplGlfw_Shutdown();
igDestroyContext(NULL);

CleanupDeviceD3D();
glfwDestroyWindow(window);
glfwTerminate();

return 0;
}

// Helper functions to use DirectX11
bool CreateDeviceD3D(HWND hWnd)
{
// Setup swap chain
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;
//createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
D3D_FEATURE_LEVEL featureLevel;
const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, };
if (D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, 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 = nullptr; }
if (g_pd3dDeviceContext) { g_pd3dDeviceContext->Release(); g_pd3dDeviceContext = nullptr; }
if (g_pd3dDevice) { g_pd3dDevice->Release(); g_pd3dDevice = nullptr; }
}

void CreateRenderTarget()
{
ID3D11Texture2D* pBackBuffer;
g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
g_pd3dDevice->CreateRenderTargetView(pBackBuffer, nullptr, &g_mainRenderTargetView);
pBackBuffer->Release();
}

void CleanupRenderTarget()
{
if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = nullptr; }
}

0 comments on commit b68f772

Please sign in to comment.