* Don't allocate memory in PlayList_Init() while we don't need it yet
* Make sure to set '''global''' pointers to NULL when free()ing them * Also set the "size" variables belonging to these pointers to zero * Use a swap(a, b) style function for variable swapping (in PlayList_Shuffle()) rather than obfuscating code * Make sure that PlayList_CurrentSong() and PlayList_NextSong() return "const char*" rather than "char*" to make sure no one touches memory he/she shouldn't * Some general changes to conform with coding style (most notably: braces on a line of their own) git-svn-id: svn+ssh://svn.gna.org/svn/warzone/trunk@2712 4a71c877-e1ca-e34f-864e-861f7616d084master
parent
aa4bb6c529
commit
6268b6fff2
|
@ -34,7 +34,7 @@ typedef struct {
|
|||
char** songs;
|
||||
unsigned int nb_songs;
|
||||
unsigned int list_size;
|
||||
BOOL shuffle;
|
||||
bool shuffle;
|
||||
} WZ_TRACK;
|
||||
|
||||
static unsigned int current_track = 0;
|
||||
|
@ -42,25 +42,34 @@ static unsigned int current_song = 0;
|
|||
|
||||
static WZ_TRACK playlist[NB_TRACKS];
|
||||
|
||||
void PlayList_Init(void) {
|
||||
void PlayList_Init()
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < NB_TRACKS; ++i) {
|
||||
playlist[i].songs = (char**)malloc(2*sizeof(char*));
|
||||
playlist[i].list_size = 2;
|
||||
for (i = 0; i < NB_TRACKS; ++i)
|
||||
{
|
||||
playlist[i].songs = NULL;
|
||||
playlist[i].list_size = 0;
|
||||
playlist[i].nb_songs = 0;
|
||||
memset( playlist[i].songs, 0, playlist[i].list_size*sizeof(char*) );
|
||||
}
|
||||
}
|
||||
|
||||
void PlayList_Quit(void) {
|
||||
void PlayList_Quit()
|
||||
{
|
||||
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] );
|
||||
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 );
|
||||
|
||||
free(playlist[i].songs);
|
||||
|
||||
playlist[i].songs = NULL;
|
||||
playlist[i].list_size = 0;
|
||||
playlist[i].nb_songs = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,14 +114,14 @@ bool PlayList_Read(const char* path)
|
|||
current_track = 1;
|
||||
free(path_to_music);
|
||||
path_to_music = NULL;
|
||||
playlist[current_track].shuffle = FALSE;
|
||||
playlist[current_track].shuffle = false;
|
||||
}
|
||||
else if (strncmp(line_buf, "[menu]", 6) == 0)
|
||||
{
|
||||
current_track = 2;
|
||||
free(path_to_music);
|
||||
path_to_music = NULL;
|
||||
playlist[current_track].shuffle = FALSE;
|
||||
playlist[current_track].shuffle = false;
|
||||
}
|
||||
else if (strncmp(line_buf, "path=", 5) == 0)
|
||||
{
|
||||
|
@ -132,7 +141,7 @@ bool PlayList_Read(const char* path)
|
|||
{
|
||||
if (strcmp(strtok(line_buf+8, "\n"), "yes") == 0)
|
||||
{
|
||||
playlist[current_track].shuffle = TRUE;
|
||||
playlist[current_track].shuffle = true;
|
||||
}
|
||||
debug( LOG_SOUND, "playlist: shuffle = yes" );
|
||||
}
|
||||
|
@ -218,47 +227,69 @@ bool PlayList_Read(const char* path)
|
|||
return true;
|
||||
}
|
||||
|
||||
static void PlayList_Shuffle(void) {
|
||||
if (playlist[current_track].shuffle) {
|
||||
static void swap_charp(char** a, char** b)
|
||||
{
|
||||
char* tmp = *a;
|
||||
|
||||
*a = *b;
|
||||
*b = tmp;
|
||||
}
|
||||
|
||||
static void PlayList_Shuffle()
|
||||
{
|
||||
if (playlist[current_track].shuffle)
|
||||
{
|
||||
unsigned int i;
|
||||
|
||||
for (i = playlist[current_track].nb_songs-1; i > 0; --i) {
|
||||
for (i = playlist[current_track].nb_songs-1; i > 0; --i)
|
||||
{
|
||||
unsigned int j = rand() % (i + 1);
|
||||
char* swap = playlist[current_track].songs[j];
|
||||
|
||||
playlist[current_track].songs[j] = playlist[current_track].songs[i];
|
||||
playlist[current_track].songs[i] = swap;
|
||||
swap_charp(&playlist[current_track].songs[i], &playlist[current_track].songs[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PlayList_SetTrack(unsigned int t) {
|
||||
if (t < NB_TRACKS) {
|
||||
void PlayList_SetTrack(unsigned int t)
|
||||
{
|
||||
if (t < NB_TRACKS)
|
||||
{
|
||||
current_track = t;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
current_track = 0;
|
||||
}
|
||||
PlayList_Shuffle();
|
||||
current_song = 0;
|
||||
}
|
||||
|
||||
char* PlayList_CurrentSong(void) {
|
||||
if (current_song >= playlist[current_track].nb_songs) {
|
||||
return NULL;
|
||||
} else {
|
||||
const char* PlayList_CurrentSong()
|
||||
{
|
||||
if (current_song < playlist[current_track].nb_songs)
|
||||
{
|
||||
return playlist[current_track].songs[current_song];
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
char* PlayList_NextSong(void) {
|
||||
if (++current_song >= playlist[current_track].nb_songs) {
|
||||
const char* PlayList_NextSong()
|
||||
{
|
||||
// If we're at the end of the playlist
|
||||
if (++current_song >= playlist[current_track].nb_songs)
|
||||
{
|
||||
// Shuffle the playlist (again)
|
||||
PlayList_Shuffle();
|
||||
|
||||
// Jump to the start of the playlist
|
||||
current_song = 0;
|
||||
}
|
||||
|
||||
if (playlist[current_track].nb_songs == 0) {
|
||||
if (playlist[current_track].nb_songs == 0)
|
||||
{
|
||||
return NULL;
|
||||
} else {
|
||||
return playlist[current_track].songs[current_song];
|
||||
}
|
||||
|
||||
return playlist[current_track].songs[current_song];
|
||||
}
|
||||
|
|
|
@ -27,7 +27,7 @@ void PlayList_Init(void);
|
|||
void PlayList_Quit(void);
|
||||
bool PlayList_Read(const char* path);
|
||||
void PlayList_SetTrack(unsigned int t);
|
||||
char* PlayList_CurrentSong(void);
|
||||
char* PlayList_NextSong(void);
|
||||
const char* PlayList_CurrentSong(void);
|
||||
const char* PlayList_NextSong(void);
|
||||
|
||||
#endif /* _PLAYLIST_H_ */
|
||||
|
|
Loading…
Reference in New Issue