main.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_NODE_EDITOR */
  31. #ifdef INCLUDE_ALL
  32. #define INCLUDE_STYLE
  33. #define INCLUDE_CALCULATOR
  34. #define INCLUDE_CANVAS
  35. #define INCLUDE_OVERVIEW
  36. #define INCLUDE_NODE_EDITOR
  37. #endif
  38. #ifdef INCLUDE_STYLE
  39. #include "../../demo/common/style.c"
  40. #endif
  41. #ifdef INCLUDE_CALCULATOR
  42. #include "../../demo/common/calculator.c"
  43. #endif
  44. #ifdef INCLUDE_CANVAS
  45. #include "../../demo/common/canvas.c"
  46. #endif
  47. #ifdef INCLUDE_OVERVIEW
  48. #include "../../demo/common/overview.c"
  49. #endif
  50. #ifdef INCLUDE_NODE_EDITOR
  51. #include "../../demo/common/node_editor.c"
  52. #endif
  53. /* ===============================================================
  54. *
  55. * DEMO
  56. *
  57. * ===============================================================*/
  58. static LRESULT CALLBACK
  59. WindowProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
  60. {
  61. switch (msg)
  62. {
  63. case WM_DESTROY:
  64. PostQuitMessage(0);
  65. return 0;
  66. }
  67. if (nk_gdi_handle_event(wnd, msg, wparam, lparam))
  68. return 0;
  69. return DefWindowProcW(wnd, msg, wparam, lparam);
  70. }
  71. int main(void)
  72. {
  73. GdiFont* font;
  74. struct nk_context *ctx;
  75. WNDCLASSW wc;
  76. ATOM atom;
  77. RECT rect = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
  78. DWORD style = WS_OVERLAPPEDWINDOW;
  79. DWORD exstyle = WS_EX_APPWINDOW;
  80. HWND wnd;
  81. HDC dc;
  82. int running = 1;
  83. int needs_refresh = 1;
  84. /* Win32 */
  85. memset(&wc, 0, sizeof(wc));
  86. wc.style = CS_DBLCLKS;
  87. wc.lpfnWndProc = WindowProc;
  88. wc.hInstance = GetModuleHandleW(0);
  89. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  90. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  91. wc.lpszClassName = L"NuklearWindowClass";
  92. atom = RegisterClassW(&wc);
  93. AdjustWindowRectEx(&rect, style, FALSE, exstyle);
  94. wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear GDI Demo",
  95. style | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
  96. rect.right - rect.left, rect.bottom - rect.top,
  97. NULL, NULL, wc.hInstance, NULL);
  98. dc = GetDC(wnd);
  99. /* GUI */
  100. font = nk_gdifont_create("Arial", 14);
  101. ctx = nk_gdi_init(font, dc, WINDOW_WIDTH, WINDOW_HEIGHT);
  102. /* style.c */
  103. #ifdef INCLUDE_STYLE
  104. /* ease regression testing during Nuklear release process; not needed for anything else */
  105. #ifdef STYLE_WHITE
  106. set_style(ctx, THEME_WHITE);
  107. #elif defined(STYLE_RED)
  108. set_style(ctx, THEME_RED);
  109. #elif defined(STYLE_BLUE)
  110. set_style(ctx, THEME_BLUE);
  111. #elif defined(STYLE_DARK)
  112. set_style(ctx, THEME_DARK);
  113. #endif
  114. #endif
  115. while (running)
  116. {
  117. /* Input */
  118. MSG msg;
  119. nk_input_begin(ctx);
  120. if (needs_refresh == 0) {
  121. if (GetMessageW(&msg, NULL, 0, 0) <= 0)
  122. running = 0;
  123. else {
  124. TranslateMessage(&msg);
  125. DispatchMessageW(&msg);
  126. }
  127. needs_refresh = 1;
  128. } else needs_refresh = 0;
  129. while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
  130. if (msg.message == WM_QUIT)
  131. running = 0;
  132. TranslateMessage(&msg);
  133. DispatchMessageW(&msg);
  134. needs_refresh = 1;
  135. }
  136. nk_input_end(ctx);
  137. /* GUI */
  138. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  139. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  140. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  141. {
  142. enum {EASY, HARD};
  143. static int op = EASY;
  144. static int property = 20;
  145. nk_layout_row_static(ctx, 30, 80, 1);
  146. if (nk_button_label(ctx, "button"))
  147. fprintf(stdout, "button pressed\n");
  148. nk_layout_row_dynamic(ctx, 30, 2);
  149. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  150. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  151. nk_layout_row_dynamic(ctx, 22, 1);
  152. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  153. }
  154. nk_end(ctx);
  155. /* -------------- EXAMPLES ---------------- */
  156. #ifdef INCLUDE_CALCULATOR
  157. calculator(ctx);
  158. #endif
  159. #ifdef INCLUDE_CANVAS
  160. canvas(ctx);
  161. #endif
  162. #ifdef INCLUDE_OVERVIEW
  163. overview(ctx);
  164. #endif
  165. #ifdef INCLUDE_NODE_EDITOR
  166. node_editor(ctx);
  167. #endif
  168. /* ----------------------------------------- */
  169. /* Draw */
  170. nk_gdi_render(nk_rgb(30,30,30));
  171. }
  172. nk_gdifont_del(font);
  173. ReleaseDC(wnd, dc);
  174. UnregisterClassW(wc.lpszClassName, wc.hInstance);
  175. return 0;
  176. }