Page MenuHomePhabricator (Chris)

No OneTemporary

Authored By
Unknown
Size
14 KB
Referenced Files
None
Subscribers
None
diff --git a/data/music/mams-menu_initial.ogg b/data/music/mams-menu_initial.ogg
new file mode 100644
index 0000000..1c49074
Binary files /dev/null and b/data/music/mams-menu_initial.ogg differ
diff --git a/data/music/mams-menu_loop.ogg b/data/music/mams-menu_loop.ogg
new file mode 100644
index 0000000..65333bb
Binary files /dev/null and b/data/music/mams-menu_loop.ogg differ
diff --git a/data/music/menu.music b/data/music/menu.music
new file mode 100644
index 0000000..7c6d4ed
--- /dev/null
+++ b/data/music/menu.music
@@ -0,0 +1,15 @@
+# Main theme - by Juho-Petteri Yliuntinen
+# CC-BY-SA 3.0 Unported: http://creativecommons.org/licenses/by-sa/3.0/
+
+music(menu){
+ file=mams-menu_initial.ogg
+ trackname="Menu theme"
+ author=Juho-Petteri Yliuntinen
+ license="CC-BY-SA 3.0 Unported"
+ start=0
+ volume=128
+ loop=1
+ loopfile=mams-menu_loop.ogg
+ loopstart=0
+ loopend=-1
+}
\ No newline at end of file
diff --git a/src/MusicManager.cpp b/src/MusicManager.cpp
index ec4958f..7de1192 100644
--- a/src/MusicManager.cpp
+++ b/src/MusicManager.cpp
@@ -1,324 +1,336 @@
/****************************************************************************
** Copyright (C) 2011 Luka Horvat <redreaper132 at gmail.com>
** Copyright (C) 2011 Edward Lii <edward_iii at myway.com>
** Copyright (C) 2011 O. Bahri Gordebak <gordebak at gmail.com>
**
**
** This file may be used under the terms of the GNU General Public
** License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#include "MusicManager.h"
#include "TreeStorageNode.h"
#include "POASerializer.h"
#include "FileManager.h"
#include "Functions.h"
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
MusicManager::MusicManager(){
Mix_HookMusicFinished(musicStoppedHook);
Mix_VolumeMusic(MIX_MAX_VOLUME);
enabled=false;
currentList="default";
lastTime=0;
playing=NULL;
}
MusicManager::~MusicManager(){
//We call destroy().
destroy();
}
void MusicManager::destroy(){
//Loop through the imageCollection and free them.
std::map<std::string,Music*>::iterator i;
for(i=musicCollection.begin();i!=musicCollection.end();i++){
if(i->second!=NULL){
Mix_FreeMusic(i->second->music);
}
}
playing=NULL;
//And clear the collection and the lists.
musicCollection.clear();
musicLists.clear();
}
void MusicManager::setEnabled(bool enable){
//Set the new status.
if(enabled!=enable)
enabled=enable;
else
return;
if(enable){
//It got turned on, so start the menu music.
playMusic("menu",false);
}else{
//Stop the current music.
Mix_HaltMusic();
}
}
string MusicManager::loadMusic(const std::string &file){
//Open the .music file.
ifstream musicFile;
musicFile.open(file.c_str());
string returnString="";
//Check if the file exists.
if(musicFile){
//Now parse the file.
TreeStorageNode obj;
{
POASerializer objSerializer;
if(!objSerializer.readNode(musicFile,&obj,true)){
cerr<<"ERROR: Invalid file format of music description file."<<endl;
}
}
//Loop through the entries.
for(unsigned int i=0;i<obj.subNodes.size();i++){
TreeStorageNode* obj1=obj.subNodes[i];
if(obj1==NULL)
continue;
if(obj1->value.size()>=1 && obj1->name=="music"){
//Make sure that this music isn't already loaded.
map<string,Music*>::iterator it=musicCollection.find(obj1->value[0]);
if(it==musicCollection.end()){
//We've found an entry for a music file.
Music* music=new Music;
//Load some data.
for(map<string,vector<string> >::iterator i=obj1->attributes.begin();i!=obj1->attributes.end();i++){
if(i->first=="file"){
//Load the music file.
music->music=Mix_LoadMUS((getDataPath()+"music/"+i->second[0]).c_str());
}
+ if(i->first=="loopfile"){
+ //Load the loop file.
+ music->loop=Mix_LoadMUS((getDataPath()+"music/"+i->second[0]).c_str());
+ }
if(i->first=="trackname"){
music->trackName=i->second[0];
}
if(i->first=="author"){
music->author=i->second[0];
}
+ if(i->first=="license"){
+ music->license=i->second[0];
+ }
if(i->first=="start"){
music->start=(atoi(i->second[0].c_str()));
}
if(i->first=="volume"){
music->volume=(atoi(i->second[0].c_str()));
}
if(i->first=="loopstart"){
music->loopStart=(atoi(i->second[0].c_str()));
}
if(i->first=="loopend"){
music->loopEnd=(atoi(i->second[0].c_str()));
}
}
//Set the default value for lastTime.
music->lastTime=-1;
music->name=obj1->value[0];
//Now add it to the collection.
musicCollection[obj1->value[0]]=music;
}
//Add the name of the music to the return string even if it's already loaded.
//This is to allow music to be in multiple music lists.
if(!returnString.empty())
returnString+=',';
returnString+=obj1->value[0];
}
}
}
//Return the return string.
return returnString;
}
bool MusicManager::loadMusicList(const std::string &file){
//Open the .list file.
ifstream musicFile;
musicFile.open(file.c_str());
//Check if the file exists.
if(musicFile){
//Now parse the file.
TreeStorageNode obj;
{
POASerializer objSerializer;
if(!objSerializer.readNode(musicFile,&obj,true)){
cerr<<"ERROR: Invalid file format of music list file."<<endl;
return false;
}
}
//Get the name of the list.
std::string name;
{
map<string,vector<string> >::iterator it=obj.attributes.find("name");
if(it!=obj.attributes.end()){
name=obj.attributes["name"][0];
}else{
cerr<<"ERROR: No name for music list "<<file<<endl;
return false;
}
}
//Check if the list isn't already loaded.
std::map<std::string,std::vector<std::string> >::iterator it=musicLists.find(name);
if(it!=musicLists.end())
return true;
//Loop through the entries.
for(unsigned int i=0;i<obj.subNodes.size();i++){
TreeStorageNode* obj1=obj.subNodes[i];
if(obj1==NULL)
continue;
if(obj1->value.size()>=1 && obj1->name=="musicfile"){
//Load the music file.
string result=loadMusic(getDataPath()+"music/"+obj1->value[0]);
if(!result.empty()){
if(result.find(',')!=string::npos){
size_t pos=result.find(',');
while(pos!=string::npos){
musicLists[name].push_back(result.substr(pos,result.find(',',pos+1)));
}
}else{
musicLists[name].push_back(result);
}
}
}
}
}else{
cerr<<"ERROR: Unable to open music list file "<<file<<endl;
return false;
}
//Nothing went wrong so return true.
return true;
}
void MusicManager::playMusic(const std::string &name,bool fade){
//Make sure music is enabled.
if(!enabled)
return;
//Check if the music is in the collection.
Music* music=musicCollection[name];
if(music==NULL){
cerr<<"ERROR: Unable to play music "<<name<<endl;
return;
}
//Now check if we should fade the previous one out.
if(fade){
- Mix_FadeOutMusic(375);
+ Mix_FadeOutMusic(375);
//Set the next music.
nextMusic=name;
}else{
- if(music->loopStart<=0){
+ if(music->loopStart<=0 && music->loop==NULL){
Mix_FadeInMusicPos(music->music,-1,0,music->start);
}else{
Mix_FadeInMusicPos(music->music,0,0,music->start);
}
Mix_VolumeMusic(music->volume);
//Set the playing pointer.
playing=music;
}
}
void MusicManager::pickMusic(){
//Make sure the currentList exists.
vector<std::string> list=musicLists[currentList];
if(currentList.empty()){
cerr<<"ERROR: Unkown music list "<<currentList<<endl;
return;
}
//Shuffle the list.
random_shuffle(list.begin(),list.end());
//Now loop through the music and search the oldest.
Music* oldest=NULL;
for(unsigned int i=0;i<list.size();i++){
//Check if oldest is set.
if(oldest==NULL){
//It isn't so pick the first music.
oldest=musicCollection[list[i]];
continue;
}
//Check if this song is null.
if(musicCollection[list[i]]==NULL)
continue;
//Check if this music is never played.
if(musicCollection[list[i]]->lastTime==-1){
oldest=musicCollection[list[i]];
//And break out.
break;
}
//Check if this music is older.
if(musicCollection[list[i]]->lastTime<oldest->lastTime){
oldest=musicCollection[list[i]];
}
}
//Check if oldest ins't null.
if(oldest!=NULL){
playMusic(oldest->name);
//Set the lastTime and increase it.
oldest->lastTime=lastTime;
lastTime++;
}
}
void MusicManager::musicStopped(){
//Check if there's a music to play.
if(!nextMusic.empty()){
//Check if the music is in the collection.
Music* music=musicCollection[nextMusic];
if(music==NULL){
cerr<<"ERROR: Unable to play music "<<nextMusic<<endl;
return;
}
if(music->loopStart<=0){
Mix_FadeInMusicPos(music->music,-1,375,music->start);
}else{
Mix_FadeInMusicPos(music->music,0,375,music->start);
}
Mix_VolumeMusic(music->volume);
//Set playing.
playing=music;
//Now reset nextMusic.
nextMusic.clear();
}else{
- //This is for looping the end of music.
- Mix_FadeInMusicPos(playing->music,0,0,playing->loopStart);
+ //Check what kind of loop.
+ if(playing->loop!=NULL){
+ Mix_FadeInMusicPos(playing->loop,-1,0,playing->loopStart);
+ }else{
+ //This is for looping the end of music.
+ Mix_FadeInMusicPos(playing->music,0,0,playing->loopStart);
+ }
}
}
void MusicManager::setMusicList(const string &list){
//Check if the list exists.
}
diff --git a/src/MusicManager.h b/src/MusicManager.h
index babd308..eee723d 100644
--- a/src/MusicManager.h
+++ b/src/MusicManager.h
@@ -1,126 +1,130 @@
/****************************************************************************
** Copyright (C) 2011 Luka Horvat <redreaper132 at gmail.com>
** Copyright (C) 2011 Edward Lii <edward_iii at myway.com>
** Copyright (C) 2011 O. Bahri Gordebak <gordebak at gmail.com>
**
**
** This file may be used under the terms of the GNU General Public
** License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/
#ifndef MUSICMANAGER_H
#define MUSICMANAGER_H
#include <SDL/SDL.h>
#include <SDL/SDL_mixer.h>
#include <string>
#include <map>
#include <vector>
//Class for loading and playing the music.
class MusicManager{
private:
//This structure is used to hold music information.
struct Music{
//Pointer to the actual music.
Mix_Music* music;
+ //Pointer to the loop music, if any.
+ Mix_Music* loop;
//String containing the name of the music.
//This is the same name as in the musicCollection.
- std::string name;
+ std::string name;
//String containing the track name of the music.
//This is the name given by the author.
std::string trackName;
//String containing the name of the author.
std::string author;
+ //String containing the license the music is released under.
+ std::string license;
//Integer containing the time where to start playing.
int start;
//The volume to play the music with.
//Scale 0-128.
int volume;
//Integer containing the loopstart.
int loopStart;
//Integer containing the loopend.
//NOTE: loopend doesn't work and is thus ignored.
int loopEnd;
//Integer used to keep track when which music was played.
int lastTime;
};
public:
//Constructor.
MusicManager();
//Destructor.
~MusicManager();
//Destroys the music.
void destroy();
//Method that will either disable or enable music.
//enable: Boolean if the musicManager should be enabled or not.
void setEnabled(bool enable=true);
//This method will load one music file and add it to the collection.
//file: The filename of the music file.
//Returns: String containing the loaded music comma sperated, it's empty if it fails.
std::string loadMusic(const std::string &file);
//This method will load from a music list.
//file: The filename of the music list file.
//Returns: True if no error occurs.
bool loadMusicList(const std::string &file);
//This method will start playing a music file.
//name: The name of the song.
//fade: Boolean if it should fade the current one out or not.
void playMusic(const std::string &name,bool fade=true);
//This method will pick music from the current music list.
void pickMusic();
//Method that will be called when a music stopped.
void musicStopped();
//Set the music list.
//list: The name of the list.
void setMusicList(const std::string &list);
private:
//Boolean if the MusicManager is enabled or not.
//The default value is false meaning that the MusicManager has to be enabled before the music starts.
bool enabled;
//Integer that is used to keep track of the last played song.
int lastTime;
//Pointer to the music struct that is currently playing.
Music* playing;
//String containing the name of the music to play when the previous one stopped.
//This means that it will be checked in the musicStopped method.
std::string nextMusic;
//String containing the name of the current music list.
std::string currentList;
//Map containing the music.
//The key is the name of the music and the value is a pointer to the Mix_Music.
std::map<std::string,Music*> musicCollection;
//Map containing the music lists.
//The key is the name of the list and the value is an array of music names.
std::map<std::string,std::vector<std::string> > musicLists;
};
#endif

File Metadata

Mime Type
text/x-diff
Expires
Sat, Jun 20, 7:32 PM (1 w, 1 d ago)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
72955
Default Alt Text
(14 KB)

Event Timeline