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