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.

153 lines
4.6 KiB

  1. #include <SDL.h>
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include "../src/SDL_Clicky.h"
  5. void clickedSimpleContainer(void *c_, int x, int y) {
  6. SimpleContainer *c = c_;
  7. printf("Clicked inside of SimpleContainer at %p with size: %d x %d\n", c_, c->super.dimensions.w, c->super.dimensions.h);
  8. }
  9. void drawSimpleContainer(void *c_, SDL_Renderer* renderer) {
  10. SimpleContainer *c = c_;
  11. SDL_SetRenderDrawColor(renderer, 0x7F, 0x7F, 0, 0xFF);
  12. SDL_RenderFillRect(renderer, &(c->super.dimensions));
  13. }
  14. bool quit = false;
  15. void clickFunc_QuitButton(void *c_, int x, int y) {
  16. quit = true;
  17. }
  18. int main() {
  19. if(SDL_Init(SDL_INIT_VIDEO) != 0) {
  20. fprintf(stderr, "Could not initialize SDL: %s\n", SDL_GetError());
  21. return 1;
  22. }
  23. printf("SDL Initialized\n");
  24. SDL_Window* screen;
  25. screen = SDL_CreateWindow("My Game Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
  26. if(!screen) {
  27. fprintf(stderr, "Could not set video mode: %s\n", SDL_GetError());
  28. return 1;
  29. }
  30. SDL_Renderer* renderer = SDL_CreateRenderer(screen, -1, SDL_RENDERER_PRESENTVSYNC);
  31. if(!renderer) {
  32. SDL_DestroyWindow(screen);
  33. fprintf(stderr, "Could not create renderer: %s", SDL_GetError());
  34. }
  35. SimpleContainer sc;
  36. sc.super.dimensions.x = 0;
  37. sc.super.dimensions.y = 0;
  38. sc.super.dimensions.w = 100;
  39. sc.super.dimensions.h = 100;
  40. sc.super.isdown = false;
  41. sc.super.clickFunc = clickedSimpleContainer;
  42. sc.super.releaseFunc = NULL;
  43. sc.drawFunc = drawSimpleContainer;
  44. Clickable c2;
  45. c2.dimensions.x = 590;
  46. c2.dimensions.y = 0;
  47. c2.dimensions.w = 50;
  48. c2.dimensions.h = 50;
  49. c2.isdown = false;
  50. c2.clickFunc = clickFunc_QuitButton;
  51. c2.releaseFunc = NULL;
  52. SDL_Rect screenBounds;
  53. screenBounds.x = 100;
  54. screenBounds.y = 0;
  55. screenBounds.w = 440 - screenBounds.x;
  56. screenBounds.h = 280 - screenBounds.y;
  57. Dragable d;
  58. d.super.dimensions.x = 200;
  59. d.super.dimensions.y = 100;
  60. d.super.dimensions.w = 50;
  61. d.super.dimensions.h = 50;
  62. d.super.isdown = false;
  63. d.super.clickFunc = Dragable_ClickFunc;
  64. d.super.releaseFunc = Dragable_ReleaseFunc;
  65. d.boundaries = &screenBounds;
  66. d.coordinatesToDrag = &(d.super.dimensions); //Drag itself.
  67. SDL_Event e;
  68. while(!quit) {
  69. while(SDL_PollEvent(&e)) {
  70. switch(e.type) {
  71. case SDL_QUIT:
  72. quit = true;
  73. break;
  74. case SDL_MOUSEBUTTONDOWN:
  75. /*if(Clickable_CheckBounds(&c ,e.button.x, e.button.y) == true) {
  76. //printf("Clicked clickable at addr %p\n", &c);
  77. c.clickFunc((void*)&c, e.button.x, e.button.y);
  78. }
  79. if(Clickable_CheckBounds(&(d.super) ,e.button.x, e.button.y) == true) {
  80. //printf("Clicked clickable at addr %p\n", &c);
  81. d.super.clickFunc((void*)&(d.super), e.button.x, e.button.y);
  82. }*/
  83. Clicky_ClickClickable((void*)&sc, e.button.x, e.button.y);
  84. Clicky_ClickClickable((void*)&c2, e.button.x, e.button.y);
  85. Clicky_ClickClickable((void*)&d, e.button.x, e.button.y); //d has a Clickable as supertype.
  86. break;
  87. case SDL_MOUSEBUTTONUP:
  88. /*if(Clickable_CheckBounds(&c ,e.button.x, e.button.y) == true) {
  89. printf("Clicked clickable at addr %p\n", &c);
  90. c.clickFunc((void*)&c, e.button.x, e.button.y);
  91. }*/
  92. /*if(Clickable_CheckBounds(&(d.super) ,e.button.x, e.button.y) == true) {
  93. //printf("Released clickable at addr %p\n", &c);
  94. d.super.releaseFunc((void*)&(d.super), e.button.x, e.button.y);
  95. }*/
  96. //These shouldn't be necessary, but they're there.
  97. //Clicky_ReleaseClickable((void*)&c, e.button.x, e.button.y);
  98. //Clicky_ReleaseClickable((void*)&d, e.button.x, e.button.y); //d has a Clickable as supertype.
  99. Clicky_ReleaseHeldClickable(e.button.x, e.button.y);
  100. break;
  101. case SDL_MOUSEMOTION:
  102. Clicky_DragUpdate(e.motion.x, e.motion.y);
  103. break;
  104. }
  105. }
  106. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  107. SDL_RenderClear(renderer);
  108. //SDL_SetRenderDrawColor(renderer, 0xFF, 0, 0, 0xFF);
  109. //SDL_RenderFillRect(renderer, &(c.dimensions));
  110. sc.drawFunc(&sc, renderer);
  111. SDL_SetRenderDrawColor(renderer, 0xFF, 0, 0, 0xFF);
  112. SDL_RenderFillRect(renderer, &(c2.dimensions));
  113. SDL_SetRenderDrawColor(renderer, 0x7F, 0, 0, 0xFF);
  114. SDL_RenderFillRect(renderer, &(screenBounds));
  115. SDL_SetRenderDrawColor(renderer, 0xFF, 0, 0, 0xFF);
  116. SDL_RenderFillRect(renderer, &(d.super.dimensions));
  117. SDL_RenderPresent(renderer);
  118. }
  119. SDL_Quit();
  120. printf("SDL Shutdown\n");
  121. return 0;
  122. }