(svn r5669) - Backport from trunk (r5464, r3641): 0.4
authorDarkvater
Mon, 31 Jul 2006 11:50:23 +0000
branch0.4
changeset 10065 fc91a7eca19e
parent 10064 c63f74223041
child 10066 250f5564fcae
(svn r5669) - Backport from trunk (r5464, r3641):
Codechange: verify the presence of music files in the gm folder. Slightly altered r5464
to exclude the addition of music.c and left out the extra functionality. While in essence
this is not a true fix, several people have reported a rising CPU usage because Dmusic
kept indefinitely looping the file list. This should solve that.
music.h
music_gui.c
settings.c
sound.h
strings.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/music.h	Mon Jul 31 11:50:23 2006 +0000
@@ -0,0 +1,16 @@
+/* $Id */
+
+#ifndef MUSIC_H
+#define MUSIC_H
+
+#define NUM_SONGS_PLAYLIST 33
+#define NUM_SONGS_AVAILABLE 22
+
+typedef struct SongSpecs {
+	char filename[256];
+	char song_name[64];
+} SongSpecs;
+
+extern const SongSpecs origin_songs_specs[NUM_SONGS_AVAILABLE];
+
+#endif //MUSIC_H
--- a/music_gui.c	Mon Jul 31 11:29:18 2006 +0000
+++ b/music_gui.c	Mon Jul 31 11:50:23 2006 +0000
@@ -10,12 +10,12 @@
 #include "hal.h"
 #include "macros.h"
 #include "variables.h"
+#include "music.h"
 
 static byte _music_wnd_cursong;
 static bool _song_is_active;
-static byte _cur_playlist[33];
+static byte _cur_playlist[NUM_SONGS_PLAYLIST];
 
-#define NUM_SONGS_AVAILABLE 22
 
 
 static byte _playlist_all[] = {
@@ -43,33 +43,31 @@
 	msf.custom_2,
 };
 
