1 /* $Id$ */ |
|
2 |
|
3 /********************************************************************* |
|
4 * OpenTTD: An Open Source Transport Tycoon Deluxe clone * |
|
5 * Copyright (c) 2002-2004 OpenTTD Developers. All Rights Reserved. * |
|
6 * * |
|
7 * Web site: http://openttd.sourceforge.net/ * |
|
8 *********************************************************************/ |
|
9 |
|
10 /* |
|
11 * This program is free software; you can redistribute it and/or modify |
|
12 * it under the terms of the GNU General Public License as published by |
|
13 * the Free Software Foundation; either version 2 of the License, or |
|
14 * (at your option) any later version. |
|
15 * |
|
16 * This program is distributed in the hope that it will be useful, |
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
19 * GNU General Public License for more details. |
|
20 * |
|
21 * You should have received a copy of the GNU General Public License |
|
22 * along with this program; if not, write to the Free Software |
|
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
|
24 */ |
|
25 |
|
26 /* DirectMusic driver for Win32 */ |
|
27 /* Based on dxmci from TTDPatch */ |
|
28 |
|
29 #include "../stdafx.h" |
|
30 |
|
31 #ifdef WIN32_ENABLE_DIRECTMUSIC_SUPPORT |
|
32 |
|
33 // for gcc, the GUIDs are available in a library instead |
|
34 #ifndef __GNUC__ |
|
35 #define INITGUID |
|
36 #endif |
|
37 |
|
38 #ifdef __cplusplus |
|
39 extern "C" { |
|
40 #endif |
|
41 |
|
42 #include "../openttd.h" |
|
43 #include "../debug.h" |
|
44 |
|
45 #ifdef __cplusplus |
|
46 } |
|
47 #endif |
|
48 |
|
49 #include <windows.h> |
|
50 #include <stdio.h> |
|
51 |
|
52 #include <dmksctrl.h> |
|
53 #include <dmusici.h> |
|
54 #include <dmusicc.h> |
|
55 #include <dmusicf.h> |
|
56 |
|
57 #define MSGBOX(output) DEBUG(misc, 0) ("DirectMusic driver: %s\n", output); //MessageBox(NULL, output, "dxmci",MB_OK); |
|
58 |
|
59 static void MultiToWide(WCHAR* to, const char* from) |
|
60 { |
|
61 MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, from, -1, to, _MAX_PATH); |
|
62 } |
|
63 |
|
64 // the performance object controls manipulation of the segments |
|
65 static IDirectMusicPerformance *performance = NULL; |
|
66 |
|
67 // the segment object is where the MIDI data is stored for playback |
|
68 static IDirectMusicSegment *segment = NULL; |
|
69 |
|
70 // the loader bject can load many types of DMusic related files |
|
71 static IDirectMusicLoader *loader = NULL; |
|
72 |
|
73 // whether we've initialized COM or not (when deciding whether to shut down) |
|
74 static int COMInitialized = 0; |
|
75 |
|
76 |
|
77 extern "C" bool LoadLibraryList(void **proc, const char *dll); |
|
78 |
|
79 // Use lazy linking |
|
80 struct ProcPtrs { |
|
81 unsigned long (WINAPI *CoCreateInstance)(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID * ppv); |
|
82 HRESULT (WINAPI *CoInitialize)( LPVOID pvReserved ); |
|
83 void (WINAPI *CoUninitialize)( ); |
|
84 }; |
|
85 |
|
86 #define M(x) x "\0" |
|
87 static const char ole_files[] = |
|
88 M("ole32.dll") |
|
89 M("CoCreateInstance") |
|
90 M("CoInitialize") |
|
91 M("CoUninitialize") |
|
92 M("") |
|
93 ; |
|
94 #undef M |
|
95 |
|
96 |
|
97 static ProcPtrs _proc; |
|
98 |
|
99 static bool LoadOleDLL(void) |
|
100 { |
|
101 if (_proc.CoCreateInstance != NULL) |
|
102 return true; |
|
103 if (!LoadLibraryList((void**)&_proc, ole_files)) |
|
104 return false; |
|
105 return true; |
|
106 } |
|
107 |
|
108 |
|
109 #ifdef __cplusplus |
|
110 extern "C" { |
|
111 #endif |
|
112 |
|
113 // Initialize COM and DirectMusic |
|
114 bool InitDirectMusic(void) |
|
115 { |
|
116 if (NULL != performance) |
|
117 return true; |
|
118 |
|
119 // Initialize COM |
|
120 if (!COMInitialized) { |
|
121 if (!LoadOleDLL()) { |
|
122 MSGBOX("ole32.dll load failed"); |
|
123 return false; |
|
124 } |
|
125 |
|
126 _proc.CoInitialize(NULL); |
|
127 COMInitialized = 1; |
|
128 } |
|
129 |
|
130 // Create the performance object via CoCreateInstance |
|
131 if (FAILED(_proc.CoCreateInstance( |
|
132 CLSID_DirectMusicPerformance, |
|
133 NULL, |
|
134 CLSCTX_INPROC, |
|
135 IID_IDirectMusicPerformance, |
|
136 (LPVOID*)&performance |
|
137 ))) { |
|
138 MSGBOX("Failed to create the performance object"); |
|
139 return false; |
|
140 } |
|
141 |
|
142 // Initialize it |
|
143 if (FAILED(performance->Init(NULL, NULL, NULL))) { |
|
144 MSGBOX("Failed to initialize performance object"); |
|
145 return false; |
|
146 } |
|
147 |
|
148 // Choose default Windows synth |
|
149 if (FAILED(performance->AddPort(NULL))) { |
|
150 MSGBOX("AddPort failed"); |
|
151 return false; |
|
152 } |
|
153 |
|
154 // now we'll create the loader object. This will be used to load the |
|
155 // midi file for our demo. Again, we need to use CoCreateInstance |
|
156 // and pass the appropriate ID parameters |
|
157 if (FAILED(_proc.CoCreateInstance( |
|
158 CLSID_DirectMusicLoader, |
|
159 NULL, |
|
160 CLSCTX_INPROC, |
|
161 IID_IDirectMusicLoader, |
|
162 (LPVOID*)&loader |
|
163 ))) { |
|
164 MSGBOX("Failed to create loader object"); |
|
165 return false; |
|
166 } |
|
167 |
|
168 // that's it for initialization. If we made it this far we |
|
169 // were successful. |
|
170 return true; |
|
171 } |
|
172 |
|
173 // Releases memory used by all of the initialized |
|
174 // DirectMusic objects in the program |
|
175 void ReleaseSegment(void) |
|
176 { |
|
177 if (NULL != segment) { |
|
178 segment->Release(); |
|
179 segment = NULL; |
|
180 } |
|
181 } |
|
182 |
|
183 void ShutdownDirectMusic(void) |
|
184 { |
|
185 // release everything but the segment, which the performance |
|
186 // will release automatically (and it'll crash if it's been |
|
187 // released already) |
|
188 |
|
189 if (NULL != loader) { |
|
190 loader->Release(); |
|
191 loader = NULL; |
|
192 } |
|
193 |
|
194 if (NULL != performance) { |
|
195 performance->CloseDown(); |
|
196 performance->Release(); |
|
197 performance = NULL; |
|
198 } |
|
199 |
|
200 if (COMInitialized) { |
|
201 _proc.CoUninitialize(); |
|
202 COMInitialized = 0; |
|
203 } |
|
204 } |
|
205 |
|
206 // Load MIDI file for playing |
|
207 bool LoadMIDI(const char *directory, const char *filename) |
|
208 { |
|
209 DMUS_OBJECTDESC obj_desc; |
|
210 WCHAR w_directory[_MAX_PATH]; // utf-16 version of the directory name. |
|
211 WCHAR w_filename[_MAX_PATH]; // utf-16 version of the file name |
|
212 |
|
213 if (NULL == performance) |
|
214 return false; |
|
215 |
|
216 MultiToWide(w_directory, directory); |
|
217 |
|
218 if (FAILED(loader->SetSearchDirectory( |
|
219 GUID_DirectMusicAllTypes, w_directory, FALSE |
|
220 ))) { |
|
221 MSGBOX("LoadMIDI: SetSearchDirectory failed"); |
|
222 return false; |
|
223 } |
|
224 |
|
225 // set up the loader object info |
|
226 ZeroMemory(&obj_desc, sizeof(obj_desc)); |
|
227 obj_desc.dwSize = sizeof(obj_desc); |
|
228 |
|
229 MultiToWide(w_filename, filename); |
|
230 obj_desc.guidClass = CLSID_DirectMusicSegment; |
|
231 |
|
232 wcscpy(obj_desc.wszFileName, w_filename); |
|
233 obj_desc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME; |
|
234 |
|
235 // release the existing segment if we have any |
|
236 if (NULL != segment) |
|
237 ReleaseSegment(); |
|
238 |
|
239 // and make a new segment |
|
240 if (FAILED(loader->GetObject( |
|
241 &obj_desc, IID_IDirectMusicSegment, (LPVOID*)&segment |
|
242 ))) { |
|
243 MSGBOX("LoadMIDI: Get object failed"); |
|
244 return false; |
|
245 } |
|
246 |
|
247 // next we need to tell the segment what kind of data it contains. We do this |
|
248 // with the IDirectMusicSegment::SetParam function. |
|
249 if (FAILED(segment->SetParam( |
|
250 GUID_StandardMIDIFile, 0xFFFFFFFF, 0, 0, performance |
|
251 ))) { |
|
252 MSGBOX("LoadMIDI: SetParam (MIDI file) failed"); |
|
253 return false; |
|
254 } |
|
255 |
|
256 // finally, we need to tell the segment to 'download' the instruments |
|
257 if (FAILED(segment->SetParam(GUID_Download, 0xFFFFFFFF, 0, 0, performance))) { |
|
258 MSGBOX("LoadMIDI: Failed to download instruments"); |
|
259 return false; |
|
260 } |
|
261 |
|
262 // at this point, the MIDI file is loaded and ready to play! |
|
263 return true; |
|
264 } |
|
265 |
|
266 // Start playing the MIDI file |
|
267 void PlaySegment(void) |
|
268 { |
|
269 if (NULL == performance) |
|
270 return; |
|
271 |
|
272 if (FAILED(performance->PlaySegment(segment, 0, 0, NULL))) { |
|
273 MSGBOX("PlaySegment failed"); |
|
274 } |
|
275 } |
|
276 |
|
277 // Stop playing |
|
278 void StopSegment(void) |
|
279 { |
|
280 if (NULL == performance || NULL == segment) |
|
281 return; |
|
282 |
|
283 if (FAILED(performance->Stop(segment, NULL, 0, 0))) { |
|
284 MSGBOX("StopSegment failed"); |
|
285 } |
|
286 } |
|
287 |
|
288 // Find out whether playing has started or stopped |
|
289 bool IsSegmentPlaying(void) |
|
290 { |
|
291 if (NULL == performance || NULL == segment) |
|
292 return false; |
|
293 |
|
294 // IsPlaying return S_OK if the segment is currently playing |
|
295 return performance->IsPlaying(segment, NULL) == S_OK; |
|
296 } |
|
297 |
|
298 void SetVolume(long vol) |
|
299 { |
|
300 long db; |
|
301 |
|
302 if (performance == NULL && !InitDirectMusic()) |
|
303 return; |
|
304 |
|
305 db = ((vol >> 21) & 0x7ff) - 1000; |
|
306 performance->SetGlobalParam(GUID_PerfMasterVolume, &db, sizeof(db)); |
|
307 } |
|
308 |
|
309 #if defined(__cplusplus) |
|
310 } |
|
311 #endif |
|
312 |
|
313 #endif /* WIN32_ENABLE_DIRECTMUSIC_SUPPORT */ |
|