-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPoliedro GUI.cpp
243 lines (191 loc) · 6.32 KB
/
Poliedro GUI.cpp
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "Includes.h"
#include "GuiRenderer.h"
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
HWND hWnd;
ImGuiContext* main_context;
D3DPRESENT_PARAMETERS d3dpp;
void create_d3d_devices( HWND hWnd, LPDIRECT3DDEVICE9& pDevice );
void render_frame( void );
void clean_d3d_devices( void );
void init_imgui( LPDIRECT3DDEVICE9 pDevice, HWND window, ImGuiContext*& Context );
void reset_devices( );
extern LRESULT ImGui_ImplWin32_WndProcHandler( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );
LRESULT CALLBACK WindowProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) {
WNDCLASSEX wc;
ZeroMemory( &wc, sizeof( WNDCLASSEX ) );
wc.cbSize = sizeof( WNDCLASSEX );
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor( NULL, IDC_ARROW );
wc.lpszClassName = "poliedro_gui";
if ( !RegisterClassExA( &wc ) )
MessageBox( 0, "Failed to register class", "", 0 );
hWnd = CreateWindowExA( NULL,
"poliedro_gui",
"gui",
WS_POPUP,
100, 100,
620, 500,
NULL,
NULL,
hInstance,
NULL );
if ( !hWnd ) {
MessageBox( 0, "Failed to create window", "", 0 );
return 0;
}
ShowWindow( hWnd, nCmdShow );
create_d3d_devices( hWnd, d3ddev );
init_imgui( d3ddev, hWnd, main_context );
MSG msg;
while ( TRUE )
{
while ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
if ( msg.message == WM_QUIT )
break;
render_frame( );
static bool bOnce = false;
if ( !bOnce ) {
ShowWindow( hWnd, false );
bOnce = true;
}
}
clean_d3d_devices( );
return msg.wParam;
}
LRESULT CALLBACK WindowProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam ) {
if ( ImGui_ImplWin32_WndProcHandler( hWnd, message, wParam, lParam ) )
return true;
POINT p;
int pos;
switch ( message )
{
case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
break;
case WM_LBUTTONDOWN:
pos = GetMessagePos( );
POINTS ps = MAKEPOINTS( pos );
p = { ps.x,ps.y };
ScreenToClient( hWnd, &p );
if ( p.y < 25 && p.x < 460 ) {
ReleaseCapture( );
SendMessage( hWnd, WM_SYSCOMMAND, 0xf012, 0 );
}
break;
case WM_SIZE:
if ( d3ddev != NULL && wParam != SIZE_MINIMIZED )
{
d3dpp.BackBufferWidth = LOWORD( lParam );
d3dpp.BackBufferHeight = HIWORD( lParam );
reset_devices( );
}
return 0;
case WM_SYSCOMMAND:
if ( ( wParam & 0xfff0 ) == SC_KEYMENU )
return 0;
break;
case WM_DPICHANGED:
if ( ImGui::GetIO( ).ConfigFlags & ImGuiConfigFlags_DpiEnableScaleViewports )
{
const RECT* suggested_rect = ( RECT* ) lParam;
::SetWindowPos( hWnd, NULL, suggested_rect->left, suggested_rect->top, suggested_rect->right - suggested_rect->left, suggested_rect->bottom - suggested_rect->top, SWP_NOZORDER | SWP_NOACTIVATE );
}
break;
}
return DefWindowProc( hWnd, message, wParam, lParam );
}
void create_d3d_devices( HWND hWnd, LPDIRECT3DDEVICE9& pDevice )
{
d3d = Direct3DCreate9( D3D_SDK_VERSION ); // create the Direct3D interface
ZeroMemory( &d3dpp, sizeof( d3dpp ) ); // clear out the struct for use
d3dpp.Windowed = TRUE; // program windowed, not fullscreen
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames
d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
// create a device class using this information and the info from the d3dpp stuct
d3d->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&pDevice );
if ( !pDevice )
MessageBox( 0, "Failed to create device", "", 0 );
}
void init_imgui( LPDIRECT3DDEVICE9 pDevice, HWND window, ImGuiContext*& Context ) {
IMGUI_CHECKVERSION( );
ImGui_ImplWin32_EnableDpiAwareness( );
Context = ImGui::CreateContext( );
ImGuiIO* io = &ImGui::GetIO( );
io->IniFilename = NULL;
io->ConfigWindowsMoveFromTitleBarOnly = true;
io->ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;
ImGui_ImplDX9_Init( pDevice );
ImGui_ImplWin32_Init( window );
ImGui::StyleColorsDark( );
ImGuiStyle* style = &ImGui::GetStyle( );
style->WindowTitleAlign = ImVec2( 0.5f, 0.5f );
style->WindowPadding = ImVec2( 15, 8 );
style->WindowRounding = 5.0f;
style->FramePadding = ImVec2( 5, 5 );
style->FrameRounding = 4.0f;
style->ItemSpacing = ImVec2( 12, 8 );
style->ItemInnerSpacing = ImVec2( 8, 6 );
style->IndentSpacing = 25.0f;
style->ScrollbarSize = 15.0f;
style->ScrollbarRounding = 9.0f;
style->GrabMinSize = 5.0f;
style->GrabRounding = 3.0f;
}
void render_frame( void )
{
d3ddev->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 255, 255, 255 ), 1.0f, 0 );
ImGui_ImplDX9_NewFrame( );
ImGui_ImplWin32_NewFrame( );
ImGui::NewFrame( );
static gui_renderer* g_render = new gui_renderer( d3ddev );
g_render->draw( );
if ( !g_render->b_draw )
SendMessage( hWnd, WM_DESTROY, 0, 0 );
ImGui::EndFrame( );
d3ddev->SetRenderState( D3DRS_ZENABLE, false );
d3ddev->SetRenderState( D3DRS_ALPHABLENDENABLE, false );
d3ddev->SetRenderState( D3DRS_SCISSORTESTENABLE, false );
ImVec4 clear_color = ImVec4( 0.45f, 0.55f, 0.60f, 1.00f );
D3DCOLOR clear_col_dx = D3DCOLOR_RGBA( ( int ) ( clear_color.x * 255.0f ), ( int ) ( clear_color.y * 255.0f ), ( int ) ( clear_color.z * 255.0f ), ( int ) ( clear_color.w * 255.0f ) );
d3ddev->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, clear_col_dx, 1.0f, 0 );
if ( d3ddev->BeginScene( ) >= 0 )
{
ImGui::Render( );
ImGui_ImplDX9_RenderDrawData( ImGui::GetDrawData( ) );
d3ddev->EndScene( );
}
ImGui::UpdatePlatformWindows( );
ImGui::RenderPlatformWindowsDefault( );
HRESULT result = d3ddev->Present( NULL, NULL, NULL, NULL );
if ( result == D3DERR_DEVICELOST && d3ddev->TestCooperativeLevel( ) == D3DERR_DEVICENOTRESET )
reset_devices( );
}
void clean_d3d_devices( void )
{
d3ddev->Release( );
d3d->Release( );
}
void reset_devices( )
{
ImGui_ImplDX9_InvalidateDeviceObjects( );
HRESULT hr = d3ddev->Reset( &d3dpp );
if ( hr == D3DERR_INVALIDCALL )
IM_ASSERT( 0 );
ImGui_ImplDX9_CreateDeviceObjects( );
}