main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include <SDL.h>
  2. #include <SDL_mouse.h>
  3. #include <SDL_keyboard.h>
  4. #ifndef MIN
  5. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  6. #endif
  7. #ifndef MAX
  8. #define MAX(a,b) ((a) < (b) ? (b) : (a))
  9. #endif
  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_INCLUDE_FONT_BAKING
  16. #define NK_INCLUDE_DEFAULT_FONT
  17. #define NK_INCLUDE_SOFTWARE_FONT
  18. #include "../../../nuklear.h"
  19. #define NK_RAWFB_IMPLEMENTATION
  20. #include "../nuklear_rawfb.h"
  21. /* ===============================================================
  22. *
  23. * EXAMPLE
  24. *
  25. * ===============================================================*/
  26. /* This are some code examples to provide a small overview of what can be
  27. * done with this library. To try out an example uncomment the defines */
  28. /*#define INCLUDE_ALL */
  29. /*#define INCLUDE_STYLE */
  30. /*#define INCLUDE_CALCULATOR */
  31. /*#define INCLUDE_CANVAS */
  32. #define INCLUDE_OVERVIEW
  33. /*#define INCLUDE_CONFIGURATOR */
  34. /*#define INCLUDE_NODE_EDITOR */
  35. #ifdef INCLUDE_ALL
  36. #define INCLUDE_STYLE
  37. #define INCLUDE_CALCULATOR
  38. #define INCLUDE_CANVAS
  39. #define INCLUDE_OVERVIEW
  40. #define INCLUDE_CONFIGURATOR
  41. #define INCLUDE_NODE_EDITOR
  42. #endif
  43. #ifdef INCLUDE_STYLE
  44. #include "../../common/style.c"
  45. #endif
  46. #ifdef INCLUDE_CALCULATOR
  47. #include "../../common/calculator.c"
  48. #endif
  49. #ifdef INCLUDE_CANVAS
  50. #include "../../common/canvas.c"
  51. #endif
  52. #ifdef INCLUDE_OVERVIEW
  53. #include "../../common/overview.c"
  54. #endif
  55. #ifdef INCLUDE_CONFIGURATOR
  56. #include "../../common/style_configurator.c"
  57. #endif
  58. #ifdef INCLUDE_NODE_EDITOR
  59. #include "../../common/node_editor.c"
  60. #endif
  61. static int translate_sdl_key(struct SDL_Keysym const *k)
  62. {
  63. /*keyboard handling left as an exercise for the reader */
  64. NK_UNUSED(k);
  65. return NK_KEY_NONE;
  66. }
  67. static int sdl_button_to_nk(int button)
  68. {
  69. switch(button)
  70. {
  71. default:
  72. /* ft */
  73. case SDL_BUTTON_LEFT:
  74. return NK_BUTTON_LEFT;
  75. break;
  76. case SDL_BUTTON_MIDDLE:
  77. return NK_BUTTON_MIDDLE;
  78. break;
  79. case SDL_BUTTON_RIGHT:
  80. return NK_BUTTON_RIGHT;
  81. break;
  82. }
  83. }
  84. #if 0
  85. static void
  86. grid_demo(struct nk_context *ctx)
  87. {
  88. static char text[3][64];
  89. static int text_len[3];
  90. static const char *items[] = {"Item 0","item 1","item 2"};
  91. static int selected_item = 0;
  92. static int check = 1;
  93. int i;
  94. if (nk_begin(ctx, "Grid Demo", nk_rect(600, 350, 275, 250),
  95. NK_WINDOW_TITLE|NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|
  96. NK_WINDOW_NO_SCROLLBAR))
  97. {
  98. nk_layout_row_dynamic(ctx, 30, 2);
  99. nk_label(ctx, "Floating point:", NK_TEXT_RIGHT);
  100. nk_edit_string(ctx, NK_EDIT_FIELD, text[0], &text_len[0], 64, nk_filter_float);
  101. nk_label(ctx, "Hexadecimal:", NK_TEXT_RIGHT);
  102. nk_edit_string(ctx, NK_EDIT_FIELD, text[1], &text_len[1], 64, nk_filter_hex);
  103. nk_label(ctx, "Binary:", NK_TEXT_RIGHT);
  104. nk_edit_string(ctx, NK_EDIT_FIELD, text[2], &text_len[2], 64, nk_filter_binary);
  105. nk_label(ctx, "Checkbox:", NK_TEXT_RIGHT);
  106. nk_checkbox_label(ctx, "Check me", &check);
  107. nk_label(ctx, "Combobox:", NK_TEXT_RIGHT);
  108. if (nk_combo_begin_label(ctx, items[selected_item], nk_vec2(nk_widget_width(ctx), 200))) {
  109. nk_layout_row_dynamic(ctx, 25, 1);
  110. for (i = 0; i < 3; ++i)
  111. if (nk_combo_item_label(ctx, items[i], NK_TEXT_LEFT))
  112. selected_item = i;
  113. nk_combo_end(ctx);
  114. }
  115. }
  116. nk_end(ctx);
  117. }
  118. #endif
  119. int main(int argc, char **argv)
  120. {
  121. struct nk_color clear = {0,100,0,255};
  122. struct nk_vec2 vec;
  123. struct nk_rect bounds = {40,40,0,0};
  124. struct rawfb_context *context;
  125. struct rawfb_pl pl;
  126. unsigned char tex_scratch[512 * 512];
  127. SDL_DisplayMode dm;
  128. SDL_Window *window;
  129. SDL_Renderer *renderer;
  130. SDL_Texture *tex;
  131. SDL_Surface *surface;
  132. #ifdef INCLUDE_CONFIGURATOR
  133. static struct nk_color color_table[NK_COLOR_COUNT];
  134. memcpy(color_table, nk_default_color_style, sizeof(color_table));
  135. #endif
  136. NK_UNUSED(argc);
  137. NK_UNUSED(argv);
  138. SDL_Init(SDL_INIT_VIDEO);
  139. printf("sdl init called...\n");
  140. SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
  141. SDL_GetDesktopDisplayMode(0, &dm);
  142. printf("desktop display mode %d %d\n", dm.w, dm.h);
  143. window = SDL_CreateWindow("Puzzle", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, dm.w-200,dm.h-200, SDL_WINDOW_OPENGL);
  144. if (!window)
  145. {
  146. printf("can't open window!\n");
  147. exit(1);
  148. }
  149. renderer = SDL_CreateRenderer(window, -1, 0);
  150. surface = SDL_CreateRGBSurfaceWithFormat(0, dm.w-200, dm.h-200, 32, SDL_PIXELFORMAT_ARGB8888);
  151. pl.bytesPerPixel = surface->format->BytesPerPixel;
  152. pl.rshift = surface->format->Rshift;
  153. pl.gshift = surface->format->Gshift;
  154. pl.bshift = surface->format->Bshift;
  155. pl.ashift = surface->format->Ashift;
  156. pl.rloss = surface->format->Rloss;
  157. pl.gloss = surface->format->Gloss;
  158. pl.bloss = surface->format->Bloss;
  159. pl.aloss = surface->format->Aloss;
  160. context = nk_rawfb_init(surface->pixels, tex_scratch, surface->w, surface->h, surface->pitch, pl);
  161. while(1)
  162. {
  163. SDL_Event event;
  164. nk_input_begin(&(context->ctx));
  165. while (SDL_PollEvent(&event))
  166. {
  167. switch(event.type)
  168. {
  169. case SDL_QUIT:
  170. exit(0);
  171. break;
  172. case SDL_KEYDOWN:
  173. nk_input_key(&(context->ctx), translate_sdl_key(&event.key.keysym), 1);
  174. break;
  175. case SDL_KEYUP:
  176. nk_input_key(&(context->ctx), translate_sdl_key(&event.key.keysym), 0);
  177. break;
  178. case SDL_MOUSEMOTION:
  179. nk_input_motion(&(context->ctx), event.motion.x, event.motion.y);
  180. break;
  181. case SDL_MOUSEBUTTONDOWN:
  182. nk_input_button(&(context->ctx), sdl_button_to_nk(event.button.button), event.button.x, event.button.y,1);
  183. break;
  184. case SDL_MOUSEBUTTONUP:
  185. nk_input_button(&(context->ctx), sdl_button_to_nk(event.button.button), event.button.x, event.button.y,0);
  186. break;
  187. case SDL_MOUSEWHEEL:
  188. vec.x = event.wheel.preciseX;
  189. vec.y = event.wheel.preciseY;
  190. nk_input_scroll(&(context->ctx), vec );
  191. break;
  192. }
  193. }
  194. nk_input_end(&(context->ctx));
  195. bounds.w = 400;
  196. bounds.h = 400;
  197. if (nk_begin(&(context->ctx), "Test", bounds, NK_WINDOW_MOVABLE | NK_WINDOW_SCALABLE | NK_WINDOW_TITLE))
  198. {
  199. enum {EASY, HARD};
  200. static int op = EASY;
  201. static int property = 20;
  202. nk_layout_row_static(&(context->ctx), 30, 80, 1);
  203. if (nk_button_label(&(context->ctx), "button")){
  204. printf("button pressed\n");
  205. }
  206. nk_layout_row_dynamic(&(context->ctx), 40, 2);
  207. if (nk_option_label(&(context->ctx), "easy", op == EASY)) op = EASY;
  208. if (nk_option_label(&(context->ctx), "hard", op == HARD)) op = HARD;
  209. nk_layout_row_dynamic(&(context->ctx), 45, 1);
  210. nk_property_int(&(context->ctx), "Compression:", 0, &property, 100, 10, 1);
  211. }
  212. nk_end(&(context->ctx));
  213. /* grid_demo(&(context->ctx)); */
  214. /* -------------- EXAMPLES ---------------- */
  215. #ifdef INCLUDE_CALCULATOR
  216. calculator(&(context->ctx));
  217. #endif
  218. #ifdef INCLUDE_CANVAS
  219. canvas(&(context->ctx));
  220. #endif
  221. #ifdef INCLUDE_OVERVIEW
  222. overview(&(context->ctx));
  223. #endif
  224. #ifdef INCLUDE_CONFIGURATOR
  225. style_configurator(&(context->ctx), color_table);
  226. #endif
  227. #ifdef INCLUDE_NODE_EDITOR
  228. node_editor(&(context->ctx));
  229. #endif
  230. /* ----------------------------------------- */
  231. nk_rawfb_render(context, clear, 1);
  232. tex = SDL_CreateTextureFromSurface(renderer, surface);
  233. SDL_RenderCopy(renderer, tex, NULL, NULL);
  234. SDL_RenderPresent(renderer);
  235. SDL_DestroyTexture(tex);
  236. }
  237. nk_rawfb_shutdown(context);
  238. SDL_FreeSurface(surface);
  239. SDL_DestroyRenderer(renderer);
  240. SDL_DestroyWindow(window);
  241. }