main.c 5.0 KB

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