main.c 11 KB

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