Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
6 KB
Referenced Files
None
Subscribers
None
diff --git a/util/memory.h b/util/memory.h
index bbdab7e2..7d9e130d 100644
--- a/util/memory.h
+++ b/util/memory.h
@@ -1,104 +1,104 @@
#ifndef _paintown_3ac0ba2587944345f2241085269c24f8
#define _paintown_3ac0ba2587944345f2241085269c24f8
#include <allegro.h>
namespace Memory{
struct memory{
memory(unsigned char * stream, int length):
stream(stream),
position(stream),
length(length){
}
/* points to the head */
unsigned char * stream;
/* points to the current position */
unsigned char * position;
int length;
};
static int pf_fclose(void *userdata){
return 0;
/* nothing */
}
static int pf_getc(void *userdata){
memory * m = (memory*) userdata;
if (m->position < m->stream + m->length){
unsigned char x = *m->position;
m->position += 1;
return x;
}
return EOF;
}
static int pf_ungetc(int c, void *userdata){
memory * m = (memory*) userdata;
if (m->position > m->stream){
m->position -= 1;
return c;
} else {
return EOF;
}
}
static long pf_fread(void *p, long n, void *userdata){
memory *m = (memory*) userdata;
unsigned char *cp = (unsigned char *)p;
long i;
int c;
for (i=0; i<n; i++) {
if ((c = pf_getc(m)) == EOF)
break;
*(cp++) = c;
}
return i;
}
static int pf_putc(int c, void *userdata){
return EOF;
}
static long pf_fwrite(const void *p, long n, void *userdata){
return EOF;
}
static int pf_fseek(void *userdata, int offset){
memory * m = (memory*) userdata;
if (offset >= 0 && offset < m->length){
m->position = m->stream + offset;
return 0;
} else {
return -1;
}
}
static int pf_feof(void *userdata){
memory * m = (memory*) userdata;
return m->position >= m->stream + m->length;
}
static int pf_ferror(void *userdata){
memory * m = (memory*) userdata;
return m->position < m->stream || m->position >= m->stream + m->length;
}
- PACKFILE_VTABLE makeTable(){
+ static PACKFILE_VTABLE makeTable(){
PACKFILE_VTABLE table;
table.pf_fclose = Memory::pf_fclose;
table.pf_getc = Memory::pf_getc;
table.pf_ungetc = Memory::pf_ungetc;
table.pf_fread = Memory::pf_fread;
table.pf_putc = Memory::pf_putc;
table.pf_fwrite = Memory::pf_fwrite;
table.pf_fseek = Memory::pf_fseek;
table.pf_feof = Memory::pf_feof;
table.pf_ferror = Memory::pf_ferror;
return table;
}
}
#endif
diff --git a/util/sound.cpp b/util/sound.cpp
index 8539260c..197e5aa7 100644
--- a/util/sound.cpp
+++ b/util/sound.cpp
@@ -1,107 +1,119 @@
#include <allegro.h>
#include <string>
+#include "memory.h"
#include "sound.h"
#include "load_exception.h"
using namespace std;
Sound::Sound():
-my_sound( NULL ),
-own( NULL ){
+my_sound(NULL),
+own(NULL){
}
Sound::Sound(const char * data, int length):
my_sound(NULL),
own(NULL){
-}
+ PACKFILE_VTABLE table = Memory::makeTable();
+ Memory::memory memory((unsigned char *) data, length);
-Sound::Sound( const string & path ) throw( LoadException ):
-my_sound( NULL ),
-own( NULL ){
+ PACKFILE * pack = pack_fopen_vtable(&table, &memory);
+ my_sound = load_wav_pf(pack);
+ pack_fclose(pack);
+ if (!my_sound){
+ throw LoadException("Could not load wav data");
+ }
+
+ own = new int;
+ *own = 1;
+}
- my_sound = load_sample( path.c_str() );
+Sound::Sound(const string & path) throw( LoadException ):
+my_sound(NULL ),
+own(NULL){
+ my_sound = load_sample( path.c_str() );
- if ( !my_sound ){
- string xf( "Could not load " );
- xf += path;
- throw LoadException( xf );
- }
+ if ( !my_sound ){
+ string xf( "Could not load " );
+ xf += path;
+ throw LoadException(xf);
+ }
- own = new int;
- *own = 1;
+ own = new int;
+ *own = 1;
}
Sound::Sound( const Sound & copy ):
my_sound( NULL ),
own( NULL ){
- own = copy.own;
- if ( own ){
- *own += 1;
- }
- my_sound = copy.my_sound;
+ own = copy.own;
+ if ( own ){
+ *own += 1;
+ }
+ my_sound = copy.my_sound;
}
void Sound::destroy(){
- if ( own ){
- *own -= 1;
- if ( *own == 0 ){
- delete own;
- destroy_sample( my_sound );
- own = NULL;
- }
- }
+ if ( own ){
+ *own -= 1;
+ if ( *own == 0 ){
+ delete own;
+ destroy_sample( my_sound );
+ own = NULL;
+ }
+ }
}
Sound & Sound::operator=( const Sound & rhs ){
- if ( own ){
- destroy();
- }
- own = rhs.own;
- if ( own ){
- *own += 1;
- }
- my_sound = rhs.my_sound;
+ if ( own ){
+ destroy();
+ }
+ own = rhs.own;
+ if ( own ){
+ *own += 1;
+ }
+ my_sound = rhs.my_sound;
- return *this;
+ return *this;
}
void Sound::stop(){
if (my_sound){
stop_sample(my_sound);
}
}
void Sound::play(){
- if ( my_sound ){
+ if (my_sound){
play_sample( my_sound, 255, 128, 1000, false );
}
}
void Sound::play( int volume, int pan ){
- if ( my_sound ){
- int p = pan;
- if ( p > 255 ){
- p = 255;
- } else if ( p < 0 ){
- p = 0;
- }
- int v = volume;
- if ( v < 0 ){
- v = 0;
- } else if ( v > 255 ){
- v = 255;
- }
- play_sample( my_sound, v, p, 1000, false );
- }
+ if ( my_sound ){
+ int p = pan;
+ if ( p > 255 ){
+ p = 255;
+ } else if ( p < 0 ){
+ p = 0;
+ }
+ int v = volume;
+ if ( v < 0 ){
+ v = 0;
+ } else if ( v > 255 ){
+ v = 255;
+ }
+ play_sample( my_sound, v, p, 1000, false );
+ }
}
void Sound::playLoop(){
- if ( my_sound ){
- play_sample( my_sound, 255, 128, 1000, true );
- }
+ if ( my_sound ){
+ play_sample( my_sound, 255, 128, 1000, true );
+ }
}
Sound::~Sound(){
- destroy();
+ destroy();
}

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jun 11, 1:27 PM (3 w, 4 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
69242
Default Alt Text
(6 KB)

Event Timeline