author | Darkvater |
Thu, 02 Jun 2005 19:30:21 +0000 | |
changeset 1891 | 862800791170 |
parent 1302 | 29f313f85ec5 |
child 2166 | fe25867ec83e |
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 |
||
1891
862800791170
(svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents:
1302
diff
changeset
|
40 |
#include "openttd.h" |
1302 | 41 |
#include "debug.h" |
0 | 42 |
#include "sound.h" |
43 |
#include "hal.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 VARIANT int |
|
58 |
||
59 |
#define MSGBOX(output) DEBUG(misc, 0) ("DirectMusic driver: %s\n", output); //MessageBox(NULL, output, "dxmci",MB_OK); |
|
60 |
#define MULTI_TO_WIDE( x,y ) MultiByteToWideChar( CP_ACP,MB_PRECOMPOSED, y,-1,x,_MAX_PATH); |
|
61 |
||
62 |
// the performance object controls manipulation of the segments |
|
63 |
IDirectMusicPerformance *performance = NULL; |
|
64 |
||
65 |
// the segment object is where the MIDI data is stored for playback |
|
66 |
IDirectMusicSegment *segment = NULL; |
|
67 |
||
68 |
// 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
|
69 |
IDirectMusicLoader *loader = NULL; |
0 | 70 |
|
71 |
// whether we've initialized COM or not (when deciding whether to shut down) |
|
72 |
int COMInitialized = 0; |
|
73 |
||
74 |
||
75 |
extern "C" bool LoadLibraryList(void **proc, const char *dll); |
|
76 |
||
77 |
// Use lazy linking |
|
78 |
struct ProcPtrs { |
|
79 |
unsigned long (WINAPI *CoCreateInstance)(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext, REFIID riid, LPVOID * ppv); |
|
80 |
HRESULT (WINAPI *CoInitialize)( LPVOID pvReserved ); |
|
81 |
void (WINAPI *CoUninitialize)( ); |
|
82 |
}; |
|
83 |
||
84 |
#define M(x) x "\0" |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
85 |
static const char ole_files[] = |
0 | 86 |
M("ole32.dll") |
87 |
M("CoCreateInstance") |
|
88 |
M("CoInitialize") |
|
89 |
M("CoUninitialize") |
|
90 |
M("") |
|
91 |
; |
|
92 |
#undef M |
|
93 |
||
94 |
||
95 |
static ProcPtrs _proc; |
|
96 |
||
1117 | 97 |
static bool LoadOleDLL(void) |
0 | 98 |
{ |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
99 |
if (_proc.CoCreateInstance != NULL) |
0 | 100 |
return true; |
101 |
if (!LoadLibraryList((void**)&_proc, ole_files)) |
|
102 |
return false; |
|
103 |
return true; |
|
104 |
} |
|
105 |
||
106 |
||
107 |
#ifdef __cplusplus |
|
108 |
extern "C" { |
|
109 |
#endif |
|
110 |
||
111 |
// Initialize COM and DirectMusic |
|
112 |
bool InitDirectMusic (void) |
|
113 |
{ |
|
114 |
if (NULL != performance) |
|
115 |
return true; |
|
116 |
||
117 |
// Initialize COM |
|
118 |
if (!COMInitialized) { |
|
119 |
if (!LoadOleDLL()) { |
|
120 |
MSGBOX("ole32.dll load failed"); |
|
121 |
return false; |
|
122 |
} |
|
123 |
||
124 |
_proc.CoInitialize(NULL); |
|
125 |
COMInitialized = 1; |
|
126 |
} |
|
127 |
||
128 |
// Create the performance object via CoCreateInstance |
|
129 |
if (FAILED(_proc.CoCreateInstance( |
|
130 |
(REFCLSID)CLSID_DirectMusicPerformance, |
|
131 |
NULL, |
|
132 |
CLSCTX_INPROC, |
|
133 |
(REFIID)IID_IDirectMusicPerformance, |
|
134 |
(LPVOID *)&performance))) { |
|
135 |
MSGBOX ("Failed to create the performance object"); |
|
136 |
return false; |
|
137 |
} |
|
138 |
||
139 |
// Initialize it |
|
140 |
if (FAILED(performance->Init(NULL, NULL, NULL))) { |
|
141 |
MSGBOX("Failed to initialize performance object"); |
|
142 |
return false; |
|
143 |
} |
|
144 |
||
145 |
// Choose default Windows synth |
|
146 |
if (FAILED(performance->AddPort (NULL))) { |
|
147 |
MSGBOX("AddPort failed"); |
|
148 |
return false; |
|
149 |
} |
|
150 |
||
151 |
// now we'll create the loader object. This will be used to load the |
|
152 |
// midi file for our demo. Again, we need to use CoCreateInstance |
|
153 |
// and pass the appropriate ID parameters |
|
154 |
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
|
155 |
NULL, CLSCTX_INPROC, |
0 | 156 |
(REFIID)IID_IDirectMusicLoader, |
157 |
(LPVOID *)&loader))) { |
|
158 |
MSGBOX("Failed to create loader object"); |
|
159 |
return false; |
|
160 |
} |
|
161 |
||
162 |
// that's it for initialization. If we made it this far we |
|
163 |
// were successful. |
|
164 |
return true; |
|
165 |
} |
|
166 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
167 |
// Releases memory used by all of the initialized |
0 | 168 |
// DirectMusic objects in the program |
169 |
void ReleaseSegment (void) |
|
170 |
{ |
|
171 |
if (NULL != segment) { |
|
172 |
segment->Release (); |
|
173 |
segment = NULL; |
|
174 |
} |
|
175 |
} |
|
176 |
void ShutdownDirectMusic (void) |
|
177 |
{ |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
178 |
// 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
|
179 |
// will release automatically (and it'll crash if it's been |
0 | 180 |
// released already) |
181 |
||
182 |
if (NULL != loader) { |
|
183 |
loader->Release (); |
|
184 |
loader = NULL; |
|
185 |
} |
|
186 |
||
187 |
if (NULL != performance) |
|
188 |
{ |
|
189 |
performance->CloseDown (); |
|
190 |
performance->Release (); |
|
191 |
performance = NULL; |
|
192 |
} |
|
193 |
||
194 |
if (COMInitialized) { |
|
195 |
_proc.CoUninitialize(); |
|
196 |
COMInitialized = 0; |
|
197 |
} |
|
198 |
} |
|
199 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
200 |
// Load MIDI file for playing |
0 | 201 |
bool LoadMIDI (char *directory, char *filename) |
202 |
{ |
|
203 |
DMUS_OBJECTDESC obj_desc; |
|
204 |
WCHAR w_directory[_MAX_PATH]; // utf-16 version of the directory name. |
|
205 |
WCHAR w_filename[_MAX_PATH]; // utf-16 version of the file name |
|
206 |
||
207 |
if (NULL == performance) |
|
208 |
return false; |
|
209 |
||
210 |
MULTI_TO_WIDE(w_directory,directory); |
|
211 |
||
212 |
if (FAILED(loader->SetSearchDirectory((REFGUID) GUID_DirectMusicAllTypes, |
|
213 |
w_directory, FALSE))) { |
|
214 |
MSGBOX("LoadMIDI: SetSearchDirectory failed"); |
|
215 |
return false; |
|
216 |
} |
|
217 |
||
218 |
// set up the loader object info |
|
219 |
ZeroMemory (&obj_desc, sizeof (obj_desc)); |
|
220 |
obj_desc.dwSize = sizeof (obj_desc); |
|
221 |
||
222 |
MULTI_TO_WIDE(w_filename,filename); |
|
223 |
obj_desc.guidClass = CLSID_DirectMusicSegment; |
|
224 |
||
225 |
wcscpy (obj_desc.wszFileName,w_filename); |
|
226 |
obj_desc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME; |
|
227 |
||
228 |
// release the existing segment if we have any |
|
229 |
if (NULL != segment) |
|
230 |
ReleaseSegment(); |
|
231 |
||
232 |
// and make a new segment |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
233 |
if (FAILED(loader->GetObject(&obj_desc, |
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
234 |
(REFIID)IID_IDirectMusicSegment, |
0 | 235 |
(LPVOID *) &segment))) { |
236 |
MSGBOX("LoadMIDI: Get object failed"); |
|
237 |
return FALSE; |
|
238 |
} |
|
239 |
||
240 |
// next we need to tell the segment what kind of data it contains. We do this |
|
241 |
// with the IDirectMusicSegment::SetParam function. |
|
242 |
if (FAILED(segment->SetParam((REFGUID)GUID_StandardMIDIFile, |
|
243 |
-1, 0, 0, (LPVOID)performance))) { |
|
244 |
MSGBOX("LoadMIDI: SetParam (MIDI file) failed"); |
|
245 |
return false; |
|
246 |
} |
|
247 |
||
248 |
// finally, we need to tell the segment to 'download' the instruments |
|
249 |
if (FAILED(segment->SetParam((REFGUID)GUID_Download, |
|
250 |
-1, 0, 0, (LPVOID)performance))) { |
|
251 |
MSGBOX("LoadMIDI: Failed to download instruments"); |
|
252 |
return false; |
|
253 |
} |
|
254 |
||
255 |
// at this point, the MIDI file is loaded and ready to play! |
|
256 |
return true; |
|
257 |
} |
|
258 |
||
259 |
// Start playing the MIDI file |
|
260 |
void PlaySegment (void) |
|
261 |
{ |
|
262 |
if (NULL == performance) |
|
263 |
return; |
|
264 |
||
265 |
if (FAILED(performance->PlaySegment(segment, 0, 0, NULL))) { |
|
266 |
MSGBOX("PlaySegment failed"); |
|
267 |
} |
|
268 |
} |
|
269 |
||
270 |
// Stop playing |
|
271 |
void StopSegment (void) |
|
272 |
{ |
|
273 |
if (NULL == performance || NULL == segment) |
|
274 |
return; |
|
275 |
||
276 |
if (FAILED(performance->Stop(segment, NULL, 0, 0))) { |
|
277 |
MSGBOX("StopSegment failed"); |
|
278 |
} |
|
279 |
} |
|
280 |
||
281 |
// Find out whether playing has started or stopped |
|
282 |
bool IsSegmentPlaying (void) |
|
283 |
{ |
|
284 |
if (NULL == performance || NULL == segment) |
|
285 |
return FALSE; |
|
286 |
||
287 |
// IsPlaying return S_OK if the segment is currently playing |
|
288 |
return performance->IsPlaying(segment, NULL) == S_OK ? TRUE : FALSE; |
|
289 |
} |
|
290 |
||
291 |
void SetVolume(long vol) |
|
292 |
{ |
|
293 |
long db; |
|
294 |
||
295 |
if (performance == NULL && !InitDirectMusic()) |
|
296 |
return; |
|
297 |
||
298 |
db = ((vol >> 21) & 0x7ff) - 1000; |
|
299 |
performance->SetGlobalParam(GUID_PerfMasterVolume, &db, sizeof(db)); |
|
300 |
} |
|
301 |
||
302 |
#if defined(__cplusplus) |
|
303 |
} |
|
304 |
#endif |
|
305 |
||
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
|
306 |
#endif /* WIN32_ENABLE_DIRECTMUSIC_SUPPORT */ |