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