Permalink
Browse files

moved obj2bag and bag2obj to helper.h

  • Loading branch information...
1 parent 67c08e0 commit 8573a51b5b7e795218d17194ef834442390db9c5 Tobias Leich committed Nov 11, 2010
Showing with 103 additions and 131 deletions.
  1. +5 −19 src/SDLx/Layer.xs
  2. +6 −30 src/SDLx/LayerManager.h
  3. +11 −26 src/SDLx/LayerManager.xs
  4. +6 −5 src/SDLx/Surface.xs
  5. +5 −29 src/SDLx/Validate.h
  6. +3 −2 src/SDLx/Validate.xs
  7. +45 −0 src/helper.h
  8. +22 −20 typemap
View
@@ -2,6 +2,7 @@
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
+#include "helper.h"
#ifndef aTHX_
#define aTHX_
@@ -10,21 +11,6 @@
#include <SDL.h>
#include "SDLx/LayerManager.h"
-PerlInterpreter * perl = NULL;
-
-SV *_sv_ref( void *object, int p_size, int s_size, char *package )
-{
- SV *ref = newSV( p_size );
- void *copy = safemalloc( s_size );
- memcpy( copy, object, s_size );
-
- void** pointers = malloc(2 * sizeof(void*));
- pointers[0] = (void*)copy;
- pointers[1] = (void*)perl;
-
- return newSVsv(sv_setref_pv(ref, package, (void *)pointers));
-}
-
MODULE = SDLx::Layer PACKAGE = SDLx::Layer PREFIX = layerx_
@@ -123,7 +109,7 @@ layerx_surface( layer, ... )
CODE:
if(items > 1)
{
- SDL_Surface *surface = bag_to_surface(ST(1));
+ SDL_Surface *surface = (SDL_Surface *)bag2obj(ST(1));
layer->surface = SDL_ConvertSurface(surface, surface->format, surface->flags);
layer->touched = 1;
layer->manager->saved = 0;
@@ -225,7 +211,7 @@ SV *
layerx_foreground( bag )
SV *bag
CODE:
- SDLx_Layer *layer = bag_to_layer(bag);
+ SDLx_Layer *layer = (SDLx_Layer *)bag2obj(bag);
SDLx_LayerManager *manager = layer->manager;
int index = layer->index; // we cant trust its value
layer->manager->saved = 0;
@@ -243,10 +229,10 @@ layerx_foreground( bag )
for(i = index; i < av_len(manager->layers); i++)
{
AvARRAY(manager->layers)[i] = AvARRAY(manager->layers)[i + 1];
- bag_to_layer(AvARRAY(manager->layers)[i])->index = i;
+ ((SDLx_Layer *)bag2obj(AvARRAY(manager->layers)[i]))->index = i;
}
AvARRAY(manager->layers)[i] = bag;
- bag_to_layer(AvARRAY(manager->layers)[i])->index = i;
+ ((SDLx_Layer *)bag2obj(AvARRAY(manager->layers)[i]))->index = i;
SvREFCNT_inc( bag );
RETVAL = newSVsv(bag);
SvREFCNT_inc(RETVAL);
View
@@ -1,4 +1,6 @@
+#include "helper.h"
+
typedef struct SDLx_LayerManager
{
AV *layers;
@@ -21,32 +23,6 @@ typedef struct SDLx_Layer
HV *data;
} SDLx_Layer;
-SDLx_Layer *bag_to_layer( SV *bag )
-{
- SDLx_Layer *layer = NULL;
-
- if( sv_isobject(bag) && (SvTYPE(SvRV(bag)) == SVt_PVMG) )
- {
- void **pointers = (void **)(SvIV((SV *)SvRV( bag )));
- layer = (SDLx_Layer *)(pointers[0]);
- }
-
- return layer;
-}
-
-SDL_Surface *bag_to_surface( SV *bag )
-{
- SDL_Surface *surface = NULL;
-
- if( sv_isobject(bag) && (SvTYPE(SvRV(bag)) == SVt_PVMG) )
- {
- void **pointers = (void **)(SvIV((SV *)SvRV( bag )));
- surface = (SDL_Surface *)(pointers[0]);
- }
-
- return surface;
-}
-
int intersection( SDLx_Layer *layer1, SDLx_Layer *layer2 )
{
if(
@@ -90,7 +66,7 @@ AV *layers_behind( SDLx_Layer *layer)
for( i = layer->index - 1; i >= 0; i-- )
{
SV *bag = *av_fetch(layer->manager->layers, i, 0);
- SDLx_Layer *layer2 = bag_to_layer(bag);
+ SDLx_Layer *layer2 = (SDLx_Layer *)bag2obj(bag);
if(intersection( layer, layer2 ) || intersection( layer2, layer ))
{
// TODO checking transparency
@@ -102,7 +78,7 @@ AV *layers_behind( SDLx_Layer *layer)
if(count)
{
- AV *behind = layers_behind(bag_to_layer(*av_fetch(matches, av_len(matches), 0)));
+ AV *behind = layers_behind((SDLx_Layer *)bag2obj(*av_fetch(matches, av_len(matches), 0)));
if(av_len(behind) >= 0)
{
for( i = 0; i <= av_len(behind); i++ )
@@ -125,7 +101,7 @@ AV *layers_ahead( SDLx_Layer *layer )
for( i = layer->index + 1; i <= av_len(layer->manager->layers); i++ )
{
SV *bag = *av_fetch(layer->manager->layers, i, 0);
- SDLx_Layer *layer2 = bag_to_layer(bag);
+ SDLx_Layer *layer2 = (SDLx_Layer *)bag2obj(bag);
if(intersection( layer, layer2 ) || intersection( layer2, layer ))
{
// TODO checking transparency
@@ -137,7 +113,7 @@ AV *layers_ahead( SDLx_Layer *layer )
if(count)
{
- AV *ahead = layers_ahead(bag_to_layer(*av_fetch(matches, av_len(matches), 0)));
+ AV *ahead = layers_ahead((SDLx_Layer *)bag2obj(*av_fetch(matches, av_len(matches), 0)));
if(av_len(ahead) >= 0)
{
for( i = 0; i <= av_len(ahead); i++ )
View
@@ -2,6 +2,7 @@
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
+#include "helper.h"
#ifndef aTHX_
#define aTHX_
@@ -10,21 +11,6 @@
#include <SDL.h>
#include "SDLx/LayerManager.h"
-PerlInterpreter * perl = NULL;
-
-SV *_sv_ref( void *object, int p_size, int s_size, char *package )
-{
- SV *ref = newSV( p_size );
- void *copy = safemalloc( s_size );
- memcpy( copy, object, s_size );
-
- void** pointers = malloc(2 * sizeof(void*));
- pointers[0] = (void*)copy;
- pointers[1] = (void*)perl;
-
- return newSVsv(sv_setref_pv(ref, package, (void *)pointers));
-}
-
int _get_pixel( SDL_Surface *surface, int x, int y )
{
int value = 0;
@@ -66,8 +52,7 @@ lmx_add( manager, bag )
CODE:
if( sv_isobject(bag) && (SvTYPE(SvRV(bag)) == SVt_PVMG) )
{
- void **pointers = (void**)(SvIV((SV*)SvRV( bag )));
- SDLx_Layer *layer = (SDLx_Layer *)(pointers[0]);
+ SDLx_Layer *layer = (SDLx_Layer *)bag2obj(bag);
layer->index = av_len( manager->layers ) + 1;
layer->manager = manager;
layer->touched = 1;
@@ -123,7 +108,7 @@ lmx_blit( manager, dest )
while(index < length)
{
- SDLx_Layer *layer = bag_to_layer(*av_fetch(manager->layers, index, 0));
+ SDLx_Layer *layer = (SDLx_Layer *)bag2obj(*av_fetch(manager->layers, index, 0));
if(layer->attached == 0)
{
@@ -164,7 +149,7 @@ lmx_blit( manager, dest )
index = 0;
while(index < length)
{
- SDLx_Layer *layer = bag_to_layer(*av_fetch(manager->layers, index, 0));
+ SDLx_Layer *layer = (SDLx_Layer *)bag2obj(*av_fetch(manager->layers, index, 0));
if(layer->attached == 1)
{
@@ -201,7 +186,7 @@ lmx_by_position( manager, x, y )
for( i = av_len( manager->layers ); i >= 0 && match == NULL; i-- )
{
SV *bag = *av_fetch(manager->layers, i, 0);
- SDLx_Layer *layer = bag_to_layer(bag);
+ SDLx_Layer *layer = (SDLx_Layer *)bag2obj(bag);
SDL_Rect *clip = layer->clip;
SDL_Rect *pos = layer->pos;
SDL_Surface *surf = layer->surface;
@@ -232,7 +217,7 @@ lmx_ahead( manager, index )
SDLx_LayerManager *manager
int index
CODE:
- SDLx_Layer *layer = bag_to_layer(*av_fetch(manager->layers, index, 0));
+ SDLx_Layer *layer = (SDLx_Layer *)bag2obj(*av_fetch(manager->layers, index, 0));
RETVAL = layers_ahead( layer );
OUTPUT:
RETVAL
@@ -242,7 +227,7 @@ lmx_behind( manager, index )
SDLx_LayerManager *manager
int index
CODE:
- SDLx_Layer *layer = bag_to_layer(*av_fetch(manager->layers, index, 0));
+ SDLx_Layer *layer = (SDLx_Layer *)bag2obj(*av_fetch(manager->layers, index, 0));
RETVAL = layers_behind( layer );
OUTPUT:
RETVAL
@@ -273,7 +258,7 @@ lmx_attach( manager, ... )
int i;
for( i = 1; i < items; i++ )
{
- SDLx_Layer *layer = bag_to_layer(ST(i));
+ SDLx_Layer *layer = (SDLx_Layer *)bag2obj(ST(i));
layer->attached = 1;
layer->attached_pos->x = layer->pos->x;
layer->attached_pos->y = layer->pos->y;
@@ -296,7 +281,7 @@ lmx_detach_xy( manager, x = -1, y = -1 )
int offset_y = 0;
while(index < length)
{
- SDLx_Layer *layer = bag_to_layer(*av_fetch(manager->layers, index, 0));
+ SDLx_Layer *layer = (SDLx_Layer *)bag2obj(*av_fetch(manager->layers, index, 0));
if(layer->attached == 1)
{
@@ -330,7 +315,7 @@ lmx_detach_back( manager )
int length = av_len( manager->layers ) + 1;
while(index < length)
{
- SDLx_Layer *layer = bag_to_layer(*av_fetch(manager->layers, index, 0));
+ SDLx_Layer *layer = (SDLx_Layer *)bag2obj(*av_fetch(manager->layers, index, 0));
if(layer->attached == 1)
{
@@ -352,7 +337,7 @@ lmx_foreground( manager, ... )
int x;
for(x = 1; x < items; x++)
{
- SDLx_Layer *layer = bag_to_layer(ST(x));
+ SDLx_Layer *layer = (SDLx_Layer *)bag2obj(ST(x));
SDLx_LayerManager *manager = layer->manager;
int index = layer->index; // we cant trust its value
int i;
View
@@ -2,6 +2,7 @@
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
+#include "helper.h"
#ifndef aTHX_
#define aTHX_
@@ -185,7 +186,7 @@ surfacex_draw_rect ( surface, rt, color )
if( SvOK(rt) )
{
int newly_created_rect = 0;
- SDL_Rect* v_rect = (SDL_Rect*)bag_to_obj(rect( rt, &newly_created_rect ));
+ SDL_Rect* v_rect = (SDL_Rect*)bag2obj(rect( rt, &newly_created_rect ));
r_rect.x = v_rect->x;
r_rect.y = v_rect->y;
r_rect.w = v_rect->w;
@@ -201,15 +202,15 @@ surfacex_blit( src, dest, ... )
CODE:
src = surface(src);
dest = surface(dest);
- SDL_Surface *_src = (SDL_Surface *)bag_to_obj(src);
- SDL_Surface *_dest = (SDL_Surface *)bag_to_obj(dest);
+ SDL_Surface *_src = (SDL_Surface *)bag2obj(src);
+ SDL_Surface *_dest = (SDL_Surface *)bag2obj(dest);
SDL_Rect _src_rect;
SDL_Rect _dest_rect;
int newly_created_rect = 0;
if( items > 2 && SvOK(ST(2)) )
- _src_rect = *(SDL_Rect *)bag_to_obj( rect(ST(2), &newly_created_rect) );
+ _src_rect = *(SDL_Rect *)bag2obj( rect(ST(2), &newly_created_rect) );
else
{
_src_rect.x = 0;
@@ -219,7 +220,7 @@ surfacex_blit( src, dest, ... )
}
if( items > 3 && SvOK(ST(3)) )
- _dest_rect = *(SDL_Rect *)bag_to_obj( rect(ST(3), &newly_created_rect) );
+ _dest_rect = *(SDL_Rect *)bag2obj( rect(ST(3), &newly_created_rect) );
else
{
_dest_rect.x = 0;
View
@@ -1,16 +1,5 @@
#include <SDL.h>
-
-SV *obj_make( int size_ptr, void* obj, char* CLASS )
-{
- SV * objref = newSV( size_ptr );
- void** pointers = malloc(2 * sizeof(void*));
- pointers[0] = (void*)obj;
- pointers[1] = (void*)PERL_GET_CONTEXT;
-
- sv_setref_pv( objref, CLASS, (void *)pointers);
-
- return objref;
-}
+#include "helper.h"
SV *rect( SV *rect, int* new_rect_made)
{
@@ -25,7 +14,7 @@ SV *rect( SV *rect, int* new_rect_made)
r->y = 0;
r->w = 0;
r->h = 0;
- retval = obj_make( sizeof( SDL_Rect *), (void *)(r), "SDL::Rect" );
+ retval = obj2bag( sizeof( SDL_Rect *), (void *)(r), "SDL::Rect" );
}
else if( sv_derived_from(rect, "ARRAY") )
{
@@ -44,7 +33,7 @@ SV *rect( SV *rect, int* new_rect_made)
}
r->x = ra[0]; r->y = ra[1]; r->w = ra[2]; r->h= ra[3];
- retval = obj_make( sizeof( SDL_Rect *), (void *)(r), "SDL::Rect" );
+ retval = obj2bag( sizeof( SDL_Rect *), (void *)(r), "SDL::Rect" );
}
else if( sv_isobject(rect) && sv_derived_from(rect, "SDL::Rect") )
{
@@ -69,19 +58,6 @@ SV *surface( SV *surface )
return NULL;
}
-void *bag_to_obj( SV *bag )
-{
- void *obj = NULL;
-
- if( sv_isobject(bag) && (SvTYPE(SvRV(bag)) == SVt_PVMG) )
- {
- void **pointers = (void **)(SvIV((SV *)SvRV( bag )));
- obj = (void *)(pointers[0]);
- }
-
- return obj;
-}
-
char *_color_format( SV *color )
{
char *retval = NULL;
@@ -180,7 +156,7 @@ AV* __list_rgb( SV* color )
{
RETVAL = newAV();
sv_2mortal((SV *)RETVAL);
- SDL_Color *_color = (SDL_Color *)bag_to_obj(color);
+ SDL_Color *_color = (SDL_Color *)bag2obj(color);
av_push(RETVAL, newSVuv(_color->r));
av_push(RETVAL, newSVuv(_color->g));
av_push(RETVAL, newSVuv(_color->b));
@@ -220,7 +196,7 @@ AV* __list_rgba( SV* color )
{
RETVAL = newAV();
sv_2mortal((SV *)RETVAL);
- SDL_Color *_color = (SDL_Color*)bag_to_obj(color);
+ SDL_Color *_color = (SDL_Color*)bag2obj(color);
av_push(RETVAL, newSVuv(_color->r));
av_push(RETVAL, newSVuv(_color->g));
av_push(RETVAL, newSVuv(_color->b));
Oops, something went wrong.

0 comments on commit 8573a51

Please sign in to comment.