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