File: music_manager.cpp

package info (click to toggle)
epiphany 0.7.0+0-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 7,532 kB
  • sloc: cpp: 6,404; ansic: 141; makefile: 78; sh: 7
file content (132 lines) | stat: -rw-r--r-- 2,949 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/***************************************************************************
                          music_manager.cpp  -  description
                             -------------------
    begin                : Jun 13 2007
    copyright            : (C) 2007 by Giuseppe D'Aqui'
    email                : kumber@tiscalinet.it
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License, Version 2,      *
 *   as published by the Free Software Foundation.                         *
 *                                                                         *
 ***************************************************************************/

#include "dephine.h"
#include <SDL/SDL_mixer.h>
#include "music_manager.h"
#include "resource_factory.h"

void Music_Manager::init()
{
	//FIXME: we should auto-resize the vector
	m_musics.resize(25);
	
	music_enabled = true;
	
	load_musics();
	
	this->set_volume(Epiconfig::instance()->get_volume_music());
}


void Music_Manager::load_musics()
{

	Resource_Factory* res_factory=Resource_Factory::instance();

	std::string res_path(res_factory->get_resource_path());

	res_path+= "/sfx/";

	//FIXME should check the existence of music files
	m_musics[MUS_MENU]=Mix_LoadMUS((res_path+"epiphany_menu.ogg").c_str());

	m_musics[MUS_CREDITS]=Mix_LoadMUS((res_path+"epiphany_credits.ogg").c_str());

	m_musics[MUS_GAME]=Mix_LoadMUS((res_path+"epiphany_game.ogg").c_str());

	m_musics[MUS_READY]=Mix_LoadMUS((res_path+"epiphany_ready.ogg").c_str());

}

void Music_Manager::deinit()
{
	Mix_FreeMusic(m_musics[MUS_MENU]);
	Mix_FreeMusic(m_musics[MUS_CREDITS]);
	Mix_FreeMusic(m_musics[MUS_GAME]);
	Mix_FreeMusic(m_musics[MUS_READY]);
	Epiconfig::instance()->set_volume_music(this->get_volume());
}

Mix_Music* Music_Manager::get_music(Music_Type music)
{
	//FIXME: it should assert the existence of sample
	return m_musics[music];
	
}




void Music_Manager::play(Music_Type type)
{
	if(music_enabled == false)
		return;
		
	Mix_Music* music = get_music(type);
	
	if(Mix_PlayMusic(music, -1)==-1) {
    	DEBWARN("Mix_PlayMusic: "<< Mix_GetError()<<"\n");
	}
}


Uint32 Music_Manager::get_max_volume()
{
	return MIX_MAX_VOLUME/16;
}




void Music_Manager::set_volume(Uint32 value)
{
	Mix_VolumeMusic(value*16);
}




Uint32 Music_Manager::get_volume()
{
	return Mix_VolumeMusic(-1)/16;
}


void Music_Manager::disable_music()
{
	music_enabled=false;
}

void Music_Manager::enable_music()
{
	music_enabled = true;
}



Music_Manager* Music_Manager::_instance = 0;

Music_Manager* Music_Manager::instance()
{
	if(_instance==0)
	{
		_instance=new Music_Manager();
		_instance->init();
	}
	return _instance;
}