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