Expose SDL::MapRGB
[sdlgit/SDL_perl.git] / src / SDL.xs
index 599340d..6e07fe1 100644 (file)
@@ -97,7 +97,27 @@ static int sdl_perl_use_smpeg_audio = 0;
 #define HAVE_TLS_CONTEXT
 #endif
 
-#include "src/defines.h"
+/* For windows  */
+#ifndef SDL_PERL_DEFINES_H
+#define SDL_PERL_DEFINES_H
+
+#ifdef HAVE_TLS_CONTEXT
+PerlInterpreter *parent_perl = NULL;
+extern PerlInterpreter *parent_perl;
+#define GET_TLS_CONTEXT parent_perl =  PERL_GET_CONTEXT;
+#define ENTER_TLS_CONTEXT \
+        PerlInterpreter *current_perl = PERL_GET_CONTEXT; \
+               PERL_SET_CONTEXT(parent_perl); { \
+                                       PerlInterpreter *my_perl = parent_perl;
+#define LEAVE_TLS_CONTEXT \
+                                               } PERL_SET_CONTEXT(current_perl);
+#else
+#define GET_TLS_CONTEXT         /* TLS context not enabled */
+#define ENTER_TLS_CONTEXT       /* TLS context not enabled */
+#define LEAVE_TLS_CONTEXT       /* TLS context not enabled */
+#endif
+
+#endif
 
 Uint32 
 sdl_perl_timer_callback ( Uint32 interval, void* param )
@@ -838,22 +858,22 @@ GetKeyName ( sym )
        OUTPUT:
                RETVAL
 
+#ifdef HAVE_SDL_IMAGE
+
 SDL_Surface *
-CreateRGBSurface (flags, width, height, depth, Rmask, Gmask, Bmask, Amask )
-       Uint32 flags
-       int width
-       int height
-       int depth
-       Uint32 Rmask
-       Uint32 Gmask
-       Uint32 Bmask
-       Uint32 Amask
+IMG_Load ( filename )
+       char *filename
        CODE:
-               RETVAL = SDL_CreateRGBSurface ( flags, width, height,
-                               depth, Rmask, Gmask, Bmask, Amask );
-       OUTPUT: 
+               char* CLASS = "SDL::Surface";
+               RETVAL = IMG_Load(filename);
+       OUTPUT:
                RETVAL
 
+#endif
+
+=for comment
+
+Comment out for now as it does not compile
 
 SDL_Surface *
 CreateRGBSurfaceFrom (pixels, width, height, depth, pitch, Rmask, Gmask, Bmask, Amask )
