PageRenderTime 16ms CodeModel.GetById 11ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Core/Video.xs

http://github.com/PerlGameDev/SDL
Unknown | 590 lines | 512 code | 78 blank | 0 comment | 0 complexity | 5f69212b6ad3e3a7b14a1863844760b2 MD5 | raw file
  1#include "EXTERN.h"
  2#include "perl.h"
  3#include "XSUB.h"
  4#include "ppport.h"
  5#include "helper.h"
  6
  7#ifndef aTHX_
  8#define aTHX_
  9#endif
 10
 11#include <SDL.h>
 12
 13void _uinta_free(Uint16* av, int len_from_av_len)
 14{
 15	if( av != NULL)
 16	  return;
 17
 18	safefree(av);
 19}
 20
 21Uint16* av_to_uint16 (AV* av)
 22{
 23	int len = av_len(av);
 24	if( len != -1)
 25	{
 26		int i;
 27		Uint16* table = (Uint16 *)safemalloc(sizeof(Uint16)*(len+1));
 28		for ( i = 0; i < len+1 ; i++ )
 29		{
 30			SV ** temp = av_fetch(av,i,0);
 31			if( temp != NULL )
 32			{
 33				table[i] = (Uint16) SvIV( *temp );
 34			}
 35			else
 36			{
 37				table[i] = 0;
 38			}
 39		}
 40		return table;
 41	}
 42	return NULL;
 43}
 44
 45
 46
 47MODULE = SDL::Video 	PACKAGE = SDL::Video    PREFIX = video_
 48
 49=for documentation
 50
 51The Following are XS bindings to the Video category in the SDL API v2.1.13
 52
 53Describe on the SDL API site.
 54
 55See: L<http:/*www.libsdl.org/cgi/docwiki.cgi/SDL_API#head-813f033ec44914f267f32195aba7d9aff8c410c0> */
 56
 57=cut
 58
 59SDL_Surface *
 60video_get_video_surface()
 61	PREINIT:
 62		char* CLASS = "SDL::Surface";
 63	CODE:
 64		RETVAL = SDL_GetVideoSurface();
 65	OUTPUT:
 66		RETVAL
 67
 68
 69SDL_VideoInfo*
 70video_get_video_info()
 71	PREINIT:
 72		char* CLASS = "SDL::VideoInfo";
 73	CODE:
 74		RETVAL = (SDL_VideoInfo *) SDL_GetVideoInfo();
 75
 76	OUTPUT:
 77		RETVAL
 78
 79SV *
 80video_video_driver_name( )
 81
 82	CODE:
 83		char buffer[1024];
 84		if ( SDL_VideoDriverName(buffer, 1024) != NULL )
 85		{
 86			RETVAL =  newSVpv(buffer, 0);
 87		}
 88		else
 89			 XSRETURN_UNDEF;
 90	OUTPUT:
 91		RETVAL
 92
 93AV*
 94video_list_modes ( format, flags )
 95	Uint32 flags
 96	SDL_PixelFormat *format
 97
 98	CODE:
 99		SDL_Rect **mode;
