OpenTTD Source  20241108-master-g80f628063a
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 std::optional<std::string_view> MusicDriver_Allegro::Start(const StringList &)
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 std::nullopt;
50 }
51 
53 {
54  if (_midi != nullptr) destroy_midi(_midi);
55  _midi = nullptr;
56 
57  if (--_allegro_instance_count == 0) allegro_exit();
58 }
59 
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 
74 {
75  stop_midi();
76 }
77 
79 {
80  return midi_pos >= 0;
81 }
82 
83 void MusicDriver_Allegro::SetVolume(uint8_t vol)
84 {
85  set_volume(-1, vol);
86 }
87 
88 #endif /* WITH_ALLEGRO */
Base support for playing music via allegro.
Factory for allegro's music player.
Definition: allegro_m.h:33
void SetVolume(uint8_t vol) override
Set the volume, if possible.
void StopSong() override
Stop playing the current song.
std::optional< std::string_view > Start(const StringList &param) override
Start this driver.
void PlaySong(const MusicSongInfo &song) override
Play a particular song.
bool IsSongPlaying() override
Are we currently playing a song?
void Stop() override
Stop this driver.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
static struct @11 _midi
Metadata about the midi we're playing.
std::vector< std::string > StringList
Type for a list of strings.
Definition: string_type.h:60
static std::string GetSMFFile(const MusicSongInfo &song)
Get the name of a Standard MIDI File for a given song.
Definition: midifile.cpp:1028
Metadata about a music track.