@@ -876,18 +896,6 @@ CreateRGBSurfaceFrom (pixels, width, height, depth, pitch, Rmask, Gmask, Bmask,
        OUTPUT: 
                RETVAL
 
-#ifdef HAVE_SDL_IMAGE
-
-SDL_Surface *
-IMGLoad ( fname )
-       char *fname
-       CODE:
-               RETVAL = IMG_Load(fname);
-       OUTPUT:
-               RETVAL
-
-#endif
-
 SDL_Surface*
 SurfaceCopy ( surface )
        SDL_Surface *surface
@@ -1059,6 +1067,12 @@ SurfacePixels ( surface )
        OUTPUT:
                RETVAL
 
+=cut
+
+=for comment
+
+Comment out for now as it does not compile
+
 SDL_Color*
 SurfacePixel ( surface, x, y, ... )
        SDL_Surface *surface
@@ -1152,6 +1166,7 @@ GetVideoSurface ()
        OUTPUT:
                RETVAL
 
+=cut
 
 HV *
 VideoInfo ()
@@ -1185,63 +1200,6 @@ VideoInfo ()
        OUTPUT:
                RETVAL
 
-SDL_Rect *
-NewRect ( x, y, w, h )
-       Sint16 x
-       Sint16 y
-       Uint16 w
-       Uint16 h
-       CODE:
-               RETVAL = (SDL_Rect *) safemalloc (sizeof(SDL_Rect));
-               RETVAL->x = x;
-               RETVAL->y = y;
-               RETVAL->w = w;
-               RETVAL->h = h;
-       OUTPUT:
-               RETVAL
-
-void
-FreeRect ( rect )
-       SDL_Rect *rect
-       CODE:
-               safefree(rect);
-
-Sint16
-RectX ( rect, ... )
-       SDL_Rect *rect
-       CODE:
-               if (items > 1 ) rect->x = SvIV(ST(1)); 
-               RETVAL = rect->x;
-       OUTPUT:
-               RETVAL
-
-Sint16
-RectY ( rect, ... )
-       SDL_Rect *rect
-       CODE:
-               if (items > 1 ) rect->y = SvIV(ST(1)); 
-               RETVAL = rect->y;
-       OUTPUT:
-               RETVAL
-
-Uint16
-RectW ( rect, ... )
-       SDL_Rect *rect
-       CODE:
-               if (items > 1 ) rect->w = SvIV(ST(1)); 
-               RETVAL = rect->w;
-       OUTPUT:
-               RETVAL
-
-Uint16
-RectH ( rect, ... )
-       SDL_Rect *rect
-       CODE:
-               if (items > 1 ) rect->h = SvIV(ST(1)); 
-               RETVAL = rect->h;
-       OUTPUT:
-               RETVAL
-
 AV*
 ListModes ( format, flags )
        Uint32 flags
@@ -1262,73 +1220,74 @@ ListModes ( format, flags )
        OUTPUT:
                RETVAL
 
-
-SDL_Color *
-NewColor ( r, g, b )
-       Uint8 r
-       Uint8 g
-       Uint8 b
+SDL_Palette *
+NewPalette ( number )
+       int number
        CODE:
-               RETVAL = (SDL_Color *) safemalloc(sizeof(SDL_Color));
-               RETVAL->r = r;
-               RETVAL->g = g;
-               RETVAL->b = b;
+               RETVAL = (SDL_Palette *)safemalloc(sizeof(SDL_Palette));
+               RETVAL->colors = (SDL_Color *)safemalloc(number * 
+                                               sizeof(SDL_Color));
+               RETVAL->ncolors = number;
        OUTPUT:
                RETVAL
 
-Uint8
-ColorR ( color, ... )
-       SDL_Color *color
+int
+PaletteNColors ( palette, ... )
+       SDL_Palette *palette
        CODE:
-               if (items > 1 ) color->r = SvIV(ST(1)); 
-               RETVAL = color->r;
+               if ( items > 1 ) palette->ncolors = SvIV(ST(1));
+               RETVAL = palette->ncolors;
        OUTPUT:
                RETVAL
 
-Uint8
-ColorG ( color, ... )
-       SDL_Color *color
+SDL_Surface *
+SetVideoMode ( width, height, bpp, flags )
+
+       int width
+       int height
+       int bpp
+       Uint32 flags
+
        CODE:
-               if (items > 1 ) color->g = SvIV(ST(1)); 
-               RETVAL = color->g;
+               char *CLASS = "SDL::Surface";
+               RETVAL = SDL_SetVideoMode(width,height,bpp,flags);
        OUTPUT:
                RETVAL
 
-Uint8
-ColorB ( color, ... )
-       SDL_Color *color
+int
+Flip ( surface )
+       SDL_Surface *surface
        CODE:
-               if (items > 1 ) color->b = SvIV(ST(1)); 
-               RETVAL = color->b;
+               RETVAL = SDL_Flip(surface);
        OUTPUT:
                RETVAL
 
-void
-FreeColor ( color )
-       SDL_Color *color
-       CODE:
-               return; safefree(color);
-
-SDL_Palette *
-NewPalette ( number )
-       int number
+int
+BlitSurface ( src, src_rect, dest, dest_rect )
+       SDL_Surface *src
+       SDL_Surface *dest
+       SDL_Rect *src_rect
+       SDL_Rect *dest_rect
        CODE:
-               RETVAL = (SDL_Palette *)safemalloc(sizeof(SDL_Palette));
-               RETVAL->colors = (SDL_Color *)safemalloc(number * 
-                                               sizeof(SDL_Color));
-               RETVAL->ncolors = number;
+               RETVAL = SDL_BlitSurface(src,src_rect,dest,dest_rect);
        OUTPUT:
                RETVAL
 
-int
-PaletteNColors ( palette, ... )
-       SDL_Palette *palette
+Uint32
+MapRGB ( pixel_format, r, g, b )
+       SDL_PixelFormat *pixel_format
+       Uint8 r
+       Uint8 g
+       Uint8 b
        CODE:
-               if ( items > 1 ) palette->ncolors = SvIV(ST(1));
-               RETVAL = palette->ncolors;
+               RETVAL = SDL_MapRGB(pixel_format,r,g,b);
        OUTPUT:
                RETVAL
 
+=for comment
+
+Comment out for now as it does not compile
+
 SDL_Color *
 PaletteColors ( palette, index, ... )
        SDL_Palette *palette
@@ -1343,6 +1302,8 @@ PaletteColors ( palette, index, ... )
        OUTPUT:
                RETVAL
 
+
+
 int
 VideoModeOK ( width, height, bpp, flags )
        int width
@@ -1382,7 +1343,8 @@ UpdateRects ( surface, ... )
                SDL_Rect *rects, *temp;
                int num_rects,i;
                if ( items < 2 ) return;
-               num_rects = items - 1;  
+               num_rects = items - 1;
+               SDL_UpdateRects(surface,num_rects,rects);                       
                rects = (SDL_Rect *)safemalloc(sizeof(SDL_Rect)*items);
                for(i=0;i<num_rects;i++) {
                        temp = (SDL_Rect *)SvIV(ST(i+1));
@@ -1425,17 +1387,6 @@ all_done:
                RETVAL
 
 Uint32
-MapRGB ( surface, r, g, b )
-       SDL_Surface *surface
-       Uint8 r
-       Uint8 g
-       Uint8 b
-       CODE:
-               RETVAL = SDL_MapRGB(surface->format,r,g,b);
-       OUTPUT:
-               RETVAL
-
-Uint32
 MapRGBA ( surface, r, g, b, a )
        SDL_Surface *surface
        Uint8 r
@@ -1574,22 +1525,16 @@ ConvertRGBA ( surface )
        OUTPUT:
                RETVAL
 
-int
-BlitSurface ( src, src_rect, dest, dest_rect )
-       SDL_Surface *src
-       SDL_Rect *src_rect
-       SDL_Surface *dest
-       SDL_Rect *dest_rect
-       CODE:
-               RETVAL = SDL_BlitSurface(src,src_rect,dest,dest_rect);
-       OUTPUT:
-               RETVAL
+=cut
 
 int
 FillRect ( dest, dest_rect, color )
        SDL_Surface *dest
-       SDL_Rect *dest_rect
+       
        SDL_Color *color
+       
+       SDL_Rect *dest_rect
+
        CODE:
                Uint32 pixel = SDL_MapRGB(dest->format,color->r,color->g,color->b);
                RETVAL = SDL_FillRect(dest,dest_rect,pixel);
@@ -2215,6 +2160,15 @@ MixPlayingMusic()
        OUTPUT:
                RETVAL
 
+int
+MixSetPanning ( channel, left, right )
+       int channel
+       int left
+       int right
+       CODE:
+               RETVAL = Mix_SetPanning(channel, left, right);
+       OUTPUT:
+               RETVAL
 
 void
 MixCloseAudio ()
@@ -2411,7 +2365,7 @@ JoyAxisEventAxis ( e )
         OUTPUT:
                 RETVAL
 
-Uint8
+Sint16
 JoyAxisEventValue ( e )
         SDL_Event *e
         CODE:
@@ -2506,14 +2460,12 @@ SetClipRect ( surface, rect )
        CODE:
                SDL_SetClipRect(surface,rect);
        
-SDL_Rect*
-GetClipRect ( surface )
+void
+GetClipRect ( surface, rect )
        SDL_Surface *surface
+       SDL_Rect *rect;
        CODE:
-               RETVAL = (SDL_Rect*) safemalloc(sizeof(SDL_Rect));
-               SDL_GetClipRect(surface,RETVAL);
-       OUTPUT:
-               RETVAL
+               SDL_GetClipRect(surface, rect);
 
 
 #ifdef HAVE_SDL_NET
@@ -2944,9 +2896,19 @@ TTFSizeText ( font, text )
        CODE:
                int w,h;
                RETVAL = newAV();
-               TTF_SizeText(font,text,&w,&h);
-               av_push(RETVAL,newSViv(w));
-               av_push(RETVAL,newSViv(h));
+               if(TTF_SizeText(font,text,&w,&h))
+               {
+                       printf("TTF error at TTFSizeText: %s \n", TTF_GetError());
+                       Perl_croak (aTHX_ "TTF error \n");
+               }
+               else
+               {
+                       av_push(RETVAL,newSViv(w));
+                       av_push(RETVAL,newSViv(h));
+                       sv_2mortal((SV*)RETVAL);
+               }
+               
+       
        OUTPUT:
                RETVAL
 
@@ -2957,9 +2919,19 @@ TTFSizeUTF8 ( font, text )
        CODE:
                int w,h;
                RETVAL = newAV();
-               TTF_SizeUTF8(font,text,&w,&h);
-               av_push(RETVAL,newSViv(w));
-               av_push(RETVAL,newSViv(h));
+               if(TTF_SizeUTF8(font,text,&w,&h))
+               {
+                       av_push(RETVAL,newSViv(w));
+                       av_push(RETVAL,newSViv(h));
+                       sv_2mortal((SV*)RETVAL);
+
+               }
+               else
+               {
+                       printf("TTF error at TTFSizeUTF8 with : %s \n", TTF_GetError());
+                       Perl_croak (aTHX_ "TTF error \n");
+               }
+               
        OUTPUT:
                RETVAL
 
@@ -2970,12 +2942,24 @@ TTFSizeUNICODE ( font, text )
        CODE:
                int w,h;
                RETVAL = newAV();
-               TTF_SizeUNICODE(font,text,&w,&h);
-               av_push(RETVAL,newSViv(w));
-               av_push(RETVAL,newSViv(h));
+               if(TTF_SizeUNICODE(font,text,&w,&h))
+               {
+                       av_push(RETVAL,newSViv(w));
+                       av_push(RETVAL,newSViv(h));
+                       sv_2mortal((SV*)RETVAL);
+
+               }
+               else
+               {
+                       printf("TTF error at TTFSizeUNICODE : %s \n", TTF_GetError()); 
+                       Perl_croak (aTHX_ "TTF error \n");
+               }
+
        OUTPUT:
                RETVAL
 
+=for comment
+
 SDL_Surface*
 TTFRenderTextSolid ( font, text, fg )
        TTF_Font *font
@@ -3105,6 +3089,7 @@ TTFCloseFont ( font )
        TTF_Font *font
        CODE:
                TTF_CloseFont(font);
+               font=NULL; //to be safe http://sdl.beuc.net/sdl.wiki/SDL_ttf_copy_Functions_Management_TTF_CloseFont
 
 SDL_Surface*
 TTFPutString ( font, mode, surface, x, y, fg, bg, text )
@@ -3198,6 +3183,8 @@ TTFPutString ( font, mode, surface, x, y, fg, bg, text )
        OUTPUT:
                RETVAL
 
+=cut
+
 #endif
 
 SDL_Overlay*
@@ -3637,6 +3624,8 @@ SMPEGGetInfo ( mpeg )
 
 #ifdef HAVE_SDL_GFX
 
+=cut
+
 SDL_Surface *
 GFXRotoZoom ( src, angle, zoom, smooth)
        SDL_Surface * src
@@ -3659,6 +3648,8 @@ GFXZoom ( src, zoomx, zoomy, smooth)
        OUTPUT:
                RETVAL
 
+=cut
+
 int
 GFXPixelColor ( dst, x, y, color )
        SDL_Surface* dst