OpenTTD Source: src/music/allegro_m.cpp Source File

OpenTTD Source 12.2

allegro_m.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of OpenTTD.
3 * 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.
4 * 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.
5 * 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/>.
6 */
7
10 #ifdef WITH_ALLEGRO
11
12 #include "../stdafx.h"
13 #include "../debug.h"
14 #include "allegro_m.h"
15 #include "midifile.hpp"
16 #include <allegro.h>
17
18 #include "../safeguards.h"
19
20 static FMusicDriver_Allegro iFMusicDriver_Allegro;
21 static MIDI *_midi = nullptr;
22
27 extern int _allegro_instance_count;
28
29 const char *MusicDriver_Allegro::Start(const StringList &param)
30 {
31 if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, nullptr)) {
32 Debug(driver, 0, "allegro: install_allegro failed '{}'", allegro_error);
33 return "Failed to set up Allegro";
34 }
35 _allegro_instance_count++;
36
37 /* Initialise the sound */
38 if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, nullptr) != 0) {
39 Debug(driver, 0, "allegro: install_sound failed '{}'", allegro_error);
40 return "Failed to set up Allegro sound";
41 }
42
43 /* Okay, there's no soundcard */
44 if (midi_card == MIDI_NONE) {
45 Debug(driver, 0, "allegro: no midi card found");
46 return "No sound card found";
47 }
48
49 return nullptr;
50 }
51
52 void MusicDriver_Allegro::Stop()
53 {
54 if (_midi != nullptr) destroy_midi(_midi);
55 _midi = nullptr;
56
57 if (--_allegro_instance_count == 0) allegro_exit();
58 }
59
60 void MusicDriver_Allegro::PlaySong(const MusicSongInfo &song)
61 {
62 std::string filename = MidiFile::GetSMFFile(song);
63
64 if (_midi != nullptr) destroy_midi(_midi);
65 if (!filename.empty()) {
66 _midi = load_midi(filename.c_str());
67 play_midi(_midi, false);
68 } else {
69 _midi = nullptr;
70 }
71 }
72
73 void MusicDriver_Allegro::StopSong()
74 {
75 stop_midi();
76 }
77
78 bool MusicDriver_Allegro::IsSongPlaying()
79 {
80 return midi_pos >= 0;
81 }
82
83 void MusicDriver_Allegro::SetVolume(byte vol)
84 {
85 set_volume(-1, vol);
86 }
87
88 #endif /* WITH_ALLEGRO */
FMusicDriver_Allegro
Factory for allegro's music player.
Definition: allegro_m.h:33
_midi
static struct @23 _midi
Metadata about the midi we're playing.
MusicSongInfo
Metadata about a music track.
Definition: base_media_base.h:291
StringList
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:58
allegro_m.h
MusicDriver_Allegro::IsSongPlaying
bool IsSongPlaying() override
Are we currently playing a song?
MidiFile::GetSMFFile
static std::string GetSMFFile(const MusicSongInfo &song)
Get the name of a Standard MIDI File for a given song.
Definition: midifile.cpp:1048
MusicDriver_Allegro::Start
const char * Start(const StringList &param) override
Start this driver.
MusicDriver_Allegro::StopSong
void StopSong() override
Stop playing the current song.
MusicDriver_Allegro::PlaySong
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
MusicDriver_Allegro::SetVolume
void SetVolume(byte vol) override
Set the volume, if possible.
MusicDriver_Allegro::Stop
void Stop() override
Stop this driver.