PageRenderTime 20ms CodeModel.GetById 9ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

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