main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. #define WINDOW_WIDTH 800
  11. #define WINDOW_HEIGHT 600
  12. #define NK_INCLUDE_FIXED_TYPES
  13. #define NK_INCLUDE_STANDARD_IO
  14. #define NK_INCLUDE_STANDARD_VARARGS
  15. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  16. #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
  17. #define NK_INCLUDE_FONT_BAKING
  18. #define NK_INCLUDE_DEFAULT_FONT
  19. #define NK_IMPLEMENTATION
  20. #define NK_D3D9_IMPLEMENTATION
  21. #include "../../nuklear.h"
  22. #include "nuklear_d3d9.h"
  23. /* ===============================================================
  24. *
  25. * EXAMPLE
  26. *
  27. * ===============================================================*/
  28. /* This are some code examples to provide a small overview of what can be
  29. * done with this library. To try out an example uncomment the defines */
  30. /*#define INCLUDE_ALL */
  31. /*#define INCLUDE_STYLE */
  32. /*#define INCLUDE_CALCULATOR */
  33. /*#define INCLUDE_CANVAS */
  34. /*#define INCLUDE_OVERVIEW */
  35. /*#define INCLUDE_NODE_EDITOR */
  36. #ifdef INCLUDE_ALL
  37. #define INCLUDE_STYLE
  38. #define INCLUDE_CALCULATOR
  39. #define INCLUDE_CANVAS
  40. #define INCLUDE_OVERVIEW
  41. #define INCLUDE_NODE_EDITOR
  42. #endif
  43. #ifdef INCLUDE_STYLE
  44. #include "../../demo/common/style.c"
  45. #endif
  46. #ifdef INCLUDE_CALCULATOR
  47. #include "../../demo/common/calculator.c"
  48. #endif
  49. #ifdef INCLUDE_CANVAS
  50. #include "../../demo/common/canvas.c"
  51. #endif
  52. #ifdef INCLUDE_OVERVIEW
  53. #include "../../demo/common/overview.c"
  54. #endif
  55. #ifdef INCLUDE_NODE_EDITOR
  56. #include "../../demo/common/node_editor.c"
  57. #endif
  58. /* ===============================================================
  59. *
  60. * DEMO
  61. *
  62. * ===============================================================*/
  63. static IDirect3DDevice9 *device;
  64. static IDirect3DDevice9Ex *deviceEx;
  65. static D3DPRESENT_PARAMETERS present;
  66. static LRESULT CALLBACK
  67. WindowProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
  68. {
  69. switch (msg)
  70. {
  71. case WM_DESTROY:
  72. PostQuitMessage(0);
  73. return 0;
  74. case WM_SIZE:
  75. if (device)
  76. {
  77. UINT width = LOWORD(lparam);
  78. UINT height = HIWORD(lparam);
  79. if (width != 0 && height != 0 &&
  80. (width != present.BackBufferWidth || height != present.BackBufferHeight))
  81. {
  82. HRESULT hr;
  83. nk_d3d9_release();
  84. present.BackBufferWidth = width;
  85. present.BackBufferHeight = height;
  86. hr = IDirect3DDevice9_Reset(device, &present);
  87. NK_ASSERT(SUCCEEDED(hr));
  88. nk_d3d9_resize(width, height);
  89. }
  90. }
  91. break;
  92. }
  93. if (nk_d3d9_handle_event(wnd, msg, wparam, lparam))
  94. return 0;
  95. return DefWindowProcW(wnd, msg, wparam, lparam);
  96. }
  97. static void create_d3d9_device(HWND wnd)
  98. {
  99. HRESULT hr;
  100. present.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
  101. present.BackBufferWidth = WINDOW_WIDTH;
  102. present.BackBufferHeight = WINDOW_HEIGHT;
  103. present.BackBufferFormat = D3DFMT_X8R8G8B8;
  104. present.BackBufferCount = 1;
  105. present.MultiSampleType = D3DMULTISAMPLE_NONE;
  106. present.SwapEffect = D3DSWAPEFFECT_DISCARD;
  107. present.hDeviceWindow = wnd;
  108. present.EnableAutoDepthStencil = TRUE;
  109. present.AutoDepthStencilFormat = D3DFMT_D24S8;
  110. present.Flags = D3DPRESENTFLAG_DISCARD_DEPTHSTENCIL;
  111. present.Windowed = TRUE;
  112. {/* first try to create Direct3D9Ex device if possible (on Windows 7+) */
  113. typedef HRESULT WINAPI Direct3DCreate9ExPtr(UINT, IDirect3D9Ex**);
  114. Direct3DCreate9ExPtr *Direct3DCreate9Ex = (void *)GetProcAddress(GetModuleHandleA("d3d9.dll"), "Direct3DCreate9Ex");
  115. if (Direct3DCreate9Ex) {
  116. IDirect3D9Ex *d3d9ex;
  117. if (SUCCEEDED(Direct3DCreate9Ex(D3D_SDK_VERSION, &d3d9ex))) {
  118. hr = IDirect3D9Ex_CreateDeviceEx(d3d9ex, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd,
  119. D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE | D3DCREATE_FPU_PRESERVE,
  120. &present, NULL, &deviceEx);
  121. if (SUCCEEDED(hr)) {
  122. device = (IDirect3DDevice9 *)deviceEx;
  123. } else {
  124. /* hardware vertex processing not supported, no big deal
  125. retry with software vertex processing */
  126. hr = IDirect3D9Ex_CreateDeviceEx(d3d9ex, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd,
  127. D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE | D3DCREATE_FPU_PRESERVE,
  128. &present, NULL, &deviceEx);
  129. if (SUCCEEDED(hr)) {
  130. device = (IDirect3DDevice9 *)deviceEx;
  131. }
  132. }
  133. IDirect3D9Ex_Release(d3d9ex);
  134. }
  135. }
  136. }
  137. if (!device) {
  138. /* otherwise do regular D3D9 setup */
  139. IDirect3D9 *d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  140. hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd,
  141. D3DCREATE_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE | D3DCREATE_FPU_PRESERVE,
  142. &present, &device);
  143. if (FAILED(hr)) {
  144. /* hardware vertex processing not supported, no big deal
  145. retry with software vertex processing */
  146. hr = IDirect3D9_CreateDevice(d3d9, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wnd,
  147. D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE | D3DCREATE_FPU_PRESERVE,
  148. &present, &device);
  149. NK_ASSERT(SUCCEEDED(hr));
  150. }
  151. IDirect3D9_Release(d3d9);
  152. }
  153. }
  154. int main(void)
  155. {
  156. struct nk_context *ctx;
  157. struct nk_colorf bg;
  158. WNDCLASSW wc;
  159. RECT rect = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
  160. DWORD style = WS_OVERLAPPEDWINDOW;
  161. DWORD exstyle = WS_EX_APPWINDOW;
  162. HWND wnd;
  163. int running = 1;
  164. /* Win32 */
  165. memset(&wc, 0, sizeof(wc));
  166. wc.style = CS_DBLCLKS;
  167. wc.lpfnWndProc = WindowProc;
  168. wc.hInstance = GetModuleHandleW(0);
  169. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  170. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  171. wc.lpszClassName = L"NuklearWindowClass";
  172. RegisterClassW(&wc);
  173. AdjustWindowRectEx(&rect, style, FALSE, exstyle);
  174. wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear Direct3D 9 Demo",
  175. style | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
  176. rect.right - rect.left, rect.bottom - rect.top,
  177. NULL, NULL, wc.hInstance, NULL);
  178. create_d3d9_device(wnd);
  179. /* GUI */
  180. ctx = nk_d3d9_init(device, WINDOW_WIDTH, WINDOW_HEIGHT);
  181. /* Load Fonts: if none of these are loaded a default font will be used */
  182. /* Load Cursor: if you uncomment cursor loading please hide the cursor */
  183. {struct nk_font_atlas *atlas;
  184. nk_d3d9_font_stash_begin(&atlas);
  185. /*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../extra_font/DroidSans.ttf", 14, 0);*/
  186. /*struct nk_font *robot = nk_font_atlas_add_from_file(atlas, "../../extra_font/Roboto-Regular.ttf", 14, 0);*/
  187. /*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
  188. /*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../extra_font/ProggyClean.ttf", 12, 0);*/
  189. /*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../extra_font/ProggyTiny.ttf", 10, 0);*/
  190. /*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../extra_font/Cousine-Regular.ttf", 13, 0);*/
  191. nk_d3d9_font_stash_end();
  192. /*nk_style_load_all_cursors(ctx, atlas->cursors);*/
  193. /*nk_style_set_font(ctx, &droid->handle)*/;}
  194. bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
  195. while (running)
  196. {
  197. /* Input */
  198. MSG msg;
  199. nk_input_begin(ctx);
  200. while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
  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_NODE_EDITOR
  249. node_editor(ctx);
  250. #endif
  251. /* ----------------------------------------- */
  252. /* Draw */
  253. {
  254. HRESULT hr;
  255. hr = IDirect3DDevice9_Clear(device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER | D3DCLEAR_STENCIL,
  256. D3DCOLOR_COLORVALUE(bg.r, bg.g, bg.b, bg.a), 0.0f, 0);
  257. NK_ASSERT(SUCCEEDED(hr));
  258. hr = IDirect3DDevice9_BeginScene(device);
  259. NK_ASSERT(SUCCEEDED(hr));
  260. nk_d3d9_render(NK_ANTI_ALIASING_ON);
  261. hr = IDirect3DDevice9_EndScene(device);
  262. NK_ASSERT(SUCCEEDED(hr));
  263. if (deviceEx) {
  264. hr = IDirect3DDevice9Ex_PresentEx(deviceEx, NULL, NULL, NULL, NULL, 0);
  265. } else {
  266. hr = IDirect3DDevice9_Present(device, NULL, NULL, NULL, NULL);
  267. }
  268. if (hr == D3DERR_DEVICELOST || hr == D3DERR_DEVICEHUNG || hr == D3DERR_DEVICEREMOVED) {
  269. /* to recover from this, you'll need to recreate device and all the resources */
  270. MessageBoxW(NULL, L"D3D9 device is lost or removed!", L"Error", 0);
  271. break;
  272. } else if (hr == S_PRESENT_OCCLUDED) {
  273. /* window is not visible, so vsync won't work. Let's sleep a bit to reduce CPU usage */
  274. Sleep(10);
  275. }
  276. NK_ASSERT(SUCCEEDED(hr));
  277. }
  278. }
  279. nk_d3d9_shutdown();
  280. if (deviceEx)IDirect3DDevice9Ex_Release(deviceEx);
  281. else IDirect3DDevice9_Release(device);
  282. UnregisterClassW(wc.lpszClassName, wc.hInstance);
  283. return 0;
  284. }