main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /* nuklear - 1.32.0 - public domain */
  2. #define WIN32_LEAN_AND_MEAN
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <limits.h>
  8. #define WINDOW_WIDTH 800
  9. #define WINDOW_HEIGHT 600
  10. #define NK_INCLUDE_FIXED_TYPES
  11. #define NK_INCLUDE_STANDARD_IO
  12. #define NK_INCLUDE_STANDARD_VARARGS
  13. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  14. #define NK_IMPLEMENTATION
  15. #define NK_GDI_IMPLEMENTATION
  16. #include "../../nuklear.h"
  17. #include "nuklear_gdi.h"
  18. /* ===============================================================
  19. *
  20. * EXAMPLE
  21. *
  22. * ===============================================================*/
  23. /* This are some code examples to provide a small overview of what can be
  24. * done with this library. To try out an example uncomment the defines */
  25. /*#define INCLUDE_ALL */
  26. /*#define INCLUDE_STYLE */
  27. /*#define INCLUDE_CALCULATOR */
  28. /*#define INCLUDE_CANVAS */
  29. #define INCLUDE_OVERVIEW
  30. /*#define INCLUDE_CONFIGURATOR */
  31. /*#define INCLUDE_NODE_EDITOR */
  32. #ifdef 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. #endif
  40. #ifdef INCLUDE_STYLE
  41. #include "../../demo/common/style.c"
  42. #endif
  43. #ifdef INCLUDE_CALCULATOR
  44. #include "../../demo/common/calculator.c"
  45. #endif
  46. #ifdef INCLUDE_CANVAS
  47. #include "../../demo/common/canvas.c"
  48. #endif
  49. #ifdef INCLUDE_OVERVIEW
  50. #include "../../demo/common/overview.c"
  51. #endif
  52. #ifdef INCLUDE_CONFIGURATOR
  53. #include "../../demo/common/style_configurator.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 LRESULT CALLBACK
  64. WindowProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
  65. {
  66. switch (msg)
  67. {
  68. case WM_DESTROY:
  69. PostQuitMessage(0);
  70. return 0;
  71. }
  72. if (nk_gdi_handle_event(wnd, msg, wparam, lparam))
  73. return 0;
  74. return DefWindowProcW(wnd, msg, wparam, lparam);
  75. }
  76. int main(void)
  77. {
  78. GdiFont* font;
  79. struct nk_context *ctx;
  80. WNDCLASSW wc;
  81. ATOM atom;
  82. RECT rect = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
  83. DWORD style = WS_OVERLAPPEDWINDOW;
  84. DWORD exstyle = WS_EX_APPWINDOW;
  85. HWND wnd;
  86. HDC dc;
  87. int running = 1;
  88. int needs_refresh = 1;
  89. #ifdef INCLUDE_CONFIGURATOR
  90. static struct nk_color color_table[NK_COLOR_COUNT];
  91. memcpy(color_table, nk_default_color_style, sizeof(color_table));
  92. #endif
  93. /* Win32 */
  94. memset(&wc, 0, sizeof(wc));
  95. wc.style = CS_DBLCLKS;
  96. wc.lpfnWndProc = WindowProc;
  97. wc.hInstance = GetModuleHandleW(0);
  98. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  99. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  100. wc.lpszClassName = L"NuklearWindowClass";
  101. atom = RegisterClassW(&wc);
  102. AdjustWindowRectEx(&rect, style, FALSE, exstyle);
  103. wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear GDI Demo",
  104. style | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
  105. rect.right - rect.left, rect.bottom - rect.top,
  106. NULL, NULL, wc.hInstance, NULL);
  107. dc = GetDC(wnd);
  108. /* GUI */
  109. font = nk_gdifont_create("Arial", 14);
  110. ctx = nk_gdi_init(font, dc, WINDOW_WIDTH, WINDOW_HEIGHT);
  111. while (running)
  112. {
  113. /* Input */
  114. MSG msg;
  115. nk_input_begin(ctx);
  116. if (needs_refresh == 0) {
  117. if (GetMessageW(&msg, NULL, 0, 0) <= 0)
  118. running = 0;
  119. else {
  120. TranslateMessage(&msg);
  121. DispatchMessageW(&msg);
  122. }
  123. needs_refresh = 1;
  124. } else needs_refresh = 0;
  125. while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
  126. if (msg.message == WM_QUIT)
  127. running = 0;
  128. TranslateMessage(&msg);
  129. DispatchMessageW(&msg);
  130. needs_refresh = 1;
  131. }
  132. nk_input_end(ctx);
  133. /* GUI */
  134. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  135. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  136. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  137. {
  138. enum {EASY, HARD};
  139. static int op = EASY;
  140. static int property = 20;
  141. nk_layout_row_static(ctx, 30, 80, 1);
  142. if (nk_button_label(ctx, "button"))
  143. fprintf(stdout, "button pressed\n");
  144. nk_layout_row_dynamic(ctx, 30, 2);
  145. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  146. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  147. nk_layout_row_dynamic(ctx, 22, 1);
  148. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  149. }
  150. nk_end(ctx);
  151. /* -------------- EXAMPLES ---------------- */
  152. #ifdef INCLUDE_CALCULATOR
  153. calculator(ctx);
  154. #endif
  155. #ifdef INCLUDE_CANVAS
  156. canvas(ctx);
  157. #endif
  158. #ifdef INCLUDE_OVERVIEW
  159. overview(ctx);
  160. #endif
  161. #ifdef INCLUDE_CONFIGURATOR
  162. style_configurator(ctx, color_table);
  163. #endif
  164. #ifdef INCLUDE_NODE_EDITOR
  165. node_editor(ctx);
  166. #endif
  167. /* ----------------------------------------- */
  168. /* Draw */
  169. nk_gdi_render(nk_rgb(30,30,30));
  170. }
  171. nk_gdifont_del(font);
  172. ReleaseDC(wnd, dc);
  173. UnregisterClassW(wc.lpszClassName, wc.hInstance);
  174. return 0;
  175. }