2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-11-25 23:21:58 +00:00
|
|
|
/** @file allegro_m.cpp Playing music via allegro. */
|
|
|
|
|
|
|
|
#ifdef WITH_ALLEGRO
|
|
|
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../debug.h"
|
|
|
|
#include "allegro_m.h"
|
2018-03-21 16:12:29 +00:00
|
|
|
#include "midifile.hpp"
|
2008-11-25 23:21:58 +00:00
|
|
|
#include <allegro.h>
|
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../safeguards.h"
|
|
|
|
|
2008-11-25 23:21:58 +00:00
|
|
|
static FMusicDriver_Allegro iFMusicDriver_Allegro;
|
2019-04-10 21:07:06 +00:00
|
|
|
static MIDI *_midi = nullptr;
|
2008-11-25 23:21:58 +00:00
|
|
|
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* There are multiple modules that might be using Allegro and
|
2010-08-01 19:44:49 +00:00
|
|
|
* Allegro can only be initiated once.
|
|
|
|
*/
|
2008-11-25 23:21:58 +00:00
|
|
|
extern int _allegro_instance_count;
|
|
|
|
|
2020-05-17 21:32:08 +00:00
|
|
|
const char *MusicDriver_Allegro::Start(const StringList ¶m)
|
2008-11-25 23:21:58 +00:00
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (_allegro_instance_count == 0 && install_allegro(SYSTEM_AUTODETECT, &errno, nullptr)) {
|
2009-10-17 14:39:43 +00:00
|
|
|
DEBUG(driver, 0, "allegro: install_allegro failed '%s'", allegro_error);
|
|
|
|
return "Failed to set up Allegro";
|
|
|
|
}
|
2008-11-25 23:21:58 +00:00
|
|
|
_allegro_instance_count++;
|
|
|
|
|
|
|
|
/* Initialise the sound */
|
2019-04-10 21:07:06 +00:00
|
|
|
if (install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, nullptr) != 0) {
|
2009-10-17 14:39:43 +00:00
|
|
|
DEBUG(driver, 0, "allegro: install_sound failed '%s'", allegro_error);
|
|
|
|
return "Failed to set up Allegro sound";
|
|
|
|
}
|
2008-11-25 23:21:58 +00:00
|
|
|
|
|
|
|
/* Okay, there's no soundcard */
|
|
|
|
if (midi_card == MIDI_NONE) {
|
|
|
|
DEBUG(driver, 0, "allegro: no midi card found");
|
2009-06-30 12:38:18 +00:00
|
|
|
return "No sound card found";
|
2008-11-25 23:21:58 +00:00
|
|
|
}
|
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
return nullptr;
|
2008-11-25 23:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MusicDriver_Allegro::Stop()
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
if (_midi != nullptr) destroy_midi(_midi);
|
|
|
|
_midi = nullptr;
|
2008-11-25 23:21:58 +00:00
|
|
|
|
|
|
|
if (--_allegro_instance_count == 0) allegro_exit();
|
|
|
|
}
|
|
|
|
|
2018-03-17 13:51:30 +00:00
|
|
|
void MusicDriver_Allegro::PlaySong(const MusicSongInfo &song)
|
2008-11-25 23:21:58 +00:00
|
|
|
{
|
2018-03-21 16:12:29 +00:00
|
|
|
std::string filename = MidiFile::GetSMFFile(song);
|
2018-03-17 13:51:30 +00:00
|
|
|
|
2019-04-10 21:07:06 +00:00
|
|
|
if (_midi != nullptr) destroy_midi(_midi);
|
2018-03-21 16:12:29 +00:00
|
|
|
if (!filename.empty()) {
|
|
|
|
_midi = load_midi(filename.c_str());
|
|
|
|
play_midi(_midi, false);
|
|
|
|
} else {
|
2019-04-10 21:07:06 +00:00
|
|
|
_midi = nullptr;
|
2018-03-21 16:12:29 +00:00
|
|
|
}
|
2008-11-25 23:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MusicDriver_Allegro::StopSong()
|
|
|
|
{
|
|
|
|
stop_midi();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MusicDriver_Allegro::IsSongPlaying()
|
|
|
|
{
|
|
|
|
return midi_pos >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MusicDriver_Allegro::SetVolume(byte vol)
|
|
|
|
{
|
|
|
|
set_volume(-1, vol);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* WITH_ALLEGRO */
|