music/dmusic.c
changeset 2171 008122046f7f
parent 2168 01b4b1c5720a
child 2186 461a2aff3486
equal deleted inserted replaced
2170:b8a5a7dc0cd2 2171:008122046f7f
       
     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 #include "openttd.h"
       
    32 #include "string.h"
       
    33 #include "variables.h"
       
    34 #include "sound.h"
       
    35 #include "music/dmusic.h"
       
    36 
       
    37 static const char * DMusicMidiStart(const char * const *parm);
       
    38 static void DMusicMidiStop(void);
       
    39 static void DMusicMidiPlaySong(const char *filename);
       
    40 static void DMusicMidiStopSong(void);
       
    41 static bool DMusicMidiIsSongPlaying(void);
       
    42 static void DMusicMidiSetVolume(byte vol);
       
    43 
       
    44 const HalMusicDriver _dmusic_midi_driver = {
       
    45 	DMusicMidiStart,
       
    46 	DMusicMidiStop,
       
    47 	DMusicMidiPlaySong,
       
    48 	DMusicMidiStopSong,
       
    49 	DMusicMidiIsSongPlaying,
       
    50 	DMusicMidiSetVolume,
       
    51 };
       
    52 
       
    53 extern bool LoadMIDI (char *directory, char *filename);
       
    54 extern bool InitDirectMusic (void);
       
    55 extern void ReleaseSegment (void);
       
    56 extern void ShutdownDirectMusic (void);
       
    57 extern bool LoadMIDI (char *directory, char *filename);
       
    58 extern void PlaySegment (void);
       
    59 extern void StopSegment (void);
       
    60 extern bool IsSegmentPlaying (void);
       
    61 extern void SetVolume (long);
       
    62 
       
    63 static bool seeking = false;
       
    64 
       
    65 static const char * DMusicMidiStart(const char * const *parm)
       
    66 {
       
    67 	return InitDirectMusic() ? NULL : "Unable to initialize DirectMusic";
       
    68 }
       
    69 
       
    70 static void DMusicMidiStop(void)
       
    71 {
       
    72 	StopSegment();
       
    73 }
       
    74 
       
    75 static void DMusicMidiPlaySong(const char *filename)
       
    76 {
       
    77 	char *pos;
       
    78 	char dir[MAX_PATH];
       
    79 	char file[MAX_PATH];
       
    80 
       
    81 	// split full path into directory and file components
       
    82 	ttd_strlcpy(dir, filename, MAX_PATH);
       
    83 	pos = strrchr(dir, '\\') + 1;
       
    84 	ttd_strlcpy(file, pos, MAX_PATH);
       
    85 	*pos = '\0';
       
    86 
       
    87 	LoadMIDI(dir, file);
       
    88 	PlaySegment();
       
    89 	seeking = true;
       
    90 }
       
    91 
       
    92 static void DMusicMidiStopSong(void)
       
    93 {
       
    94 	StopSegment();
       
    95 }
       
    96 
       
    97 static bool DMusicMidiIsSongPlaying(void)
       
    98 {
       
    99 	/* Not the nicest code, but there is a short delay before playing actually 
       
   100 	 * starts. OpenTTD makes no provision for this. */
       
   101 	if (!IsSegmentPlaying() && seeking) return true;
       
   102 	if (IsSegmentPlaying()) seeking = false;
       
   103 
       
   104 	return IsSegmentPlaying();
       
   105 }
       
   106 
       
   107 static void DMusicMidiSetVolume(byte vol)
       
   108 {
       
   109 	SetVolume(vol);
       
   110 }
       
   111 
       
   112 #endif /* WIN32_ENABLE_DIRECTMUSIC_SUPPORT */