PageRenderTime 27ms CodeModel.GetById 20ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Mixer/Music.xs

http://github.com/PerlGameDev/SDL
Unknown | 341 lines | 282 code | 59 blank | 0 comment | 0 complexity | 931a874247b2b4e75f477c98249d1449 MD5 | raw file
  1#include "EXTERN.h"
  2#include "perl.h"
  3#include "XSUB.h"
  4#include "ppport.h"
  5#include "defines.h"
  6
  7#include <SDL.h>
  8
  9#ifdef HAVE_SDL_MIXER
 10#include <SDL_mixer.h>
 11
 12char *cb  = NULL;
 13char *fcb = NULL;
 14
 15void mix_func(void *udata, Uint8 *stream, int len)
 16{
 17	PERL_SET_CONTEXT(parent_perl);
 18	dSP;                                       /* initialize stack pointer          */
 19	ENTER;                                     /* everything created after here     */
 20	SAVETMPS;                                  /* ...is a temporary variable.       */
 21
 22	PUSHMARK(SP);                              /* remember the stack pointer        */
 23	XPUSHs(sv_2mortal(newSViv(*(int*)udata))); /* push something onto the stack     */
 24	XPUSHs(sv_2mortal(newSViv(len)));
 25	*(int*)udata = *(int*)udata + len;
 26	PUTBACK;                                   /* make local stack pointer global   */
 27
 28	if(cb != NULL)
 29	{
 30		int count = call_pv(cb, G_ARRAY);      /* call the function                 */
 31		SPAGAIN;                               /* refresh stack pointer             */
 32		
 33		if(count == len + 1)
 34		{
 35			int i;
 36			
 37			for(i=0; i<len; i++)
 38				stream[i] = POPi;              /* pop the return value from stack   */
 39		}
 40
 41		PUTBACK;
 42	}
 43
 44	FREETMPS;                                  /* free that return value            */
 45	LEAVE;                                     /* ...and the XPUSHed "mortal" args. */
 46}
 47
 48void mix_finished(void)
 49{
 50	PERL_SET_CONTEXT(parent_perl);
 51	dSP;                                       /* initialize stack pointer          */
 52	PUSHMARK(SP);                              /* remember the stack pointer        */
 53
 54	if(fcb != NULL)
 55	{
 56		call_pv(fcb, G_DISCARD|G_VOID);        /* call the function                 */
 57	}
 58}
 59
 60#endif
 61
 62MODULE = SDL::Mixer::Music 	PACKAGE = SDL::Mixer::Music    PREFIX = mixmus_
 63
 64=for documentation
 65
 66SDL_mixer bindings
 67
 68See: http:/*www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html */
 69
 70=cut
 71
 72#ifdef HAVE_SDL_MIXER
 73
 74#if (SDL_MIXER_MAJOR_VERSION >= 1) && (SDL_MIXER_MINOR_VERSION >= 2) && (SDL_MIXER_PATCHLEVEL >= 9)
 75
 76int
 77mixmus_get_num_music_decoders( )
 78	CODE:
 79		RETVAL = Mix_GetNumMusicDecoders();
 80	OUTPUT:
 81		RETVAL
 82
 83const char *
 84mixmus_get_music_decoder( index )
 85	int index
 86	CODE:
 87		RETVAL = Mix_GetMusicDecoder(index);
 88	OUTPUT:
 89		RETVAL
 90
 91#else
 92
 93int
 94mixmus_get_num_music_decoders( )
 95	CODE:
 96		warn("SDL_mixer >= 1.2.9 needed for SDL::Mixer::Music::getnum_music_decoders()");
 97		XSRETURN_UNDEF;
 98	OUTPUT:
 99		RETVAL
