Use a 2D array to allow playing of songs by track number in future

This commit is contained in:
kreatordxx 2009-01-13 12:39:03 +00:00
parent f1544295dd
commit 81de582264
2 changed files with 48 additions and 83 deletions

View file

@ -3,6 +3,7 @@ D1X-Rebirth Changelog
20090113 20090113
-------- --------
main/collide.c, main/physics.c: Adjusted D1 Lifter collision damage according to D2 to scale with FPS as Melee combats can happen in every frame; Actually set velocity from movement when colliding with objects main/collide.c, main/physics.c: Adjusted D1 Lifter collision damage according to D2 to scale with FPS as Melee combats can happen in every frame; Actually set velocity from movement when colliding with objects
arch/sdl/jukebox.c: Use a 2D array to allow playing of songs by track number in future
20081230 20081230
-------- --------

View file

@ -20,63 +20,30 @@
#define JUKEBOX_HUDMSG_PLAYING "Now playing:" #define JUKEBOX_HUDMSG_PLAYING "Now playing:"
#define JUKEBOX_HUDMSG_STOPPED "Jukebox stopped" #define JUKEBOX_HUDMSG_STOPPED "Jukebox stopped"
static int jukebox_loaded = 0; static int jukebox_playing = -1;
static int jukebox_playing = 0; static int jukebox_numsongs = 0;
static dl_list *JukeboxSongs; static char **JukeboxSongs = NULL;
char hud_msg_buf[MUSIC_HUDMSG_MAXLEN+4]; char hud_msg_buf[MUSIC_HUDMSG_MAXLEN+4];
char *select_prev_song(dl_list *list) {
char *ret;
if (dl_is_empty(list)) {
ret = NULL;
}
else {
if (list->first == list->current) {
list->current = list->last;
}
else {
dl_backward(list);
}
ret = (char *) list->current->data;
}
return ret;
}
char *select_next_song(dl_list *list) {
char *ret;
if (dl_is_empty(list)) {
ret = NULL;
}
else {
if (list->last == list->current) {
list->current = list->first;
}
else {
dl_forward(list);
}
ret = (char *) list->current->data;
}
return ret;
}
void jukebox_unload() void jukebox_unload()
{ {
int i;
if (JukeboxSongs == NULL) if (JukeboxSongs == NULL)
return; return;
while (JukeboxSongs->first!=NULL) for (i = 0; JukeboxSongs[i]!=NULL; i++)
{ {
dl_remove(JukeboxSongs,JukeboxSongs->first); free(JukeboxSongs[i]);
} }
d_free(JukeboxSongs); free(JukeboxSongs);
jukebox_loaded = 0; JukeboxSongs = NULL;
} }
/* Loads music file names from a given directory */ /* Loads music file names from a given directory */
void jukebox_load() { void jukebox_load() {
int count = 0; int count;
char **files;
char *music_exts[] = { ".mp3", ".ogg", ".wav", ".aif", NULL }; char *music_exts[] = { ".mp3", ".ogg", ".wav", ".aif", NULL };
static char curpath[PATH_MAX+1]; static char curpath[PATH_MAX+1];
@ -86,45 +53,38 @@ void jukebox_load() {
jukebox_unload(); jukebox_unload();
} }
if (jukebox_loaded) if (JukeboxSongs)
return; return;
if (GameCfg.JukeboxOn) { if (GameCfg.JukeboxOn) {
// Adding as a mount point is an option, but wouldn't work for older versions of PhysicsFS // Adding as a mount point is an option, but wouldn't work for older versions of PhysicsFS
PHYSFS_addToSearchPath(GameCfg.JukeboxPath, 1); PHYSFS_addToSearchPath(GameCfg.JukeboxPath, 1);
JukeboxSongs = dl_init(); JukeboxSongs = PHYSFSX_findFiles("", music_exts);
files = PHYSFSX_findFiles("", music_exts);
if (files != NULL && *files != NULL) { if (JukeboxSongs != NULL) {
char **i; for (count = 0; JukeboxSongs[count]!=NULL; count++) {}
for (i=files; *i!=NULL; i++)
{
dl_add(JukeboxSongs, *i);
count++;
}
if (count) if (count)
{ {
con_printf(CON_DEBUG,"Jukebox: %d music file(s) found in %s\n", count, GameCfg.JukeboxPath); con_printf(CON_DEBUG,"Jukebox: %d music file(s) found in %s\n", count, GameCfg.JukeboxPath);
memcpy(curpath,GameCfg.JukeboxPath,PATH_MAX); memcpy(curpath,GameCfg.JukeboxPath,PATH_MAX);
jukebox_loaded = 1; jukebox_numsongs = count;
} }
else { con_printf(CON_DEBUG,"Jukebox music could not be found!\n"); } else { con_printf(CON_DEBUG,"Jukebox music could not be found!\n"); }
} }
else else
{ Int3(); } // should at least find a directory in some search path, otherwise how did D2X load? { Int3(); } // should at least find a directory in some search path, otherwise how did D2X load?
if (files != NULL)
free(files);
} }
} }
void jukebox_play(int loop) { void jukebox_play(int loop) {
char *music_filename; char *music_filename;
if (!jukebox_loaded) return; if (!JukeboxSongs) return;
music_filename = (char *) JukeboxSongs->current->data;
if (jukebox_playing == -1)
jukebox_playing = 0; // For now - will allow track numbers as arguments in future
music_filename = JukeboxSongs[jukebox_playing];
mix_play_file(music_filename, loop); mix_play_file(music_filename, loop);
@ -138,19 +98,18 @@ void jukebox_play(int loop) {
} }
hud_message(MSGC_GAME_FEEDBACK, "%s %s", JUKEBOX_HUDMSG_PLAYING, hud_msg_buf); hud_message(MSGC_GAME_FEEDBACK, "%s %s", JUKEBOX_HUDMSG_PLAYING, hud_msg_buf);
jukebox_playing = 1;
} }
void jukebox_stop() { void jukebox_stop() {
if (!jukebox_loaded) return; // since this function is also used for stopping MIDI if (!JukeboxSongs) return; // since this function is also used for stopping MIDI
mix_stop_music(); mix_stop_music();
if (jukebox_playing) if (jukebox_playing != -1)
hud_message(MSGC_GAME_FEEDBACK, JUKEBOX_HUDMSG_STOPPED); hud_message(MSGC_GAME_FEEDBACK, JUKEBOX_HUDMSG_STOPPED);
jukebox_playing = 0; jukebox_playing = -1;
} }
void jukebox_pause_resume() { void jukebox_pause_resume() {
if (!jukebox_loaded) return; if (!JukeboxSongs) return;
if (Mix_PausedMusic()) if (Mix_PausedMusic())
{ {
@ -165,42 +124,47 @@ void jukebox_pause_resume() {
} }
void jukebox_hook_stop() { void jukebox_hook_stop() {
if (!jukebox_loaded) return; if (!JukeboxSongs) return;
} }
void jukebox_hook_next() { void jukebox_hook_next() {
if (!jukebox_loaded) return; if (!JukeboxSongs) return;
if (jukebox_playing) jukebox_next(); if (jukebox_playing != -1) jukebox_next();
} }
void jukebox_next() { void jukebox_next() {
if (!jukebox_loaded) return; if (!JukeboxSongs || jukebox_playing == -1) return;
select_next_song(JukeboxSongs);
if (jukebox_playing) jukebox_play(1); jukebox_playing++;
if (jukebox_playing == jukebox_numsongs)
jukebox_playing = 0;
jukebox_play(1);
} }
void jukebox_prev() { void jukebox_prev() {
if (!jukebox_loaded) return; if (!JukeboxSongs || jukebox_playing == -1) return;
select_prev_song(JukeboxSongs);
if (jukebox_playing) jukebox_play(1); jukebox_playing--;
if (jukebox_playing == -1)
jukebox_playing = jukebox_numsongs - 1;
jukebox_play(1);
} }
char *jukebox_current() { char *jukebox_current() {
return JukeboxSongs->current->data; return JukeboxSongs[jukebox_playing];
} }
int jukebox_is_loaded() { return jukebox_loaded; } int jukebox_is_loaded() { return (JukeboxSongs != NULL); }
int jukebox_is_playing() { return jukebox_playing; } int jukebox_is_playing() { return jukebox_playing + 1; }
void jukebox_list() { void jukebox_list() {
dl_item *curr; int i;
if (!jukebox_loaded) return; if (!JukeboxSongs) return;
if (dl_is_empty(JukeboxSongs)) { if (!(*JukeboxSongs)) {
con_printf(CON_DEBUG,"* No songs have been found\n"); con_printf(CON_DEBUG,"* No songs have been found\n");
} }
else { else {
for (curr = JukeboxSongs->first; curr != NULL; curr = curr->next) { for (i = 0; i < jukebox_numsongs; i++)
con_printf(CON_DEBUG,"* %s\n", (char *) curr->data); con_printf(CON_DEBUG,"* %s\n", JukeboxSongs[i]);
}
} }
} }