我正在关注TheCherno游戏引擎系列,我尝试为DirectX和Win32实现他的代码(他使用glfw和openGL),但是由于某种原因,我的代码除了Windows窗口外没有显示任何内容。我尝试检查过针对win32和DirectX的imGui的其他教师实现,但仍然没有任何UI。 有人可以向我解释因为没有编译错误而我在做什么错?您认为实现imGui的最佳快速方法是什么?
#include "pch.h"
#include "ImGuiLayer.h"
#include "imgui.h"
#include "Platform/DirectX11/imgui_impl_dx11.h"
#include "Platform/Windows/imgui_impl_win32.h"
#include "DirectX11.h"
#include "Engine/App.h"
Engine::ImGuiLayer::ImGuiLayer()
:
Layer("ImGuiLayer")
{
}
Engine::ImGuiLayer::~ImGuiLayer()
{
}
void Engine::ImGuiLayer::OnAttach()
{
IMGUI_CHECKVERSION();
ImGui::StyleColorsDark();
ImGuiIO& io = ImGui::GetIO();
io.BackendFlags |= ImGuiBackendFlags_HasMouseCursors;
io.BackendFlags |= ImGuiBackendFlags_HasSetMousePos ;
io.KeyMap[ImGuiKey_Tab] = VK_TAB;
io.KeyMap[ImGuiKey_LeftArrow] = VK_LEFT;
io.KeyMap[ImGuiKey_RightArrow] = VK_RIGHT;
io.KeyMap[ImGuiKey_UpArrow] = VK_UP;
io.KeyMap[ImGuiKey_DownArrow] = VK_DOWN;
io.KeyMap[ImGuiKey_PageUp] = VK_PRIOR;
io.KeyMap[ImGuiKey_PageDown] = VK_NEXT;
io.KeyMap[ImGuiKey_Home] = VK_HOME;
io.KeyMap[ImGuiKey_End] = VK_END;
io.KeyMap[ImGuiKey_Insert] = VK_INSERT;
io.KeyMap[ImGuiKey_Delete] = VK_DELETE;
io.KeyMap[ImGuiKey_Backspace] = VK_BACK;
io.KeyMap[ImGuiKey_Space] = VK_SPACE;
io.KeyMap[ImGuiKey_Enter] = VK_RETURN;
io.KeyMap[ImGuiKey_Escape] = VK_ESCAPE;
io.KeyMap[ImGuiKey_KeyPadEnter] = VK_RETURN;
io.KeyMap[ImGuiKey_A] = 'A';
io.KeyMap[ImGuiKey_C] = 'C';
io.KeyMap[ImGuiKey_V] = 'V';
io.KeyMap[ImGuiKey_X] = 'X';
io.KeyMap[ImGuiKey_Y] = 'Y';
io.KeyMap[ImGuiKey_Z] = 'Z';
//ENG_ASSERT(ImGui_ImplDX11_Init(DirectX11::Graphics::Get_Device(), DirectX11::Graphics::Get_Context()), "Failed to initialize directX11 ImGui");
ImGui_ImplDX11_Init(DirectX11::Graphics::Get_Device(), DirectX11::Graphics::Get_Context()), "Failed to initialize directX11 ImGui";
}
void Engine::ImGuiLayer::OnDetach()
{
}
void Engine::ImGuiLayer::OnUpdate()
{
ImGuiIO& io = ImGui::GetIO();
App& app = App::Get();
io.DisplaySize = ImVec2(app.GetWindow().GetWidth(), app.GetWindow().GetHeight());
float time = (float)DirectX11::Get_Time();
io.DeltaTime = m_Time > 0.0f ? (time - m_Time) : (1.0f / 60.0f);
m_Time = time;
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
static bool show = true;
ImGui::ShowDemoWindow(&show);
ImGui::Render();
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
}
void Engine::ImGuiLayer::OnEvent(Event& event)
{
}
还有我的窗口
#include "pch.h"
#include "WindowsWindow.h"
#include "imgui.h"
#include "imgui_impl_win32.h"
void Engine::WindowsWindow::Init(const WindowProps& props)
{
m_Data.Title = props.Title;
m_Data.Width = props.Width;
m_Data.Height = props.Height;
// calculate window size based on desired client region size
RECT wr;
wr.left = 100;
wr.right = m_Data.Width + wr.left;
wr.top = 100;
wr.bottom = m_Data.Height + wr.top;
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);
//String to LPCWSTR
std::string s = m_Data.Title;
int wchars_num = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, NULL, 0);
wchar_t* wstr = new wchar_t[wchars_num];
MultiByteToWideChar(CP_UTF8, 0, s.c_str(), -1, wstr, wchars_num);
LPCWSTR tempTitle = std::wstring(m_Data.Title.begin(), m_Data.Title.end()).c_str();
hWnd = CreateWindow(wc.name, wstr, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, wr.right - wr.left, wr.bottom - wr.top, nullptr, nullptr, wc.hInst, this);
delete wstr;
ENG_CORE_ASSERT(hWnd, "Window creation");
ShowWindow(hWnd, SW_SHOWDEFAULT);
ImGui::CreateContext();
ImGui_ImplWin32_Init(hWnd);
DirectX11::Graphics::Init(hWnd);
}