main.c 12 KB

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