OpenTTD
allegro_m.cpp
Go to the documentation of this file.
1 /* $Id$ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * OpenTTD 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, version 2.
6  * OpenTTD 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.
7  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8  */
9 
12 #ifdef WITH_ALLEGRO
13 
14 #include "../stdafx.h"
15 #include "../debug.h"
16 #include "allegro_m.h"
17 #include "midifile.hpp"
18 #include <allegro.h>
19 
20 #include "../safeguards.h"
21 
22 static FMusicDriver_Allegro iFMusicDriver_Allegro;
23 static MIDI *_midi = NULL;
24 
29 extern int _allegro_instance_count;
30 
31 const char *MusicDriver_Allegro::Start(const char * const *param)
32 {
33  if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, NULL)) {
34  DEBUG(driver, 0, "allegro: install_allegro failed '%s'", allegro_error);
35  return "Failed to set up Allegro";
36  }
37  _allegro_instance_count++;
38 
39  /* Initialise the sound */
40  if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL) != 0) {
41  DEBUG(driver, 0, "allegro: install_sound failed '%s'", allegro_error);
42  return "Failed to set up Allegro sound";
43  }
44 
45  /* Okay, there's no soundcard */
46  if (midi_card == MIDI_NONE) {
47  DEBUG(driver, 0, "allegro: no midi card found");
48  return "No sound card found";
49  }
50 
51  return NULL;
52 }
53 
55 {
56  if (_midi != NULL) destroy_midi(_midi);
57  _midi = NULL;
58 
59  if (--_allegro_instance_count == 0) allegro_exit();
60 }
61 
63 {
64  std::string filename = MidiFile::GetSMFFile(song);
65 
66  if (_midi != NULL) destroy_midi(_midi);
67  if (!filename.empty()) {
68  _midi = load_midi(filename.c_str());
69  play_midi(_midi, false);
70  } else {
71  _midi = NULL;
72  }
73 }
74 
76 {
77  stop_midi();
78 }
79 
81 {
82  return midi_pos >= 0;
83 }
84 
85 void MusicDriver_Allegro::SetVolume(byte vol)
86 {
87  set_volume(-1, vol);
88 }
89 
90 #endif /* WITH_ALLEGRO */
Metadata about a music track.
void Stop()
Stop this driver.
Factory for allegro&#39;s music player.
Definition: allegro_m.h:35
void SetVolume(byte vol)
Set the volume, if possible.
Base support for playing music via allegro.
bool IsSongPlaying()
Are we currently playing a song?
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:36
static std::string GetSMFFile(const MusicSongInfo &song)
Get the name of a Standard MIDI File for a given song.
Definition: midifile.cpp:1041
const char * Start(const char *const *param)
Start this driver.
void PlaySong(const MusicSongInfo &song)
Play a particular song.
static struct @24 _midi
Metadata about the midi we&#39;re playing.
void StopSong()
Stop playing the current song.