Implement MixChunk
Leon Brocard [Fri, 6 Nov 2009 13:04:08 +0000 (13:04 +0000)]
Build.PL
lib/SDL/MixChunk.pm [new file with mode: 0644]
src/Core/objects/MixChunk.xs [new file with mode: 0644]
src/SDL.xs
t/core_mixchunk.t [new file with mode: 0644]
typemap

index 595c8da..265853c 100644 (file)
--- a/Build.PL
+++ b/Build.PL
@@ -91,6 +91,13 @@ my %subsystems =
                },
                libraries => [qw( SDL )],
        },
+       MixChunk => {
+               file      => {
+                       from  => 'src/Core/objects/MixChunk.xs',
+                       to    => 'lib/SDL/MixChunk.xs',
+               },
+               libraries => [qw( SDL )],
+       },
        Palette => {
                file      => {
                        from  => 'src/Core/objects/Palette.xs',
diff --git a/lib/SDL/MixChunk.pm b/lib/SDL/MixChunk.pm
new file mode 100644 (file)
index 0000000..4ae4370
--- /dev/null
@@ -0,0 +1,9 @@
+package SDL::MixChunk;
+use strict;
+use warnings;
+require Exporter;
+require DynaLoader;
+our @ISA = qw(Exporter DynaLoader);
+bootstrap SDL::MixChunk;
+
+1;
diff --git a/src/Core/objects/MixChunk.xs b/src/Core/objects/MixChunk.xs
new file mode 100644 (file)
index 0000000..71c5652
--- /dev/null
@@ -0,0 +1,48 @@
+#include "EXTERN.h"
+#include "perl.h"
+#include "XSUB.h"
+
+#ifndef aTHX_
+#define aTHX_
+#endif
+
+#include <SDL.h>
+#include <SDL_mixer.h>
+
+MODULE = SDL::MixChunk         PACKAGE = SDL::MixChunk    PREFIX = mixchunk_
+
+=for documentation
+
+SDL_MixChunk - Stores audio data in memory
+
+  typedef struct {
+          int allocated;
+          Uint8 *abuf;
+          Uint32 alen;
+          Uint8 volume;
+  } Mix_Chunk;
+
+=cut
+
+Uint32
+mixchunk_alen ( mixchunk )
+       Mix_Chunk *mixchunk
+       CODE:
+               RETVAL = mixchunk->alen;
+       OUTPUT:
+               RETVAL
+
+Uint8
+mixchunk_volume ( mixchunk, ... )
+       Mix_Chunk *mixchunk
+       CODE:
+               if (items > 1 ) mixchunk->volume = SvIV(ST(1)); 
+               RETVAL = mixchunk->volume;
+       OUTPUT:
+               RETVAL
+
+void
+mixchunk_DESTROY(mixchunk)
+       Mix_Chunk *mixchunk
+       CODE:
+               Mix_FreeChunk(mixchunk);
index c539815..69ad5f7 100644 (file)
@@ -1189,8 +1189,15 @@ MixQuerySpec ()
 Mix_Chunk *
 MixLoadWAV ( filename )
        char *filename
+       PREINIT:
+               char * CLASS = "SDL::MixChunk";
        CODE:
-               RETVAL = Mix_LoadWAV(filename);
+               Mix_Chunk * mixchunk;
+               mixchunk = Mix_LoadWAV(filename);
+               if (mixchunk == NULL) {
+                 fprintf(stderr, "Could not load %s\n", filename);
+               }
+               RETVAL = mixchunk;
        OUTPUT:
                RETVAL
 
@@ -1205,6 +1212,8 @@ MixLoadMusic ( filename )
 Mix_Chunk *
 MixQuickLoadWAV ( buf )
        Uint8 *buf
+       PREINIT:
+               char * CLASS = "SDL::MixChunk";
        CODE:
                RETVAL = Mix_QuickLoad_WAV(buf);
        OUTPUT:
diff --git a/t/core_mixchunk.t b/t/core_mixchunk.t
new file mode 100644 (file)
index 0000000..acdad88
--- /dev/null
@@ -0,0 +1,25 @@
+#!perl
+use strict;
+use warnings;
+use SDL;
+use SDL::MixChunk;
+use Test::More tests => 6;
+
+is( SDL::init(SDL_INIT_AUDIO), 0, '[init] returns 0 on success' );
+
+is( SDL::MixOpenAudio(
+        SDL::MIX_DEFAULT_FREQUENCY(), SDL::MIX_DEFAULT_FORMAT(),
+        SDL::MIX_DEFAULT_CHANNELS(),  4096
+    ),
+    0,
+    'MixOpenAudio passed'
+);
+
+my $mix_chunk = SDL::MixLoadWAV('test/data/sample.wav');
+isa_ok( $mix_chunk, 'SDL::MixChunk' );
+
+is( $mix_chunk->volume, 128, 'Default volume is 128' );
+$mix_chunk->volume(100);
+is( $mix_chunk->volume, 100, 'Can change volume to 100' );
+
+is( $mix_chunk->alen, 963424, 'Alen is 963424' );
diff --git a/typemap b/typemap
index b65aa5d..d142372 100644 (file)
--- a/typemap
+++ b/typemap
@@ -29,7 +29,7 @@ SDL_Cursor *          T_PTR
 SDL_AudioSpec *                T_PTR
 SDL_AudioCVT *         T_PTR
 Mix_Fading             T_UV
-Mix_Chunk *            T_PTR
+Mix_Chunk *            O_OBJECT
 Mix_Music *            T_PTR
 SDL_GLattr              T_IV
 int *                  T_PTR