Wrap PixelFormat
[sdlgit/SDL_perl.git] / src / Core / objects / Surface.xs
CommitLineData
88a46efc 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
11MODULE = SDL::Surface PACKAGE = SDL::Surface PREFIX = surface_
12
13=for documentation
14
15SDL_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
31SDL_Surface *
32surface_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
50d9130a 48SDL_PixelFormat *
49surface_format ( surface )
50 SDL_Surface *surface
51 CODE:
52 char* CLASS = "SDL::PixelFormat";
53 RETVAL = surface->format;
54 OUTPUT:
55 RETVAL
56
88a46efc 57int
88a46efc 58surface_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
68void
69surface_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
78void
79surface_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
98SDL_Surface *
99surface_display ( surface )
100 SDL_Surface *surface
101 CODE:
41a5a9ee 102 char* CLASS = "SDL::Surface";
88a46efc 103 RETVAL = SDL_DisplayFormat(surface);
104 OUTPUT:
105 RETVAL
106
41a5a9ee 107SDL_Surface *
108surface_display_alpha ( surface )
109 SDL_Surface *surface
110 CODE:
111 char* CLASS = "SDL::Surface";
112 RETVAL = SDL_DisplayFormatAlpha(surface);
113 OUTPUT:
114 RETVAL
115
88a46efc 116Uint16
18ef5a29 117surface_pitch( surface )
118 SDL_Surface *surface
119 CODE:
120 RETVAL = surface->pitch;
121 OUTPUT:
122 RETVAL
123
124Uint16
88a46efc 125surface_w ( surface )
126 SDL_Surface *surface
127 CODE:
128 RETVAL = surface->w;
129 OUTPUT:
130 RETVAL
131
132Uint16
133surface_h ( surface )
134 SDL_Surface *surface
135 CODE:
136 RETVAL = surface->h;
137 OUTPUT:
138 RETVAL
139
18ef5a29 140IV
141surface_get_pixels(surface)
142 SDL_Surface *surface
143 CODE:
b7ed9095 144 if(!surface->pixels) croak("Incomplete surface");
793e8806 145 RETVAL = PTR2IV(surface->pixels);
18ef5a29 146 OUTPUT:
793e8806 147 RETVAL
18ef5a29 148
149void
150surface_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
88a46efc 165void
166surface_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);