author | peter1138 |
Sun, 03 Aug 2008 17:35:08 +0000 | |
changeset 9834 | 7474149531c1 |
parent 9582 | 3a55657869eb |
child 10299 | 946c84fdc58e |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
2 |
||
9111
48ce04029fe4
(svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents:
8844
diff
changeset
|
3 |
/** @file driver.cpp Base for all driver handling. */ |
6125
a6fff965707c
(svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents:
6034
diff
changeset
|
4 |
|
2171
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
5 |
#include "stdafx.h" |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
6 |
#include "openttd.h" |
2210
b76184e0929c
(svn r2728) -Fix/Feature: Change the driver probing algorithm
tron
parents:
2186
diff
changeset
|
7 |
#include "debug.h" |
2171
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
8 |
#include "driver.h" |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
9 |
|
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
7045
diff
changeset
|
10 |
#include "sound/sound_driver.hpp" |
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
7045
diff
changeset
|
11 |
#include "music/music_driver.hpp" |
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
7045
diff
changeset
|
12 |
#include "video/video_driver.hpp" |
2222
ce4beae3ce18
(svn r2740) MSCV doesn't seem to like forward declared static arrays
tron
parents:
2219
diff
changeset
|
13 |
|
8275
f17d0c863ee3
(svn r11839) -Codechange: move some variables from variables.h to a more logical location.
rubidium
parents:
8214
diff
changeset
|
14 |
VideoDriver *_video_driver; |
f17d0c863ee3
(svn r11839) -Codechange: move some variables from variables.h to a more logical location.
rubidium
parents:
8214
diff
changeset
|
15 |
char _ini_videodriver[32]; |
f17d0c863ee3
(svn r11839) -Codechange: move some variables from variables.h to a more logical location.
rubidium
parents:
8214
diff
changeset
|
16 |
int _num_resolutions; |
9533
e8b86b70c5f6
(svn r13537) -Fix [FS#2090](r13523): QSortT won't work this way, use Dimension instead of uint16[2] for resolutions
smatz
parents:
9489
diff
changeset
|
17 |
Dimension _resolutions[32]; |
e8b86b70c5f6
(svn r13537) -Fix [FS#2090](r13523): QSortT won't work this way, use Dimension instead of uint16[2] for resolutions
smatz
parents:
9489
diff
changeset
|
18 |
Dimension _cur_resolution; |
8275
f17d0c863ee3
(svn r11839) -Codechange: move some variables from variables.h to a more logical location.
rubidium
parents:
8214
diff
changeset
|
19 |
|
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
7045
diff
changeset
|
20 |
SoundDriver *_sound_driver; |
8275
f17d0c863ee3
(svn r11839) -Codechange: move some variables from variables.h to a more logical location.
rubidium
parents:
8214
diff
changeset
|
21 |
char _ini_sounddriver[32]; |
f17d0c863ee3
(svn r11839) -Codechange: move some variables from variables.h to a more logical location.
rubidium
parents:
8214
diff
changeset
|
22 |
|
7170
923946ec324f
(svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents:
7045
diff
changeset
|
23 |
MusicDriver *_music_driver; |
8275
f17d0c863ee3
(svn r11839) -Codechange: move some variables from variables.h to a more logical location.
rubidium
parents:
8214
diff
changeset
|
24 |
char _ini_musicdriver[32]; |
f17d0c863ee3
(svn r11839) -Codechange: move some variables from variables.h to a more logical location.
rubidium
parents:
8214
diff
changeset
|
25 |
|
f17d0c863ee3
(svn r11839) -Codechange: move some variables from variables.h to a more logical location.
rubidium
parents:
8214
diff
changeset
|
26 |
char _ini_blitter[32]; |
2171
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
27 |
|
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
28 |
static const char* GetDriverParam(const char* const* parm, const char* name) |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
29 |
{ |
2458
dbaf5dffbb28
(svn r2984) Use adequate types, this should aid portability a bit
tron
parents:
2303
diff
changeset
|
30 |
size_t len; |
2171
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
31 |
|
2224
af2f147779d3
(svn r2742) Get rid of some void pointers plus a few minor changes, like 0 -> '\0'
tron
parents:
2222
diff
changeset
|
32 |
if (parm == NULL) return NULL; |
af2f147779d3
(svn r2742) Get rid of some void pointers plus a few minor changes, like 0 -> '\0'
tron
parents:
2222
diff
changeset
|
33 |
|
af2f147779d3
(svn r2742) Get rid of some void pointers plus a few minor changes, like 0 -> '\0'
tron
parents:
2222
diff
changeset
|
34 |
len = strlen(name); |
2171
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
35 |
for (; *parm != NULL; parm++) { |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
36 |
const char* p = *parm; |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
37 |
|
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
38 |
if (strncmp(p, name, len) == 0) { |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
39 |
if (p[len] == '=') return p + len + 1; |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
40 |
if (p[len] == '\0') return p + len; |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
41 |
} |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
42 |
} |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
43 |
return NULL; |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
44 |
} |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
45 |
|
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
46 |
bool GetDriverParamBool(const char* const* parm, const char* name) |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
47 |
{ |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
48 |
return GetDriverParam(parm, name) != NULL; |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
49 |
} |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
50 |
|
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
51 |
int GetDriverParamInt(const char* const* parm, const char* name, int def) |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
52 |
{ |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
53 |
const char* p = GetDriverParam(parm, name); |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
54 |
return p != NULL ? atoi(p) : def; |
60334c9ca477
(svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents:
diff
changeset
|
55 |
} |
8844
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
56 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
57 |
/** |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
58 |
* Find the requested driver and return its class. |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
59 |
* @param name the driver to select. |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
60 |
* @post Sets the driver so GetCurrentDriver() returns it too. |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
61 |
*/ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
62 |
const Driver *DriverFactoryBase::SelectDriver(const char *name, Driver::Type type) |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
63 |
{ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
64 |
if (GetDrivers().size() == 0) return NULL; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
65 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
66 |
if (*name == '\0') { |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
67 |
/* Probe for this driver */ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
68 |
for (int priority = 10; priority >= 0; priority--) { |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
69 |
Drivers::iterator it = GetDrivers().begin(); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
70 |
for (; it != GetDrivers().end(); ++it) { |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
71 |
DriverFactoryBase *d = (*it).second; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
72 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
73 |
/* Check driver type */ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
74 |
if (d->type != type) continue; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
75 |
if (d->priority != priority) continue; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
76 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
77 |
Driver *newd = d->CreateInstance(); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
78 |
const char *err = newd->Start(NULL); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
79 |
if (err == NULL) { |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
80 |
DEBUG(driver, 1, "Successfully probed %s driver '%s'", GetDriverTypeName(type), d->name); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
81 |
delete *GetActiveDriver(type); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
82 |
*GetActiveDriver(type) = newd; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
83 |
return newd; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
84 |
} |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
85 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
86 |
DEBUG(driver, 1, "Probing %s driver '%s' failed with error: %s", GetDriverTypeName(type), d->name, err); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
87 |
delete newd; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
88 |
} |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
89 |
} |
9470
08424e2e79e4
(svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents:
9436
diff
changeset
|
90 |
usererror("Couldn't find any suitable %s driver", GetDriverTypeName(type)); |
8844
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
91 |
} else { |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
92 |
char *parm; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
93 |
char buffer[256]; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
94 |
const char *parms[32]; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
95 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
96 |
/* Extract the driver name and put parameter list in parm */ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
97 |
strecpy(buffer, name, lastof(buffer)); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
98 |
parm = strchr(buffer, ':'); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
99 |
parms[0] = NULL; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
100 |
if (parm != NULL) { |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
101 |
uint np = 0; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
102 |
/* Tokenize the parm. */ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
103 |
do { |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
104 |
*parm++ = '\0'; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
105 |
if (np < lengthof(parms) - 1) parms[np++] = parm; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
106 |
while (*parm != '\0' && *parm != ',') parm++; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
107 |
} while (*parm == ','); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
108 |
parms[np] = NULL; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
109 |
} |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
110 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
111 |
/* Find this driver */ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
112 |
Drivers::iterator it = GetDrivers().begin(); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
113 |
for (; it != GetDrivers().end(); ++it) { |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
114 |
DriverFactoryBase *d = (*it).second; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
115 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
116 |
/* Check driver type */ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
117 |
if (d->type != type) continue; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
118 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
119 |
/* Check driver name */ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
120 |
if (strcasecmp(buffer, d->name) != 0) continue; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
121 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
122 |
/* Found our driver, let's try it */ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
123 |
Driver *newd = d->CreateInstance(); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
124 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
125 |
const char *err = newd->Start(parms); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
126 |
if (err != NULL) { |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
127 |
delete newd; |
9470
08424e2e79e4
(svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents:
9436
diff
changeset
|
128 |
usererror("Unable to load driver '%s'. The error was: %s", d->name, err); |
8844
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
129 |
} |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
130 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
131 |
DEBUG(driver, 1, "Successfully loaded %s driver '%s'", GetDriverTypeName(type), d->name); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
132 |
delete *GetActiveDriver(type); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
133 |
*GetActiveDriver(type) = newd; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
134 |
return newd; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
135 |
} |
9470
08424e2e79e4
(svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents:
9436
diff
changeset
|
136 |
usererror("No such %s driver: %s\n", GetDriverTypeName(type), buffer); |
8844
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
137 |
} |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
138 |
} |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
139 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
140 |
/** |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
141 |
* Register a driver internally, based on its name. |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
142 |
* @param name the name of the driver. |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
143 |
* @note an assert() will be trigger if 2 driver with the same name try to register. |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
144 |
*/ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
145 |
void DriverFactoryBase::RegisterDriver(const char *name, Driver::Type type, int priority) |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
146 |
{ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
147 |
/* Don't register nameless Drivers */ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
148 |
if (name == NULL) return; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
149 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
150 |
this->name = strdup(name); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
151 |
this->type = type; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
152 |
this->priority = priority; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
153 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
154 |
/* Prefix the name with driver type to make it unique */ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
155 |
char buf[32]; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
156 |
strecpy(buf, GetDriverTypeName(type), lastof(buf)); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
157 |
strecpy(buf + 5, name, lastof(buf)); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
158 |
|
9582
3a55657869eb
(svn r13619) -Codechange: use 'const char *' instead of std::string for blitter and driver names
smatz
parents:
9533
diff
changeset
|
159 |
const char *longname = strdup(buf); |
3a55657869eb
(svn r13619) -Codechange: use 'const char *' instead of std::string for blitter and driver names
smatz
parents:
9533
diff
changeset
|
160 |
|
3a55657869eb
(svn r13619) -Codechange: use 'const char *' instead of std::string for blitter and driver names
smatz
parents:
9533
diff
changeset
|
161 |
std::pair<Drivers::iterator, bool> P = GetDrivers().insert(Drivers::value_type(longname, this)); |
8844
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
162 |
assert(P.second); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
163 |
} |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
164 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
165 |
/** |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
166 |
* Build a human readable list of available drivers, grouped by type. |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
167 |
*/ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
168 |
char *DriverFactoryBase::GetDriversInfo(char *p, const char *last) |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
169 |
{ |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
170 |
for (Driver::Type type = Driver::DT_BEGIN; type != Driver::DT_END; type++) { |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
171 |
p += snprintf(p, last - p, "List of %s drivers:\n", GetDriverTypeName(type)); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
172 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
173 |
for (int priority = 10; priority >= 0; priority--) { |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
174 |
Drivers::iterator it = GetDrivers().begin(); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
175 |
for (; it != GetDrivers().end(); it++) { |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
176 |
DriverFactoryBase *d = (*it).second; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
177 |
if (d->type != type) continue; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
178 |
if (d->priority != priority) continue; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
179 |
p += snprintf(p, last - p, "%18s: %s\n", d->name, d->GetDescription()); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
180 |
} |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
181 |
} |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
182 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
183 |
p += snprintf(p, last - p, "\n"); |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
184 |
} |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
185 |
|
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
186 |
return p; |
acb298434295
(svn r12594) -Codechange: move large functions from driver.h to driver.cpp to reduce binary size and compile time
smatz
parents:
8275
diff
changeset
|
187 |
} |
9489
1a097da71d88
(svn r13462) -Codechange: move DriverFactoryBase destructor definition from header file (saves ~16kB)
smatz
parents:
9470
diff
changeset
|
188 |
|
1a097da71d88
(svn r13462) -Codechange: move DriverFactoryBase destructor definition from header file (saves ~16kB)
smatz
parents:
9470
diff
changeset
|
189 |
/** Frees memory used for this->name |
1a097da71d88
(svn r13462) -Codechange: move DriverFactoryBase destructor definition from header file (saves ~16kB)
smatz
parents:
9470
diff
changeset
|
190 |
*/ |
1a097da71d88
(svn r13462) -Codechange: move DriverFactoryBase destructor definition from header file (saves ~16kB)
smatz
parents:
9470
diff
changeset
|
191 |
DriverFactoryBase::~DriverFactoryBase() { |
1a097da71d88
(svn r13462) -Codechange: move DriverFactoryBase destructor definition from header file (saves ~16kB)
smatz
parents:
9470
diff
changeset
|
192 |
if (this->name == NULL) return; |
1a097da71d88
(svn r13462) -Codechange: move DriverFactoryBase destructor definition from header file (saves ~16kB)
smatz
parents:
9470
diff
changeset
|
193 |
|
1a097da71d88
(svn r13462) -Codechange: move DriverFactoryBase destructor definition from header file (saves ~16kB)
smatz
parents:
9470
diff
changeset
|
194 |
/* Prefix the name with driver type to make it unique */ |
1a097da71d88
(svn r13462) -Codechange: move DriverFactoryBase destructor definition from header file (saves ~16kB)
smatz
parents:
9470
diff
changeset
|
195 |
char buf[32]; |
1a097da71d88
(svn r13462) -Codechange: move DriverFactoryBase destructor definition from header file (saves ~16kB)
smatz
parents:
9470
diff
changeset
|
196 |
strecpy(buf, GetDriverTypeName(type), lastof(buf)); |
1a097da71d88
(svn r13462) -Codechange: move DriverFactoryBase destructor definition from header file (saves ~16kB)
smatz
parents:
9470
diff
changeset
|
197 |
strecpy(buf + 5, this->name, lastof(buf)); |
1a097da71d88
(svn r13462) -Codechange: move DriverFactoryBase destructor definition from header file (saves ~16kB)
smatz
parents:
9470
diff
changeset
|
198 |
|
9582
3a55657869eb
(svn r13619) -Codechange: use 'const char *' instead of std::string for blitter and driver names
smatz
parents:
9533
diff
changeset
|
199 |
Drivers::iterator it = GetDrivers().find(buf); |
3a55657869eb
(svn r13619) -Codechange: use 'const char *' instead of std::string for blitter and driver names
smatz
parents:
9533
diff
changeset
|
200 |
assert(it != GetDrivers().end()); |
3a55657869eb
(svn r13619) -Codechange: use 'const char *' instead of std::string for blitter and driver names
smatz
parents:
9533
diff
changeset
|
201 |
|
3a55657869eb
(svn r13619) -Codechange: use 'const char *' instead of std::string for blitter and driver names
smatz
parents:
9533
diff
changeset
|
202 |
const char *longname = (*it).first; |
3a55657869eb
(svn r13619) -Codechange: use 'const char *' instead of std::string for blitter and driver names
smatz
parents:
9533
diff
changeset
|
203 |
|
3a55657869eb
(svn r13619) -Codechange: use 'const char *' instead of std::string for blitter and driver names
smatz
parents:
9533
diff
changeset
|
204 |
GetDrivers().erase(it); |
3a55657869eb
(svn r13619) -Codechange: use 'const char *' instead of std::string for blitter and driver names
smatz
parents:
9533
diff
changeset
|
205 |
free((void *)longname); |
3a55657869eb
(svn r13619) -Codechange: use 'const char *' instead of std::string for blitter and driver names
smatz
parents:
9533
diff
changeset
|
206 |
|
9489
1a097da71d88
(svn r13462) -Codechange: move DriverFactoryBase destructor definition from header file (saves ~16kB)
smatz
parents:
9470
diff
changeset
|
207 |
if (GetDrivers().empty()) delete &GetDrivers(); |
9582
3a55657869eb
(svn r13619) -Codechange: use 'const char *' instead of std::string for blitter and driver names
smatz
parents:
9533
diff
changeset
|
208 |
free((void *)this->name); |
9489
1a097da71d88
(svn r13462) -Codechange: move DriverFactoryBase destructor definition from header file (saves ~16kB)
smatz
parents:
9470
diff
changeset
|
209 |
} |