~lewiscawte/+junk/openttd-stable

« back to all changes in this revision

Viewing changes to src/driver.h

  • Committer: Lewis Cawte
  • Date: 2011-06-11 09:45:09 UTC
  • Revision ID: lewis@dev.lewiscawte.info-20110611094509-c5dqw2ot6pb9t10i
Adding stable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: driver.h 20283 2010-08-01 19:22:34Z frosch $ */
 
2
 
 
3
/*
 
4
 * This file is part of OpenTTD.
 
5
 * 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.
 
6
 * 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.
 
7
 * 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/>.
 
8
 */
 
9
 
 
10
/** @file driver.h Base for all drivers (video, sound, music, etc). */
 
11
 
 
12
#ifndef DRIVER_H
 
13
#define DRIVER_H
 
14
 
 
15
#include "core/enum_type.hpp"
 
16
#include "core/string_compare_type.hpp"
 
17
#include <map>
 
18
 
 
19
const char *GetDriverParam(const char * const *parm, const char *name);
 
20
bool GetDriverParamBool(const char * const *parm, const char *name);
 
21
int GetDriverParamInt(const char * const *parm, const char *name, int def);
 
22
 
 
23
class Driver {
 
24
public:
 
25
        virtual const char *Start(const char * const *parm) = 0;
 
26
 
 
27
        virtual void Stop() = 0;
 
28
 
 
29
        virtual ~Driver() { }
 
30
 
 
31
        /** The type of driver */
 
32
        enum Type {
 
33
                DT_BEGIN = 0, ///< Helper for iteration
 
34
                DT_MUSIC = 0, ///< A music driver, needs to be before sound to properly shut down extmidi forked music players
 
35
                DT_SOUND,     ///< A sound driver
 
36
                DT_VIDEO,     ///< A video driver
 
37
                DT_END,       ///< Helper for iteration
 
38
        };
 
39
 
 
40
        virtual const char *GetName() const = 0;
 
41
};
 
42
 
 
43
DECLARE_POSTFIX_INCREMENT(Driver::Type)
 
44
 
 
45
 
 
46
class DriverFactoryBase {
 
47
private:
 
48
        Driver::Type type;
 
49
        const char *name;
 
50
        int priority;
 
51
 
 
52
        typedef std::map<const char *, DriverFactoryBase *, StringCompare> Drivers;
 
53
 
 
54
        static Drivers &GetDrivers()
 
55
        {
 
56
                static Drivers &s_drivers = *new Drivers();
 
57
                return s_drivers;
 
58
        }
 
59
 
 
60
        static Driver **GetActiveDriver(Driver::Type type)
 
61
        {
 
62
                static Driver *s_driver[3] = { NULL, NULL, NULL };
 
63
                return &s_driver[type];
 
64
        }
 
65
 
 
66
        static const char *GetDriverTypeName(Driver::Type type)
 
67
        {
 
68
                static const char * const driver_type_name[] = { "music", "sound", "video" };
 
69
                return driver_type_name[type];
 
70
        }
 
71
 
 
72
protected:
 
73
        void RegisterDriver(const char *name, Driver::Type type, int priority);
 
74
 
 
75
public:
 
76
        DriverFactoryBase() :
 
77
                name(NULL)
 
78
        {}
 
79
 
 
80
        virtual ~DriverFactoryBase();
 
81
 
 
82
        /**
 
83
         * Shuts down all active drivers
 
84
         */
 
85
        static void ShutdownDrivers()
 
86
        {
 
87
                for (Driver::Type dt = Driver::DT_BEGIN; dt < Driver::DT_END; dt++) {
 
88
                        Driver *driver = *GetActiveDriver(dt);
 
89
                        if (driver != NULL) driver->Stop();
 
90
                }
 
91
        }
 
92
 
 
93
        static Driver *SelectDriver(const char *name, Driver::Type type);
 
94
        static char *GetDriversInfo(char *p, const char *last);
 
95
 
 
96
        /**
 
97
         * Get a nice description of the driver-class.
 
98
         */
 
99
        virtual const char *GetDescription() = 0;
 
100
 
 
101
        /**
 
102
         * Create an instance of this driver-class.
 
103
         */
 
104
        virtual Driver *CreateInstance() = 0;
 
105
};
 
106
 
 
107
#endif /* DRIVER_H */