1 /* nuklear - 1.32.0 - public domain */
2 #define COBJMACROS
3 #define WIN32_LEAN_AND_MEAN
4 #include <windows.h>
5 #include <d3d9.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <limits.h>
9 #include <time.h>
10 
11 #define WINDOW_WIDTH 800
12 #define WINDOW_HEIGHT 600
13 
14 #define NK_INCLUDE_FIXED_TYPES
15 #define NK_INCLUDE_STANDARD_IO
16 #define NK_INCLUDE_STANDARD_VARARGS
17 #define NK_INCLUDE_DEFAULT_ALLOCATOR
18 #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
19 #define NK_INCLUDE_FONT_BAKING
20 #define NK_INCLUDE_DEFAULT_FONT
21 #define NK_IMPLEMENTATION
22 #define NK_D3D9_IMPLEMENTATION
23 #include "../../nuklear.h"
24 #include "nuklear_d3d9.h"
25 
26 /* ===============================================================
27  *
28  *                          EXAMPLE
29  *
30  * ===============================================================*/
31 /* This are some code examples to provide a small overview of what can be
32  * done with this library. To try out an example uncomment the defines */
33 /*#define INCLUDE_ALL */
34 /*#define INCLUDE_STYLE */
35 /*#define INCLUDE_CALCULATOR */
36 /*#define INCLUDE_OVERVIEW */
37 /*#define INCLUDE_NODE_EDITOR */
38 
39 #ifdef INCLUDE_ALL
40   #define INCLUDE_STYLE
41   #define INCLUDE_CALCULATOR
42   #define INCLUDE_OVERVIEW
43   #define INCLUDE_NODE_EDITOR
44 #endif
45 
46 #ifdef INCLUDE_STYLE
47   #include "../style.c"
48 #endif
49 #ifdef INCLUDE_CALCULATOR
50   #include "../calculator.c"
51 #endif
52 #ifdef INCLUDE_OVERVIEW
53   #include "../overview.c"
54 #endif
55 #ifdef INCLUDE_NODE_EDITOR
56   #include "../node_editor.c"
57 #endif
58 
59 /* ===============================================================
60  *
61  *                          DEMO
62  *
63  * ===============================================================*/
64 static IDirect3DDevice9 *device;
65 static IDirect3DDevice9Ex *deviceEx;
66 static D3DPRESENT_PARAMETERS present;
67 
68 static LRESULT CALLBACK
WindowProc(HWND wnd,UINT msg,WPARAM wparam,LPARAM lparam)69 WindowProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
70 {
71     switch (msg)
72     {
73     case WM_DESTROY:
74         PostQuitMessage(0);
75         return 0;
76 
77     case WM_SIZE:
78         if (device)
79         {
80             UINT width = LOWORD(lparam);
81             UINT height = HIWORD(lparam);
82             if (width != 0 && height != 0 &&
83                 (width != present.BackBufferWidth || height != present.BackBufferHeight))
84             {
85                 nk_d3d9_release();
86                 present.BackBufferWidth = width;
87                 present.BackBufferHeight = height;
88                 HRESULT hr = IDirect3DDevice9_Reset(device, &present);
89                 NK_ASSERT(SUCCEEDED(hr));
90                 nk_d3d9_resize(width, height);
91             }
92         }
93         break;
94     }
95 
96     if (nk_d3d9_handle_event(wnd, msg, wparam, lparam))
97         return 0;
98 
99     return DefWindowProcW(wnd, msg, wparam, lparam);
100 }
101 
create_d3d9_device(HWND wnd)102 static void create_d3d9_device(HWND wnd)
103 {
104     HRESULT hr;
105 
106     present.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
107     present.BackBufferWidth = WINDOW_WIDTH;
108     present.BackBufferHeight = WINDOW_HEIGHT;
109     present.BackBufferFormat = D3DFMT_X8R8G8B8;
110     present.BackBufferCount = 1;
111     present.MultiSampleType = D3DMULTISAMPLE_NONE;
112     present.SwapEffect = D3DSWAPEFFECT_DISCARD;
113     present.hDeviceWindow = wnd;
114     present.EnableAutoDepthStencil = TRUE;
115     present.AutoDepthStencilFormat = D3DFMT_D24S8;
116     present.Flags = D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL;
117     present.Windowed = TRUE;
118 
119     {/* first try to create Direct3D9Ex device if possible (on Windows 7+) */
120         typedef HRESULT WINAPI Direct3DCreate9ExPtr(UINT, IDirect3D9Ex**);
121         Direct3DCreate9ExPtr *Direct3DCreate9Ex = (void *)GetProcAddress(GetModuleHandleA("d3d9.dll"), "Direct3DCreate9Ex");
122         if (Direct3DCreate9Ex) {
123             IDirect3D9Ex *d3d9ex;
124             if (SUCCEEDED(Direct3DCreate9Ex(D3D_SDK_VERSION, &d3d9ex))) {
125                 hr = IDirect3D9Ex_CreateDeviceEx(d3d9ex, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd,
126                     D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE | D3DCREATE_FPU_PRESERVE,
127                     &present, NULL, &deviceEx);
128                 if (SUCCEEDED(hr)) {
129                     device = (IDirect3DDevice9 *)deviceEx;
130                 } else {
131                     /* hardware vertex processing not supported, no big deal
132                     retry with software vertex processing */
133                     hr = IDirect3D9Ex_CreateDeviceEx(d3d9ex, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd,
134                         D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE | D3DCREATE_FPU_PRESERVE,
135                         &present, NULL, &deviceEx);
136                     if (SUCCEEDED(hr)) {
137                         device = (IDirect3DDevice9 *)deviceEx;
138                     }
139                 }
140                 IDirect3D9Ex_Release(d3d9ex);
141             }
142         }
143     }
144 
145     if (!device) {
146         /* otherwise do regular D3D9 setup */
147         IDirect3D9 *d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
148 
149         hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd,
150             D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE | D3DCREATE_FPU_PRESERVE,
151             &present, &device);
152         if (FAILED(hr)) {
153             /* hardware vertex processing not supported, no big deal
154             retry with software vertex processing */
155             hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd,
156                 D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE | D3DCREATE_FPU_PRESERVE,
157                 &present, &device);
158             NK_ASSERT(SUCCEEDED(hr));
159         }
160         IDirect3D9_Release(d3d9);
161     }
162 }
163 
main(void)164 int main(void)
165 {
166     struct nk_context *ctx;
167     struct nk_colorf bg;
168 
169     WNDCLASSW wc;
170     RECT rect = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
171     DWORD style = WS_OVERLAPPEDWINDOW;
172     DWORD exstyle = WS_EX_APPWINDOW;
173     HWND wnd;
174     int running = 1;
175 
176     /* Win32 */
177     memset(&wc, 0, sizeof(wc));
178     wc.style = CS_DBLCLKS;
179     wc.lpfnWndProc = WindowProc;
180     wc.hInstance = GetModuleHandleW(0);
181     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
182     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
183     wc.lpszClassName = L"NuklearWindowClass";
184     RegisterClassW(&wc);
185 
186     AdjustWindowRectEx(&rect, style, FALSE, exstyle);
187 
188     wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear Demo",
189         style | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
190         rect.right - rect.left, rect.bottom - rect.top,
191         NULL, NULL, wc.hInstance, NULL);
192 
193     create_d3d9_device(wnd);
194 
195     /* GUI */
196     ctx = nk_d3d9_init(device, WINDOW_WIDTH, WINDOW_HEIGHT);
197     /* Load Fonts: if none of these are loaded a default font will be used  */
198     /* Load Cursor: if you uncomment cursor loading please hide the cursor */
199     {struct nk_font_atlas *atlas;
200     nk_d3d9_font_stash_begin(&atlas);
201     /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../extra_font/DroidSans.ttf", 14, 0);*/
202     /*struct nk_font *robot = nk_font_atlas_add_from_file(atlas, "../../extra_font/Roboto-Regular.ttf", 14, 0);*/
203     /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
204     /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../extra_font/ProggyClean.ttf", 12, 0);*/
205     /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../extra_font/ProggyTiny.ttf", 10, 0);*/
206     /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../extra_font/Cousine-Regular.ttf", 13, 0);*/
207     nk_d3d9_font_stash_end();
208     /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
209     /*nk_style_set_font(ctx, &droid->handle)*/;}
210 
211     /* style.c */
212     #ifdef INCLUDE_STYLE
213     /*set_style(ctx, THEME_WHITE);*/
214     /*set_style(ctx, THEME_RED);*/
215     /*set_style(ctx, THEME_BLUE);*/
216     /*set_style(ctx, THEME_DARK);*/
217     #endif
218 
219     bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
220     while (running)
221     {
222         /* Input */
223         MSG msg;
224         nk_input_begin(ctx);
225         while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
226             if (msg.message == WM_QUIT)
227                 running = 0;
228             TranslateMessage(&msg);
229             DispatchMessageW(&msg);
230         }
231         nk_input_end(ctx);
232 
233         /* GUI */
234         if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
235             NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
236             NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
237         {
238             enum {EASY, HARD};
239             static int op = EASY;
240             static int property = 20;
241 
242             nk_layout_row_static(ctx, 30, 80, 1);
243             if (nk_button_label(ctx, "button"))
244                 fprintf(stdout, "button pressed\n");
245             nk_layout_row_dynamic(ctx, 30, 2);
246             if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
247             if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
248             nk_layout_row_dynamic(ctx, 22, 1);
249             nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
250 
251             nk_layout_row_dynamic(ctx, 20, 1);
252             nk_label(ctx, "background:", NK_TEXT_LEFT);
253             nk_layout_row_dynamic(ctx, 25, 1);
254             if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
255                 nk_layout_row_dynamic(ctx, 120, 1);
256                 bg = nk_color_picker(ctx, bg, NK_RGBA);
257                 nk_layout_row_dynamic(ctx, 25, 1);
258                 bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
259                 bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
260                 bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
261                 bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
262                 nk_combo_end(ctx);
263             }
264         }
265         nk_end(ctx);
266 
267         /* -------------- EXAMPLES ---------------- */
268         #ifdef INCLUDE_CALCULATOR
269           calculator(ctx);
270         #endif
271         #ifdef INCLUDE_OVERVIEW
272           overview(ctx);
273         #endif
274         #ifdef INCLUDE_NODE_EDITOR
275           node_editor(ctx);
276         #endif
277         /* ----------------------------------------- */
278 
279         /* Draw */
280         {
281             HRESULT hr;
282             hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL,
283                 D3DCOLOR_COLORVALUE(bg.a, bg.r, bg.g, bg.b), 0.0f, 0);
284             NK_ASSERT(SUCCEEDED(hr));
285 
286             hr = IDirect3DDevice9_BeginScene(device);
287             NK_ASSERT(SUCCEEDED(hr));
288             nk_d3d9_render(NK_ANTI_ALIASING_ON);
289             hr = IDirect3DDevice9_EndScene(device);
290             NK_ASSERT(SUCCEEDED(hr));
291 
292             if (deviceEx) {
293                 hr = IDirect3DDevice9Ex_PresentEx(deviceEx, NULL, NULL, NULL, NULL, 0);
294             } else {
295                 hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
296             }
297             if (hr == D3DERR_DEVICELOST || hr == D3DERR_DEVICEHUNG || hr == D3DERR_DEVICEREMOVED) {
298                 /* to recover from this, you'll need to recreate device and all the resources */
299                 MessageBoxW(NULL, L"D3D9 device is lost or removed!", L"Error", 0);
300                 break;
301             } else if (hr == S_PRESENT_OCCLUDED) {
302                 /* window is not visible, so vsync won't work. Let's sleep a bit to reduce CPU usage */
303                 Sleep(10);
304             }
305             NK_ASSERT(SUCCEEDED(hr));
306         }
307     }
308     nk_d3d9_shutdown();
309     if (deviceEx)IDirect3DDevice9Ex_Release(deviceEx);
310     else IDirect3DDevice9_Release(device);
311     UnregisterClassW(wc.lpszClassName, wc.hInstance);
312     return 0;
313 }
314