/src/SDLx/Layer.xs

http://github.com/PerlGameDev/SDL · Unknown · 252 lines · 224 code · 28 blank · 0 comment · 0 complexity · da64bd127818b447b190783d347d54e8 MD5 · raw file

  1. #include "EXTERN.h"
  2. #include "perl.h"
  3. #include "XSUB.h"
  4. #include "ppport.h"
  5. #include "helper.h"
  6. #ifndef aTHX_
  7. #define aTHX_
  8. #endif
  9. #include <SDL.h>
  10. #include "SDLx/LayerManager.h"
  11. MODULE = SDLx::Layer PACKAGE = SDLx::Layer PREFIX = layerx_
  12. SDLx_Layer *
  13. layerx_new( CLASS, surface, ... )
  14. char* CLASS
  15. SDL_Surface *surface
  16. CODE:
  17. RETVAL = (SDLx_Layer *)safemalloc( sizeof(SDLx_Layer) );
  18. RETVAL->index = -1;
  19. RETVAL->surface = (SDL_Surface *)safemalloc( sizeof(SDL_Surface) );
  20. RETVAL->clip = (SDL_Rect *)safemalloc( sizeof(SDL_Rect) );
  21. RETVAL->pos = (SDL_Rect *)safemalloc( sizeof(SDL_Rect) );
  22. RETVAL->touched = 1;
  23. RETVAL->attached = 0;
  24. RETVAL->attached_pos = (SDL_Rect *)safemalloc( sizeof(SDL_Rect) );
  25. RETVAL->attached_rel = (SDL_Rect *)safemalloc( sizeof(SDL_Rect) );
  26. RETVAL->surface = SDL_ConvertSurface(surface, surface->format, surface->flags);
  27. (RETVAL->pos)->x = 0;
  28. (RETVAL->pos)->y = 0;
  29. (RETVAL->pos)->w = (RETVAL->surface)->w;
  30. (RETVAL->pos)->h = (RETVAL->surface)->h;
  31. (RETVAL->clip)->x = 0;
  32. (RETVAL->clip)->y = 0;
  33. (RETVAL->clip)->w = (RETVAL->surface)->w;
  34. (RETVAL->clip)->h = (RETVAL->surface)->h;
  35. if(SvROK(ST(items - 1)) && SVt_PVHV == SvTYPE(SvRV(ST(items - 1))))
  36. {
  37. RETVAL->data = (HV *)SvRV(ST(items - 1));
  38. /*if(SvREFCNT(RETVAL->data) < 2) */
  39. SvREFCNT_inc(RETVAL->data);
  40. items--;
  41. }
  42. else
  43. RETVAL->data = (HV *)NULL;
  44. if(items > 2)
  45. (RETVAL->pos)->x = SvIV(ST(2));
  46. if(items > 3)
  47. (RETVAL->pos)->y = SvIV(ST(3));
  48. if(items > 4)
  49. (RETVAL->clip)->x = SvIV(ST(4));
  50. if(items > 5)
  51. (RETVAL->clip)->y = SvIV(ST(5));
  52. if(items > 6)
  53. (RETVAL->clip)->w = SvIV(ST(6));
  54. if(items > 7)
  55. (RETVAL->clip)->h = SvIV(ST(7));
  56. OUTPUT:
  57. RETVAL
  58. int
  59. layerx_index( layer )
  60. SDLx_Layer *layer
  61. CODE:
  62. RETVAL = layer->index;
  63. OUTPUT:
  64. RETVAL
  65. int
  66. layerx_x( layer )
  67. SDLx_Layer *layer
  68. CODE:
  69. RETVAL = (layer->pos)->x;
  70. OUTPUT:
  71. RETVAL
  72. int
  73. layerx_y( layer )
  74. SDLx_Layer *layer
  75. CODE:
  76. RETVAL = (layer->pos)->y;
  77. OUTPUT:
  78. RETVAL
  79. int
  80. layerx_w( layer )
  81. SDLx_Layer *layer
  82. CODE:
  83. RETVAL = (layer->clip)->w;
  84. OUTPUT:
  85. RETVAL
  86. int
  87. layerx_h( layer )
  88. SDLx_Layer *layer
  89. CODE:
  90. RETVAL = (layer->clip)->h;
  91. OUTPUT:
  92. RETVAL
  93. SV *
  94. layerx_surface( layer, ... )
  95. SDLx_Layer *layer
  96. CODE:
  97. if(items > 1)
  98. {
  99. SDL_Surface *surface = (SDL_Surface *)bag2obj(ST(1));
  100. layer->surface = SDL_ConvertSurface(surface, surface->format, surface->flags);
  101. layer->touched = 1;
  102. layer->manager->saved = 0;
  103. layer->pos->w = layer->surface->w;
  104. layer->pos->h = layer->surface->h;
  105. layer->clip->w = layer->surface->w;
  106. layer->clip->h = layer->surface->h;
  107. }
  108. RETVAL = _sv_ref( layer->surface, sizeof(SDL_Surface *), sizeof(SDL_Surface), "SDL::Surface" );
  109. OUTPUT:
  110. RETVAL
  111. SV *
  112. layerx_clip( layer )
  113. SDLx_Layer *layer
  114. CODE:
  115. RETVAL = _sv_ref( layer->clip, sizeof(SDL_Rect *), sizeof(SDL_Rect), "SDL::Rect" );
  116. OUTPUT:
  117. RETVAL
  118. SV *
  119. layerx_pos( layer, ... )
  120. SDLx_Layer *layer
  121. CODE:
  122. if(items == 3)
  123. {
  124. layer->attached = 2;
  125. layer->pos->x = SvIV(ST(1));
  126. layer->pos->y = SvIV(ST(2));
  127. }
  128. RETVAL = _sv_ref( layer->pos, sizeof(SDL_Rect *), sizeof(SDL_Rect), "SDL::Rect" );
  129. OUTPUT:
  130. RETVAL
  131. HV *
  132. layerx_data( layer, ... )
  133. SDLx_Layer *layer
  134. CODE:
  135. if(items > 1)
  136. {
  137. layer->data = (HV *)SvRV(ST(1));
  138. SvREFCNT_inc(layer->data);
  139. }
  140. if((HV *)NULL == layer->data)
  141. XSRETURN_UNDEF;
  142. else
  143. RETVAL = layer->data;
  144. OUTPUT:
  145. RETVAL
  146. AV *
  147. layerx_ahead( layer )
  148. SDLx_Layer *layer
  149. CODE:
  150. RETVAL = layers_ahead( layer );
  151. OUTPUT:
  152. RETVAL
  153. AV *
  154. layerx_behind( layer )
  155. SDLx_Layer *layer
  156. CODE:
  157. RETVAL = layers_behind( layer );
  158. OUTPUT:
  159. RETVAL
  160. void
  161. layerx_attach( layer, x = -1, y = -1 )
  162. SDLx_Layer *layer
  163. int x
  164. int y
  165. CODE:
  166. if(-1 == x || -1 == y)
  167. SDL_GetMouseState(&x, &y);
  168. layer->attached = 1;
  169. layer->attached_pos->x = layer->pos->x;
  170. layer->attached_pos->y = layer->pos->x;
  171. layer->attached_rel->x = layer->pos->x - x;
  172. layer->attached_rel->y = layer->pos->y - y;
  173. layer->manager->saved = 0;
  174. AV *
  175. layerx_detach_xy( layer, x = -1, y = -1 )
  176. SDLx_Layer *layer
  177. int x
  178. int y
  179. CODE:
  180. layer->attached = 0;
  181. layer->pos->x = x;
  182. layer->pos->y = y;
  183. layer->manager->saved = 0;
  184. RETVAL = newAV();
  185. av_store(RETVAL, 0, newSViv(layer->attached_pos->x));
  186. av_store(RETVAL, 1, newSViv(layer->attached_pos->y));
  187. OUTPUT:
  188. RETVAL
  189. SV *
  190. layerx_foreground( bag )
  191. SV *bag
  192. CODE:
  193. SDLx_Layer *layer = (SDLx_Layer *)bag2obj(bag);
  194. SDLx_LayerManager *manager = layer->manager;
  195. int index = layer->index; /* we cant trust its value */
  196. layer->manager->saved = 0;
  197. int i;
  198. for(i = 0; i <= av_len(manager->layers); i++)
  199. {
  200. if(*av_fetch(manager->layers, i, 0) == bag) /* what bag do we have? => finding the right layer index */
  201. {
  202. index = i;
  203. break;
  204. }
  205. }
  206. for(i = index; i < av_len(manager->layers); i++)
  207. {
  208. AvARRAY(manager->layers)[i] = AvARRAY(manager->layers)[i + 1];
  209. ((SDLx_Layer *)bag2obj(AvARRAY(manager->layers)[i]))->index = i;
  210. }
  211. AvARRAY(manager->layers)[i] = bag;
  212. ((SDLx_Layer *)bag2obj(AvARRAY(manager->layers)[i]))->index = i;
  213. SvREFCNT_inc( bag );
  214. RETVAL = newSVsv(bag);
  215. SvREFCNT_inc(RETVAL);
  216. OUTPUT:
  217. RETVAL
  218. void
  219. layerx_DESTROY( layer )
  220. SDLx_Layer *layer
  221. CODE:
  222. /*if((HV *)NULL != layer->data) // Attempt to free unreferenced scalar */
  223. /*SvREFCNT_dec(layer->data); */
  224. safefree(layer);