You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

174 lines
5.6 KiB

  1. #include <SDL.h>
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include "../src/SDL_Clicky.h"
  5. void clickedClickable(void *c_, int x, int y) {
  6. Clickable *c = c_;
  7. printf("Size of clicked clickable at %p: %d x %d\n", c_, c->dimensions.w, c->dimensions.h);
  8. }
  9. void clickable_draw_red(void* c_, SDL_Renderer* r) {
  10. Clickable *c = c_;
  11. SDL_SetRenderDrawColor(r, 0xFF, 0, 0, 0xFF);
  12. SDL_RenderFillRect(r, &(c->dimensions));
  13. }
  14. void clickable_draw_darkred(void* c_, SDL_Renderer* r) {
  15. Clickable *c = c_;
  16. SDL_SetRenderDrawColor(r, 0x99, 0, 0, 0xFF);
  17. SDL_RenderFillRect(r, &(c->dimensions));
  18. }
  19. bool quit = false;
  20. void clickFunc_QuitButton(void *c_, int x, int y) {
  21. quit = true;
  22. }
  23. int main() {
  24. if(SDL_Init(SDL_INIT_VIDEO) != 0) {
  25. fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
  26. return 1;
  27. }
  28. printf("SDL Initialized\n");
  29. SDL_Window* screen;
  30. screen = SDL_CreateWindow("My Game Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
  31. if(!screen) {
  32. fprintf(stderr, "Could not set video mode: %s\n", SDL_GetError());
  33. return 1;
  34. }
  35. SDL_Renderer* renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_PRESENTVSYNC);
  36. if(!renderer) {
  37. SDL_DestroyWindow(screen);
  38. fprintf(stderr, "Could not create renderer: %s", SDL_GetError());
  39. }
  40. Clickable_Hierarchy hierarchy = {
  41. .num_children = 0,
  42. .num_children_slots = 4,
  43. .children = malloc(sizeof(Clickable_Hierarchy_Element)*4),
  44. };
  45. Clickable c;
  46. c.dimensions.x = 0;
  47. c.dimensions.y = 0;
  48. c.dimensions.w = 100;
  49. c.dimensions.h = 100;
  50. c.isdown = false;
  51. c.clickFunc = clickedClickable;
  52. c.releaseFunc = NULL;
  53. c.drawFunc = clickable_draw_red;
  54. Clicky_Clickable_Hierarchy_Add_Clickable(&hierarchy, &c);
  55. Clickable c2;
  56. c2.dimensions.x = 590;
  57. c2.dimensions.y = 0;
  58. c2.dimensions.w = 50;
  59. c2.dimensions.h = 50;
  60. c2.isdown = false;
  61. c2.clickFunc = clickFunc_QuitButton;
  62. c2.releaseFunc = NULL;
  63. c2.drawFunc = clickable_draw_red;
  64. Clicky_Clickable_Hierarchy_Add_Clickable(&hierarchy, &c2);
  65. SDL_Rect screenBounds;
  66. screenBounds.x = 100;
  67. screenBounds.y = 0;
  68. screenBounds.w = 340;
  69. screenBounds.h = 300;
  70. Dragable d;
  71. d.super.dimensions.x = 200;
  72. d.super.dimensions.y = 100;
  73. d.super.dimensions.w = 50;
  74. d.super.dimensions.h = 50;
  75. d.super.isdown = false;
  76. d.super.clickFunc = Dragable_ClickFunc;
  77. d.super.releaseFunc = Dragable_ReleaseFunc;
  78. d.super.drawFunc = clickable_draw_red;
  79. d.boundaries = &screenBounds;
  80. d.coordinatesToDrag = &(d.super.dimensions); //Drag itself.
  81. Clicky_Clickable_Hierarchy_Add_Clickable(&hierarchy, &(d.super));
  82. //Container thing testing!
  83. Scrollbar_Container* scrollbar = Scrollbar_Container_Constructor(25, 150, 50, 100, 20);
  84. scrollbar->super.drawFunc = clickable_draw_darkred;
  85. scrollbar->bar.super.drawFunc = clickable_draw_red;
  86. //Clicky_Clickable_Hierarchy_Add_Clickable(&hierarchy, &(scrollbar->bar.super));
  87. Clickable_Hierarchy *sh = Clicky_Clickable_Hierarchy_Add_Subtree(&hierarchy, &(scrollbar->super), 1);
  88. Clicky_Clickable_Hierarchy_Add_Clickable(sh, &(scrollbar->bar.super));
  89. SDL_Event e;
  90. while(!quit) {
  91. while(SDL_PollEvent(&e)) {
  92. switch(e.type) {
  93. case SDL_QUIT:
  94. quit = true;
  95. break;
  96. case SDL_MOUSEBUTTONDOWN:
  97. /*if(Clickable_CheckBounds(&c ,e.button.x, e.button.y) == true) {
  98. //printf("Clicked clickable at addr %p\n", &c);
  99. c.clickFunc((void*)&c, e.button.x, e.button.y);
  100. }
  101. if(Clickable_CheckBounds(&(d.super) ,e.button.x, e.button.y) == true) {
  102. //printf("Clicked clickable at addr %p\n", &c);
  103. d.super.clickFunc((void*)&(d.super), e.button.x, e.button.y);
  104. }*/
  105. //Clicky_ClickClickable((void*)&c, e.button.x, e.button.y);
  106. //Clicky_ClickClickable((void*)&c2, e.button.x, e.button.y);
  107. //Clicky_ClickClickable((void*)&d, e.button.x, e.button.y); //d has a Clickable as supertype.
  108. Clicky_Clickable_Hierarchy_Click(&hierarchy, e.button.x, e.button.y);
  109. break;
  110. case SDL_MOUSEBUTTONUP:
  111. /*if(Clickable_CheckBounds(&c ,e.button.x, e.button.y) == true) {
  112. printf("Clicked clickable at addr %p\n", &c);
  113. c.clickFunc((void*)&c, e.button.x, e.button.y);
  114. }*/
  115. /*if(Clickable_CheckBounds(&(d.super) ,e.button.x, e.button.y) == true) {
  116. //printf("Released clickable at addr %p\n", &c);
  117. d.super.releaseFunc((void*)&(d.super), e.button.x, e.button.y);
  118. }*/
  119. //These shouldn't be necessary, but they're there.
  120. //Clicky_ReleaseClickable((void*)&c, e.button.x, e.button.y);
  121. //Clicky_ReleaseClickable((void*)&d, e.button.x, e.button.y); //d has a Clickable as supertype.
  122. Clicky_ReleaseHeldClickable(e.button.x, e.button.y);
  123. break;
  124. case SDL_MOUSEMOTION:
  125. Clicky_DragUpdate(e.motion.x, e.motion.y);
  126. break;
  127. }
  128. }
  129. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  130. SDL_RenderClear(renderer);
  131. //Instead of doing this, the dragable should be put into a container, which should be drawn using the hierarchy.
  132. // The way it is now, the rendering of the things in the hierarchy will all be laid on top, even if they should logically be drawn below.
  133. SDL_SetRenderDrawColor(renderer, 0x7F, 0, 0, 0xFF);
  134. SDL_RenderFillRect(renderer, &(screenBounds));
  135. Clicky_Clickable_Hierarchy_Render(&hierarchy, renderer);
  136. SDL_RenderPresent(renderer);
  137. SDL_Delay(50);
  138. }
  139. SDL_Quit();
  140. printf("SDL Shutdown\n");
  141. return 0;
  142. }