100		RETVAL = newAV();
101		sv_2mortal((SV*)RETVAL);
102		mode = SDL_ListModes(format,flags);
103		if (mode == (SDL_Rect**)-1 ) {
104			av_push(RETVAL,newSVpv("all",0));
105		} else if (! mode ) {
106			av_push(RETVAL,newSVpv("none",0));
107		} else {
108			int i;
109			for (i=0; mode[i]; ++i)
110				av_push(RETVAL, cpy2bag( (void *)mode[i], sizeof(SDL_Rect *), sizeof(SDL_Rect), "SDL::Rect" ));
111		}
112	OUTPUT:
113		RETVAL
114
115
116int
117video_video_mode_ok ( width, height, bpp, flags )
118	int width
119	int height
120	int bpp
121	Uint32 flags
122	CODE:
123		RETVAL = SDL_VideoModeOK(width,height,bpp,flags);
124	OUTPUT:
125		RETVAL
126
127
128SDL_Surface *
129video_set_video_mode ( width, height, bpp, flags )
130	int width
131	int height
132	int bpp
133	Uint32 flags
134	PREINIT:
135		char* CLASS = "SDL::Surface";
136	CODE:
137		RETVAL = SDL_SetVideoMode(width,height,bpp,flags);
138	OUTPUT:
139		RETVAL
140
141
142void
143video_update_rect ( surface, x, y, w ,h )
144	SDL_Surface *surface
145	int x
146	int y
147	int w
148	int h
149	CODE:
150		SDL_UpdateRect(surface,x,y,w,h);
151
152void
153video_update_rects ( surface, ... )
154	SDL_Surface *surface
155	CODE:
156		SDL_Rect* rects;
157		int num_rects,i;
158		if ( items < 2 ) return;
159		num_rects = items - 1;
160		rects = (SDL_Rect *)safemalloc(sizeof(SDL_Rect)*items);
161		for(i=0;i<num_rects;i++) {
162                        void** pointers = (void**)INT2PTR(void *, SvIV((SV *)SvRV( ST(i + 1) )));
163			rects[i] = *(SDL_Rect *)(pointers[0]);
164		}
165		SDL_UpdateRects(surface,num_rects,rects);
166		safefree(rects);
167
168
169int
170video_flip ( surface )
171	SDL_Surface *surface
172	CODE:
173		RETVAL = SDL_Flip(surface);
174	OUTPUT:
175		RETVAL
176
177int
178video_set_colors ( surface, start, ... )
179	SDL_Surface *surface
180	int start
181	CODE:
182		if ( items < 3 )
183			RETVAL = 0;
184		else
185		{
186			int i;
187			int length        = items - 2;
188			SDL_Color *colors = (SDL_Color *)safemalloc(sizeof(SDL_Color) * (length + 1));
189			for ( i = 0; i < length ; i++ ) {
190				SDL_Color *temp = (SDL_Color *)bag2obj( ST(i + 2) );
191				colors[i].r     = temp->r;
192				colors[i].g     = temp->g;
193				colors[i].b     = temp->b;
194			}
195			RETVAL = SDL_SetColors(surface, colors, start, length);
196			safefree(colors);
197		}
198	OUTPUT:
199		RETVAL
200
201int
202video_set_palette ( surface, flags, start, ... )
203	SDL_Surface *surface
204	int flags
205	int start
206	CODE:
207		if ( items < 4 )
208			RETVAL = 0;
209		else
210		{
211			int i;
212			int length        = items - 3;
213			SDL_Color *colors = (SDL_Color *)safemalloc(sizeof(SDL_Color) * (length + 1));
214			for ( i = 0; i < length ; i++ ) {
215				SDL_Color *temp = (SDL_Color *)bag2obj( ST(i + 3) );
216				colors[i].r     = temp->r;
217				colors[i].g     = temp->g;
218				colors[i].b     = temp->b;
219			}
220			RETVAL = SDL_SetPalette(surface, flags, colors, start, length);
221			safefree(colors);
222		}
223	OUTPUT:
224		RETVAL
225
226int
227video_set_gamma(r, g, b)
228	float r;
229	float g;
230	float b;
231	CODE:
232		RETVAL = SDL_SetGamma(r,g,b);
233	OUTPUT:
234		RETVAL
235
236int
237video_get_gamma_ramp( redtable, greentable, bluetable )
238	AV* redtable;
239	AV* greentable;
240	AV* bluetable;
241	CODE:
242		Uint16 red_ramp[256];
243		Uint16 green_ramp[256];
244		Uint16 blue_ramp[256];
245		int i;
246		RETVAL = SDL_GetGammaRamp(red_ramp, green_ramp, blue_ramp);
247		for ( i=0; i<256; ++i ) {
248			av_push(redtable,newSViv(red_ramp[i]));
249			av_push(greentable,newSViv(green_ramp[i]));
250			av_push(bluetable,newSViv(blue_ramp[i]));
251		}
252	OUTPUT:
253		RETVAL
254
255int
256video_set_gamma_ramp( rt, gt, bt )
257	AV* rt;
258	AV* gt;
259	AV* bt;
260	CODE:
261		Uint16 *redtable, *greentable, *bluetable;
262		redtable = av_to_uint16(rt);
263		greentable = av_to_uint16(gt);
264		bluetable = av_to_uint16(bt);
265		RETVAL =  SDL_SetGammaRamp(redtable, greentable, bluetable);
266		_uinta_free(redtable, av_len(rt) );
267		_uinta_free(greentable, av_len(gt) );
268		_uinta_free(bluetable, av_len(bt) );
269	OUTPUT:
270		RETVAL
271
272
273
274Uint32
275video_map_RGB ( pixel_format, r, g, b )
276	SDL_PixelFormat *pixel_format
277	Uint8 r
278	Uint8 g
279	Uint8 b
280	CODE:
281		RETVAL = SDL_MapRGB(pixel_format, r,g,b);
282       	OUTPUT:
283		RETVAL
284
285Uint32
286video_map_RGBA ( pixel_format, r, g, b, a )
287	SDL_PixelFormat *pixel_format
288	Uint8 r
289	Uint8 g
290	Uint8 b
291	Uint8 a
292	CODE:
293		RETVAL = SDL_MapRGBA(pixel_format, r,g,b,a );
294	OUTPUT:
295		RETVAL
296
297int
298video_lock_surface ( surface )
299	SDL_Surface *surface
300	CODE:
301		RETVAL = SDL_LockSurface(surface);
302	OUTPUT:
303		RETVAL
304
305void
306video_unlock_surface ( surface )
307	SDL_Surface *surface
308	CODE:
309		SDL_UnlockSurface(surface);
310
311
312SDL_Surface *
313video_convert_surface( src, fmt, flags)
314	SDL_Surface* src
315	SDL_PixelFormat* fmt
316	Uint32	flags
317	PREINIT:
318		char *CLASS = "SDL::Surface";
319	CODE:
320		RETVAL = SDL_ConvertSurface(src, fmt, flags);
321	OUTPUT:
322		RETVAL
323
324
325SDL_Surface *
326video_display_format ( surface )
327	SDL_Surface *surface
328	PREINIT:
329		char* CLASS = "SDL::Surface";
330	CODE:
331		RETVAL = SDL_DisplayFormat(surface);
332	OUTPUT:
333		RETVAL
334
335SDL_Surface *
336video_display_format_alpha ( surface )
337	SDL_Surface *surface
338	PREINIT:
339		char* CLASS = "SDL::Surface";
340	CODE:
341		RETVAL = SDL_DisplayFormatAlpha(surface);
342	OUTPUT:
343		RETVAL
344
345
346int
347video_set_color_key ( surface, flag, key )
348	SDL_Surface *surface
349	Uint32 flag
350	SV *key
351	CODE:
352		Uint32 pixel;
353		if(SvOK(key) && SvIOK(key))
354			pixel = (Uint32)SvUV(key);
355		else
356		{
357			SDL_Color *color = (SDL_Color *)bag2obj(key);
358			pixel            = SDL_MapRGB(surface->format, color->r, color->g, color->b);
359		}
360		RETVAL = SDL_SetColorKey(surface,flag,pixel);
361	OUTPUT:
362		RETVAL
363
364int
365video_set_alpha ( surface, flag, alpha )
366	SDL_Surface *surface
367	Uint32 flag
368	Uint8 alpha
369	CODE:
370		RETVAL = SDL_SetAlpha(surface,flag,alpha);
371	OUTPUT:
372		RETVAL
373
374AV *
375video_get_RGB ( pixel_format, pixel )
376	SDL_PixelFormat *pixel_format
377	Uint32 pixel
378	CODE:
379		Uint8 r,g,b;
380		SDL_GetRGB(pixel,pixel_format,&r,&g,&b);
381		RETVAL = newAV();
382		sv_2mortal((SV*)RETVAL);
383		av_push(RETVAL,newSViv(r));
384		av_push(RETVAL,newSViv(g));
385		av_push(RETVAL,newSViv(b));
386	OUTPUT:
387		RETVAL
388
389AV *
390video_get_RGBA ( pixel_format, pixel )
391	SDL_PixelFormat *pixel_format
392	Uint32 pixel
393	CODE:
394		Uint8 r,g,b,a;
395		SDL_GetRGBA(pixel,pixel_format,&r,&g,&b,&a);
396		RETVAL = newAV();
397		sv_2mortal((SV*)RETVAL);
398		av_push(RETVAL,newSViv(r));
399		av_push(RETVAL,newSViv(g));
400		av_push(RETVAL,newSViv(b));
401		av_push(RETVAL,newSViv(a));
402	OUTPUT:
403		RETVAL
404
405SDL_Surface*
406video_load_BMP ( filename )
407	char *filename
408	PREINIT:
409		char* CLASS = "SDL::Surface";
410	CODE:
411		RETVAL = SDL_LoadBMP(filename);
412	OUTPUT:
413		RETVAL
414
415int
416save_BMP ( surface, filename )
417	SDL_Surface *surface
418	char *filename
419	CODE:
420		RETVAL = SDL_SaveBMP(surface,filename);
421	OUTPUT:
422		RETVAL
423
424int
425fill_rect ( dest, dest_rect, pixel )
426	SDL_Surface *dest
427	SDL_Rect *dest_rect
428	Uint32 pixel
429	CODE:
430		RETVAL = SDL_FillRect(dest,dest_rect,pixel);
431	OUTPUT:
432		RETVAL
433
434int
435blit_surface ( src, src_rect_bag, dest, dest_rect_bag )
436	SDL_Surface *src
437	SDL_Surface *dest
438	SV *src_rect_bag
439	SV *dest_rect_bag
440	CODE:
441		SDL_Rect *src_rect  = NULL;
442		SDL_Rect *dest_rect = NULL;
443
444		if(SvOK(src_rect_bag))
445			src_rect = (SDL_Rect *)bag2obj(src_rect_bag);
446
447		if(SvOK(dest_rect_bag))
448			dest_rect = (SDL_Rect *)bag2obj(dest_rect_bag);
449
450		RETVAL = SDL_BlitSurface(src,src_rect,dest,dest_rect);
451	OUTPUT:
452		RETVAL
453
454void
455set_clip_rect ( surface, rect )
456	SDL_Surface *surface
457	SDL_Rect *rect
458	CODE:
459		SDL_SetClipRect(surface,rect);
460
461void
462get_clip_rect ( surface, rect )
463	SDL_Surface *surface
464	SDL_Rect *rect;
465	CODE:
466		SDL_GetClipRect(surface, rect);
467
468
469
470int
471video_lock_YUV_overlay ( overlay )
472	SDL_Overlay *overlay
473	CODE:
474		RETVAL = SDL_LockYUVOverlay(overlay);
475	OUTPUT:
476		RETVAL
477
478void
479video_unlock_YUV_overlay ( overlay )
480        SDL_Overlay *overlay
481        CODE:
482                SDL_UnlockYUVOverlay(overlay);
483
484int
485video_display_YUV_overlay ( overlay, dstrect )
486	SDL_Overlay *overlay
487	SDL_Rect *dstrect
488	CODE:
489		RETVAL = SDL_DisplayYUVOverlay ( overlay, dstrect );
490	OUTPUT:
491		RETVAL
492
493
494int
495video_GL_load_library ( path )
496	char *path
497	CODE:
498		RETVAL = SDL_GL_LoadLibrary(path);
499	OUTPUT:
500		RETVAL
501
502void*
503video_GL_get_proc_address ( proc )
504	char *proc
505	CODE:
506		RETVAL = SDL_GL_GetProcAddress(proc);
507	OUTPUT:
508		RETVAL
509
510int
511video_GL_set_attribute ( attr,  value )
512	int        attr
513	int        value
514	CODE:
515		RETVAL = SDL_GL_SetAttribute(attr, value);
516	OUTPUT:
517	        RETVAL
518
519AV *
520video_GL_get_attribute ( attr )
521	int        attr
522	CODE:
523		int value;
524		RETVAL = newAV();
525		sv_2mortal((SV*)RETVAL);
526		av_push(RETVAL,newSViv(SDL_GL_GetAttribute(attr, &value)));
527		av_push(RETVAL,newSViv(value));
528	OUTPUT:
529	        RETVAL
530
531void
532video_GL_swap_buffers ()
533	CODE:
534		SDL_GL_SwapBuffers ();
535
536void
537video_wm_set_caption ( title, icon )
538	char *title
539	char *icon
540	CODE:
541		SDL_WM_SetCaption(title,icon);
542
543AV *
544video_wm_get_caption ()
545	CODE:
546		char *title,*icon;
547		SDL_WM_GetCaption(&title,&icon);
548		RETVAL = newAV();
549		sv_2mortal((SV*)RETVAL);
550		av_push(RETVAL,newSVpv(title,0));
551		av_push(RETVAL,newSVpv(icon,0));
552	OUTPUT:
553		RETVAL
554
555void
556video_wm_set_icon ( icon )
557	SDL_Surface *icon
558	CODE:
559		SDL_WM_SetIcon(icon,NULL);
560
561Uint32
562video_wm_grab_input ( mode )
563	Uint32 mode
564	CODE:
565		RETVAL = SDL_WM_GrabInput(mode);
566	OUTPUT:
567		RETVAL
568
569int
570video_wm_iconify_window ()
571	CODE:
572		RETVAL = SDL_WM_IconifyWindow();
573	OUTPUT:
574		RETVAL
575
576int
577video_wm_toggle_fullscreen ( surface )
578	SDL_Surface *surface
579	CODE:
580		RETVAL = SDL_WM_ToggleFullScreen(surface);
581	OUTPUT:
582		RETVAL
583
584int
585video_MUSTLOCK ( surface )
586	SDL_Surface *surface
587	CODE:
588		RETVAL = SDL_MUSTLOCK(surface);
589	OUTPUT:
590		RETVAL