Update: Translations from eints
[openttd-github.git] / src / music / allegro_m.cpp
blobce9184f15f278196678d9897b462f67aff1f9ecf
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 */
8 /** @file allegro_m.cpp Playing music via allegro. */
10 #ifdef WITH_ALLEGRO
12 #include "../stdafx.h"
13 #include "../debug.h"
14 #include "allegro_m.h"
15 #include "midifile.hpp"
16 #include <allegro.h>
18 #include "../safeguards.h"
20 static FMusicDriver_Allegro iFMusicDriver_Allegro;
21 static MIDI *_midi = nullptr;
23 /**
24 * There are multiple modules that might be using Allegro and
25 * Allegro can only be initiated once.
27 extern int _allegro_instance_count;
29 std::optional<std::string_view> MusicDriver_Allegro::Start(const StringList &)
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";
35 _allegro_instance_count++;
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";
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";
49 return std::nullopt;
52 void MusicDriver_Allegro::Stop()
54 if (_midi != nullptr) destroy_midi(_midi);
55 _midi = nullptr;
57 if (--_allegro_instance_count == 0) allegro_exit();
60 void MusicDriver_Allegro::PlaySong(const MusicSongInfo &song)
62 std::string filename = MidiFile::GetSMFFile(song);
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;
73 void MusicDriver_Allegro::StopSong()
75 stop_midi();
78 bool MusicDriver_Allegro::IsSongPlaying()
80 return midi_pos >= 0;
83 void MusicDriver_Allegro::SetVolume(uint8_t vol)
85 set_volume(-1, vol);
88 #endif /* WITH_ALLEGRO */