Move Surface.xs to src/Core/objects/
[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
49 SDL_Surface *
50 surface_load (CLASS, filename )
51         char* CLASS
52         char* filename
53         CODE:
54                 RETVAL = IMG_Load(filename);
55         OUTPUT:
56                 RETVAL
57
58 int
59 surface_blit ( src, src_rect, dest, dest_rect )
60         SDL_Surface *src
61         SDL_Surface *dest
62         SDL_Rect *src_rect
63         SDL_Rect *dest_rect
64         CODE:
65                 RETVAL = SDL_BlitSurface(src,src_rect,dest,dest_rect);
66         OUTPUT:
67                 RETVAL
68
69 int
70 surface_fill_rect ( dest, dest_rect, color )
71         SDL_Surface *dest
72         SDL_Color *color
73         SDL_Rect *dest_rect
74         CODE:
75                 Uint32 pixel = SDL_MapRGB(dest->format,color->r,color->g,color->b);
76                 RETVAL = SDL_FillRect(dest,dest_rect,pixel);
77         OUTPUT:
78                 RETVAL
79                 
80 void
81 surface_update_rect ( surface, x, y, w ,h )
82         SDL_Surface *surface
83         int x
84         int y
85         int w
86         int h
87         CODE:
88                 SDL_UpdateRect(surface,x,y,w,h);
89
90 void
91 surface_update_rects ( surface, ... )
92         SDL_Surface *surface
93         CODE:
94                 SDL_Rect *rects, *temp;
95                 int num_rects,i;
96                 if ( items < 2 ) return;
97                 num_rects = items - 1;
98                         
99                 rects = (SDL_Rect *)safemalloc(sizeof(SDL_Rect)*items);
100                 for(i=0;i<num_rects;i++) {
101                         temp = (SDL_Rect *)SvIV(ST(i+1));
102                         rects[i].x = temp->x;
103                         rects[i].y = temp->y;
104                         rects[i].w = temp->w;
105                         rects[i].h = temp->h;
106                 } 
107                 SDL_UpdateRects(surface,num_rects,rects);
108                 safefree(rects);
109
110 SDL_Surface *
111 surface_display ( surface )
112         SDL_Surface *surface
113         CODE:
114                 char* CLASS = "SDL::Surface";
115                 RETVAL = SDL_DisplayFormat(surface);
116         OUTPUT:
117                 RETVAL
118
119 SDL_Surface *
120 surface_display_alpha ( surface )
121         SDL_Surface *surface
122         CODE:
123                 char* CLASS = "SDL::Surface";
124                 RETVAL = SDL_DisplayFormatAlpha(surface);
125         OUTPUT:
126                 RETVAL
127
128 Uint16
129 surface_pitch( surface )
130         SDL_Surface *surface
131         CODE:
132                 RETVAL = surface->pitch;
133         OUTPUT:
134                 RETVAL
135
136 Uint16
137 surface_w ( surface )
138         SDL_Surface *surface
139         CODE:
140                 RETVAL = surface->w;
141         OUTPUT:
142                 RETVAL
143
144 Uint16
145 surface_h ( surface )
146         SDL_Surface *surface
147         CODE:
148                 RETVAL = surface->h;
149         OUTPUT:
150                 RETVAL
151
152 IV
153 surface_get_pixels(surface)
154         SDL_Surface *surface
155         CODE:
156           if(!surface->pixels) croak("Incomplete surface");
157           RETVAL = PTR2IV(surface->pixels);
158         OUTPUT:
159           RETVAL
160
161 void
162 surface_set_pixels(surface, pixels)
163         SDL_Surface *surface
164
165         SV *pixels
166
167         PREINIT:
168           STRLEN len;
169           void *p;
170
171         CODE:
172           p = SvPV(pixels, len);
173           if (len > surface->pitch*surface->h)
174                 len = surface->pitch*surface->h;
175           memcpy(surface->pixels, p, len);
176
177 void
178 surface_DESTROY(surface)
179         SDL_Surface *surface
180         CODE:
181                 Uint8* pixels = surface->pixels;
182                 Uint32 flags = surface->flags;
183                 SDL_FreeSurface(surface);
184                 if (flags & SDL_PREALLOC)
185                         Safefree(pixels);