main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 <limits.h>
  7. #include <time.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_GDIP_IMPLEMENTATION
  16. #include "../../nuklear.h"
  17. #include "nuklear_gdip.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. case WM_DESTROY:
  68. PostQuitMessage(0);
  69. return 0;
  70. }
  71. if (nk_gdip_handle_event(wnd, msg, wparam, lparam))
  72. return 0;
  73. return DefWindowProcW(wnd, msg, wparam, lparam);
  74. }
  75. int main(void)
  76. {
  77. GdipFont* font;
  78. struct nk_context *ctx;
  79. WNDCLASSW wc;
  80. RECT rect = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
  81. DWORD style = WS_OVERLAPPEDWINDOW;
  82. DWORD exstyle = WS_EX_APPWINDOW;
  83. HWND wnd;
  84. int running = 1;
  85. int needs_refresh = 1;
  86. #ifdef INCLUDE_CONFIGURATOR
  87. static struct nk_color color_table[NK_COLOR_COUNT];
  88. memcpy(color_table, nk_default_color_style, sizeof(color_table));
  89. #endif
  90. /* Win32 */
  91. memset(&wc, 0, sizeof(wc));
  92. wc.style = CS_DBLCLKS;
  93. wc.lpfnWndProc = WindowProc;
  94. wc.hInstance = GetModuleHandleW(0);
  95. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  96. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  97. wc.lpszClassName = L"NuklearWindowClass";
  98. RegisterClassW(&wc);
  99. AdjustWindowRectEx(&rect, style, FALSE, exstyle);
  100. wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear GDI+ Demo",
  101. style | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
  102. rect.right - rect.left, rect.bottom - rect.top,
  103. NULL, NULL, wc.hInstance, NULL);
  104. /* GUI */
  105. ctx = nk_gdip_init(wnd, WINDOW_WIDTH, WINDOW_HEIGHT);
  106. font = nk_gdipfont_create("Arial", 12);
  107. nk_gdip_set_font(font);
  108. while (running)
  109. {
  110. /* Input */
  111. MSG msg;
  112. nk_input_begin(ctx);
  113. if (needs_refresh == 0) {
  114. if (GetMessageW(&msg, NULL, 0, 0) <= 0)
  115. running = 0;
  116. else {
  117. TranslateMessage(&msg);
  118. DispatchMessageW(&msg);
  119. }
  120. needs_refresh = 1;
  121. } else needs_refresh = 0;
  122. while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
  123. if (msg.message == WM_QUIT)
  124. running = 0;
  125. TranslateMessage(&msg);
  126. DispatchMessageW(&msg);
  127. needs_refresh = 1;
  128. }
  129. nk_input_end(ctx);
  130. /* GUI */
  131. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  132. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  133. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  134. {
  135. enum {EASY, HARD};
  136. static int op = EASY;
  137. static int property = 20;
  138. nk_layout_row_static(ctx, 30, 80, 1);
  139. if (nk_button_label(ctx, "button"))
  140. fprintf(stdout, "button pressed\n");
  141. nk_layout_row_dynamic(ctx, 30, 2);
  142. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  143. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  144. nk_layout_row_dynamic(ctx, 22, 1);
  145. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  146. }
  147. nk_end(ctx);
  148. /* -------------- EXAMPLES ---------------- */
  149. #ifdef INCLUDE_CALCULATOR
  150. calculator(ctx);
  151. #endif
  152. #ifdef INCLUDE_CANVAS
  153. canvas(ctx);
  154. #endif
  155. #ifdef INCLUDE_OVERVIEW
  156. overview(ctx);
  157. #endif
  158. #ifdef INCLUDE_CONFIGURATOR
  159. style_configurator(ctx, color_table);
  160. #endif
  161. #ifdef INCLUDE_NODE_EDITOR
  162. node_editor(ctx);
  163. #endif
  164. /* ----------------------------------------- */
  165. /* Draw */
  166. nk_gdip_render(NK_ANTI_ALIASING_ON, nk_rgb(30,30,30));
  167. }
  168. nk_gdipfont_del(font);
  169. nk_gdip_shutdown();
  170. UnregisterClassW(wc.lpszClassName, wc.hInstance);
  171. return 0;
  172. }