UI: fix for back buffer option in scene buttons, by Thomas Dinges.
[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 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.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  *
24  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
25  * All rights reserved.
26  *
27  * The Original Code is: all of this file.
28  *
29  * Contributor(s): none yet.
30  *
31  * ***** END GPL LICENSE BLOCK *****
32  */
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 #include "SND_DeviceManager.h"
39 #include "SND_DependKludge.h"
40 #include "SND_DummyDevice.h"
41 #ifdef USE_FMOD
42 #include "SND_FmodDevice.h"
43 #endif
44 #ifdef USE_OPENAL
45 #include "SND_OpenALDevice.h"
46 #endif
47
48 SND_IAudioDevice* SND_DeviceManager::m_instance = NULL;
49 int SND_DeviceManager::m_subscriptions = 0;
50
51 #ifdef USE_OPENAL
52 int SND_DeviceManager::m_device_type = snd_e_openaldevice;
53 #else
54 #       ifdef USE_FMOD
55 int SND_DeviceManager::m_device_type = snd_e_fmoddevice;
56 #       else
57 int SND_DeviceManager::m_device_type = snd_e_dummydevice;
58 #       endif
59 #endif
60
61 void SND_DeviceManager::Subscribe()
62 {
63         ++m_subscriptions;
64 }
65
66
67
68 void SND_DeviceManager::Unsubscribe()
69 {
70         --m_subscriptions;
71
72         // only release memory if there is a m_instance but no subscriptions left
73         if (m_subscriptions == 0 && m_instance)
74         {
75                 delete m_instance;
76                 m_instance = NULL;
77         }
78         
79         if (m_subscriptions < 0)
80                 m_subscriptions = 0;
81 }
82
83
84
85 SND_IAudioDevice* SND_DeviceManager::Instance()
86 {
87         // only give away an instance if there are subscriptions
88         if (m_subscriptions)
89         {
90                 // if there's no instance yet, set and create a new one
91                 if (m_instance == NULL)
92                 {
93                         SetDeviceType(m_device_type);
94                 }
95
96                 return m_instance;
97         }
98         else
99         {
100                 return NULL;
101         }
102 }
103                 
104
105
106 void SND_DeviceManager::SetDeviceType(int device_type)
107 {
108         // if we want to change devicetype, first delete the old one
109         if (m_instance)
110         {
111                 delete m_instance;
112                 m_instance = NULL;
113         }
114         
115         // let's create the chosen device
116         switch (device_type)
117         {
118 #ifdef USE_FMOD
119         case snd_e_fmoddevice:
120                 {
121                         m_instance = new SND_FmodDevice();
122                         m_device_type = device_type;
123                         break;
124                 }
125 #endif
126 #ifdef USE_OPENAL
127         case snd_e_openaldevice:
128                 {
129                         m_instance = new SND_OpenALDevice();
130                         m_device_type = device_type;
131                         break;
132                 }
133 #endif
134         default:
135                 {
136                         m_instance = new SND_DummyDevice();
137                         m_device_type = device_type;
138                         break;
139                 }
140         }
141 }