author | tron |
Fri, 22 Jul 2005 18:19:06 +0000 | |
changeset 2168 | 01b4b1c5720a |
parent 2166 | d57d078cf659 |
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
92a3b0aa0946
(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 |
||
2168 | 57 |
#define MSGBOX(output) DEBUG(misc, 0) ("DirectMusic driver: %s\n", output); //MessageBox(NULL, output, "dxmci",MB_OK); |
0 | 58 |
|
2168 | 59 |
static void MultiToWide(WCHAR* to, const char* from) |
60 |
{ |
|
61 |
MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, from, -1, to, _MAX_PATH); |
|
62 |
} |
|
0 | 63 |
|
64 |
// the performance object controls manipulation of the segments |
|
2168 | 65 |
static IDirectMusicPerformance *performance = NULL; |
0 | 66 |
|
67 |
// the segment object is where the MIDI data is stored for playback |
|
2168 | 68 |
static IDirectMusicSegment *segment = NULL; |
0 | 69 |
|
70 |
// the loader bject can load many types of DMusic related files |
|
2168 | 71 |
static IDirectMusicLoader *loader = NULL; |
0 | 72 |
|
73 |
// whether we've initialized COM or not (when deciding whether to shut down) |
|
2168 | 74 |
static int COMInitialized = 0; |
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" |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
87 |
static const char ole_files[] = |
0 | 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 |
||
1117 | 99 |
static bool LoadOleDLL(void) |
0 | 100 |
{ |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
101 |
if (_proc.CoCreateInstance != NULL) |
0 | 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 |
|
2168 | 114 |
bool InitDirectMusic(void) |
0 | 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( |
|
2168 | 132 |
CLSID_DirectMusicPerformance, |
133 |
NULL, |
|
134 |
CLSCTX_INPROC, |
|
135 |
IID_IDirectMusicPerformance, |
|
136 |
(LPVOID*)&performance |
|
137 |
))) { |
|
138 |
MSGBOX("Failed to create the performance object"); |
|
0 | 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 |
|
2168 | 149 |
if (FAILED(performance->AddPort(NULL))) { |
0 | 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 |
|
2168 | 157 |
if (FAILED(_proc.CoCreateInstance( |
158 |
CLSID_DirectMusicLoader, |
|
159 |
NULL, |
|
160 |
CLSCTX_INPROC, |
|
161 |
IID_IDirectMusicLoader, |
|
162 |
(LPVOID*)&loader |
|
163 |
))) { |
|
0 | 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 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
173 |
// Releases memory used by all of the initialized |
0 | 174 |
// DirectMusic objects in the program |
2168 | 175 |
void ReleaseSegment(void) |
0 | 176 |
{ |
177 |
if (NULL != segment) { |
|
2168 | 178 |
segment->Release(); |
0 | 179 |
segment = NULL; |
180 |
} |
|
181 |
} |
|
2168 | 182 |
|
183 |
void ShutdownDirectMusic(void) |
|
0 | 184 |
{ |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
185 |
// 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
|
186 |
// will release automatically (and it'll crash if it's been |
0 | 187 |
// released already) |
188 |
||
189 |
if (NULL != loader) { |
|
2168 | 190 |
loader->Release(); |
0 | 191 |
loader = NULL; |
192 |
} |
|
193 |
||
2168 | 194 |
if (NULL != performance) { |
195 |
performance->CloseDown(); |
|
196 |
performance->Release(); |
|
0 | 197 |
performance = NULL; |
198 |
} |
|
199 |
||
200 |
if (COMInitialized) { |
|
201 |
_proc.CoUninitialize(); |
|
202 |
COMInitialized = 0; |
|
203 |
} |
|
204 |
} |
|
205 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
0
diff
changeset
|
206 |
// Load MIDI file for playing |
2168 | 207 |
bool LoadMIDI(const char *directory, const char *filename) |
0 | 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 |
||
2168 | 216 |
MultiToWide(w_directory, directory); |
0 | 217 |
|
2168 | 218 |
if (FAILED(loader->SetSearchDirectory( |
219 |
GUID_DirectMusicAllTypes, w_directory, FALSE |
|
220 |
))) { |
|
0 | 221 |
MSGBOX("LoadMIDI: SetSearchDirectory failed"); |
222 |
return false; |
|
223 |
} |
|
224 |
||
225 |
// set up the loader object info |
|
2168 | 226 |
ZeroMemory(&obj_desc, sizeof(obj_desc)); |
227 |
obj_desc.dwSize = sizeof(obj_desc); |
|
0 | 228 |
|
2168 | 229 |
MultiToWide(w_filename, filename); |
0 | 230 |
obj_desc.guidClass = CLSID_DirectMusicSegment; |
231 |
||
2168 | 232 |
wcscpy(obj_desc.wszFileName, w_filename); |
0 | 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 |
|
2168 | 240 |
if (FAILED(loader->GetObject( |
241 |
&obj_desc, IID_IDirectMusicSegment, (LPVOID*)&segment |
|
242 |
))) { |
|
0 | 243 |
MSGBOX("LoadMIDI: Get object failed"); |
2168 | 244 |
return false; |
0 | 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. |
|
2168 | 249 |
if (FAILED(segment->SetParam( |
250 |
GUID_StandardMIDIFile, 0xFFFFFFFF, 0, 0, performance |
|
251 |
))) { |
|
0 | 252 |
MSGBOX("LoadMIDI: SetParam (MIDI file) failed"); |
253 |
return false; |
|
254 |
} |
|
255 |
||
256 |
// finally, we need to tell the segment to 'download' the instruments |
|
2168 | 257 |
if (FAILED(segment->SetParam(GUID_Download, 0xFFFFFFFF, 0, 0, performance))) { |
0 | 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 |
|
2168 | 267 |
void PlaySegment(void) |
0 | 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 |
|
2168 | 278 |
void StopSegment(void) |
0 | 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 |
|
2168 | 289 |
bool IsSegmentPlaying(void) |
0 | 290 |
{ |
291 |
if (NULL == performance || NULL == segment) |
|
2168 | 292 |
return false; |
0 | 293 |
|
294 |
// IsPlaying return S_OK if the segment is currently playing |
|
2168 | 295 |
return performance->IsPlaying(segment, NULL) == S_OK; |
0 | 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 |
||
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
|
313 |
#endif /* WIN32_ENABLE_DIRECTMUSIC_SUPPORT */ |