Let the game engine manage it's own sound scene. This is to fix bug 1415 (Patch...
[blender.git] / intern / SoundSystem / intern / SND_DeviceManager.cpp
1 /*
2  * SND_DeviceManager.h
3  *
4  * singleton for creating, switching and deleting audiodevices
5  *
6  * $Id$
7  *
8  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version. The Blender
14  * Foundation also sells licenses for use in proprietary software under
15  * the Blender License.  See http://www.blender.org/BL/ for information
16  * about this.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
26  *
27  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
28  * All rights reserved.
29  *
30  * The Original Code is: all of this file.
31  *
32  * Contributor(s): none yet.
33  *
34  * ***** END GPL/BL DUAL LICENSE BLOCK *****
35  */
36
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 #endif
40
41 #include "SND_DeviceManager.h"
42 #include "SND_DependKludge.h"
43 #include "SND_DummyDevice.h"
44 #ifdef USE_FMOD
45 #include "SND_FmodDevice.h"
46 #endif
47 #ifdef USE_OPENAL
48 #include "SND_OpenALDevice.h"
49 #endif
50
51 SND_IAudioDevice* SND_DeviceManager::m_instance = NULL;
52 int SND_DeviceManager::m_subscriptions = 0;
53
54 #ifdef USE_OPENAL
55 int SND_DeviceManager::m_device_type = snd_e_openaldevice;
56 #else
57 #       ifdef USE_FMOD
58 int SND_DeviceManager::m_device_type = snd_e_fmoddevice;
59 #       else
60 int SND_DeviceManager::m_device_type = snd_e_dummydevice;
61 #       endif
62 #endif
63
64 void SND_DeviceManager::Subscribe()
65 {
66         ++m_subscriptions;
67 }
68
69
70
71 void SND_DeviceManager::Unsubscribe()
72 {
73         --m_subscriptions;
74
75         // only release memory if there is a m_instance but no subscriptions left
76         if (m_subscriptions == 0 && m_instance)
77         {
78                 delete m_instance;
79                 m_instance = NULL;
80         }
81         
82         if (m_subscriptions < 0)
83                 m_subscriptions = 0;
84 }
85
86
87
88 SND_IAudioDevice* SND_DeviceManager::Instance()
89 {
90         // only give away an instance if there are subscriptions
91         if (m_subscriptions)
92         {
93                 // if there's no instance yet, set and create a new one
94                 if (m_instance == NULL)
95                 {
96                         SetDeviceType(m_device_type);
97                 }
98
99                 return m_instance;
100         }
101         else
102         {
103                 return NULL;
104         }
105 }
106                 
107
108
109 void SND_DeviceManager::SetDeviceType(int device_type)
110 {
111         // if we want to change devicetype, first delete the old one
112         if (m_instance)
113         {
114                 delete m_instance;
115                 m_instance = NULL;
116         }
117         
118         // let's create the chosen device
119         switch (device_type)
120         {
121 #ifdef USE_FMOD
122         case snd_e_fmoddevice:
123                 {
124                         m_instance = new SND_FmodDevice();
125                         m_device_type = device_type;
126                         break;
127                 }
128 #endif
129 #ifdef USE_OPENAL
130         case snd_e_openaldevice:
131                 {
132                         m_instance = new SND_OpenALDevice();
133                         m_device_type = device_type;
134                         break;
135                 }
136 #endif
137         default:
138                 {
139                         m_instance = new SND_DummyDevice();
140                         m_device_type = device_type;
141                         break;
142                 }
143         }
144 }