main.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. while (running)
  103. {
  104. /* Input */
  105. MSG msg;
  106. nk_input_begin(ctx);
  107. if (needs_refresh == 0) {
  108. if (GetMessageW(&msg, NULL, 0, 0) <= 0)
  109. running = 0;
  110. else {
  111. TranslateMessage(&msg);
  112. DispatchMessageW(&msg);
  113. }
  114. needs_refresh = 1;
  115. } else needs_refresh = 0;
  116. while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE)) {
  117. if (msg.message == WM_QUIT)
  118. running = 0;
  119. TranslateMessage(&msg);
  120. DispatchMessageW(&msg);
  121. needs_refresh = 1;
  122. }
  123. nk_input_end(ctx);
  124. /* GUI */
  125. if (nk_begin(ctx, "Demo", nk_rect(50, 50, 200, 200),
  126. NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
  127. NK_WINDOW_CLOSABLE|NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
  128. {
  129. enum {EASY, HARD};
  130. static int op = EASY;
  131. static int property = 20;
  132. nk_layout_row_static(ctx, 30, 80, 1);
  133. if (nk_button_label(ctx, "button"))
  134. fprintf(stdout, "button pressed\n");
  135. nk_layout_row_dynamic(ctx, 30, 2);
  136. if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
  137. if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
  138. nk_layout_row_dynamic(ctx, 22, 1);
  139. nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
  140. }
  141. nk_end(ctx);
  142. /* -------------- EXAMPLES ---------------- */
  143. #ifdef INCLUDE_CALCULATOR
  144. calculator(ctx);
  145. #endif
  146. #ifdef INCLUDE_CANVAS
  147. canvas(ctx);
  148. #endif
  149. #ifdef INCLUDE_OVERVIEW
  150. overview(ctx);
  151. #endif
  152. #ifdef INCLUDE_NODE_EDITOR
  153. node_editor(ctx);
  154. #endif
  155. /* ----------------------------------------- */
  156. /* Draw */
  157. nk_gdi_render(nk_rgb(30,30,30));
  158. }
  159. nk_gdifont_del(font);
  160. ReleaseDC(wnd, dc);
  161. UnregisterClassW(wc.lpszClassName, wc.hInstance);
  162. return 0;
  163. }