/src/SDLx/Timer.xs

http://github.com/PerlGameDev/SDL · Unknown · 156 lines · 135 code · 21 blank · 0 comment · 0 complexity · c79e5b41fabb051e91cb8f6fcb944dbe MD5 · raw file

  1. #include "EXTERN.h"
  2. #include "perl.h"
  3. #include "XSUB.h"
  4. #include "ppport.h"
  5. #ifndef aTHX_
  6. #define aTHX_
  7. #endif
  8. #include <SDL.h>
  9. #include "SDLx/Timer.h"
  10. MODULE = SDLx::Controller::Timer PACKAGE = SDLx::Controller::Timer PREFIX = timerx_
  11. sdlx_timer *
  12. timerx_new (CLASS, ... )
  13. char* CLASS
  14. CODE:
  15. RETVAL = (sdlx_timer *)safemalloc( sizeof(sdlx_timer) );
  16. RETVAL->started_ticks = 0;
  17. RETVAL->paused_ticks = 0;
  18. RETVAL->started = 0;
  19. RETVAL->paused = 0;
  20. OUTPUT:
  21. RETVAL
  22. int
  23. timerx_started_ticks ( timer, ... )
  24. sdlx_timer *timer
  25. CODE:
  26. if (items > 1 ) timer->started_ticks = SvIV(ST(1));
  27. RETVAL = timer->started_ticks;
  28. OUTPUT:
  29. RETVAL
  30. int
  31. timerx_paused_ticks ( timer, ... )
  32. sdlx_timer *timer
  33. CODE:
  34. if (items > 1 ) timer->paused_ticks = SvIV(ST(1));
  35. RETVAL = timer->paused_ticks;
  36. OUTPUT:
  37. RETVAL
  38. int
  39. timerx_started ( timer, ... )
  40. sdlx_timer *timer
  41. CODE:
  42. if (items > 1 ) timer->started = SvIV(ST(1));
  43. RETVAL = timer->started;
  44. OUTPUT:
  45. RETVAL
  46. int
  47. timerx_paused ( timer, ... )
  48. sdlx_timer *timer
  49. CODE:
  50. if (items > 1 ) timer->paused = SvIV(ST(1));
  51. RETVAL = timer->paused;
  52. OUTPUT:
  53. RETVAL
  54. void
  55. timerx_start ( timer )
  56. sdlx_timer *timer
  57. CODE:
  58. timer->started = 1;
  59. timer->started_ticks = SDL_GetTicks();
  60. void
  61. timerx_stop ( timer )
  62. sdlx_timer *timer
  63. CODE:
  64. timer->started = 0;
  65. timer->paused = 0;
  66. void
  67. timerx_pause ( timer )
  68. sdlx_timer *timer
  69. CODE:
  70. if( timer->started == 1 && timer->paused == 0)
  71. {
  72. timer->paused = 1;
  73. timer->paused_ticks = SDL_GetTicks() - timer->started_ticks;
  74. }
  75. void
  76. timerx_unpause ( timer )
  77. sdlx_timer *timer
  78. CODE:
  79. timer->paused = 0;
  80. timer->started_ticks = SDL_GetTicks() - timer->started_ticks;
  81. timer->paused_ticks = 0;
  82. int
  83. timerx_get_ticks ( timer )
  84. sdlx_timer *timer
  85. CODE:
  86. if(timer->started == 1)
  87. {
  88. if(timer->paused == 1)
  89. {
  90. RETVAL = timer->paused_ticks;
  91. }
  92. else
  93. {
  94. int update = SDL_GetTicks();
  95. int diff = update - timer->started_ticks;
  96. RETVAL = diff;
  97. }
  98. }
  99. else
  100. {
  101. RETVAL = 0;
  102. }
  103. OUTPUT:
  104. RETVAL
  105. int
  106. timerx_is_started ( timer )
  107. sdlx_timer *timer
  108. CODE:
  109. RETVAL = timer->started;
  110. OUTPUT:
  111. RETVAL
  112. int
  113. timerx_is_paused ( timer)
  114. sdlx_timer *timer
  115. CODE:
  116. RETVAL = timer->paused;
  117. OUTPUT:
  118. RETVAL
  119. void
  120. timerx_DESTROY(bag)
  121. SV *bag
  122. CODE:
  123. if( sv_isobject(bag) && (SvTYPE(SvRV(bag)) == SVt_PVMG) ) {
  124. void** pointers = (void**)INT2PTR(void *, SvIV((SV *)SvRV( bag )));
  125. sdlx_timer * timer = (sdlx_timer*)(pointers[0]);
  126. if (PERL_GET_CONTEXT == pointers[1]) {
  127. pointers[0] = NULL;
  128. safefree( pointers );
  129. safefree(timer);
  130. }
  131. } else if (bag == 0) {
  132. XSRETURN(0);
  133. } else {
  134. XSRETURN_UNDEF;
  135. }