d3b3608eff658a8c0ff9ef2bb0570af2ff117d87
[sdlgit/SDL_perl.git] / src / Core / Video.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
12 static Uint16* av_to_uint16 (AV* av)
13 {
14         int len = av_len(av);
15         if( len != -1)
16         {
17         int i;
18         Uint16* table = (Uint16 *)safemalloc(sizeof(Uint16)*(len));
19         for ( i = 0; i < len+1 ; i++ ){ 
20                 SV ** temp = av_fetch(av,i,0);
21               if( temp != NULL)
22                 {
23                         table[i] =  (Uint16 *) SvIV(  *temp   );
24                 }
25                 else { table[i] =0; }
26
27         }
28         return table;
29         }
30         return NULL;
31 }
32
33
34
35 MODULE = SDL::Video     PACKAGE = SDL::Video    PREFIX = video_
36
37 =for documentation
38
39 The Following are XS bindings to the Video category in the SDL API v2.1.13
40
41 Describe on the SDL API site.
42
43 See: L<http://www.libsdl.org/cgi/docwiki.cgi/SDL_API#head-813f033ec44914f267f32195aba7d9aff8c410c0>
44
45 =cut
46
47 SDL_Surface *
48 video_get_video_surface()
49         PREINIT:
50                 char* CLASS = "SDL::Surface";
51         CODE:
52                 RETVAL = SDL_GetVideoSurface();
53         OUTPUT:
54                 RETVAL
55
56
57 SDL_VideoInfo*
58 video_get_video_info()
59         PREINIT:
60                 char* CLASS = "SDL::VideoInfo";
61         CODE:
62                 RETVAL = (SDL_VideoInfo *) SDL_GetVideoInfo();
63
64         OUTPUT:
65                 RETVAL
66
67 SV *
68 video_video_driver_name( )
69         
70         CODE:
71                 char buffer[1024];
72                 if ( SDL_VideoDriverName(buffer, 1024) != NULL ) 
73                 { 
74                         RETVAL =  newSVpv(buffer, 0);
75                 } 
76                 else 
77                          XSRETURN_UNDEF;        
78         OUTPUT:
79                 RETVAL
80
81 AV*
82 list_modes ( format, flags )
83         Uint32 flags
84         SDL_PixelFormat *format
85
86         CODE:
87                 SDL_Rect **mode;
88                 RETVAL = newAV();
89                 mode = SDL_ListModes(format,flags);
90                 if (mode == (SDL_Rect**)-1 ) {
91                         av_push(RETVAL,newSVpv("all",0));
92                 } else if (! mode ) {
93                         av_push(RETVAL,newSVpv("none",0));
94                 } else {
95                         for (;*mode;mode++) {
96                                 av_push(RETVAL,newSViv(PTR2IV(*mode)));
97                         }
98                 }
99         OUTPUT:
100                 RETVAL
101
102
103 int
104 video_video_mode_ok ( width, height, bpp, flags )
105         int width
106         int height
107         int bpp
108         Uint32 flags
109         CODE:
110                 RETVAL = SDL_VideoModeOK(width,height,bpp,flags);
111         OUTPUT:
112                 RETVAL
113
114
115 SDL_Surface *
116 video_set_video_mode ( width, height, bpp, flags )
117         int width
118         int height
119         int bpp
120         Uint32 flags
121         PREINIT:
122                 char* CLASS = "SDL::Surface";
123         CODE:
124                 RETVAL = SDL_SetVideoMode(width,height,bpp,flags);
125         OUTPUT:
126                 RETVAL
127
128
129 void
130 video_update_rect ( surface, x, y, w ,h )
131         SDL_Surface *surface
132         int x
133         int y
134         int w
135         int h
136         CODE:
137                 SDL_UpdateRect(surface,x,y,w,h);
138
139 void
140 video_update_rects ( surface, ... )
141         SDL_Surface *surface
142         CODE:
143                 SDL_Rect *rects;
144                 int num_rects,i;
145                 if ( items < 2 ) return;
146                 num_rects = items - 1;
147                 rects = (SDL_Rect *)safemalloc(sizeof(SDL_Rect)*items);
148                 for(i=0;i<num_rects;i++) {
149                         rects[i] = *(SDL_Rect *)SvIV((SV*)SvRV( ST(i + 1) ));
150                 }
151                 SDL_UpdateRects(surface,num_rects,rects);
152                 safefree(rects);
153
154
155 int
156 video_flip ( surface )
157         SDL_Surface *surface
158         CODE:
159                 RETVAL = SDL_Flip(surface);
160         OUTPUT:
161                 RETVAL
162
163 int
164 video_set_colors ( surface, start, ... )
165         SDL_Surface *surface
166         int start
167         CODE:
168                 SDL_Color *colors,*temp;
169                 int i, length;
170                 if ( items < 3 ) { RETVAL = 0;}
171                 else
172                 {
173                 length = items - 2;
174                 colors = (SDL_Color *)safemalloc(sizeof(SDL_Color)*(length+1));
175                 for ( i = 0; i < length ; i++ ) {
176                         temp = (SDL_Color *)SvIV(ST(i+2));
177                         colors[i].r = temp->r;
178                         colors[i].g = temp->g;
179                         colors[i].b = temp->b;
180                 }
181                 RETVAL = SDL_SetColors(surface, colors, start, length );
182                 safefree(colors);
183                 }       
184
185         OUTPUT: 
186                 RETVAL
187
188 int
189 video_set_palette ( surface, flags, start, ... )
190         SDL_Surface *surface
191         int flags
192         int start
193
194         CODE:
195                 SDL_Color *colors,*temp;
196                 int i, length;
197                 if ( items < 4 ) { 
198                 RETVAL = 0;
199                         }
200                 else
201                 {               
202                 length = items - 3;
203                 colors = (SDL_Color *)safemalloc(sizeof(SDL_Color)*(length+1));
204                 for ( i = 0; i < length ; i++ ){ 
205                         temp = (SDL_Color *)SvIV(ST(i+3));
206                         colors[i].r = temp->r;
207                         colors[i].g = temp->g;
208                         colors[i].b = temp->b;
209                 }
210                 RETVAL = SDL_SetPalette(surface, flags, colors, start, length );
211                 safefree(colors);
212                 }
213         OUTPUT: 
214                 RETVAL
215
216 int
217 video_set_gamma(r, g, b)
218         float r;
219         float g;
220         float b;
221         CODE:
222                 RETVAL = SDL_SetGamma(r,g,b);
223         OUTPUT: 
224                 RETVAL
225
226         
227 int
228 video_set_gamma_ramp( rt, gt, bt )
229         AV* rt;
230         AV* gt;
231         AV* bt;
232         CODE:
233                 Uint16 *redtable, *greentable, *bluetable;
234                 redtable = av_to_uint16(rt);
235                 greentable = av_to_uint16(gt);
236                 bluetable = av_to_uint16(bt);
237                 RETVAL =  SDL_SetGammaRamp(redtable, greentable, bluetable);
238                 if( redtable != NULL) { safefree(redtable); }
239                 if( greentable != NULL) { safefree(greentable); }
240                 if( bluetable != NULL) { safefree(bluetable); } 
241         OUTPUT:
242                 RETVAL 
243
244
245
246 Uint32
247 video_map_RGB ( pixel_format, r, g, b )
248         SDL_PixelFormat *pixel_format
249         Uint8 r
250         Uint8 g
251         Uint8 b
252         CODE:
253                 RETVAL = SDL_MapRGB(pixel_format,r,g,b);
254         OUTPUT:
255                 RETVAL
256
257 Uint32
258 video_map_RGBA ( pixel_format, r, g, b, a )
259         SDL_PixelFormat *pixel_format
260         Uint8 r
261         Uint8 g
262         Uint8 b 
263         Uint8 a
264         CODE:
265                 RETVAL = SDL_MapRGB(pixel_format,r,g,b);
266         OUTPUT:
267                 RETVAL
268