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