[Scummvm-cvs-logs] SF.net SVN: scummvm:[41343] scummvm/trunk/engines/sci

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Sun Jun 7 19:06:52 CEST 2009


Revision: 41343
          http://scummvm.svn.sourceforge.net/scummvm/?rev=41343&view=rev
Author:   fingolfin
Date:     2009-06-07 17:06:51 +0000 (Sun, 07 Jun 2009)

Log Message:
-----------
SCI: Objectified SongLibrary

Modified Paths:
--------------
    scummvm/trunk/engines/sci/engine/ksound.cpp
    scummvm/trunk/engines/sci/engine/savegame.cpp
    scummvm/trunk/engines/sci/sfx/core.cpp
    scummvm/trunk/engines/sci/sfx/songlib.cpp
    scummvm/trunk/engines/sci/sfx/songlib.h

Modified: scummvm/trunk/engines/sci/engine/ksound.cpp
===================================================================
--- scummvm/trunk/engines/sci/engine/ksound.cpp	2009-06-07 17:06:32 UTC (rev 41342)
+++ scummvm/trunk/engines/sci/engine/ksound.cpp	2009-06-07 17:06:51 UTC (rev 41343)
@@ -493,7 +493,7 @@
 			s->_sound.sfx_song_set_status(handle, SOUND_STATUS_PLAYING);
 			s->_sound.sfx_song_set_loops(handle, looping);
 			s->_sound.sfx_song_renice(handle, pri);
-			song_lib_set_restore_behavior(s->_sound._songlib, handle, rb);
+			s->_sound._songlib.setSongRestoreBehavior(handle, rb);
 		}
 
 		break;
@@ -798,7 +798,7 @@
 		int looping = GET_SEL32V(obj, loop);
 		//int vol = GET_SEL32V(obj, vol);
 		int pri = GET_SEL32V(obj, pri);
