Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
9 KB
Referenced Files
None
Subscribers
None
diff --git a/util/funcs.cpp b/util/funcs.cpp
index cb469e4c..527cc318 100644
--- a/util/funcs.cpp
+++ b/util/funcs.cpp
@@ -1,40 +1,40 @@
#include "funcs.h"
#include <allegro.h>
/*
inline int rnd( int q ){
if ( q <= 0 ) return 0;
return (int)( rand() % q );
}
*/
-int rnd( int q, int min, int range ){
+int Util::rnd( int q, int min, int range ){
return q - min + rnd( range );
}
-int rnd( int min, int max ){
+int Util::rnd( int min, int max ){
return rnd( max - min ) + min;
}
-void blend_palette( int * pal, int mp, int sc, int ec ) {
+void Util::blend_palette( int * pal, int mp, int sc, int ec ) {
ASSERT( pal );
ASSERT( mp != 0 );
int sc_r = getr( sc );
int sc_g = getg( sc );
int sc_b = getb( sc );
int ec_r = getr( ec );
int ec_g = getg( ec );
int ec_b = getb( ec );
for ( int q = 0; q < mp; q++ ) {
float j = (float)( q ) / (float)( mp );
int f_r = (int)( 0.5 + (float)( sc_r ) + (float)( ec_r-sc_r ) * j );
int f_g = (int)( 0.5 + (float)( sc_g ) + (float)( ec_g-sc_g ) * j );
int f_b = (int)( 0.5 + (float)( sc_b ) + (float)( ec_b-sc_b ) * j );
pal[q] = makecol( f_r, f_g, f_b );
}
}
diff --git a/util/funcs.h b/util/funcs.h
index f095a318..f0dac51f 100644
--- a/util/funcs.h
+++ b/util/funcs.h
@@ -1,20 +1,24 @@
#ifndef _funcs_h
#define _funcs_h
#include <stdlib.h>
+namespace Util{
+
// int rnd( int q );
inline int rnd( int q ){
if ( q <= 0 ) return 0;
return (int)( rand() % q );
}
/* return a random number + some range between min/max */
int rnd( int q, int min, int max );
/* return a number between min/max */
int rnd( int min, int max );
void blend_palette( int * pal, int mp, int sc, int ec );
+}
+
#endif
diff --git a/util/music.cpp b/util/music.cpp
new file mode 100644
index 00000000..c0eaf637
--- /dev/null
+++ b/util/music.cpp
@@ -0,0 +1,356 @@
+#include "util/music.h"
+#include "util/funcs.h"
+#include <string>
+#include <aldumb.h>
+#include <iostream>
+// #include "defs.h"
+
+#ifdef WINDOWS
+#include <winalleg.h>
+#endif
+
+#include <pthread.h>
+
+using namespace std;
+
+static Music * instance = NULL;
+
+static double volume = 1.0;
+// static bool muted = false;
+static pthread_t musicThread;
+static pthread_mutex_t musicMutex;
+static bool alive = true;
+
+static void * playMusic( void * );
+
+#define synchronized for( int __l( ! pthread_mutex_lock( &musicMutex ) ); __l; __l = 0, pthread_mutex_unlock( &musicMutex ) )
+
+#define LOCK pthread_mutex_lock( &musicMutex );
+#define UNLOCK pthread_mutex_unlock( &musicMutex );
+
+/*
+#undef LOCK
+#undef UNLOCK
+#define LOCK
+#define UNLOCK
+*/
+
+Music::Music():
+playing( false ),
+player( NULL ),
+music_file( NULL ){
+
+ if ( instance != NULL ){
+ cerr << "Trying to instantiate music object twice!" << endl;
+ return;
+ }
+
+ instance = this;
+
+ pthread_mutex_init( &musicMutex, NULL );
+ pthread_create( &musicThread, NULL, playMusic, (void *)instance );
+}
+
+/*
+static bool isAlive(){
+ bool f = false;
+ synchronized{
+ f = alive;
+ }
+ return f;
+}
+*/
+
+static void * playMusic( void * _music ){
+ Music * music = (Music *) _music;
+
+ cout << "Playing music" << endl;
+
+ // unsigned int tick = 0;
+ // unsigned int counter;
+
+ bool playing = true;
+ while ( playing ){
+
+ LOCK;{
+ playing = alive;
+ music->doPlay();
+ }
+ UNLOCK;
+ rest( 50 );
+
+ // Util::YIELD();
+ // pthread_yield();
+ }
+
+ // cout << "Done with music thread" << endl;
+
+ return NULL;
+}
+
+double Music::getVolume(){
+ return volume;
+}
+
+void Music::doPlay(){
+ if ( this->playing ){
+ if ( al_poll_duh( this->player ) != 0 ){
+ }
+ }
+}
+
+/*
+Music::Music( const char * song ):
+volume( 1.0 ),
+muted( false ),
+player( NULL ),
+music_file( NULL ){
+
+ loadSong( song );
+
+}
+
+Music::Music( const string & song ):
+volume( 1.0 ),
+muted( false ),
+player( NULL ),
+music_file( NULL ){
+ loadSong( song );
+}
+*/
+
+bool Music::loadSong( const char * song ){
+ bool loaded = false;
+ LOCK;{
+ loaded = instance->internal_loadSong( song );
+ }
+ UNLOCK;
+ return loaded;
+ // muted = false;
+}
+
+/* remove an element from a vector at index 'pos' and return it */
+template< class Tx_ >
+static Tx_ removeVectorElement( vector< Tx_ > & toRemove, int pos ){
+ int count = 0;
+ typename vector< Tx_ >::iterator it;
+ for ( it = toRemove.begin(); it != toRemove.end() && count < pos; count++, it++ );
+
+ if ( it == toRemove.end() ){
+ /* this isnt right, but whatever */
+ return toRemove.front();
+ }
+
+ const Tx_ & removed = toRemove[ pos ];
+ toRemove.erase( it );
+ return removed;
+
+}
+
+void Music::loadSong( const vector< string > & Songs ){
+
+ /*
+ cout << "Songs = " << &Songs << endl;
+ if ( ! loadSong( "music/song5.xm" ) ){
+ cerr << "Could not load music/song5.xm" << endl;
+ }
+ return;
+ */
+
+ vector< string > _songs = Songs;
+ vector< string > songs;
+ while ( ! _songs.empty() ){
+ int i = Util::rnd( _songs.size() );
+ songs.push_back( removeVectorElement< string >( _songs, i ) );
+ }
+
+ /*
+ songs.clear();
+ songs.push_back( "music/song3.xm" );
+ */
+
+ for ( vector< string >::iterator it = songs.begin(); it != songs.end(); it++ ){
+ cout << "Trying to load song " << *it << endl;
+ if ( loadSong( *it ) ){
+ break;
+ }
+ }
+}
+
+bool Music::loadSong( const string & song ){
+ return loadSong( song.c_str() );
+}
+
+void Music::_play(){
+ if ( playing == false && this->player != NULL ){
+ al_resume_duh( this->player );
+ }
+ playing = true;
+}
+
+void Music::play(){
+ LOCK;{
+ instance->_play();
+ }
+ UNLOCK;
+}
+
+void Music::_pause(){
+ playing = false;
+ if ( this->player != NULL ){
+ al_pause_duh( this->player );
+ }
+}
+
+void Music::pause(){
+ LOCK;{
+ instance->_pause();
+ }
+ UNLOCK;
+}
+
+void Music::soften(){
+ instance->_soften();
+}
+
+void Music::_soften(){
+ if ( volume > 0.1 ) volume -= 0.1;
+ else volume = 0.0;
+
+ setVolume( volume );
+}
+
+void Music::louden(){
+ instance->_louden();
+}
+
+void Music::_louden(){
+ if ( volume < 0.9 ) volume += 0.1;
+ else volume = 1.0;
+
+ setVolume( volume );
+}
+
+void Music::mute(){
+ setVolume( 0 );
+}
+
+void Music::setVolume( double vol ){
+ volume = vol;
+ LOCK;{
+ instance->_setVolume( volume );
+ }
+ UNLOCK;
+}
+
+void Music::_setVolume( double vol ){
+
+ if ( player ){
+ al_duh_set_volume( player, vol );
+ }
+
+}
+
+Music::~Music(){
+
+ LOCK;{
+ if ( player ){
+ al_stop_duh( player );
+ unload_duh( music_file );
+ }
+
+ alive = false;
+ }
+ UNLOCK;
+
+ cout << "Waiting for music thread to die" << endl;
+ pthread_join( musicThread, NULL );
+
+}
+
+/*
+void Music::pause(){
+ al_pause_duh( player );
+}
+*/
+
+/*
+void Music::resume(){
+ al_resume_duh( player );
+}
+*/
+
+bool Music::internal_loadSong( const char * path ){
+
+ // cout << "Trying to load '" << path << "'" << endl;
+
+ if ( player != NULL ){
+ al_stop_duh( player );
+ unload_duh( music_file );
+ player = NULL;
+ music_file = NULL;
+ }
+
+ // music_file = dumb_load_mod( path );
+ /*
+ music_file = dumb_load_mod( path );
+ if ( !music_file ){
+ music_file = dumb_load_xm( path );
+ }
+ if ( !music_file ){
+ music_file = dumb_load_s3m( path );
+ }
+ if ( !music_file ){
+ music_file = dumb_load_it( path );
+ }
+ */
+
+ for ( int i = 0; i < 4; i++ ){
+ switch ( i ){
+ case 0 : {
+ music_file = dumb_load_xm( path );
+ break;
+ }
+ case 1 : {
+ music_file = dumb_load_s3m( path );
+ break;
+ }
+ case 2 : {
+ music_file = dumb_load_it( path );
+ break;
+ }
+ case 3 : {
+ music_file = dumb_load_mod( path );
+ break;
+ }
+ }
+ if ( music_file != NULL ){
+ cout << "Loaded " << path << " type " << i << endl;
+ break;
+ }
+ }
+
+ if ( music_file ){
+ int buf = 1 << 14;
+ player = al_start_duh( music_file, 2, 0, volume, buf, 22050 );
+ // cout << "Loaded music player " << player << endl;
+
+ /*
+ while ( 1 ){
+ al_poll_duh( player );
+ rest( 1 );
+ }
+ */
+
+ playing = true;
+ } else {
+ cout<<"Could not load "<<path<<endl;
+ return false;
+ }
+ return true;
+
+}
+
+#undef synchronized
+#undef LOCK
+#undef UNLOCK
diff --git a/util/music.h b/util/music.h
new file mode 100644
index 00000000..5ed0324f
--- /dev/null
+++ b/util/music.h
@@ -0,0 +1,92 @@
+#ifndef _music_class_h
+#define _music_class_h
+
+#include <string>
+#include <vector>
+
+/*
+#ifdef WINDOWS
+#include <allegro.h>
+#include <winalleg.h>
+#endif
+
+#include <pthread.h>
+*/
+
+using namespace std;
+
+struct AL_DUH_PLAYER;
+struct DUH;
+
+/* The music class. Dont be late or youll get an F!
+ */
+class Music{
+public:
+ Music();
+
+ /*
+ Music( const char * song );
+ Music( const string & song );
+ */
+
+ ~Music();
+
+ static bool loadSong( const char * song );
+ static bool loadSong( const string & song );
+
+ /* load one of the songs in 'songs' */
+ static void loadSong( const vector< string > & songs );
+
+ void doPlay();
+
+ /*
+ void _loadSong( const char * song );
+ void _loadSong( const string & song );
+ */
+
+ void _play();
+ void _pause();
+ void _soften();
+ void _louden();
+
+ static void pause();
+ static void play();
+ static void soften();
+ static void louden();
+
+ /*
+ void _pause();
+ void _resume();
+ */
+
+ /*
+ void _play();
+ int _soften();
+ int _louden();
+ void _maxVolume();
+ */
+
+ static void setVolume( double v );
+ static double getVolume();
+
+ /*
+ return (int)( volume * 100 );
+ }
+ */
+
+ static void mute();
+ static void unmute();
+
+protected:
+
+ void _setVolume( double vol );
+
+ bool playing;
+
+ bool internal_loadSong( const char * path );
+
+ AL_DUH_PLAYER * player;
+ DUH * music_file;
+};
+
+#endif

File Metadata

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

Event Timeline