-// Map the order of the song names to the numbers of the midi filenames
-static const byte midi_idx[] = {
-	 0, // Tycoon DELUXE Theme
-	 2, // Easy Driver
-	 3, // Little Red Diesel
-	17, // Cruise Control
-	 7, // Don't Walk!
-	 9, // Fell Apart On Me
-	 4, // City Groove
-	19, // Funk Central
-	 6, // Stoke It
-	12, // Road Hog
-	 5, // Aliens Ate My Railway
-	 1, // Snarl Up
-	18, // Stroll On
-	10, // Can't Get There From Here
-	 8, // Sawyer's Tune
-	13, // Hold That Train!
-	21, // Movin' On
-	15, // Goss Groove
-	16, // Small Town
-	14, // Broomer's Oil Rag
-	20, // Jammit
-	11  // Hard Drivin'
+const SongSpecs origin_songs_specs[NUM_SONGS_AVAILABLE] = {
+	{"gm_tt00.gm", "Tycoon DELUXE Theme"},
+	{"gm_tt02.gm", "Easy Driver"},
+	{"gm_tt03.gm", "Little Red Diesel"},
+	{"gm_tt17.gm", "Cruise Control"},
+	{"gm_tt07.gm", "Don't Walk!"},
+	{"gm_tt09.gm", "Fell Apart On Me"},
+	{"gm_tt04.gm", "City Groove"},
+	{"gm_tt19.gm", "Funk Central"},
+	{"gm_tt06.gm", "Stoke It"},
+	{"gm_tt12.gm", "Road Hog"},
+	{"gm_tt05.gm", "Aliens Ate My Railway"},
+	{"gm_tt01.gm", "Snarl Up"},
+	{"gm_tt18.gm", "Stroll On"},
+	{"gm_tt10.gm", "Can't Get There From Here"},
+	{"gm_tt08.gm", "Sawyer's Tune"},
+	{"gm_tt13.gm", "Hold That Train!"},
+	{"gm_tt21.gm", "Movin' On"},
+	{"gm_tt15.gm", "Goss Groove"},
+	{"gm_tt16.gm", "Small Town"},
+	{"gm_tt14.gm", "Broomer's Oil Rag"},
+	{"gm_tt20.gm", "Jammit"},
+	{"gm_tt11.gm", "Hard Drivin'"},
 };
 
-
 static void SkipToPrevSong(void)
 {
 	byte *b = _cur_playlist;
@@ -118,8 +116,8 @@
 static void DoPlaySong(void)
 {
 	char filename[256];
-	snprintf(filename, sizeof(filename), "%sgm_tt%.2d.gm",
-		_path.gm_dir, midi_idx[_music_wnd_cursong - 1]);
+	snprintf(filename, sizeof(filename), "%s%s",
+		_path.gm_dir, origin_songs_specs[_music_wnd_cursong - 1].filename);
 	_music_driver->play_song(filename);
 }
 
@@ -131,10 +129,19 @@
 static void SelectSongToPlay(void)
 {
 	uint i = 0;
+	uint j = 0;
+	char filename[256];
 
 	memset(_cur_playlist, 0, sizeof(_cur_playlist));
 	do {
-		_cur_playlist[i] = _playlists[msf.playlist][i];
+		snprintf(filename, sizeof(filename),  "%s%s",
+		_path.gm_dir, origin_songs_specs[_playlists[msf.playlist][i]].filename);
+		//we are now checking for the existence of that file prior
+		//to add it to the list of available songs
+		if (FileExists(filename)) {
+			_cur_playlist[j] = _playlists[msf.playlist][i];
+			j++;
+		}
 	} while (_playlists[msf.playlist][i++] != 0 && i < lengthof(_cur_playlist) - 1);
 
 	if (msf.shuffle) {
@@ -165,7 +172,15 @@
 {
 	if (_cur_playlist[0] == 0) {
 		SelectSongToPlay();
-		if (_cur_playlist[0] == 0) return;
+		//if there is not songs in the playlist, it may indicate
+		//no file on the gm folder, or even no gm folder.
+		//Stop the playback, then
+		if (_cur_playlist[0] == 0) {
+			_song_is_active = false;
+			_music_wnd_cursong = 0;
+			msf.playing = false;
+			return;
+		}
 	}
 	_music_wnd_cursong = _cur_playlist[0];
 	DoPlaySong();
@@ -182,13 +197,13 @@
 
 void MusicLoop(void)
 {
-	if (!msf.btn_down && _song_is_active) {
+	if (!msf.playing && _song_is_active) {
 		StopMusic();
-	} else if (msf.btn_down && !_song_is_active) {
+	} else if (msf.playing && !_song_is_active) {
 		PlayPlaylistSong();
 	}
 
-	if (_song_is_active == false) return;
+	if (!_song_is_active) return;
 
 	if (!_music_driver->is_song_playing()) {
 		if (_game_mode != GM_MENU) {
@@ -257,7 +272,7 @@
 			if (!IS_INT_INSIDE(y, 0, NUM_SONGS_AVAILABLE)) return;
 
 			p = _playlists[msf.playlist];
-			for (i = 0; i != 32; i++) {
+			for (i = 0; i != NUM_SONGS_PLAYLIST - 1; i++) {
 				if (p[i] == 0) {
 					p[i] = y + 1;
 					p[i + 1] = 0;
@@ -339,7 +354,7 @@
 					color = 0xB8;
 				}
 			}
-			GfxFillRect(187, 33 - i * 2, 200, 33 - i * 2, color);
+			GfxFillRect(187, NUM_SONGS_PLAYLIST - i * 2, 200, NUM_SONGS_PLAYLIST - i * 2, color);
 		}
 
 		GfxFillRect(60, 46, 239, 52, 0);
@@ -405,10 +420,10 @@
 			SkipToNextSong();
 			break;
 		case 4: // stop playing
-			msf.btn_down = false;
+			msf.playing = false;
 			break;
 		case 5: // start playing
-			msf.btn_down = true;
+			msf.playing = true;
 			break;
 		case 6:{ // volume sliders
 			byte *vol,new_vol;
--- a/settings.c	Mon Jul 31 11:29:18 2006 +0000
+++ b/settings.c	Mon Jul 31 11:50:23 2006 +0000
@@ -742,7 +742,7 @@
 	{"effect_vol",SDT_UINT8,	(void*)128,		&msf.effect_vol, NULL},
 	{"custom_1",	SDT_INTLIST | SDT_UINT8 | lengthof(msf.custom_1) << 16, NULL, &msf.custom_1, NULL},
 	{"custom_2",	SDT_INTLIST | SDT_UINT8 | lengthof(msf.custom_2) << 16, NULL, &msf.custom_2, NULL},
-	{"playing",		SDT_BOOL,		(void*)true,	&msf.btn_down, NULL},
+	{"playing",		SDT_BOOL,		(void*)true,	&msf.playing, NULL},
 	{"shuffle",		SDT_BOOL,		(void*)false, &msf.shuffle, NULL},
 	{"extmidi",   SDT_STRINGBUF | (lengthof(msf.extmidi)<<16), EXTERNAL_PLAYER, &msf.extmidi, NULL},
 	{NULL,				0,					NULL,					NULL,																NULL}
--- a/sound.h	Mon Jul 31 11:29:18 2006 +0000
+++ b/sound.h	Mon Jul 31 11:50:23 2006 +0000
@@ -9,7 +9,7 @@
 	byte effect_vol;
 	byte custom_1[33];
 	byte custom_2[33];
-	bool btn_down;
+	bool playing;
 	bool shuffle;
 	char extmidi[80];
 } MusicFileSettings;
--- a/strings.c	Mon Jul 31 11:29:18 2006 +0000
+++ b/strings.c	Mon Jul 31 11:50:23 2006 +0000
@@ -16,6 +16,7 @@
 #include "waypoint.h"
 #include "industry.h"
 #include "variables.h"
+#include "music.h"
 
 char _userstring[128];
 
@@ -899,31 +900,6 @@
 	return buff;
 }
 
-static const char * const _song_names[] = {
-	"Tycoon DELUXE Theme",
-	"Easy Driver",
-	"Little Red Diesel",
-	"Cruise Control",
-	"Don't Walk!",
-	"Fell Apart On Me",
-	"City Groove",
-	"Funk Central",
-	"Stoke It",
-	"Road Hog",
-	"Aliens Ate My Railway",
-	"Snarl Up",
-	"Stroll On",
-	"Can't Get There From Here",
-	"Sawyer's Tune",
-	"Hold That Train!",
-	"Movin' On",
-	"Goss Groove",
-	"Small Town",
-	"Broomer's Oil Rag",
-	"Jammit",
-	"Hard Drivin'"
-};
-
 static char *GetSpecialPlayerNameString(char *buff, int ind, const int32 *argv)
 {
 	switch (ind) {
@@ -937,7 +913,7 @@
 			return GenPresidentName(buff, GetInt32(&argv));
 
 		case 4: // song names
-			return strecpy(buff, _song_names[GetInt32(&argv) - 1], NULL);
+			return strecpy(buff, origin_songs_specs[GetInt32(&argv) - 1].song_name, NULL);
 	}
 
 	// town name?