Move blit out of Surface.xs, enable BlitSurface
[sdlgit/SDL_perl.git] / src / Core / objects / Surface.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #ifndef aTHX_
6 #define aTHX_
7 #endif
8
9 #include <SDL.h>
10
11 MODULE = SDL::Surface   PACKAGE = SDL::Surface    PREFIX = surface_
12
13 =for documentation
14
15 SDL_Surface -- Graphic surface structure 
16
17   typedef struct SDL_Surface {
18       Uint32 flags;                           /* Read-only */
19       SDL_PixelFormat *format;                /* Read-only */
20       int w, h;                               /* Read-only */
21       Uint16 pitch;                           /* Read-only */
22       void *pixels;                           /* Read-write */
23       SDL_Rect clip_rect;                     /* Read-only */
24       int refcount;                           /* Read-mostly */
25
26     /* This structure also contains private fields not shown here */
27   } SDL_Surface;
28
29 =cut
30
31 SDL_Surface *
32 surface_new (CLASS, flags, width, height, depth, Rmask, Gmask, Bmask, Amask )
33         char* CLASS
34         Uint32 flags
35         int width
36         int height
37         int depth
38         Uint32 Rmask
39         Uint32 Gmask
40         Uint32 Bmask
41         Uint32 Amask
42         CODE:
43                 RETVAL = SDL_CreateRGBSurface ( flags, width, height,
44                                 depth, Rmask, Gmask, Bmask, Amask );
45         OUTPUT:
46                 RETVAL
47
48 int
49 surface_fill_rect ( dest, dest_rect, color )
50         SDL_Surface *dest
51         SDL_Color *color
52         SDL_Rect *dest_rect
53         CODE:
54                 Uint32 pixel = SDL_MapRGB(dest->format,color->r,color->g,color->b);
55                 RETVAL = SDL_FillRect(dest,dest_rect,pixel);
56         OUTPUT:
57                 RETVAL
58                 
59 void
60 surface_update_rect ( surface, x, y, w ,h )
61         SDL_Surface *surface
62         int x
63         int y
64         int w
65         int h
66         CODE:
67                 SDL_UpdateRect(surface,x,y,w,h);
68
69 void
70 surface_update_rects ( surface, ... )
71         SDL_Surface *surface
72         CODE:
73                 SDL_Rect *rects, *temp;
74                 int num_rects,i;
75                 if ( items < 2 ) return;
76                 num_rects = items - 1;
77                         
78                 rects = (SDL_Rect *)safemalloc(sizeof(SDL_Rect)*items);
79                 for(i=0;i<num_rects;i++) {
80                         temp = (SDL_Rect *)SvIV(ST(i+1));
81                         rects[i].x = temp->x;
82                         rects[i].y = temp->y;
83                         rects[i].w = temp->w;
84                         rects[i].h = temp->h;
85                 } 
86                 SDL_UpdateRects(surface,num_rects,rects);
87                 safefree(rects);
88
89 SDL_Surface *
90 surface_display ( surface )
91         SDL_Surface *surface
92         CODE:
93                 char* CLASS = "SDL::Surface";
94                 RETVAL = SDL_DisplayFormat(surface);
95         OUTPUT:
96                 RETVAL
97
98 SDL_Surface *
99 surface_display_alpha ( surface )
100         SDL_Surface *surface
101         CODE:
102                 char* CLASS = "SDL::Surface";
103                 RETVAL = SDL_DisplayFormatAlpha(surface);
104         OUTPUT:
105                 RETVAL
106
107 Uint16
108 surface_pitch( surface )
109         SDL_Surface *surface
110         CODE:
111                 RETVAL = surface->pitch;
112         OUTPUT:
113                 RETVAL
114
115 Uint16
116 surface_w ( surface )
117         SDL_Surface *surface
118         CODE:
119                 RETVAL = surface->w;
120         OUTPUT:
121                 RETVAL
122
123 Uint16
124 surface_h ( surface )
125         SDL_Surface *surface
126         CODE:
127                 RETVAL = surface->h;
128         OUTPUT:
129                 RETVAL
130
131 IV
132 surface_get_pixels(surface)
133         SDL_Surface *surface
134         CODE:
135           if(!surface->pixels) croak("Incomplete surface");
136           RETVAL = PTR2IV(surface->pixels);
137         OUTPUT:
138           RETVAL
139
140 void
141 surface_set_pixels(surface, pixels)
142         SDL_Surface *surface
143
144         SV *pixels
145
146         PREINIT:
147           STRLEN len;
148           void *p;
149
150         CODE:
151           p = SvPV(pixels, len);
152           if (len > surface->pitch*surface->h)
153                 len = surface->pitch*surface->h;
154           memcpy(surface->pixels, p, len);
155
156 void
157 surface_DESTROY(surface)
158         SDL_Surface *surface
159         CODE:
160                 Uint8* pixels = surface->pixels;
161                 Uint32 flags = surface->flags;
162                 SDL_FreeSurface(surface);
163                 if (flags & SDL_PREALLOC)
164                         Safefree(pixels);