main.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include <windows.h>
  2. #pragma comment(linker,"\"/manifestdependency:type='win32' \
  3. name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
  4. processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
  5. /* Includes the default nuklear implementation
  6. * Includes the modified GDI backend (No more global state to allow multiple hwnd's)
  7. */
  8. #define NK_INCLUDE_FIXED_TYPES
  9. #define NK_INCLUDE_STANDARD_IO
  10. #define NK_INCLUDE_STANDARD_VARARGS
  11. #define NK_INCLUDE_DEFAULT_ALLOCATOR
  12. #define NK_IMPLEMENTATION
  13. #define NK_GDI_IMPLEMENTATION
  14. #include "../../nuklear.h"
  15. #include "nuklear_gdi.h"
  16. /* Include the window framework (the new fancy code of this demo) */
  17. #define NKGDI_IMPLEMENT_WINDOW
  18. #include "window.h"
  19. /* This callback will be called when the window is draw
  20. * You will NOT need to call nk_begin(...) and nk_end(...)
  21. * begin and end are handled by the parent code calling this
  22. * callback
  23. */
  24. int drawCallback(struct nk_context* ctx)
  25. {
  26. /* Code is from ../calculator.c */
  27. static int set = 0, prev = 0, op = 0;
  28. static const char numbers[] = "789456123";
  29. static const char ops[] = "+-*/";
  30. static double a = 0, b = 0;
  31. static double *current = &a;
  32. size_t i = 0;
  33. int solve = 0;
  34. {int len; char buffer[256];
  35. nk_layout_row_dynamic(ctx, 35, 1);
  36. len = snprintf(buffer, 256, "%.2f", *current);
  37. nk_edit_string(ctx, NK_EDIT_SIMPLE, buffer, &len, 255, nk_filter_float);
  38. buffer[len] = 0;
  39. *current = atof(buffer);}
  40. nk_layout_row_dynamic(ctx, 35, 4);
  41. for (i = 0; i < 16; ++i) {
  42. if (i >= 12 && i < 15) {
  43. if (i > 12) continue;
  44. if (nk_button_label(ctx, "C")) {
  45. a = b = op = 0; current = &a; set = 0;
  46. } if (nk_button_label(ctx, "0")) {
  47. *current = *current*10.0f; set = 0;
  48. } if (nk_button_label(ctx, "=")) {
  49. solve = 1; prev = op; op = 0;
  50. }
  51. } else if (((i+1) % 4)) {
  52. if (nk_button_text(ctx, &numbers[(i/4)*3+i%4], 1)) {
  53. *current = *current * 10.0f + numbers[(i/4)*3+i%4] - '0';
  54. set = 0;
  55. }
  56. } else if (nk_button_text(ctx, &ops[i/4], 1)) {
  57. if (!set) {
  58. if (current != &b) {
  59. current = &b;
  60. } else {
  61. prev = op;
  62. solve = 1;
  63. }
  64. }
  65. op = ops[i/4];
  66. set = 1;
  67. }
  68. }
  69. if (solve) {
  70. if (prev == '+') a = a + b;
  71. if (prev == '-') a = a - b;
  72. if (prev == '*') a = a * b;
  73. if (prev == '/') a = a / b;
  74. current = &a;
  75. if (set) current = &b;
  76. b = 0; set = 0;
  77. }
  78. return 1;
  79. }
  80. /* Main entry point - wWinMain used for UNICODE
  81. * (You can also use _tWinMain(...) to automaticaly use the ASCII or WIDE char entry point base on your build)
  82. */
  83. INT WINAPI wWinMain(HINSTANCE _In_ hInstance, HINSTANCE _In_opt_ hPrevInstance, PWSTR _In_ cmdArgs, INT _In_ cmdShow)
  84. {
  85. /* Call this first to setup all required prerequisites */
  86. nkgdi_window_init();
  87. /* Preparing two window contexts */
  88. struct nkgdi_window w1, w2;
  89. memset(&w1, 0x0, sizeof(struct nkgdi_window));
  90. memset(&w2, 0x0, sizeof(struct nkgdi_window));
  91. /* Configure and create window 1.
  92. * Note: You can allways change the direct accesible parameters later as well!
  93. */
  94. w1.allow_sizing = 0;
  95. w1.allow_maximize = 0;
  96. w1.allow_move = 0;
  97. w1.has_titlebar = 0;
  98. w1.cb_on_draw = &drawCallback;
  99. nkgdi_window_create(&w1, 500, 500, "F1", 10, 10);
  100. /* Configure and create window 2 */
  101. w2.allow_sizing = 1;
  102. w2.allow_maximize = 1;
  103. w2.allow_move = 1;
  104. w2.has_titlebar = 1;
  105. w2.cb_on_draw = &drawCallback;
  106. nkgdi_window_create(&w2, 500, 500, "F2", 520, 10);
  107. /* As long as both windows are valid (nkgdi_window_update returning 1) */
  108. while (nkgdi_window_update(&w1) && nkgdi_window_update(&w2)) Sleep(20);
  109. /* Destroy both windows context */
  110. nkgdi_window_destroy(&w1);
  111. nkgdi_window_destroy(&w2);
  112. /* Call nkgdi_window_shutdown to properly shutdown the gdi window framework */
  113. nkgdi_window_shutdown();
  114. return 0;
  115. }