2007-01-15 12:09:25 -08:00
|
|
|
/*
|
|
|
|
This file is part of Warzone 2100.
|
|
|
|
Copyright (C) 1999-2004 Eidos Interactive
|
|
|
|
Copyright (C) 2005-2007 Warzone Resurrection Project
|
|
|
|
|
|
|
|
Warzone 2100 is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Warzone 2100 is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Warzone 2100; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-06-28 10:47:08 -07:00
|
|
|
#include <stdio.h>
|
2006-06-16 12:10:23 -07:00
|
|
|
#include <physfs.h>
|
2006-11-06 06:40:07 -08:00
|
|
|
#include <string.h>
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2006-06-02 12:34:58 -07:00
|
|
|
#include "lib/framework/frame.h"
|
2007-06-28 10:47:08 -07:00
|
|
|
|
2006-09-13 14:13:19 -07:00
|
|
|
#include "playlist.h"
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
#define BUFFER_SIZE 2048
|
|
|
|
|
|
|
|
#define NB_TRACKS 3
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char** songs;
|
|
|
|
unsigned int nb_songs;
|
|
|
|
unsigned int list_size;
|
|
|
|
BOOL shuffle;
|
|
|
|
} WZ_TRACK;
|
|
|
|
|
|
|
|
static unsigned int current_track = 0;
|
|
|
|
static unsigned int current_song = 0;
|
|
|
|
|
|
|
|
static WZ_TRACK playlist[NB_TRACKS];
|
|
|
|
|
|
|
|
#define CURRENT_TRACK playlist[current_track]
|
|
|
|
|
2006-09-13 14:11:11 -07:00
|
|
|
void PlayList_Init(void) {
|
2007-06-28 10:47:08 -07:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NB_TRACKS; ++i) {
|
2006-09-17 13:39:25 -07:00
|
|
|
playlist[i].songs = (char**)malloc(2*sizeof(char*));
|
2007-06-28 10:47:08 -07:00
|
|
|
playlist[i].list_size = 2;
|
|
|
|
playlist[i].nb_songs = 0;
|
2006-02-18 10:54:37 -08:00
|
|
|
memset( playlist[i].songs, 0, playlist[i].list_size*sizeof(char*) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-13 14:11:11 -07:00
|
|
|
void PlayList_Quit(void) {
|
2006-02-18 10:54:37 -08:00
|
|
|
unsigned int i, j;
|
|
|
|
|
|
|
|
for( i = 0; i < NB_TRACKS; ++i ) {
|
|
|
|
for( j = 0; j < playlist[i].list_size; ++j ) {
|
|
|
|
free( playlist[i].songs[j] );
|
|
|
|
}
|
|
|
|
free( playlist[i].songs );
|
2007-06-28 10:47:08 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-10 14:50:09 -08:00
|
|
|
char PlayList_Read(const char* path)
|
|
|
|
{
|
2006-06-16 12:10:23 -07:00
|
|
|
PHYSFS_file * f;
|
2007-06-28 10:47:08 -07:00
|
|
|
char* path_to_music = NULL;
|
2006-11-26 02:54:22 -08:00
|
|
|
char buffer[BUFFER_SIZE], ByteBuf = '\0';
|
|
|
|
unsigned int ByteBufPos = 0;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
sprintf(buffer, "%s/music.wpl", path);
|
|
|
|
|
2006-06-16 12:10:23 -07:00
|
|
|
f = PHYSFS_openRead(buffer);
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
if (f == NULL) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-06-16 12:10:23 -07:00
|
|
|
while (!PHYSFS_eof(f)) {
|
2007-06-28 10:47:08 -07:00
|
|
|
char* filename;
|
|
|
|
|
2006-11-26 02:54:22 -08:00
|
|
|
while( ByteBufPos < BUFFER_SIZE - 1 && PHYSFS_read( f, &ByteBuf, 1, 1 ) && ByteBuf != '\n' && ByteBuf != '\r' )
|
2006-07-17 13:48:39 -07:00
|
|
|
{
|
|
|
|
buffer[ByteBufPos]=ByteBuf;
|
|
|
|
ByteBufPos++;
|
|
|
|
}
|
|
|
|
buffer[ByteBufPos]='\0';
|
|
|
|
ByteBufPos=0;
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
if (strncmp(buffer, "[game]", 6) == 0) {
|
|
|
|
current_track = 1;
|
|
|
|
free(path_to_music);
|
|
|
|
path_to_music = NULL;
|
|
|
|
CURRENT_TRACK.shuffle = FALSE;
|
|
|
|
} else if (strncmp(buffer, "[menu]", 6) == 0) {
|
|
|
|
current_track = 2;
|
|
|
|
free(path_to_music);
|
|
|
|
path_to_music = NULL;
|
|
|
|
CURRENT_TRACK.shuffle = FALSE;
|
|
|
|
} else if (strncmp(buffer, "path=", 5) == 0) {
|
|
|
|
free(path_to_music);
|
|
|
|
path_to_music = strtok(buffer+5, "\n");
|
|
|
|
if (strcmp(path_to_music, ".") == 0) {
|
|
|
|
path_to_music = strdup(path);
|
|
|
|
} else {
|
|
|
|
path_to_music = strdup(path_to_music);
|
|
|
|
}
|
2007-01-10 14:50:09 -08:00
|
|
|
debug( LOG_SOUND, " path = %s\n", path_to_music );
|
2007-06-28 10:47:08 -07:00
|
|
|
} else if (strncmp(buffer, "shuffle=", 8) == 0) {
|
|
|
|
if (strcmp(strtok(buffer+8, "\n"), "yes") == 0) {
|
|
|
|
CURRENT_TRACK.shuffle = TRUE;
|
|
|
|
}
|
2007-01-10 14:50:09 -08:00
|
|
|
debug( LOG_SOUND, " shuffle = yes\n" );
|
2007-06-28 10:47:08 -07:00
|
|
|
} else if ( buffer[0] != '\0'
|
|
|
|
&& (filename = strtok(buffer, "\n")) != NULL
|
|
|
|
&& strlen(filename) != 0) {
|
|
|
|
char* filepath;
|
|
|
|
|
|
|
|
if (path_to_music == NULL) {
|
2006-09-17 13:39:25 -07:00
|
|
|
filepath = (char*)malloc(strlen(filename)+1);
|
2007-06-28 10:47:08 -07:00
|
|
|
sprintf(filepath, "%s", filename);
|
|
|
|
} else {
|
2006-09-17 13:39:25 -07:00
|
|
|
filepath = (char*)malloc( strlen(filename)
|
2007-06-28 10:47:08 -07:00
|
|
|
+ strlen(path_to_music)+2);
|
|
|
|
sprintf(filepath, "%s/%s", path_to_music, filename);
|
|
|
|
}
|
2007-01-10 14:50:09 -08:00
|
|
|
debug( LOG_SOUND, " adding song %s\n", filepath );
|
2007-06-28 10:47:08 -07:00
|
|
|
|
|
|
|
if (CURRENT_TRACK.nb_songs == CURRENT_TRACK.list_size) {
|
|
|
|
CURRENT_TRACK.list_size <<= 1;
|
2006-09-17 13:39:25 -07:00
|
|
|
CURRENT_TRACK.songs = (char**)realloc(CURRENT_TRACK.songs,
|
2007-06-28 10:47:08 -07:00
|
|
|
CURRENT_TRACK.list_size*sizeof(char*));
|
|
|
|
}
|
|
|
|
|
|
|
|
CURRENT_TRACK.songs[CURRENT_TRACK.nb_songs++] = filepath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free(path_to_music);
|
|
|
|
|
2006-06-16 12:10:23 -07:00
|
|
|
PHYSFS_close(f);
|
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-09-13 14:14:47 -07:00
|
|
|
static void PlayList_Shuffle(void) {
|
2007-06-28 10:47:08 -07:00
|
|
|
if (CURRENT_TRACK.shuffle) {
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = CURRENT_TRACK.nb_songs-1; i > 0; --i) {
|
|
|
|
unsigned int j = rand() % (i + 1);
|
|
|
|
char* swap = CURRENT_TRACK.songs[j];
|
2006-02-18 10:54:37 -08:00
|
|
|
|
2007-06-28 10:47:08 -07:00
|
|
|
CURRENT_TRACK.songs[j] = CURRENT_TRACK.songs[i];
|
|
|
|
CURRENT_TRACK.songs[i] = swap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PlayList_SetTrack(unsigned int t) {
|
2006-11-26 02:54:22 -08:00
|
|
|
if (t < NB_TRACKS) {
|
2007-06-28 10:47:08 -07:00
|
|
|
current_track = t;
|
|
|
|
} else {
|
|
|
|
current_track = 0;
|
|
|
|
}
|
|
|
|
PlayList_Shuffle();
|
|
|
|
current_song = 0;
|
|
|
|
}
|
|
|
|
|
2006-09-13 14:11:11 -07:00
|
|
|
char* PlayList_CurrentSong(void) {
|
2007-06-28 10:47:08 -07:00
|
|
|
if (current_song >= CURRENT_TRACK.nb_songs) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return CURRENT_TRACK.songs[current_song];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-13 14:11:11 -07:00
|
|
|
char* PlayList_NextSong(void) {
|
2007-06-28 10:47:08 -07:00
|
|
|
if (++current_song >= CURRENT_TRACK.nb_songs) {
|
|
|
|
PlayList_Shuffle();
|
|
|
|
current_song = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CURRENT_TRACK.nb_songs == 0) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
return CURRENT_TRACK.songs[current_song];
|
|
|
|
}
|
|
|
|
}
|