100
101const char *
102mixmus_get_music_decoder( index )
103	int index
104	CODE:
105		warn("SDL_mixer >= 1.2.9 needed for SDL::Mixer::Music::get_music_decoder( index )");
106		XSRETURN_UNDEF;
107	OUTPUT:
108		RETVAL
109
110#endif
111
112Mix_Music *
113mixmus_load_MUS( filename )
114	char *filename
115	PREINIT:
116		char * CLASS = "SDL::Mixer::MixMusic";
117	CODE:
118		Mix_Music * mixmusic;
119		mixmusic = Mix_LoadMUS(filename);
120		if (mixmusic == NULL)
121			fprintf(stderr, "Could not load %s\n", filename);
122		RETVAL = mixmusic;
123	OUTPUT:
124		RETVAL
125
126#if VERSION_ATLEAST(SDL_MIXER_MAJOR_VERSION, SDL_MIXER_MINOR_VERSION, SDL_MIXER_PATCHLEVEL, 1, 2, 7)
127
128Mix_Music *
129mixmus_load_MUS_RW( rw )
130	SDL_RWops *rw
131	PREINIT:
132		char * CLASS = "SDL::Mixer::MixMusic";
133	CODE:
134		Mix_Music * mixmusic;
135		mixmusic = Mix_LoadMUS_RW(rw);
136		if (mixmusic == NULL)
137			fprintf(stderr, "Could not load SDL::RWOp object\n");
138		RETVAL = mixmusic;
139	OUTPUT:
140		RETVAL
141
142#else
143
144Mix_Music *
145mixmus_load_MUS_RW( rw )
146	SDL_RWops *rw
147	CODE:
148		warn("SDL_mixer >= 1.2.7 needed for SDL::Mixer::Music::load_MUS_RW( rw )");
149		XSRETURN_UNDEF;
150	OUTPUT:
151		RETVAL
152
153#endif
154
155void
156mixmus_free_music( music )
157	Mix_Music *music
158	CODE:
159		Mix_FreeMusic(music);
160
161#ifdef USE_THREADS
162
163void
164mixmus_hook_music( func = NULL, arg = 0 )
165	char *func
166	int arg
167	CODE:
168		if(func != NULL)
169		{
170			parent_perl  = PERL_GET_CONTEXT;
171			cb           = func;
172			void *arg2   = safemalloc(sizeof(int));
173			*(int*) arg2 = arg;
174			Mix_HookMusic(&mix_func, arg2);
175		}
176		else
177		{
178			Mix_HookMusic(NULL, NULL);
179			void *arg2 = Mix_GetMusicHookData();
180			if(arg2 != NULL)
181				safefree(arg2);
182		}
183
184void
185mixmus_hook_music_finished( func = NULL )
186	char *func
187	CODE:
188		if(func != NULL)
189		{
190			parent_perl  = PERL_GET_CONTEXT;
191			fcb = func;
192			Mix_HookMusicFinished(&mix_finished);
193		}
194		else
195			Mix_HookMusicFinished(NULL);
196
197#else
198
199void
200mixmus_hook_music( func = NULL, arg = 0 )
201	char *func
202	int arg
203	CODE:
204		warn("Perl need to be compiled with 'useithreads' for SDL::Mixer::Music::hook_music( func, arg )");
205
206void
207mixmus_hook_music_finished( func = NULL )
208	char *func
209	CODE:
210		warn("Perl need to be compiled with 'useithreads' for SDL::Mixer::Music::hook_music_finished( func )");
211
212#endif
213
214int
215mixmus_get_music_hook_data()
216	CODE:
217		RETVAL = *(int*)Mix_GetMusicHookData();
218	OUTPUT:
219		RETVAL
220
221int
222mixmus_play_music( music, loops )
223	Mix_Music *music
224	int loops
225	CODE:
226		RETVAL = Mix_PlayMusic(music,loops);
227	OUTPUT:
228		RETVAL
229
230int
231mixmus_fade_in_music( music, loops, ms )
232	Mix_Music *music
233	int loops
234	int ms
235	CODE:
236		RETVAL = Mix_FadeInMusic(music,loops,ms);
237	OUTPUT:
238		RETVAL
239
240int
241mixmus_fade_in_music_pos( music, loops, ms, position )
242	Mix_Music *music
243	int loops
244	int ms
245	double position
246	CODE:
247		RETVAL = Mix_FadeInMusicPos(music,loops,ms,position);
248	OUTPUT:
249		RETVAL
250
251int
252mixmus_volume_music( volume = -1 )
253	int volume
254	CODE:
255		RETVAL = Mix_VolumeMusic(volume);
256	OUTPUT:
257		RETVAL
258
259int
260mixmus_halt_music()
261	CODE:
262		RETVAL = Mix_HaltMusic();
263	OUTPUT:
264		RETVAL
265
266int
267mixmus_fade_out_music ( ms )
268	int ms
269	CODE:
270		RETVAL = Mix_FadeOutMusic(ms);
271	OUTPUT:
272		RETVAL
273
274Mix_Fading
275mixmus_fading_music()
276	CODE:
277		RETVAL = Mix_FadingMusic();
278	OUTPUT:
279		RETVAL
280
281void
282mixmus_pause_music ()
283	CODE:
284		Mix_PauseMusic();
285
286void
287mixmus_resume_music ()
288	CODE:
289		Mix_ResumeMusic();
290
291void
292mixmus_rewind_music ()
293	CODE:
294		Mix_RewindMusic();
295
296int
297mixmus_paused_music ()
298	CODE:
299		RETVAL = Mix_PausedMusic();
300	OUTPUT:
301		RETVAL
302
303int
304mixmus_playing_music()
305	CODE:
306		RETVAL = Mix_PlayingMusic();
307	OUTPUT:
308		RETVAL
309
310int
311mixmus_set_music_position( position )
312	double position
313	CODE:
314		RETVAL = Mix_SetMusicPosition(position);
315	OUTPUT:
316		RETVAL
317
318int
319mixmus_get_music_type( music = NULL )
320	Mix_Music * music
321	CODE:
322		RETVAL = Mix_GetMusicType( music );
323	OUTPUT:
324		RETVAL
325
326int
327mixmus_set_music_cmd( cmd = NULL )
328	char *cmd
329	CODE:
330		RETVAL = Mix_SetMusicCMD( cmd );
331	OUTPUT:
332		RETVAL
333
334void
335mixmus_DESTROY( music )
336	Mix_Music *music
337	CODE:
338		Mix_FreeMusic(music);
339
340
341#endif