author | tron |
Wed, 17 Nov 2004 07:57:28 +0000 | |
changeset 443 | a45be556a111 |
parent 435 | 62863eb5148a |
child 444 | b4589ccaff3a |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
||
3 |
#if defined(WITH_SDL) |
|
4 |
#include "ttd.h" |
|
5 |
#include "gfx.h" |
|
6 |
#include "sound.h" |
|
7 |
#include "window.h" |
|
8 |
#include <SDL.h> |
|
9 |
#include "player.h" |
|
10 |
#include "hal.h" |
|
11 |
||
12 |
#ifdef UNIX |
|
13 |
#include <signal.h> |
|
443
a45be556a111
(svn r652) Factorise special case for MorphOS regarding signal() handling
tron
parents:
435
diff
changeset
|
14 |
|
a45be556a111
(svn r652) Factorise special case for MorphOS regarding signal() handling
tron
parents:
435
diff
changeset
|
15 |
#ifdef __MORPHOS__ |
a45be556a111
(svn r652) Factorise special case for MorphOS regarding signal() handling
tron
parents:
435
diff
changeset
|
16 |
#define SIG_DFL (void (*)(int))0 |
a45be556a111
(svn r652) Factorise special case for MorphOS regarding signal() handling
tron
parents:
435
diff
changeset
|
17 |
#endif |
0 | 18 |
#endif |
19 |
||
20 |
#define DYNAMICALLY_LOADED_SDL |
|
21 |
||
22 |
static SDL_Surface *_sdl_screen; |
|
23 |
static int _sdl_usage; |
|
24 |
static bool _all_modes; |
|
25 |
||
26 |
#define MAX_DIRTY_RECTS 100 |
|
27 |
static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS]; |
|
28 |
static int _num_dirty_rects; |
|
29 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
30 |
#define SDL_CALL |
0 | 31 |
|
32 |
#if defined(DYNAMICALLY_LOADED_SDL) && defined(WIN32) |
|
33 |
||
34 |
bool LoadLibraryList(void **proc, const char *dll); |
|
35 |
||
36 |
typedef struct { |
|
37 |
int (SDLCALL *SDL_Init)(Uint32); |
|
38 |
int (SDLCALL *SDL_InitSubSystem)(Uint32); |
|
39 |
char *(SDLCALL *SDL_GetError)(); |
|
40 |
void (SDLCALL *SDL_QuitSubSystem)(Uint32); |
|
41 |
void (SDLCALL *SDL_UpdateRect)(SDL_Surface *, Sint32, Sint32, Uint32, Uint32); |
|
42 |
void (SDLCALL *SDL_UpdateRects)(SDL_Surface *, int, SDL_Rect *); |
|
43 |
int (SDLCALL *SDL_SetColors)(SDL_Surface *, SDL_Color *, int, int); |
|
44 |
void (SDLCALL *SDL_WM_SetCaption)(const char *, const char *); |
|
45 |
int (SDLCALL *SDL_ShowCursor)(int); |
|
46 |
void (SDLCALL *SDL_FreeSurface)(SDL_Surface *); |
|
47 |
int (SDLCALL *SDL_PollEvent)(SDL_Event *); |
|
48 |
void (SDLCALL *SDL_WarpMouse)(Uint16, Uint16); |
|
49 |
uint32 (SDLCALL *SDL_GetTicks)(); |
|
50 |
int (SDLCALL *SDL_OpenAudio)(SDL_AudioSpec *, SDL_AudioSpec*); |
|
51 |
void (SDLCALL *SDL_PauseAudio)(int); |
|
52 |
void (SDLCALL *SDL_CloseAudio)(); |
|
53 |
int (SDLCALL *SDL_LockSurface)(SDL_Surface*); |
|
54 |
void (SDLCALL *SDL_UnlockSurface)(SDL_Surface*); |
|
55 |
SDLMod (SDLCALL *SDL_GetModState)(); |
|
56 |
void (SDLCALL *SDL_Delay)(Uint32); |
|
57 |
void (SDLCALL *SDL_Quit)(); |
|
423 | 58 |
SDL_Surface *(SDLCALL *SDL_SetVideoMode)(int, int, int, Uint32); |
59 |
int (SDLCALL *SDL_EnableKeyRepeat)(int, int); |
|
0 | 60 |
void (SDLCALL *SDL_EnableUNICODE)(int); |
61 |
void (SDLCALL *SDL_VideoDriverName)(char *, int); |
|
62 |
SDL_Rect **(SDLCALL *SDL_ListModes)(void *, int); |
|
63 |
Uint8 *(SDLCALL *SDL_GetKeyState)(int *); |
|
64 |
} SDLProcs; |
|
65 |
||
66 |
#define M(x) x "\0" |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
67 |
static const char sdl_files[] = |
0 | 68 |
M("sdl.dll") |
69 |
M("SDL_Init") |
|
70 |
M("SDL_InitSubSystem") |
|
71 |
M("SDL_GetError") |
|
72 |
M("SDL_QuitSubSystem") |
|
73 |
M("SDL_UpdateRect") |
|
74 |
M("SDL_UpdateRects") |
|
75 |
M("SDL_SetColors") |
|
76 |
M("SDL_WM_SetCaption") |
|
77 |
M("SDL_ShowCursor") |
|
78 |
M("SDL_FreeSurface") |
|
79 |
M("SDL_PollEvent") |
|
80 |
M("SDL_WarpMouse") |
|
81 |
M("SDL_GetTicks") |
|
82 |
M("SDL_OpenAudio") |
|
83 |
M("SDL_PauseAudio") |
|
84 |
M("SDL_CloseAudio") |
|
85 |
M("SDL_LockSurface") |
|
86 |
M("SDL_UnlockSurface") |
|
87 |
M("SDL_GetModState") |
|
88 |
M("SDL_Delay") |
|
89 |
M("SDL_Quit") |
|
90 |
M("SDL_SetVideoMode") |
|
91 |
M("SDL_EnableKeyRepeat") |
|
92 |
M("SDL_EnableUNICODE") |
|
93 |
M("SDL_VideoDriverName") |
|
94 |
M("SDL_ListModes") |
|
95 |
M("SDL_GetKeyState") |
|
96 |
M("") |
|
97 |
; |
|
98 |
#undef M |
|
99 |
||
100 |
static SDLProcs _proc; |
|
101 |
||
423 | 102 |
static char *LoadSdlDLL(void) |
0 | 103 |
{ |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
104 |
if (_proc.SDL_Init != NULL) |
0 | 105 |
return NULL; |
106 |
if (!LoadLibraryList((void**)&_proc, sdl_files)) |
|
107 |
return "Unable to load sdl.dll"; |
|
108 |
return NULL; |
|
109 |
} |
|
110 |
||
111 |
#undef SDL_CALL |
|
112 |
#define SDL_CALL _proc. |
|
113 |
||
114 |
#endif |
|
115 |
||
116 |
||
117 |
#ifdef UNIX |
|
118 |
static void SdlAbort(int sig) |
|
119 |
{ |
|
120 |
/* Own hand-made parachute for the cases of failed assertions. */ |
|
121 |
SDL_CALL SDL_Quit(); |
|
122 |
} |
|
123 |
#endif |
|
124 |
||
125 |
||
126 |
static char *SdlOpen(uint32 x) |
|
127 |
{ |
|
128 |
#if defined(DYNAMICALLY_LOADED_SDL) && defined(WIN32) |
|
423 | 129 |
{ |
130 |
char *s = LoadSdlDLL(); |
|
131 |
if (s != NULL) return s; |
|
132 |
} |
|
0 | 133 |
#endif |
134 |
if (_sdl_usage++ == 0) { |
|
135 |
if (SDL_CALL SDL_Init(x) == -1) |
|
136 |
return SDL_CALL SDL_GetError(); |
|
423 | 137 |
} else if (x != 0) { |
0 | 138 |
if (SDL_CALL SDL_InitSubSystem(x) == -1) |
139 |
return SDL_CALL SDL_GetError(); |
|
140 |
} |
|
141 |
||
142 |
#ifdef UNIX |
|
143 |
signal(SIGABRT, SdlAbort); |
|
144 |
#endif |
|
145 |
||
146 |
return NULL; |
|
147 |
} |
|
148 |
||
149 |
static void SdlClose(uint32 x) |
|
150 |
{ |
|
423 | 151 |
if (x != 0) |
0 | 152 |
SDL_CALL SDL_QuitSubSystem(x); |
153 |
if (--_sdl_usage == 0) { |
|
154 |
SDL_CALL SDL_Quit(); |
|
423 | 155 |
#ifdef UNIX |
443
a45be556a111
(svn r652) Factorise special case for MorphOS regarding signal() handling
tron
parents:
435
diff
changeset
|
156 |
signal(SIGABRT, SIG_DFL); |
423 | 157 |
#endif |
0 | 158 |
} |
159 |
} |
|
160 |
||
161 |
static void SdlVideoMakeDirty(int left, int top, int width, int height) |
|
162 |
{ |
|
163 |
// printf("(%d,%d)-(%d,%d)\n", left, top, width, height); |
|
164 |
// _pixels_redrawn += width*height; |
|
165 |
if (_num_dirty_rects < MAX_DIRTY_RECTS) { |
|
166 |
_dirty_rects[_num_dirty_rects].x = left; |
|
167 |
_dirty_rects[_num_dirty_rects].y = top; |
|
168 |
_dirty_rects[_num_dirty_rects].w = width; |
|
169 |
_dirty_rects[_num_dirty_rects].h = height; |
|
170 |
} |
|
171 |
_num_dirty_rects++; |
|
172 |
} |
|
173 |
||
174 |
static SDL_Color pal[256]; |
|
175 |
||
423 | 176 |
static void UpdatePalette(uint start, uint end) |
177 |
{ |
|
0 | 178 |
uint i; |
179 |
byte *b; |
|
180 |
||
423 | 181 |
for (i = start, b = _cur_palette + start * 3; i != end; i++, b += 3) { |
0 | 182 |
pal[i].r = b[0]; |
183 |
pal[i].g = b[1]; |
|
184 |
pal[i].b = b[2]; |
|
185 |
pal[i].unused = b[3]; |
|
186 |
} |
|
187 |
||
188 |
SDL_CALL SDL_SetColors(_sdl_screen, pal, start, end); |
|
189 |
} |
|
190 |
||
423 | 191 |
static void InitPalette(void) |
192 |
{ |
|
0 | 193 |
UpdatePalette(0, 256); |
194 |
} |
|
195 |
||
423 | 196 |
static void CheckPaletteAnim(void) |
0 | 197 |
{ |
198 |
if(_pal_last_dirty != -1) { |
|
199 |
UpdatePalette(_pal_first_dirty, _pal_last_dirty + 1); |
|
200 |
_pal_last_dirty = -1; |
|
201 |
} |
|
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
202 |
} |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
203 |
|
423 | 204 |
static void DrawSurfaceToScreen(void) |
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
205 |
{ |
423 | 206 |
int n = _num_dirty_rects; |
207 |
if (n != 0) { |
|
0 | 208 |
_num_dirty_rects = 0; |
209 |
if (n > MAX_DIRTY_RECTS) |
|
210 |
SDL_CALL SDL_UpdateRect(_sdl_screen, 0, 0, 0, 0); |
|
423 | 211 |
else |
0 | 212 |
SDL_CALL SDL_UpdateRects(_sdl_screen, n, _dirty_rects); |
213 |
} |
|
214 |
} |
|
215 |
||
216 |
static int CDECL compare_res(const void *pa, const void *pb) |
|
217 |
{ |
|
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
196
diff
changeset
|
218 |
int x = ((const uint16*)pa)[0] - ((const uint16*)pb)[0]; |
423 | 219 |
if (x != 0) return x; |
222
b88456001397
(svn r223) -Fix: Const correctness and miscellaneous fixes. Thank you Tron for your diligent fixing of warnings (and some possibly bugs) (Tron)
darkvater
parents:
196
diff
changeset
|
220 |
return ((const uint16*)pa)[1] - ((const uint16*)pb)[1]; |
0 | 221 |
} |
222 |
||
223 |
static const uint16 default_resolutions[][2] = { |
|
423 | 224 |
{ 640, 480}, |
225 |
{ 800, 600}, |
|
226 |
{1024, 768}, |
|
227 |
{1152, 864}, |
|
228 |
{1280, 960}, |
|
229 |
{1280, 1024}, |
|
230 |
{1400, 1050}, |
|
231 |
{1600, 1200} |
|
0 | 232 |
}; |
233 |
||
423 | 234 |
static void GetVideoModes(void) |
235 |
{ |
|
0 | 236 |
int i; |
237 |
SDL_Rect **modes; |
|
238 |
||
239 |
modes = SDL_CALL SDL_ListModes(NULL, SDL_SWSURFACE + (_fullscreen ? SDL_FULLSCREEN : 0)); |
|
240 |
||
423 | 241 |
if (modes == NULL) |
245
4f6f81046437
(svn r246) -Fix: 64bit machine warnings for sdl.c (Tron)
darkvater
parents:
222
diff
changeset
|
242 |
error("sdl: no modes available"); |
119
82c0f4713cad
(svn r120) Fix: (SDL) same resolution was displayed more than once in game options
dominik
parents:
60
diff
changeset
|
243 |
|
245
4f6f81046437
(svn r246) -Fix: 64bit machine warnings for sdl.c (Tron)
darkvater
parents:
222
diff
changeset
|
244 |
_all_modes = (modes == (void*)-1); |
4f6f81046437
(svn r246) -Fix: 64bit machine warnings for sdl.c (Tron)
darkvater
parents:
222
diff
changeset
|
245 |
|
4f6f81046437
(svn r246) -Fix: 64bit machine warnings for sdl.c (Tron)
darkvater
parents:
222
diff
changeset
|
246 |
if (_all_modes) { |
0 | 247 |
// all modes available, put some default ones here |
248 |
memcpy(_resolutions, default_resolutions, sizeof(default_resolutions)); |
|
245
4f6f81046437
(svn r246) -Fix: 64bit machine warnings for sdl.c (Tron)
darkvater
parents:
222
diff
changeset
|
249 |
_num_resolutions = lengthof(default_resolutions); |
0 | 250 |
} else { |
128
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
251 |
int n = 0; |
423 | 252 |
for (i = 0; modes[i]; i++) { |
128
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
253 |
int w = modes[i]->w; |
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
254 |
int h = modes[i]->h; |
306
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
255 |
if (IS_INT_INSIDE(w, 640, MAX_SCREEN_WIDTH + 1) && |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
256 |
IS_INT_INSIDE(h, 480, MAX_SCREEN_HEIGHT + 1)) { |
128
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
257 |
int j; |
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
258 |
for (j = 0; j < n; ++j) |
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
259 |
if (_resolutions[j][0] == w && _resolutions[j][1] == h) |
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
260 |
break; |
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
261 |
if (j == n) { |
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
262 |
_resolutions[n][0] = w; |
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
263 |
_resolutions[n][1] = h; |
245
4f6f81046437
(svn r246) -Fix: 64bit machine warnings for sdl.c (Tron)
darkvater
parents:
222
diff
changeset
|
264 |
if (++n == lengthof(_resolutions)) break; |
128
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
265 |
} |
0 | 266 |
} |
267 |
} |
|
268 |
_num_resolutions = n; |
|
245
4f6f81046437
(svn r246) -Fix: 64bit machine warnings for sdl.c (Tron)
darkvater
parents:
222
diff
changeset
|
269 |
qsort(_resolutions, n, sizeof(_resolutions[0]), compare_res); |
0 | 270 |
} |
271 |
} |
|
272 |
||
273 |
static int GetAvailableVideoMode(int *w, int *h) |
|
274 |
{ |
|
275 |
int i; |
|
306
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
276 |
int best; |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
277 |
uint delta; |
0 | 278 |
|
279 |
// all modes available? |
|
280 |
if (_all_modes) |
|
281 |
return 1; |
|
282 |
||
283 |
// is the wanted mode among the available modes? |
|
306
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
284 |
for (i = 0; i != _num_resolutions; i++) { |
0 | 285 |
if(*w == _resolutions[i][0] && *h == _resolutions[i][1]) |
286 |
return 1; |
|
287 |
} |
|
288 |
||
306
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
289 |
// use the closest possible resolution |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
290 |
best = 0; |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
291 |
delta = abs((_resolutions[0][0] - *w) * (_resolutions[0][1] - *h)); |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
292 |
for (i = 1; i != _num_resolutions; ++i) { |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
293 |
uint newdelta = abs((_resolutions[i][0] - *w) * (_resolutions[i][1] - *h)); |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
294 |
if (newdelta < delta) { |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
295 |
best = i; |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
296 |
delta = newdelta; |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
297 |
} |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
298 |
} |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
299 |
*w = _resolutions[best][0]; |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
300 |
*h = _resolutions[best][1]; |
0 | 301 |
return 2; |
302 |
} |
|
303 |
||
304 |
static bool CreateMainSurface(int w, int h) |
|
305 |
{ |
|
306 |
SDL_Surface *newscreen; |
|
307 |
||
308 |
GetAvailableVideoMode(&w, &h); |
|
309 |
||
310 |
DEBUG(misc, 0) ("sdl: using mode %dx%d", w, h); |
|
311 |
||
36
c1b3b884584e
(svn r37) Do not change to HWSURFACE since it's not working!
darkvater
parents:
34
diff
changeset
|
312 |
// DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK |
423 | 313 |
newscreen = SDL_CALL SDL_SetVideoMode(w, h, 8, SDL_SWSURFACE | SDL_HWPALETTE | (_fullscreen ? SDL_FULLSCREEN : SDL_RESIZABLE)); |
314 |
if (newscreen == NULL) |
|
0 | 315 |
return false; |
316 |
||
306
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
317 |
_screen.width = newscreen->w; |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
318 |
_screen.height = newscreen->h; |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
319 |
_screen.pitch = newscreen->pitch; |
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
320 |
|
0 | 321 |
_sdl_screen = newscreen; |
322 |
InitPalette(); |
|
323 |
||
324 |
SDL_CALL SDL_WM_SetCaption("OpenTTD", "OpenTTD"); |
|
325 |
SDL_CALL SDL_ShowCursor(0); |
|
326 |
||
306
c44133836566
(svn r312) -Fix: [926105] ctrl + d bug. Longest outstanding bug has been fixed \o/ 2004-03-30 (Tron)
darkvater
parents:
302
diff
changeset
|
327 |
GameSizeChanged(); |
0 | 328 |
|
329 |
return true; |
|
330 |
} |
|
331 |
||
423 | 332 |
typedef struct VkMapping { |
0 | 333 |
uint16 vk_from; |
334 |
byte vk_count; |
|
335 |
byte map_to; |
|
336 |
} VkMapping; |
|
337 |
||
423 | 338 |
#define AS(x, z) {x, 0, z} |
339 |
#define AM(x, y, z, w) {x, y - x, z} |
|
0 | 340 |
|
341 |
static const VkMapping _vk_mapping[] = { |
|
342 |
// Pageup stuff + up/down |
|
423 | 343 |
AM(SDLK_PAGEUP, SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN), |
0 | 344 |
AS(SDLK_UP, WKC_UP), |
345 |
AS(SDLK_DOWN, WKC_DOWN), |
|
346 |
AS(SDLK_LEFT, WKC_LEFT), |
|
347 |
AS(SDLK_RIGHT, WKC_RIGHT), |
|
348 |
||
349 |
AS(SDLK_HOME, WKC_HOME), |
|
350 |
AS(SDLK_END, WKC_END), |
|
351 |
||
352 |
AS(SDLK_INSERT, WKC_INSERT), |
|
353 |
AS(SDLK_DELETE, WKC_DELETE), |
|
354 |
||
355 |
// Map letters & digits |
|
423 | 356 |
AM(SDLK_a, SDLK_z, 'A', 'Z'), |
357 |
AM(SDLK_0, SDLK_9, '0', '9'), |
|
0 | 358 |
|
359 |
AS(SDLK_ESCAPE, WKC_ESC), |
|
424
4c7808af7526
(svn r623) -Feature: [ 1066504 ] Pause key pauses the game
tron
parents:
423
diff
changeset
|
360 |
AS(SDLK_PAUSE, WKC_PAUSE), |
0 | 361 |
AS(SDLK_BACKSPACE, WKC_BACKSPACE), |
362 |
||
363 |
AS(SDLK_SPACE, WKC_SPACE), |
|
364 |
AS(SDLK_RETURN, WKC_RETURN), |
|
365 |
AS(SDLK_TAB, WKC_TAB), |
|
366 |
||
367 |
// Function keys |
|
423 | 368 |
AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12), |
0 | 369 |
|
370 |
// Numeric part. |
|
371 |
// What is the virtual keycode for numeric enter?? |
|
423 | 372 |
AM(SDLK_KP0, SDLK_KP9, WKC_NUM_0, WKC_NUM_9), |
0 | 373 |
AS(SDLK_KP_DIVIDE, WKC_NUM_DIV), |
374 |
AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL), |
|
375 |
AS(SDLK_KP_MINUS, WKC_NUM_MINUS), |
|
376 |
AS(SDLK_KP_PLUS, WKC_NUM_PLUS), |
|
377 |
AS(SDLK_KP_ENTER, WKC_NUM_ENTER), |
|
423 | 378 |
AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL) |
0 | 379 |
}; |
380 |
||
381 |
static uint32 ConvertSdlKeyIntoMy(SDL_keysym *sym) |
|
382 |
{ |
|
423 | 383 |
const VkMapping *map; |
384 |
uint key = 0; |
|
385 |
for (map = _vk_mapping; map != endof(_vk_mapping); ++map) { |
|
386 |
if ((uint)(sym->sym - map->vk_from) <= map->vk_count) { |
|
387 |
key = sym->sym - map->vk_from + map->map_to; |
|
0 | 388 |
break; |
389 |
} |
|
390 |
} |
|
129
df1a60bc0d70
(svn r130) Change: hotkey for console is Backquote (the key left to '1', regardless of keyboard layout
dominik
parents:
128
diff
changeset
|
391 |
|
df1a60bc0d70
(svn r130) Change: hotkey for console is Backquote (the key left to '1', regardless of keyboard layout
dominik
parents:
128
diff
changeset
|
392 |
// check scancode for BACKQUOTE key, because we want the key left of "1", not anything else (on non-US keyboards) |
135
638fb31434eb
(svn r136) -Feature/Fix: Console Rev #2 and WKC_BACKQUOTE this patch adds new features to the ingame console and inserts a new keymanagement for windows pcs... (sign_de)
darkvater
parents:
129
diff
changeset
|
393 |
#if defined(WIN32) |
638fb31434eb
(svn r136) -Feature/Fix: Console Rev #2 and WKC_BACKQUOTE this patch adds new features to the ingame console and inserts a new keymanagement for windows pcs... (sign_de)
darkvater
parents:
129
diff
changeset
|
394 |
if (sym->scancode == 41) key |= WKC_BACKQUOTE; |
423 | 395 |
#elif defined(__APPLE__) |
396 |
if (sym->scancode == 10) key |= WKC_BACKQUOTE; |
|
397 |
#elif defined(__MORPHOS__) |
|
398 |
if (sym->scancode == 0) key |= WKC_BACKQUOTE; // yes, that key is code '0' under MorphOS :) |
|
435
62863eb5148a
(svn r637) Add keycode for the key to open the console on BeOS
tron
parents:
424
diff
changeset
|
399 |
#elif defined(__BEOS__) |
62863eb5148a
(svn r637) Add keycode for the key to open the console on BeOS
tron
parents:
424
diff
changeset
|
400 |
if (sym->scancode == 17) key |= WKC_BACKQUOTE; |
135
638fb31434eb
(svn r136) -Feature/Fix: Console Rev #2 and WKC_BACKQUOTE this patch adds new features to the ingame console and inserts a new keymanagement for windows pcs... (sign_de)
darkvater
parents:
129
diff
changeset
|
401 |
#else |
423 | 402 |
if (sym->scancode == 49) key |= WKC_BACKQUOTE; |
135
638fb31434eb
(svn r136) -Feature/Fix: Console Rev #2 and WKC_BACKQUOTE this patch adds new features to the ingame console and inserts a new keymanagement for windows pcs... (sign_de)
darkvater
parents:
129
diff
changeset
|
403 |
#endif |
0 | 404 |
// META are the command keys on mac |
405 |
if (sym->mod & KMOD_META) key |= WKC_META; |
|
406 |
if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT; |
|
407 |
if (sym->mod & KMOD_CTRL) key |= WKC_CTRL; |
|
408 |
if (sym->mod & KMOD_ALT) key |= WKC_ALT; |
|
423 | 409 |
// these two lines really help porting hotkey combos. Uncomment to use -- Bjarni |
167 | 410 |
//printf("scancode character pressed %d\n", sym->scancode); |
411 |
//printf("unicode character pressed %d\n", sym->unicode); |
|
0 | 412 |
return (key << 16) + sym->unicode; |
413 |
} |
|
414 |
||
423 | 415 |
static int PollEvent(void) |
416 |
{ |
|
0 | 417 |
SDL_Event ev; |
418 |
||
419 |
if (!SDL_CALL SDL_PollEvent(&ev)) |
|
420 |
return -2; |
|
421 |
||
423 | 422 |
switch (ev.type) { |
0 | 423 |
case SDL_MOUSEMOTION: |
424 |
if (_cursor.fix_at) { |
|
425 |
int dx = ev.motion.x - _cursor.pos.x; |
|
426 |
int dy = ev.motion.y - _cursor.pos.y; |
|
427 |
if (dx != 0 || dy != 0) { |
|
428 |
_cursor.delta.x += dx; |
|
429 |
_cursor.delta.y += dy; |
|
430 |
SDL_CALL SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y); |
|
431 |
} |
|
432 |
} else { |
|
433 |
_cursor.delta.x = ev.motion.x - _cursor.pos.x; |
|
434 |
_cursor.delta.y = ev.motion.y - _cursor.pos.y; |
|
435 |
_cursor.pos.x = ev.motion.x; |
|
436 |
_cursor.pos.y = ev.motion.y; |
|
437 |
_cursor.dirty = true; |
|
438 |
} |
|
439 |
break; |
|
440 |
||
441 |
case SDL_MOUSEBUTTONDOWN: |
|
442 |
if (_rightclick_emulate && (SDL_CALL SDL_GetModState() & (KMOD_LCTRL | KMOD_RCTRL))) |
|
443 |
ev.button.button = SDL_BUTTON_RIGHT; |
|
444 |
||
423 | 445 |
switch (ev.button.button) { |
446 |
case SDL_BUTTON_LEFT: |
|
447 |
_left_button_down = true; |
|
448 |
break; |
|
449 |
case SDL_BUTTON_RIGHT: |
|
450 |
_right_button_down = true; |
|
451 |
_right_button_clicked = true; |
|
452 |
break; |
|
453 |
#if !defined(WIN32) |
|
454 |
case SDL_BUTTON_WHEELUP: |
|
455 |
_cursor.wheel--; |
|
456 |
break; |
|
457 |
case SDL_BUTTON_WHEELDOWN: |
|
458 |
_cursor.wheel++; |
|
459 |
break; |
|
460 |
#endif |
|
461 |
default: |
|
462 |
break; |
|
0 | 463 |
} |
464 |
break; |
|
465 |
||
466 |
case SDL_MOUSEBUTTONUP: |
|
467 |
if (_rightclick_emulate) { |
|
468 |
_right_button_down = false; |
|
469 |
_left_button_down = false; |
|
470 |
_left_button_clicked = false; |
|
471 |
} else if (ev.button.button == SDL_BUTTON_LEFT) { |
|
472 |
_left_button_down = false; |
|
473 |
_left_button_clicked = false; |
|
474 |
} else if (ev.button.button == SDL_BUTTON_RIGHT) { |
|
475 |
_right_button_down = false; |
|
476 |
} |
|
477 |
break; |
|
478 |
||
479 |
case SDL_QUIT: |
|
291
8339f2fe911d
(svn r297) Fix (SDL): Added a confirmation dialog when quitting the game
dominik
parents:
245
diff
changeset
|
480 |
// do not ask to quit on the main screen |
423 | 481 |
if (_game_mode != GM_MENU) |
291
8339f2fe911d
(svn r297) Fix (SDL): Added a confirmation dialog when quitting the game
dominik
parents:
245
diff
changeset
|
482 |
AskExitGame(); |
8339f2fe911d
(svn r297) Fix (SDL): Added a confirmation dialog when quitting the game
dominik
parents:
245
diff
changeset
|
483 |
else |
8339f2fe911d
(svn r297) Fix (SDL): Added a confirmation dialog when quitting the game
dominik
parents:
245
diff
changeset
|
484 |
return ML_QUIT; |
8339f2fe911d
(svn r297) Fix (SDL): Added a confirmation dialog when quitting the game
dominik
parents:
245
diff
changeset
|
485 |
break; |
0 | 486 |
|
487 |
case SDL_KEYDOWN: |
|
423 | 488 |
if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) && |
489 |
(ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) { |
|
0 | 490 |
_fullscreen ^= true; |
491 |
GetVideoModes(); |
|
492 |
CreateMainSurface(_screen.width, _screen.height); |
|
493 |
MarkWholeScreenDirty(); |
|
494 |
} else { |
|
495 |
_pressed_key = ConvertSdlKeyIntoMy(&ev.key.keysym); |
|
496 |
} |
|
497 |
break; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
498 |
|
0 | 499 |
case SDL_VIDEORESIZE: { |
423 | 500 |
int w = clamp(ev.resize.w, 64, MAX_SCREEN_WIDTH); |
501 |
int h = clamp(ev.resize.h, 64, MAX_SCREEN_HEIGHT); |
|
502 |
ChangeResInGame(w, h); |
|
503 |
break; |
|
504 |
} |
|
0 | 505 |
} |
506 |
return -1; |
|
507 |
} |
|
508 |
||
509 |
static const char *SdlVideoStart(char **parm) |
|
510 |
{ |
|
423 | 511 |
char buf[30]; |
0 | 512 |
|
423 | 513 |
const char *s = SdlOpen(SDL_INIT_VIDEO); |
514 |
if (s != NULL) return s; |
|
0 | 515 |
|
516 |
SDL_CALL SDL_VideoDriverName(buf, 30); |
|
517 |
DEBUG(misc, 0) ("sdl: using driver '%s'", buf); |
|
518 |
||
519 |
GetVideoModes(); |
|
520 |
CreateMainSurface(_cur_resolution[0], _cur_resolution[1]); |
|
521 |
MarkWholeScreenDirty(); |
|
522 |
||
523 |
SDL_CALL SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); |
|
524 |
SDL_CALL SDL_EnableUNICODE(1); |
|
525 |
return NULL; |
|
526 |
} |
|
527 |
||
423 | 528 |
static void SdlVideoStop(void) |
0 | 529 |
{ |
530 |
SdlClose(SDL_INIT_VIDEO); |
|
531 |
} |
|
532 |
||
423 | 533 |
static int SdlVideoMainLoop(void) |
0 | 534 |
{ |
423 | 535 |
uint32 next_tick = SDL_CALL SDL_GetTicks() + 30; |
536 |
uint32 cur_ticks; |
|
537 |
uint32 pal_tick = 0; |
|
0 | 538 |
int i; |
539 |
uint32 mod; |
|
540 |
int numkeys; |
|
541 |
Uint8 *keys; |
|
542 |
||
423 | 543 |
for (;;) { |
0 | 544 |
InteractiveRandom(); // randomness |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
545 |
|
423 | 546 |
while ((i = PollEvent()) == -1) {} |
547 |
if (i >= 0) return i; |
|
0 | 548 |
|
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
549 |
if (_exit_game) return ML_QUIT; |
0 | 550 |
|
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
551 |
mod = SDL_CALL SDL_GetModState(); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
552 |
keys = SDL_CALL SDL_GetKeyState(&numkeys); |
0 | 553 |
#if defined(_DEBUG) |
423 | 554 |
if (_shift_pressed) |
0 | 555 |
#else |
423 | 556 |
if (keys[SDLK_TAB]) |
0 | 557 |
#endif |
423 | 558 |
{ |
196
c4c33c0764a0
(svn r197) -Fix: Disable Fast Forward in network games visually (didn't work anyways, just showed graphical output)
darkvater
parents:
193
diff
changeset
|
559 |
if (!_networking) _fast_forward |= 2; |
423 | 560 |
} else if (_fast_forward & 2) { |
0 | 561 |
_fast_forward = 0; |
562 |
} |
|
563 |
||
423 | 564 |
cur_ticks = SDL_CALL SDL_GetTicks(); |
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
565 |
if ((_fast_forward && !_pause) || cur_ticks > next_tick) |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
566 |
next_tick = cur_ticks; |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
567 |
|
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
568 |
if (cur_ticks == next_tick) { |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
569 |
next_tick += 30; |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
570 |
|
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
571 |
_ctrl_pressed = !!(mod & (KMOD_LCTRL | KMOD_RCTRL)); |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
572 |
_shift_pressed = !!(mod & (KMOD_LSHIFT | KMOD_RSHIFT)); |
302
d028f2803157
(svn r308) Added debug hotkey 'caps lock' for sdl builds as well
dominik
parents:
291
diff
changeset
|
573 |
_dbg_screen_rect = !!(mod & KMOD_CAPS); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
574 |
|
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
575 |
// determine which directional keys are down |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
576 |
_dirkeys = |
423 | 577 |
(keys[SDLK_LEFT] ? 1 : 0) | |
578 |
(keys[SDLK_UP] ? 2 : 0) | |
|
579 |
(keys[SDLK_RIGHT] ? 4 : 0) | |
|
580 |
(keys[SDLK_DOWN] ? 8 : 0); |
|
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
581 |
GameLoop(); |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
582 |
|
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
583 |
_screen.dst_ptr = _sdl_screen->pixels; |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
584 |
UpdateWindows(); |
423 | 585 |
if (++pal_tick > 4) { |
586 |
CheckPaletteAnim(); |
|
587 |
pal_tick = 1; |
|
0 | 588 |
} |
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
589 |
DrawSurfaceToScreen(); |
0 | 590 |
} else { |
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
591 |
SDL_CALL SDL_Delay(1); |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
592 |
_screen.dst_ptr = _sdl_screen->pixels; |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
593 |
DrawMouseCursor(); |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
594 |
DrawSurfaceToScreen(); |
0 | 595 |
} |
596 |
} |
|
597 |
} |
|
598 |
||
599 |
static bool SdlVideoChangeRes(int w, int h) |
|
600 |
{ |
|
601 |
// see if the mode is available |
|
602 |
if (GetAvailableVideoMode(&w, &h) != 1) |
|
603 |
return false; |
|
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
604 |
|
0 | 605 |
CreateMainSurface(w, h); |
606 |
return true; |
|
607 |
} |
|
608 |
||
609 |
const HalVideoDriver _sdl_video_driver = { |
|
610 |
SdlVideoStart, |
|
611 |
SdlVideoStop, |
|
612 |
SdlVideoMakeDirty, |
|
613 |
SdlVideoMainLoop, |
|
614 |
SdlVideoChangeRes, |
|
615 |
}; |
|
616 |
||
617 |
static void CDECL fill_sound_buffer(void *userdata, Uint8 *stream, int len) |
|
618 |
{ |
|
423 | 619 |
MxMixSamples(_mixer, stream, len / 4); |
0 | 620 |
} |
621 |
||
622 |
static char *SdlSoundStart(char **parm) |
|
623 |
{ |
|
624 |
SDL_AudioSpec spec; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
625 |
|
423 | 626 |
char *s = SdlOpen(SDL_INIT_AUDIO); |
627 |
if (s != NULL) return s; |
|
628 |
||
0 | 629 |
spec.freq = GetDriverParamInt(parm, "hz", 11025); |
630 |
spec.format = AUDIO_S16SYS; |
|
631 |
spec.channels = 2; |
|
632 |
spec.samples = 512; |
|
423 | 633 |
spec.callback = fill_sound_buffer; |
0 | 634 |
SDL_CALL SDL_OpenAudio(&spec, &spec); |
635 |
SDL_CALL SDL_PauseAudio(0); |
|
636 |
return NULL; |
|
637 |
} |
|
638 |
||
423 | 639 |
static void SdlSoundStop(void) |
0 | 640 |
{ |
641 |
SDL_CALL SDL_CloseAudio(); |
|
642 |
SdlClose(SDL_INIT_AUDIO); |
|
643 |
} |
|
644 |
||
645 |
const HalSoundDriver _sdl_sound_driver = { |
|
646 |
SdlSoundStart, |
|
647 |
SdlSoundStop, |
|
648 |
}; |
|
649 |
||
650 |
||
423 | 651 |
#if 0 /* XXX what the heck is that? */ |
0 | 652 |
#include "viewport.h" |
653 |
void redsq_debug(int tile) |
|
654 |
{ |
|
655 |
_thd.redsq = tile; |
|
656 |
MarkWholeScreenDirty(); |
|
657 |
_screen.dst_ptr = _sdl_screen->pixels; |
|
658 |
UpdateWindows(); |
|
659 |
||
660 |
SdlVideoMakeDirty(0,0,_screen.width,_screen.height); |
|
661 |
DrawSurfaceToScreen(); |
|
662 |
} |
|
663 |
||
410 | 664 |
static void DbgRedraw() |
0 | 665 |
{ |
666 |
SdlVideoMakeDirty(0,0,_screen.width,_screen.height); |
|
667 |
DrawSurfaceToScreen(); |
|
668 |
} |
|
423 | 669 |
#endif |
670 |
||
0 | 671 |
#endif // WITH_SDL |