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-05-06 15:11:33 +00:00
|
|
|
/** @file driver.h Base for all drivers (video, sound, music, etc). */
|
2007-02-23 18:55:07 +00:00
|
|
|
|
2005-07-23 15:16:57 +00:00
|
|
|
#ifndef DRIVER_H
|
|
|
|
#define DRIVER_H
|
|
|
|
|
2007-12-21 08:34:53 +00:00
|
|
|
#include "core/enum_type.hpp"
|
2009-02-03 18:08:07 +00:00
|
|
|
#include "core/string_compare_type.hpp"
|
2020-05-17 21:32:08 +00:00
|
|
|
#include "string_type.h"
|
2007-07-05 12:23:54 +00:00
|
|
|
#include <map>
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2020-05-17 21:32:08 +00:00
|
|
|
const char *GetDriverParam(const StringList &parm, const char *name);
|
|
|
|
bool GetDriverParamBool(const StringList &parm, const char *name);
|
|
|
|
int GetDriverParamInt(const StringList &parm, const char *name, int def);
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2011-05-01 20:04:09 +00:00
|
|
|
/** A driver for communicating with the user. */
|
2007-07-05 12:23:54 +00:00
|
|
|
class Driver {
|
|
|
|
public:
|
2011-05-01 20:04:09 +00:00
|
|
|
/**
|
|
|
|
* Start this driver.
|
|
|
|
* @param parm Parameters passed to the driver.
|
2019-04-10 21:07:06 +00:00
|
|
|
* @return nullptr if everything went okay, otherwise an error message.
|
2011-05-01 20:04:09 +00:00
|
|
|
*/
|
2020-05-17 21:32:08 +00:00
|
|
|
virtual const char *Start(const StringList &parm) = 0;
|
2007-07-05 12:23:54 +00:00
|
|
|
|
2011-05-01 20:04:09 +00:00
|
|
|
/**
|
|
|
|
* Stop this driver.
|
|
|
|
*/
|
2007-07-05 12:23:54 +00:00
|
|
|
virtual void Stop() = 0;
|
|
|
|
|
|
|
|
virtual ~Driver() { }
|
|
|
|
|
2010-02-20 17:30:22 +00:00
|
|
|
/** The type of driver */
|
2007-07-05 12:23:54 +00:00
|
|
|
enum Type {
|
2010-02-20 17:30:22 +00:00
|
|
|
DT_BEGIN = 0, ///< Helper for iteration
|
|
|
|
DT_MUSIC = 0, ///< A music driver, needs to be before sound to properly shut down extmidi forked music players
|
|
|
|
DT_SOUND, ///< A sound driver
|
|
|
|
DT_VIDEO, ///< A video driver
|
|
|
|
DT_END, ///< Helper for iteration
|
2007-07-05 12:23:54 +00:00
|
|
|
};
|
2009-09-05 21:26:51 +00:00
|
|
|
|
2011-05-01 20:04:09 +00:00
|
|
|
/**
|
|
|
|
* Get the name of this driver.
|
|
|
|
* @return The name of the driver.
|
|
|
|
*/
|
2009-09-05 21:26:51 +00:00
|
|
|
virtual const char *GetName() const = 0;
|
2007-07-05 12:23:54 +00:00
|
|
|
};
|
|
|
|
|
2010-03-23 22:25:43 +00:00
|
|
|
DECLARE_POSTFIX_INCREMENT(Driver::Type)
|
2007-07-05 12:23:54 +00:00
|
|
|
|
|
|
|
|
2011-05-01 20:04:09 +00:00
|
|
|
/** Base for all driver factories. */
|
2007-07-05 12:23:54 +00:00
|
|
|
class DriverFactoryBase {
|
|
|
|
private:
|
2014-04-28 21:06:51 +00:00
|
|
|
friend class MusicDriver;
|
|
|
|
friend class SoundDriver;
|
|
|
|
friend class VideoDriver;
|
|
|
|
|
2013-11-25 14:30:22 +00:00
|
|
|
Driver::Type type; ///< The type of driver.
|
2013-11-25 14:26:46 +00:00
|
|
|
int priority; ///< The priority of this factory.
|
2013-11-25 14:30:22 +00:00
|
|
|
const char *name; ///< The name of the drivers of this factory.
|
2013-11-25 14:26:46 +00:00
|
|
|
const char *description; ///< The description of this driver.
|
2008-06-24 09:15:45 +00:00
|
|
|
|
2020-05-17 21:32:08 +00:00
|
|
|
typedef std::map<std::string, DriverFactoryBase *> Drivers; ///< Type for a map of drivers.
|
2007-07-05 12:23:54 +00:00
|
|
|
|
2011-05-01 20:04:09 +00:00
|
|
|
/**
|
|
|
|
* Get the map with drivers.
|
|
|
|
*/
|
2007-07-05 12:23:54 +00:00
|
|
|
static Drivers &GetDrivers()
|
|
|
|
{
|
2008-05-16 21:32:10 +00:00
|
|
|
static Drivers &s_drivers = *new Drivers();
|
2007-07-05 12:23:54 +00:00
|
|
|
return s_drivers;
|
|
|
|
}
|
|
|
|
|
2011-05-01 20:04:09 +00:00
|
|
|
/**
|
|
|
|
* Get the active driver for the given type.
|
|
|
|
* @param type The type to get the driver for.
|
|
|
|
* @return The active driver.
|
|
|
|
*/
|
2007-07-05 12:23:54 +00:00
|
|
|
static Driver **GetActiveDriver(Driver::Type type)
|
|
|
|
{
|
2019-04-10 21:07:06 +00:00
|
|
|
static Driver *s_driver[3] = { nullptr, nullptr, nullptr };
|
2007-07-05 12:23:54 +00:00
|
|
|
return &s_driver[type];
|
|
|
|
}
|
|
|
|
|
2011-05-01 20:04:09 +00:00
|
|
|
/**
|
|
|
|
* Get the driver type name.
|
|
|
|
* @param type The type of driver to get the name of.
|
|
|
|
* @return The name of the type.
|
|
|
|
*/
|
2007-07-05 12:23:54 +00:00
|
|
|
static const char *GetDriverTypeName(Driver::Type type)
|
|
|
|
{
|
2010-02-20 17:30:22 +00:00
|
|
|
static const char * const driver_type_name[] = { "music", "sound", "video" };
|
2007-07-05 12:23:54 +00:00
|
|
|
return driver_type_name[type];
|
|
|
|
}
|
|
|
|
|
2020-05-17 21:32:08 +00:00
|
|
|
static bool SelectDriverImpl(const std::string &name, Driver::Type type);
|
2014-04-28 21:06:51 +00:00
|
|
|
|
2007-07-05 12:23:54 +00:00
|
|
|
protected:
|
2013-11-25 14:26:46 +00:00
|
|
|
DriverFactoryBase(Driver::Type type, int priority, const char *name, const char *description);
|
2007-07-05 12:23:54 +00:00
|
|
|
|
2008-06-11 12:46:28 +00:00
|
|
|
virtual ~DriverFactoryBase();
|
2008-05-08 23:26:17 +00:00
|
|
|
|
2021-03-08 14:42:39 +00:00
|
|
|
/**
|
|
|
|
* Does the driver use hardware acceleration (video-drivers only).
|
|
|
|
* @return True if the driver uses hardware acceleration.
|
|
|
|
*/
|
|
|
|
virtual bool UsesHardwareAcceleration() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-25 14:26:46 +00:00
|
|
|
public:
|
2010-08-01 19:22:34 +00:00
|
|
|
/**
|
|
|
|
* Shuts down all active drivers
|
2008-05-08 23:26:17 +00:00
|
|
|
*/
|
|
|
|
static void ShutdownDrivers()
|
|
|
|
{
|
|
|
|
for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) {
|
|
|
|
Driver *driver = *GetActiveDriver(dt);
|
2019-04-10 21:07:06 +00:00
|
|
|
if (driver != nullptr) driver->Stop();
|
2008-05-08 23:26:17 +00:00
|
|
|
}
|
|
|
|
}
|
2007-07-05 12:23:54 +00:00
|
|
|
|
2020-05-17 21:32:08 +00:00
|
|
|
static void SelectDriver(const std::string &name, Driver::Type type);
|
2008-04-06 16:49:02 +00:00
|
|
|
static char *GetDriversInfo(char *p, const char *last);
|
2007-07-05 12:23:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a nice description of the driver-class.
|
2011-05-01 20:04:09 +00:00
|
|
|
* @return The description.
|
2007-07-05 12:23:54 +00:00
|
|
|
*/
|
2013-11-25 14:26:46 +00:00
|
|
|
const char *GetDescription() const
|
|
|
|
{
|
|
|
|
return this->description;
|
|
|
|
}
|
2007-07-05 12:23:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an instance of this driver-class.
|
2011-05-01 20:04:09 +00:00
|
|
|
* @return The instance.
|
2007-07-05 12:23:54 +00:00
|
|
|
*/
|
2013-11-25 14:26:46 +00:00
|
|
|
virtual Driver *CreateInstance() const = 0;
|
2007-07-05 12:23:54 +00:00
|
|
|
};
|
2005-07-23 15:16:57 +00:00
|
|
|
|
2005-09-18 20:56:44 +00:00
|
|
|
#endif /* DRIVER_H */
|