-		Song *song = song_lib_find(s->_sound._songlib, handle);
+		Song *song = s->_sound._songlib.findSong(handle);
 
 		if (GET_SEL32V(obj, nodePtr) && (song && number != song->_resourceNum)) {
 			s->_sound.sfx_song_set_status(handle, SOUND_STATUS_STOPPED);

Modified: scummvm/trunk/engines/sci/engine/savegame.cpp
===================================================================
--- scummvm/trunk/engines/sci/engine/savegame.cpp	2009-06-07 17:06:32 UTC (rev 41342)
+++ scummvm/trunk/engines/sci/engine/savegame.cpp	2009-06-07 17:06:51 UTC (rev 41343)
@@ -404,15 +404,15 @@
 static void sync_songlib_t(Common::Serializer &s, SongLibrary &obj) {
 	int songcount = 0;
 	if (s.isSaving())
-		songcount = song_lib_count(obj);
+		songcount = obj.countSongs();
 	s.syncAsUint32LE(songcount);
 
 	if (s.isLoading()) {
-		song_lib_init(&obj);
+		obj.initSounds();
 		while (songcount--) {
 			Song *newsong = (Song *)calloc(1, sizeof(Song));
 			sync_song_t(s, *newsong);
-			song_lib_add(obj, newsong);
+			obj.addSong(newsong);
 		}
 	} else {
 		Song *seeker = *(obj._lib);
@@ -698,7 +698,7 @@
 	if (s->_sound._songlib._lib)
 		seeker = *(s->_sound._songlib._lib);
 	else {
-		song_lib_init(&s->_sound._songlib);
+		s->_sound._songlib.initSounds();
 		seeker = NULL;
 	}
 
@@ -782,7 +782,7 @@
 	temp = retval->_sound._songlib;
 	retval->_sound.sfx_init(retval->resmgr, s->sfx_init_flags);
 	retval->sfx_init_flags = s->sfx_init_flags;
-	song_lib_free(retval->_sound._songlib);
+	retval->_sound._songlib.freeSounds();
 	retval->_sound._songlib = temp;
 
 	_reset_graphics_input(retval);

Modified: scummvm/trunk/engines/sci/sfx/core.cpp
===================================================================
--- scummvm/trunk/engines/sci/sfx/core.cpp	2009-06-07 17:06:32 UTC (rev 41342)
+++ scummvm/trunk/engines/sci/sfx/core.cpp	2009-06-07 17:06:51 UTC (rev 41343)
@@ -346,7 +346,6 @@
 SfxState::SfxState() {
 	_it = NULL;
 	_flags = 0;
-	memset(&_songlib, 0, sizeof(_songlib));
 	_song = NULL;
 	_suspended = 0;
 	_soundSync = 0;
@@ -485,7 +484,7 @@
 
 /* Update internal state iff only one song may be played */
 void SfxState::updateSingleSong() {
-	Song *newsong = song_lib_find_active(_songlib);
+	Song *newsong = _songlib.findFirstActive();
 
 	if (newsong != _song) {
 		freezeTime(); /* Store song delay time */
@@ -544,7 +543,7 @@
 void SfxState::updateMultiSong() {
 	Song *oldfirst = _song;
 	Song *oldseeker;
-	Song *newsong = song_lib_find_active(_songlib);
+	Song *newsong = _songlib.findFirstActive();
 	Song *newseeker;
 	Song not_playing_anymore; /* Dummy object, referenced by
 				    ** songs which are no longer
@@ -575,10 +574,8 @@
 	}
 
 	/* Second, re-generate the new song queue. */
-	for (newseeker = newsong; newseeker;
-	        newseeker = newseeker->_nextPlaying) {
-		newseeker->_nextPlaying
-		= song_lib_find_next_active(_songlib, newseeker);
+	for (newseeker = newsong; newseeker; newseeker = newseeker->_nextPlaying) {
+		newseeker->_nextPlaying = _songlib.findNextActive(newseeker);
 
 		if (newseeker == newseeker->_nextPlaying) { 
 			error("updateMultiSong() failed. Breakpoint in %s, line %d", __FILE__, __LINE__);
@@ -641,7 +638,7 @@
 #define DELAY (1000000 / SFX_TICKS_PER_SEC)
 
 void SfxState::sfx_init(ResourceManager *resmgr, int flags) {
-	song_lib_init(&_songlib);
+	_songlib.initSounds();
 	_song = NULL;
 	_flags = flags;
 	_soundSync = NULL;
@@ -691,7 +688,7 @@
 
 	g_system->getMixer()->stopAll();
 
-	song_lib_free(_songlib);
+	_songlib.freeSounds();
 
 	// Delete audio resources for CD talkie games
 	if (_audioResource) {
@@ -793,7 +790,7 @@
 /*****************/
 
 void SfxState::sfx_add_song(SongIterator *it, int priority, SongHandle handle, int number) {
-	Song *song = song_lib_find(_songlib, handle);
+	Song *song = _songlib.findSong(handle);
 
 #ifdef DEBUG_SONG_API
 	fprintf(stderr, "[sfx-core] Adding song: %08lx at %d, it=%p\n", handle, priority, it);
@@ -822,7 +819,7 @@
 			        handle, song->_status);
 			return;
 		} else {
-			song_lib_remove(_songlib, handle); /* No duplicates */
+			_songlib.removeSong(handle); /* No duplicates */
 		}
 
 	}
@@ -832,7 +829,7 @@
 	song->_hold = 0;
 	song->_loops = 0;
 	song->_wakeupTime = Audio::Timestamp(g_system->getMillis(), SFX_TICKS_PER_SEC);
-	song_lib_add(_songlib, song);
+	_songlib.addSong(song);
 	_song = NULL; /* As above */
 	update();
 
@@ -846,7 +843,7 @@
 	if (_song && _song->_handle == handle)
 		_song = NULL;
 
-	song_lib_remove(_songlib, handle);
+	_songlib.removeSong(handle);
 	update();
 }
 
@@ -859,7 +856,7 @@
 #define ASSERT_SONG(s) if (!(s)) { warning("Looking up song handle %08lx failed in %s, L%d", handle, __FILE__, __LINE__); return; }
 
 void SfxState::sfx_song_set_status(SongHandle handle, int status) {
-	Song *song = song_lib_find(_songlib, handle);
+	Song *song = _songlib.findSong(handle);
 	ASSERT_SONG(song);
 #ifdef DEBUG_SONG_API
 	fprintf(stderr, "[sfx-core] Setting song status to %d"
@@ -875,7 +872,7 @@
 #ifdef DEBUG_SONG_API
 	static const char *stopmsg[] = {"??? Should not happen", "Do not stop afterwards", "Stop afterwards"};
 #endif
-	Song *song = song_lib_find(_songlib, handle);
+	Song *song = _songlib.findSong(handle);
 
 	ASSERT_SONG(song);
 
@@ -892,7 +889,7 @@
 }
 
 void SfxState::sfx_song_renice(SongHandle handle, int priority) {
-	Song *song = song_lib_find(_songlib, handle);
+	Song *song = _songlib.findSong(handle);
 	ASSERT_SONG(song);
 #ifdef DEBUG_SONG_API
 	fprintf(stderr, "[sfx-core] Renicing song %08lx to %d\n",
@@ -905,7 +902,7 @@
 }
 
 void SfxState::sfx_song_set_loops(SongHandle handle, int loops) {
-	Song *song = song_lib_find(_songlib, handle);
+	Song *song = _songlib.findSong(handle);
 	SongIterator::Message msg = SongIterator::Message(handle, SIMSG_SET_LOOPS(loops));
 	ASSERT_SONG(song);
 
@@ -922,7 +919,7 @@
 }
 
 void SfxState::sfx_song_set_hold(SongHandle handle, int hold) {
-	Song *song = song_lib_find(_songlib, handle);
+	Song *song = _songlib.findSong(handle);
 	SongIterator::Message msg = SongIterator::Message(handle, SIMSG_SET_HOLD(hold));
 	ASSERT_SONG(song);
 
@@ -1003,7 +1000,7 @@
 	fprintf(stderr, "[sfx-core] All stop\n");
 #endif
 
-	song_lib_free(_songlib);
+	_songlib.freeSounds();
 	update();
 }
 

Modified: scummvm/trunk/engines/sci/sfx/songlib.cpp
===================================================================
--- scummvm/trunk/engines/sci/sfx/songlib.cpp	2009-06-07 17:06:32 UTC (rev 41342)
+++ scummvm/trunk/engines/sci/sfx/songlib.cpp	2009-06-07 17:06:51 UTC (rev 41343)
@@ -54,23 +54,23 @@
 	return retval;
 }
 
-void song_lib_add(const SongLibrary &songlib, Song *song) {
+void SongLibrary::addSong(Song *song) {
 	Song **seeker = NULL;
 	int pri	= song->_priority;
 
 	if (NULL == song) {
-		sciprintf("song_lib_add(): NULL passed for song\n");
+		sciprintf("addSong(): NULL passed for song\n");
 		return;
 	}
 
-	if (*(songlib._lib) == NULL) {
-		*(songlib._lib) = song;
+	if (*_lib == NULL) {
+		*_lib = song;
 		song->_next = NULL;
 
 		return;
 	}
 
-	seeker = (songlib._lib);
+	seeker = _lib;
 	while (*seeker && ((*seeker)->_priority > pri))
 		seeker = &((*seeker)->_next);
 
@@ -78,30 +78,27 @@
 	*seeker = song;
 }
 
-static void _songfree_chain(Song *song) {
-	/* Recursively free a chain of songs */
-	if (song) {
-		_songfree_chain(song->_next);
+void SongLibrary::initSounds() {
+	_lib = &_s;
+	_s = NULL;
+}
+
+void SongLibrary::freeSounds() {
+	Song *next = *_lib;
+	while (next) {
+		Song *song = next;
 		delete song->_it;
 		song->_it = NULL;
+		next = song->_next;
 		free(song);
 	}
+	*_lib = NULL;
 }
 
-void song_lib_init(SongLibrary *songlib) {
-	songlib->_lib = &(songlib->_s);
-	songlib->_s = NULL;
-}
 
-void song_lib_free(const SongLibrary &songlib) {
-	_songfree_chain(*(songlib._lib));
-	*(songlib._lib) = NULL;
-}
+Song *SongLibrary::findSong(SongHandle handle) {
+	Song *seeker = *_lib;
 
-
-Song *song_lib_find(const SongLibrary &songlib, SongHandle handle) {
-	Song *seeker = *(songlib._lib);
-
 	while (seeker) {
 		if (seeker->_handle == handle)
 			break;
@@ -111,8 +108,8 @@
 	return seeker;
 }
 
-Song *song_lib_find_next_active(const SongLibrary &songlib, Song *other) {
-	Song *seeker = other ? other->_next : *(songlib._lib);
+Song *SongLibrary::findNextActive(Song *other) {
+	Song *seeker = other ? other->_next : *_lib;
 
 	while (seeker) {
 		if ((seeker->_status == SOUND_STATUS_WAITING) ||
@@ -128,19 +125,19 @@
 	return seeker;
 }
 
-Song *song_lib_find_active(const SongLibrary &songlib) {
-	return song_lib_find_next_active(songlib, NULL);
+Song *SongLibrary::findFirstActive() {
+	return findNextActive(NULL);
 }
 
-int song_lib_remove(const SongLibrary &songlib, SongHandle handle) {
+int SongLibrary::removeSong(SongHandle handle) {
 	int retval;
-	Song *goner = *(songlib._lib);
+	Song *goner = *_lib;
 
 	if (!goner)
 		return -1;
 
 	if (goner->_handle == handle)
-		*(songlib._lib) = goner->_next;
+		*_lib = goner->_next;
 
 	else {
 		while ((goner->_next) && (goner->_next->_handle != handle))
@@ -162,11 +159,11 @@
 	return retval;
 }
 
-void song_lib_resort(const SongLibrary &songlib, Song *song) {
-	if (*(songlib._lib) == song)
-		*(songlib._lib) = song->_next;
+void SongLibrary::resortSong(Song *song) {
+	if (*_lib == song)
+		*_lib = song->_next;
 	else {
-		Song *seeker = *(songlib._lib);
+		Song *seeker = *_lib;
 
 		while (seeker->_next && (seeker->_next != song))
 			seeker = seeker->_next;
@@ -175,11 +172,11 @@
 			seeker->_next = seeker->_next->_next;
 	}
 
-	song_lib_add(songlib, song);
+	addSong(song);
 }
 
-int song_lib_count(const SongLibrary &songlib) {
-	Song *seeker = *(songlib._lib);
+int SongLibrary::countSongs() {
+	Song *seeker = *_lib;
 	int retval = 0;
 
 	while (seeker) {
@@ -190,8 +187,8 @@
 	return retval;
 }
 
-void song_lib_set_restore_behavior(const SongLibrary &songlib, SongHandle handle, RESTORE_BEHAVIOR action) {
-	Song *seeker = song_lib_find(songlib, handle);
+void SongLibrary::setSongRestoreBehavior(SongHandle handle, RESTORE_BEHAVIOR action) {
+	Song *seeker = findSong(handle);
 
 	seeker->_restoreBehavior = action;
 }

Modified: scummvm/trunk/engines/sci/sfx/songlib.h
===================================================================
--- scummvm/trunk/engines/sci/sfx/songlib.h	2009-06-07 17:06:32 UTC (rev 41342)
+++ scummvm/trunk/engines/sci/sfx/songlib.h	2009-06-07 17:06:51 UTC (rev 41343)
@@ -83,11 +83,6 @@
 };
 
 
-struct SongLibrary {
-	Song **_lib;
-	Song *_s;
-};
-
 /**************************/
 /* Song library commands: */
 /**************************/
@@ -102,79 +97,80 @@
 Song *song_new(SongHandle handle, SongIterator *it, int priority);
 
 
-/* Initializes a static song library
-** Parameters: (SongLibrary *) songlib: Pointer to the library
-**             to initialize
-** Returns   : (void)
-*/
-void song_lib_init(SongLibrary *songlib);
+class SongLibrary {
+public:
+	Song **_lib;
+	Song *_s;
 
-/* Frees a song library
-** Parameters: (SongLibrary) songlib: The library to free
-** Returns   : (void)
-*/
-void song_lib_free(const SongLibrary &songlib);
+public:
+	SongLibrary() {}
 
-/* Adds a song to a song library.
-** Parameters: (SongLibrary) songlib: An existing sound library, or NULL
-**             (Song *) song: The song to add
-** Returns   : (void)
-*/
-void song_lib_add(const SongLibrary &songlib, Song *song);
+	/** Initializes a static song library */
+	void initSounds();
 
-/* Looks up the song with the specified handle
-** Parameters: (SongLibrary) songlib: An existing sound library, may point to NULL
-**             (SongHandle) handle: The sound handle to look for
-** Returns   : (Song *) The song or NULL if it wasn't found
-*/
-Song *song_lib_find(const SongLibrary &songlib, SongHandle handle);
+	/** Frees a song library. */
+	void freeSounds();
 
-/* Finds the first song playing with the highest priority
-** Parameters: (SongLibrary) songlib: An existing sound library
-** Returns   : (Song *) The song that should be played next, or NULL if there is none
-*/
-Song *song_lib_find_active(const SongLibrary &songlib);
+	/**
+	 * Adds a song to a song library.
+	 * @param song		song to add
+	 */
+	void addSong(Song *song);
 
-/* Finds the next song playing with the highest priority
-** Parameters: (SongLibrary) songlib: The song library to operate on
-**             (Song *) song: A song previously returned from the song library
-** Returns   : (Song *) The next song to play relative to 'song', or
-**                        NULL if none are left
-** The functions 'song_lib_find_active' and 'song_lib_find_next_active
-** allow to iterate over all songs that satisfy the requirement of
-** being 'playable'.
-*/
-Song *song_lib_find_next_active(const SongLibrary &songlib, Song *song);
+	/**
+	 * Looks up the song with the specified handle.
+	 * @param handle	sound handle to look for
+	 * @return the song or NULL if it wasn't found
+	 */
+	Song *findSong(SongHandle handle);
 
-/* Removes a song from the library
-** Parameters: (SongLibrary) songlib: An existing sound library
-**             (SongHandle) handle: Handle of the song to remove
-** Returns   : (int) The status of the song that was removed
-*/
-int song_lib_remove(const SongLibrary &songlib, SongHandle handle);
+	/**
+	 * Finds the first song playing with the highest priority.
+	 * @return the song that should be played next, or NULL if there is none
+	 */
+	Song *findFirstActive();
 
-/* Removes a song from the library and sorts it in again; for use after renicing
-** Parameters: (SongLibrary) songlib: An existing sound library
-**             (Song *) song: The song to work on
-** Returns   : (void)
-*/
-void song_lib_resort(const SongLibrary &songlib, Song *song);
+	/**
+	 * Finds the next song playing with the highest priority.
+	 *
+	 * The functions 'findFirstActive' and 'findNextActive'
+	 * allow to iterate over all songs that satisfy the requirement of
+	 * being 'playable'.
+	 *
+	 * @param song		a song previously returned from the song library
+	 * @return the next song to play relative to 'song', or NULL if none are left
+	 */
+	Song *findNextActive(Song *song);
 
-/* Counts the number of songs in a song library
-** Parameters: (SongLibrary) songlib: The library to count
-** Returns   : (int) The number of songs
-*/
-int song_lib_count(const SongLibrary &songlib);
+	/**
+	 * Removes a song from the library.
+	 * @param handle		handle of the song to remove
+	 * @return the status of the song that was removed
+	 */
+	int removeSong(SongHandle handle);
 
-/* Determines what should be done with the song "handle" when
-** restoring it from a saved game.
-** Parameters: (SongLibrary) songlib: The library that contains the song
-**             (SongHandle) handle: Its handle
-**             (RESTORE_BEHAVIOR) action: The desired action
-*/
-void song_lib_set_restore_behavior(const SongLibrary &songlib, SongHandle handle,
-	RESTORE_BEHAVIOR action);
+	/**
+	 * Removes a song from the library and sorts it in again; for use after renicing.
+	 * @param son		song to work on
+	 */
+	void resortSong(Song *song);
 
+	/**
+	 * Counts the number of songs in a song library.
+	 * @return the number of songs
+	 */
+	int countSongs();
+
+	/**
+	 * Determines what should be done with the song "handle" when restoring
+	 * it from a saved game.
+	 * @param handle	sound handle being restored
+	 * @param action	desired action
+	 */
+	void setSongRestoreBehavior(SongHandle handle,
+		RESTORE_BEHAVIOR action);
+};
+
 } // End of namespace Sci
 
 #endif // SCI_SSFX_SFX_SONGLIB_H


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list