PageRenderTime 17ms CodeModel.GetById 11ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

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