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.

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