00001 /* 00002 * The Mana World 00003 * Copyright (C) 2004 The Mana World Development Team 00004 * 00005 * This file is part of The Mana World. 00006 * 00007 * This program is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * any later version. 00011 * 00012 * This program is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 */ 00021 00022 #include "resources/music.h" 00023 00024 #include "log.h" 00025 00026 Music::Music(Mix_Chunk *music): 00027 mChunk(music), 00028 mChannel(-1) 00029 { 00030 } 00031 00032 Music::~Music() 00033 { 00034 //Mix_FreeMusic(music); 00035 Mix_FreeChunk(mChunk); 00036 } 00037 00038 Resource *Music::load(void *buffer, unsigned bufferSize) 00039 { 00040 // Load the raw file data from the buffer in an RWops structure 00041 SDL_RWops *rw = SDL_RWFromMem(buffer, bufferSize); 00042 00043 // Use Mix_LoadMUS to load the raw music data 00044 //Mix_Music* music = Mix_LoadMUS_RW(rw); Need to be implemeted 00045 Mix_Chunk *tmpMusic = Mix_LoadWAV_RW(rw, 1); 00046 00047 if (tmpMusic) 00048 { 00049 return new Music(tmpMusic); 00050 } 00051 else 00052 { 00053 logger->log("Error, failed to load music: %s", Mix_GetError()); 00054 return NULL; 00055 } 00056 } 00057 00058 bool Music::play(int loops) 00059 { 00060 /* 00061 * Warning: loops should be always set to -1 (infinite) with current 00062 * implementation to avoid halting the playback of other samples 00063 */ 00064 00065 /*if (Mix_PlayMusic(music, loops)) 00066 return true;*/ 00067 Mix_VolumeChunk(mChunk, 120); 00068 mChannel = Mix_PlayChannel(-1, mChunk, loops); 00069 00070 return mChannel != -1; 00071 } 00072 00073 void Music::stop() 00074 { 00075 /* 00076 * Warning: very dungerous trick, it could try to stop channels occupied 00077 * by samples rather than the current music file 00078 */ 00079 00080 //Mix_HaltMusic(); 00081 00082 if (mChannel != -1) 00083 { 00084 Mix_HaltChannel(mChannel); 00085 } 00086 }