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