-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
220 lines (182 loc) · 7.42 KB
/
main.cpp
File metadata and controls
220 lines (182 loc) · 7.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "main.h"
#include "RenderUtils.h"
#include "search.h"
// User Variables
float sizesPercentage[2] = {0.9f, 0.7f};
char buffer[256] = "";
bool debug = false;
// Private Variables & Constants
int windowWidth, windowHeight;
bool display = false;
ULONGLONG g_lastKeyTime[256] = { 0 };
bool firstFrame = false;
float scaleFactor;
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
HWND hwnd;
DWORD dwExStyle = WS_EX_TOPMOST | WS_EX_LAYERED;
extern ID3D11Device* g_pd3dDevice;
extern ID3D11DeviceContext* g_pd3dDeviceContext;
extern IDXGISwapChain* g_pSwapChain;
extern bool g_SwapChainOccluded;
extern UINT g_ResizeWidth, g_ResizeHeight;
extern ID3D11RenderTargetView* g_mainRenderTargetView;
static bool KeyPressed(int vkCode, ULONGLONG interval = 150) { // win+tab大概200ms延迟
ULONGLONG now = GetTickCount64();
if (GetAsyncKeyState(vkCode) & 0x8000) {
if (now - g_lastKeyTime[vkCode] > interval) {
g_lastKeyTime[vkCode] = now;
return true;
}
}
return false;
}
// Main code
int main(int, char**)
{
// Make process DPI aware and obtain main monitor scale
ImGui_ImplWin32_EnableDpiAwareness();
scaleFactor = ImGui_ImplWin32_GetDpiScaleForMonitor(::MonitorFromPoint(POINT{ 0, 0 }, MONITOR_DEFAULTTOPRIMARY));
screenWidth *= scaleFactor;
screenHeight *= scaleFactor;
windowWidth = screenWidth * 0.65;
windowHeight = screenHeight * 0.3;
// Create application window
WNDCLASSEXW wc = { sizeof(wc), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(nullptr), nullptr, nullptr, nullptr, nullptr, L"LocalCipher", nullptr };
::RegisterClassExW(&wc);
hwnd = ::CreateWindowExW(
dwExStyle | WS_EX_TRANSPARENT | WS_EX_NOACTIVATE,
wc.lpszClassName,
L"LocalCipher", // 窗口标题
WS_POPUP, // 无边框窗口
(screenWidth - windowWidth) / 2,
(screenHeight - windowHeight) / 2,
windowWidth,
windowHeight,
nullptr, nullptr, wc.hInstance, nullptr
);
// allowing transparent background
ImGui_ImplWin32_EnableAlphaCompositing(hwnd);
// Initialize Direct3D
if (!CreateDeviceD3D(hwnd))
{
CleanupDeviceD3D();
::UnregisterClassW(wc.lpszClassName, wc.hInstance);
return 1;
}
// Show the window
::SetLayeredWindowAttributes(hwnd, 0, 255, LWA_ALPHA);
::ShowWindow(hwnd, SW_SHOWDEFAULT);
::UpdateWindow(hwnd);
// Setup ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
// Setup Dear ImGui style
ImGui::StyleColorsDark(); // or StyleColorsLight()
// Setup scaling
ImGuiStyle& style = ImGui::GetStyle();
style.ScaleAllSizes(scaleFactor); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this requires resetting Style + calling this again)
style.FontScaleDpi = scaleFactor; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave both here for documentation purpose)
// Setup Platform/Renderer backends
ImGui_ImplWin32_Init(hwnd);
ImGui_ImplDX11_Init(g_pd3dDevice, g_pd3dDeviceContext);
// Main loop
bool done = false;
while (!done)
{
// Poll and handle messages (inputs, window resize, etc.)
// See the WndProc() function below for our to dispatch events to the Win32 backend.
MSG msg;
while (::PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
if (msg.message == WM_QUIT)
done = true;
}
if (done)
break;
// Handle window being minimized or screen locked
if (g_SwapChainOccluded && g_pSwapChain->Present(0, DXGI_PRESENT_TEST) == DXGI_STATUS_OCCLUDED)
{
::Sleep(10);
continue;
}
g_SwapChainOccluded = false;
// Handle window resize (we don't resize directly in the WM_SIZE handler)
if (g_ResizeWidth != 0 && g_ResizeHeight != 0)
{
CleanupRenderTarget();
g_pSwapChain->ResizeBuffers(0, g_ResizeWidth, g_ResizeHeight, DXGI_FORMAT_UNKNOWN, 0);
g_ResizeWidth = g_ResizeHeight = 0;
CreateRenderTarget();
}
// Start the Dear ImGui frame
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
{
ImGuiViewport* viewport = ImGui::GetMainViewport();
ImVec2 viewportSize = viewport->Size; // DX11 window size
if (KeyPressed(VK_INSERT)) {
display = !display;
if (display) {
SetWindowLongPtr(hwnd, GWL_EXSTYLE, dwExStyle);
firstFrame = true;
}
else {
SetWindowLongPtr(hwnd, GWL_EXSTYLE, dwExStyle | WS_EX_TRANSPARENT | WS_EX_NOACTIVATE);
}
}
ImGuiWindowFlags window_flags =
ImGuiWindowFlags_NoTitleBar |
ImGuiWindowFlags_NoResize |
ImGuiWindowFlags_NoCollapse |
ImGuiWindowFlags_NoMove;
if (display)
{
SetForegroundWindow(hwnd); // SetFocus | SetActiveWindow didn't work
ImVec2 nextWindowSize = ImVec2(viewportSize.x * sizesPercentage[0], viewportSize.y * sizesPercentage[1]);
ImGui::SetNextWindowSize(nextWindowSize);
ImGui::SetNextWindowPos(
ImVec2(
(viewportSize.x - nextWindowSize.x) / 2,
(viewportSize.y - nextWindowSize.y) / 2
)
);
ImGui::Begin("LocalCipherMain", nullptr, window_flags);
ImGui::DragFloat2("Sizes", sizesPercentage, 0.001f, 0.0f, 1.0f);
if (firstFrame) ImGui::SetKeyboardFocusHere(0);
if (ImGui::InputText("##input", buffer, sizeof(buffer), ImGuiInputTextFlags_EnterReturnsTrue)) {
}
ImGui::Text("%.1f FPS @ %.0f*%.0f", io.Framerate, viewportSize.x, viewportSize.y);
ImGui::SameLine();
ImGui::Checkbox("Debug", &debug);
ImGui::End();
firstFrame = false;
}
}
// Rendering
ImGui::Render();
const float fClear[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
const float fDebug[4] = { 0.0f, 0.3f, 0.3f, 0.3f };
g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, nullptr);
g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, (debug) ? fDebug : fClear);
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
// Present
HRESULT hr = g_pSwapChain->Present(1, 0); // Present with vsync
//HRESULT hr = g_pSwapChain->Present(0, 0); // Present without vsync
g_SwapChainOccluded = (hr == DXGI_STATUS_OCCLUDED);
}
// Cleanup
ImGui_ImplDX11_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
CleanupDeviceD3D();
::DestroyWindow(hwnd);
::UnregisterClassW(wc.lpszClassName, wc.hInstance);
return 0;
}