1 /* nuklear - 1.32.0 - public domain */
2 #define COBJMACROS
3 #define WIN32_LEAN_AND_MEAN
4 #include <windows.h>
5 #include <d3d11.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 MAX_VERTEX_BUFFER 512 * 1024
15 #define MAX_INDEX_BUFFER 128 * 1024
16
17 #define NK_INCLUDE_FIXED_TYPES
18 #define NK_INCLUDE_STANDARD_IO
test_fn(double d,double * double_ptr)19 #define NK_INCLUDE_STANDARD_VARARGS
20 #define NK_INCLUDE_DEFAULT_ALLOCATOR
21 #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
22 #define NK_INCLUDE_FONT_BAKING
23 #define NK_INCLUDE_DEFAULT_FONT
24 #define NK_IMPLEMENTATION
25 #define NK_D3D11_IMPLEMENTATION
26 #include "../../nuklear.h"
27 #include "nuklear_d3d11.h"
28
29 /* ===============================================================
30 *
31 * EXAMPLE
32 *
33 * ===============================================================*/
34 /* This are some code examples to provide a small overview of what can be
35 * done with this library. To try out an example uncomment the defines */
36 /*#define INCLUDE_ALL */
37 /*#define INCLUDE_STYLE */
38 /*#define INCLUDE_CALCULATOR */
39 /*#define INCLUDE_OVERVIEW */
40 /*#define INCLUDE_NODE_EDITOR */
41
42 #ifdef INCLUDE_ALL
43 #define INCLUDE_STYLE
44 #define INCLUDE_CALCULATOR
45 #define INCLUDE_OVERVIEW
46 #define INCLUDE_NODE_EDITOR
47 #endif
48
49 #ifdef INCLUDE_STYLE
50 #include "../style.c"
51 #endif
52 #ifdef INCLUDE_CALCULATOR
53 #include "../calculator.c"
54 #endif
55 #ifdef INCLUDE_OVERVIEW
56 #include "../overview.c"
57 #endif
58 #ifdef INCLUDE_NODE_EDITOR
59 #include "../node_editor.c"
60 #endif
61
62 /* ===============================================================
63 *
64 * DEMO
65 *
66 * ===============================================================*/
67 static IDXGISwapChain *swap_chain;
68 static ID3D11Device *device;
69 static ID3D11DeviceContext *context;
70 static ID3D11RenderTargetView* rt_view;
71
72 static void
73 set_swap_chain_size(int width, int height)
74 {
75 ID3D11Texture2D *back_buffer;
76 D3D11_RENDER_TARGET_VIEW_DESC desc;
77 HRESULT hr;
78
79 if (rt_view)
80 ID3D11RenderTargetView_Release(rt_view);
81
82 ID3D11DeviceContext_OMSetRenderTargets(context, 0, NULL, NULL);
83
84 hr = IDXGISwapChain_ResizeBuffers(swap_chain, 0, width, height, DXGI_FORMAT_UNKNOWN, 0);
85 if (hr == DXGI_ERROR_DEVICE_REMOVED || hr == DXGI_ERROR_DEVICE_RESET || hr == DXGI_ERROR_DRIVER_INTERNAL_ERROR)
86 {
87 /* to recover from this, you'll need to recreate device and all the resources */
88 MessageBoxW(NULL, L"DXGI device is removed or reset!", L"Error", 0);
89 exit(0);
90 }
91 assert(SUCCEEDED(hr));
92
93 memset(&desc, 0, sizeof(desc));
94 desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
95 desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
96
97 hr = IDXGISwapChain_GetBuffer(swap_chain, 0, &IID_ID3D11Texture2D, (void **)&back_buffer);
98 assert(SUCCEEDED(hr));
99
100 hr = ID3D11Device_CreateRenderTargetView(device, (ID3D11Resource *)back_buffer, &desc, &rt_view);
101 assert(SUCCEEDED(hr));
102
103 ID3D11Texture2D_Release(back_buffer);
104 }
105
106 static LRESULT CALLBACK
107 WindowProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
108 {
109 switch (msg)
110 {
111 case WM_DESTROY:
112 PostQuitMessage(0);
113 return 0;
114
115 case WM_SIZE:
116 if (swap_chain)
117 {
118 int width = LOWORD(lparam);
119 int height = HIWORD(lparam);
120 set_swap_chain_size(width, height);
121 nk_d3d11_resize(context, width, height);
122 }
123 break;
124 }
125
126 if (nk_d3d11_handle_event(wnd, msg, wparam, lparam))
127 return 0;
128
129 return DefWindowProcW(wnd, msg, wparam, lparam);
130 }
131
132 int main(void)
133 {
134 struct nk_context *ctx;
135 struct nk_colorf bg;
136
137 WNDCLASSW wc;
138 RECT rect = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
139 DWORD style = WS_OVERLAPPEDWINDOW;
140 DWORD exstyle = WS_EX_APPWINDOW;
141 HWND wnd;
142 int running = 1;
143 HRESULT hr;
144 D3D_FEATURE_LEVEL feature_level;
145 DXGI_SWAP_CHAIN_DESC swap_chain_desc;
146
147 /* Win32 */
148 memset(&wc, 0, sizeof(wc));
149 wc.style = CS_DBLCLKS;
150 wc.lpfnWndProc = WindowProc;
151 wc.hInstance = GetModuleHandleW(0);
152 wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
153 wc.hCursor = LoadCursor(NULL, IDC_ARROW);
154 wc.lpszClassName = L"NuklearWindowClass";
155 RegisterClassW(&wc);
156
157 AdjustWindowRectEx(&rect, style, FALSE, exstyle);
158
159 wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear Demo",
160 style | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
161 rect.right - rect.left, rect.bottom - rect.top,
162 NULL, NULL, wc.hInstance, NULL);
163
164 /* D3D11 setup */
165 memset(&swap_chain_desc, 0, sizeof(swap_chain_desc));
166 swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
167 swap_chain_desc.BufferDesc.RefreshRate.Numerator = 60;
168 swap_chain_desc.BufferDesc.RefreshRate.Denominator = 1;
169 swap_chain_desc.SampleDesc.Count = 1;
170 swap_chain_desc.SampleDesc.Quality = 0;
171 swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
172 swap_chain_desc.BufferCount = 1;
173 swap_chain_desc.OutputWindow = wnd;
174 swap_chain_desc.Windowed = TRUE;
175 swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
176 swap_chain_desc.Flags = 0;
177 if (FAILED(D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE,
178 NULL, 0, NULL, 0, D3D11_SDK_VERSION, &swap_chain_desc,
179 &swap_chain, &device, &feature_level, &context)))
180 {
181 /* if hardware device fails, then try WARP high-performance
182 software rasterizer, this is useful for RDP sessions */
183 hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_WARP,
184 NULL, 0, NULL, 0, D3D11_SDK_VERSION, &swap_chain_desc,
185 &swap_chain, &device, &feature_level, &context);
186 assert(SUCCEEDED(hr));
187 }
188 set_swap_chain_size(WINDOW_WIDTH, WINDOW_HEIGHT);
189
190 /* GUI */
191 ctx = nk_d3d11_init(device, WINDOW_WIDTH, WINDOW_HEIGHT, MAX_VERTEX_BUFFER, MAX_INDEX_BUFFER);
192 /* Load Fonts: if none of these are loaded a default font will be used */
193 /* Load Cursor: if you uncomment cursor loading please hide the cursor */
194 {struct nk_font_atlas *atlas;
195 nk_d3d11_font_stash_begin(&atlas);
196 /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../extra_font/DroidSans.ttf", 14, 0);*/
197 /*struct nk_font *robot = nk_font_atlas_add_from_file(atlas, "../../extra_font/Roboto-Regular.ttf", 14, 0);*/
198 /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
199 /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../extra_font/ProggyClean.ttf", 12, 0);*/
200 /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../extra_font/ProggyTiny.ttf", 10, 0);*/
201 /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../extra_font/Cousine-Regular.ttf", 13, 0);*/
202 nk_d3d11_font_stash_end();
203 /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
204 /*nk_style_set_font(ctx, &droid->handle)*/;}
205
206 /* style.c */
207 #ifdef INCLUDE_STYLE
208 /*set_style(ctx, THEME_WHITE);*/
209 /*set_style(ctx, THEME_RED);*/
210 /*set_style(ctx, THEME_BLUE);*/
211 /*set_style(ctx, THEME_DARK);*/
212 #endif
213
214 bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
215 while (running)
216 {
217 /* Input */
218 MSG msg;
219 nk_input_begin(ctx);
220 while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
221 {
222 if (msg.message == WM_QUIT)
223 running = 0;
224 TranslateMessage(&msg);
225 DispatchMessageW(&msg);
226 }
227 nk_input_end(ctx);
228
229 /* GUI */
230 if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
231 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
232 NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
233 {
234 enum {EASY, HARD};
235 static int op = EASY;
236 static int property = 20;
237
238 nk_layout_row_static(ctx, 30, 80, 1);
239 if (nk_button_label(ctx, "button"))
240 fprintf(stdout, "button pressed\n");
241 nk_layout_row_dynamic(ctx, 30, 2);
242 if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
243 if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
244 nk_layout_row_dynamic(ctx, 22, 1);
245 nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
246
247 nk_layout_row_dynamic(ctx, 20, 1);
248 nk_label(ctx, "background:", NK_TEXT_LEFT);
249 nk_layout_row_dynamic(ctx, 25, 1);
250 if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
251 nk_layout_row_dynamic(ctx, 120, 1);
252 bg = nk_color_picker(ctx, bg, NK_RGBA);
253 nk_layout_row_dynamic(ctx, 25, 1);
254 bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
255 bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
256 bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
257 bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
258 nk_combo_end(ctx);
259 }
260 }
261 nk_end(ctx);
262
263 /* -------------- EXAMPLES ---------------- */
264 #ifdef INCLUDE_CALCULATOR
265 calculator(ctx);
266 #endif
267 #ifdef INCLUDE_OVERVIEW
268 overview(ctx);
269 #endif
270 #ifdef INCLUDE_NODE_EDITOR
271 node_editor(ctx);
272 #endif
273 /* ----------------------------------------- */
274
275 /* Draw */
276 ID3D11DeviceContext_ClearRenderTargetView(context, rt_view, &bg.r);
277 ID3D11DeviceContext_OMSetRenderTargets(context, 1, &rt_view, NULL);
278 nk_d3d11_render(context, NK_ANTI_ALIASING_ON);
279 hr = IDXGISwapChain_Present(swap_chain, 1, 0);
280 if (hr == DXGI_ERROR_DEVICE_RESET || hr == DXGI_ERROR_DEVICE_REMOVED) {
281 /* to recover from this, you'll need to recreate device and all the resources */
282 MessageBoxW(NULL, L"D3D11 device is lost or removed!", L"Error", 0);
283 break;
284 } else if (hr == DXGI_STATUS_OCCLUDED) {
285 /* window is not visible, so vsync won't work. Let's sleep a bit to reduce CPU usage */
286 Sleep(10);
287 }
288 assert(SUCCEEDED(hr));
289 }
290
291 ID3D11DeviceContext_ClearState(context);
292 nk_d3d11_shutdown();
293 ID3D11ShaderResourceView_Release(rt_view);
294 ID3D11DeviceContext_Release(context);
295 ID3D11Device_Release(device);
296 IDXGISwapChain_Release(swap_chain);
297 UnregisterClassW(wc.lpszClassName, wc.hInstance);
298 return 0;
299 }
300