main.c 10 KB

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