gcsx_event.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* GCSx
  2. ** EVENT.CPP
  3. **
  4. ** Our custom event queue to allow preempting SDL's event queue
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #include "all.h"
  24. // We need enough events to handle changing every layer of a scene
  25. // at once, with room to spare; game event queue doesn't need as
  26. // much room but we keep it safe
  27. #define MAX_EVENTS 128
  28. SDL_Event eventQueue[MAX_EVENTS];
  29. int eventHead = 0;
  30. int eventTail = 0;
  31. SDL_Event gameEventQueue[MAX_EVENTS];
  32. int gameEventHead = 0;
  33. int gameEventTail = 0;
  34. void insertEvent(const SDL_Event* customEvent) { start_func
  35. int tail = (eventTail + 1) % MAX_EVENTS;
  36. // If we're full, drop the event
  37. // This should never occur in normal usage, and
  38. // even if it does should be harmless as these are
  39. // only CLOSE, FOCUS, and OBJCHANGE events
  40. if (tail == eventHead) {
  41. /* SDL_OBJECTCHANGE events no longer use the queue
  42. **
  43. if (customEvent->type == SDL_OBJECTCHANGE) {
  44. delete (ObjChange*)customEvent->user.data1;
  45. }
  46. **
  47. */
  48. return;
  49. }
  50. eventQueue[eventTail] = *customEvent;
  51. eventTail = tail;
  52. }
  53. void preemptEvent(const SDL_Event* customEvent) { start_func
  54. int head = (eventHead + MAX_EVENTS - 1) % MAX_EVENTS;
  55. // If we're full, drop the event
  56. // This should never occur in normal usage, and
  57. // even if it does should be harmless as these are
  58. // only CLOSE, FOCUS, and OBJCHANGE events
  59. if (head == eventTail) {
  60. /* SDL_OBJECTCHANGE events no longer use the queue
  61. **
  62. if (customEvent->type == SDL_OBJECTCHANGE) {
  63. delete (ObjChange*)customEvent->user.data1;
  64. }
  65. **
  66. */
  67. return;
  68. }
  69. eventHead = head;
  70. eventQueue[eventHead] = *customEvent;
  71. }
  72. /* SDL_OBJECTCHANGE events no longer use the queue
  73. **
  74. void combinatoryEvent(const SDL_Event* customEvent) { start_func
  75. // Right now, we only know how to combine SDL_OBJCHANGE events
  76. assert(customEvent->type == SDL_OBJECTCHANGE);
  77. // Scan all existing events
  78. for (int pos = eventHead; pos != eventTail; pos = (pos + 1) % MAX_EVENTS) {
  79. // Matching type and code
  80. if ((eventQueue[pos].type == customEvent->type) &&
  81. (eventQueue[pos].user.code == customEvent->user.code)) {
  82. // We only support one type of combination right now
  83. if ((customEvent->user.code & (OBJMOD_TILE | OBJMOD_COLL)) &&
  84. (customEvent->user.code & OBJ_TILESET)) {
  85. // (for now, these must be SDL_OBJCHANGE events)
  86. const ObjChange* obj1 = (ObjChange*)customEvent->user.data1;
  87. ObjChange* obj2 = (ObjChange*)eventQueue[pos].user.data1;
  88. // Replace old event with min/max of combined events
  89. if (obj1->info1 < obj2->info1) obj2->info1 = obj1->info1;
  90. if (obj1->info2 > obj2->info2) obj2->info2 = obj1->info2;
  91. // Delete old event details
  92. delete obj1;
  93. return;
  94. }
  95. }
  96. }
  97. // No combination made
  98. preemptEvent(customEvent);
  99. }
  100. **
  101. */
  102. int eventsWaiting() { start_func
  103. if (eventTail == eventHead) return 0;
  104. return 1;
  105. }
  106. int grabEvent(SDL_Event* store) { start_func
  107. if (eventTail == eventHead) {
  108. if (SDL_PollEvent(store)) return 1;
  109. return 0;
  110. }
  111. *store = eventQueue[eventHead];
  112. eventHead = (eventHead + 1) % MAX_EVENTS;
  113. return 1;
  114. }
  115. void cleanEvents(const void* window) { start_func
  116. for (int pos = eventHead; pos != eventTail; pos = (pos + 1) % MAX_EVENTS) {
  117. if (eventQueue[pos].user.data1 == window) eventQueue[pos].type = SDL_NOEVENT;
  118. }
  119. }
  120. void clearEvents() { start_func
  121. /* SDL_OBJECTCHANGE events no longer use the queue
  122. **
  123. // Clear our events only
  124. while (eventTail != eventHead) {
  125. if (eventQueue[eventHead].type == SDL_OBJECTCHANGE) {
  126. delete (ObjChange*)eventQueue[eventHead].user.data1;
  127. }
  128. eventHead = (eventHead + 1) % MAX_EVENTS;
  129. }
  130. **
  131. */
  132. eventTail = eventHead;
  133. gameEventTail = gameEventHead;
  134. }
  135. void storeEventGame(const SDL_Event* event) { start_func
  136. int tail = (gameEventTail + 1) % MAX_EVENTS;
  137. // If we're full, drop the event
  138. if (tail == gameEventHead) return;
  139. gameEventQueue[gameEventTail] = *event;
  140. gameEventTail = tail;
  141. }
  142. int grabEventGame(SDL_Event* store) { start_func
  143. if (gameEventTail == gameEventHead) return 0;
  144. *store = gameEventQueue[gameEventHead];
  145. gameEventHead = (gameEventHead + 1) % MAX_EVENTS;
  146. return 1;
  147. }