Move display format and display format alpha out of Surface.xs
[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_rects ( surface, ... )
59         SDL_Surface *surface
60         CODE:
61                 SDL_Rect *rects, *temp;
62                 int num_rects,i;
63                 if ( items < 2 ) return;
64                 num_rects = items - 1;
65                         
66                 rects = (SDL_Rect *)safemalloc(sizeof(SDL_Rect)*items);
67                 for(i=0;i<num_rects;i++) {
68                         temp = (SDL_Rect *)SvIV(ST(i+1));
69                         rects[i].x = temp->x;
70                         rects[i].y = temp->y;
71                         rects[i].w = temp->w;
72                         rects[i].h = temp->h;
73                 } 
74                 SDL_UpdateRects(surface,num_rects,rects);
75                 safefree(rects);
76
77 Uint16
78 surface_pitch( surface )
79         SDL_Surface *surface
80         CODE:
81                 RETVAL = surface->pitch;
82         OUTPUT:
83                 RETVAL
84
85 Uint16
86 surface_w ( surface )
87         SDL_Surface *surface
88         CODE:
89                 RETVAL = surface->w;
90         OUTPUT:
91                 RETVAL
92
93 Uint16
94 surface_h ( surface )
95         SDL_Surface *surface
96         CODE:
97                 RETVAL = surface->h;
98         OUTPUT:
99                 RETVAL
100
101 IV
102 surface_get_pixels(surface)
103         SDL_Surface *surface
104         CODE:
105           if(!surface->pixels) croak("Incomplete surface");
106           RETVAL = PTR2IV(surface->pixels);
107         OUTPUT:
108           RETVAL
109
110 void
111 surface_set_pixels(surface, pixels)
112         SDL_Surface *surface
113
114         SV *pixels
115
116         PREINIT:
117           STRLEN len;
118           void *p;
119
120         CODE:
121           p = SvPV(pixels, len);
122           if (len > surface->pitch*surface->h)
123                 len = surface->pitch*surface->h;
124           memcpy(surface->pixels, p, len);
125
126 void
127 surface_DESTROY(surface)
128         SDL_Surface *surface
129         CODE:
130                 Uint8* pixels = surface->pixels;
131                 Uint32 flags = surface->flags;
132                 SDL_FreeSurface(surface);
133                 if (flags & SDL_PREALLOC)
134                         Safefree(pixels);