author | tron |
Mon, 15 Nov 2004 09:05:06 +0000 | |
changeset 423 | 9e28d68fa333 |
parent 410 | 8de2aaf20800 |
child 424 | 4c7808af7526 |
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> |
|
14 |
#endif |
|
15 |
||
16 |
#define DYNAMICALLY_LOADED_SDL |
|
17 |
||
18 |
static SDL_Surface *_sdl_screen; |
|
19 |
static int _sdl_usage; |
|
20 |
static bool _all_modes; |
|
21 |
||
22 |
#define MAX_DIRTY_RECTS 100 |
|
23 |
static SDL_Rect _dirty_rects[MAX_DIRTY_RECTS]; |
|
24 |
static int _num_dirty_rects; |
|
25 |
||
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
26 |
#define SDL_CALL |
0 | 27 |
|
28 |
#if defined(DYNAMICALLY_LOADED_SDL) && defined(WIN32) |
|
29 |
||
30 |
bool LoadLibraryList(void **proc, const char *dll); |
|
31 |
||
32 |
typedef struct { |
|
33 |
int (SDLCALL *SDL_Init)(Uint32); |
|
34 |
int (SDLCALL *SDL_InitSubSystem)(Uint32); |
|
35 |
char *(SDLCALL *SDL_GetError)(); |
|
36 |
void (SDLCALL *SDL_QuitSubSystem)(Uint32); |
|
37 |
void (SDLCALL *SDL_UpdateRect)(SDL_Surface *, Sint32, Sint32, Uint32, Uint32); |
|
38 |
void (SDLCALL *SDL_UpdateRects)(SDL_Surface *, int, SDL_Rect *); |
|
39 |
int (SDLCALL *SDL_SetColors)(SDL_Surface *, SDL_Color *, int, int); |
|
40 |
void (SDLCALL *SDL_WM_SetCaption)(const char *, const char *); |
|
41 |
int (SDLCALL *SDL_ShowCursor)(int); |
|
42 |
void (SDLCALL *SDL_FreeSurface)(SDL_Surface *); |
|
43 |
int (SDLCALL *SDL_PollEvent)(SDL_Event *); |
|
44 |
void (SDLCALL *SDL_WarpMouse)(Uint16, Uint16); |
|
45 |
uint32 (SDLCALL *SDL_GetTicks)(); |
|
46 |
int (SDLCALL *SDL_OpenAudio)(SDL_AudioSpec *, SDL_AudioSpec*); |
|
47 |
void (SDLCALL *SDL_PauseAudio)(int); |
|
48 |
void (SDLCALL *SDL_CloseAudio)(); |
|
49 |
int (SDLCALL *SDL_LockSurface)(SDL_Surface*); |
|
50 |
void (SDLCALL *SDL_UnlockSurface)(SDL_Surface*); |
|
51 |
SDLMod (SDLCALL *SDL_GetModState)(); |
|
52 |
void (SDLCALL *SDL_Delay)(Uint32); |
|
53 |
void (SDLCALL *SDL_Quit)(); |
|
423 | 54 |
SDL_Surface *(SDLCALL *SDL_SetVideoMode)(int, int, int, Uint32); |
55 |
int (SDLCALL *SDL_EnableKeyRepeat)(int, int); |
|
0 | 56 |
void (SDLCALL *SDL_EnableUNICODE)(int); |
57 |
void (SDLCALL *SDL_VideoDriverName)(char *, int); |
|
58 |
SDL_Rect **(SDLCALL *SDL_ListModes)(void *, int); |
|
59 |
Uint8 *(SDLCALL *SDL_GetKeyState)(int *); |
|
60 |
} SDLProcs; |
|
61 |
||
62 |
#define M(x) x "\0" |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
63 |
static const char sdl_files[] = |
0 | 64 |
M("sdl.dll") |
65 |
M("SDL_Init") |
|
66 |
M("SDL_InitSubSystem") |
|
67 |
M("SDL_GetError") |
|
68 |
M("SDL_QuitSubSystem") |
|
69 |
M("SDL_UpdateRect") |
|
70 |
M("SDL_UpdateRects") |
|
71 |
M("SDL_SetColors") |
|
72 |
M("SDL_WM_SetCaption") |
|
73 |
M("SDL_ShowCursor") |
|
74 |
M("SDL_FreeSurface") |
|
75 |
M("SDL_PollEvent") |
|
76 |
M("SDL_WarpMouse") |
|
77 |
M("SDL_GetTicks") |
|
78 |
M("SDL_OpenAudio") |
|
79 |
M("SDL_PauseAudio") |
|
80 |
M("SDL_CloseAudio") |
|
81 |
M("SDL_LockSurface") |
|
82 |
M("SDL_UnlockSurface") |
|
83 |
M("SDL_GetModState") |
|
84 |
M("SDL_Delay") |
|
85 |
M("SDL_Quit") |
|
86 |
M("SDL_SetVideoMode") |
|
87 |
M("SDL_EnableKeyRepeat") |
|
88 |
M("SDL_EnableUNICODE") |
|
89 |
M("SDL_VideoDriverName") |
|
90 |
M("SDL_ListModes") |
|
91 |
M("SDL_GetKeyState") |
|
92 |
M("") |
|
93 |
; |
|
94 |
#undef M |
|
95 |
||
96 |
static SDLProcs _proc; |
|
97 |
||
423 | 98 |
static char *LoadSdlDLL(void) |
0 | 99 |
{ |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
100 |
if (_proc.SDL_Init != NULL) |
0 | 101 |
return NULL; |
102 |
if (!LoadLibraryList((void**)&_proc, sdl_files)) |
|
103 |
return "Unable to load sdl.dll"; |
|
104 |
return NULL; |
|
105 |
} |
|
106 |
||
107 |
#undef SDL_CALL |
|
108 |
#define SDL_CALL _proc. |
|
109 |
||
110 |
#endif |
|
111 |
||
112 |
||
113 |
#ifdef UNIX |
|
114 |
static void SdlAbort(int sig) |
|
115 |
{ |
|
116 |
/* Own hand-made parachute for the cases of failed assertions. */ |
|
117 |
SDL_CALL SDL_Quit(); |
|
118 |
} |
|
119 |
#endif |
|
120 |
||
121 |
||
122 |
static char *SdlOpen(uint32 x) |
|
123 |
{ |
|
124 |
#if defined(DYNAMICALLY_LOADED_SDL) && defined(WIN32) |
|
423 | 125 |
{ |
126 |
char *s = LoadSdlDLL(); |
|
127 |
if (s != NULL) return s; |
|
128 |
} |
|
0 | 129 |
#endif |
130 |
if (_sdl_usage++ == 0) { |
|
131 |
if (SDL_CALL SDL_Init(x) == -1) |
|
132 |
return SDL_CALL SDL_GetError(); |
|
423 | 133 |
} else if (x != 0) { |
0 | 134 |
if (SDL_CALL SDL_InitSubSystem(x) == -1) |
135 |
return SDL_CALL SDL_GetError(); |
|
136 |
} |
|
137 |
||
138 |
#ifdef UNIX |
|
139 |
signal(SIGABRT, SdlAbort); |
|
140 |
#endif |
|
141 |
||
142 |
return NULL; |
|
143 |
} |
|
144 |
||
145 |
static void SdlClose(uint32 x) |
|
146 |
{ |
|
423 | 147 |
if (x != 0) |
0 | 148 |
SDL_CALL SDL_QuitSubSystem(x); |
149 |
if (--_sdl_usage == 0) { |
|
150 |
SDL_CALL SDL_Quit(); |
|
423 | 151 |
#ifdef UNIX |
152 |
#ifndef __MORPHOS__ |
|
153 |
signal(SIGABRT, SIG_DFL); |
|
154 |
#else |
|
155 |
signal(SIGABRT, (void (*)(int))0); |
|
156 |
#endif |
|
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), |
|
360 |
AS(SDLK_BACKSPACE, WKC_BACKSPACE), |
|
361 |
||
362 |
AS(SDLK_SPACE, WKC_SPACE), |
|
363 |
AS(SDLK_RETURN, WKC_RETURN), |
|
364 |
AS(SDLK_TAB, WKC_TAB), |
|
365 |
||
366 |
// Function keys |
|
423 | 367 |
AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12), |
0 | 368 |
|
369 |
// Numeric part. |
|
370 |
// What is the virtual keycode for numeric enter?? |
|
423 | 371 |
AM(SDLK_KP0, SDLK_KP9, WKC_NUM_0, WKC_NUM_9), |
0 | 372 |
AS(SDLK_KP_DIVIDE, WKC_NUM_DIV), |
373 |
AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL), |
|
374 |
AS(SDLK_KP_MINUS, WKC_NUM_MINUS), |
|
375 |
AS(SDLK_KP_PLUS, WKC_NUM_PLUS), |
|
376 |
AS(SDLK_KP_ENTER, WKC_NUM_ENTER), |
|
423 | 377 |
AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL) |
0 | 378 |
}; |
379 |
||
380 |
static uint32 ConvertSdlKeyIntoMy(SDL_keysym *sym) |
|
381 |
{ |
|
423 | 382 |
const VkMapping *map; |
383 |
uint key = 0; |
|
384 |
for (map = _vk_mapping; map != endof(_vk_mapping); ++map) { |
|
385 |
if ((uint)(sym->sym - map->vk_from) <= map->vk_count) { |
|
386 |
key = sym->sym - map->vk_from + map->map_to; |
|
0 | 387 |
break; |
388 |
} |
|
389 |
} |
|
129
df1a60bc0d70
(svn r130) Change: hotkey for console is Backquote (the key left to '1', regardless of keyboard layout
dominik
parents:
128
diff
changeset
|
390 |
|
df1a60bc0d70
(svn r130) Change: hotkey for console is Backquote (the key left to '1', regardless of keyboard layout
dominik
parents:
128
diff
changeset
|
391 |
// 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
|
392 |
#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
|
393 |
if (sym->scancode == 41) key |= WKC_BACKQUOTE; |
423 | 394 |
#elif defined(__APPLE__) |
395 |
if (sym->scancode == 10) key |= WKC_BACKQUOTE; |
|
396 |
#elif defined(__MORPHOS__) |
|
397 |
if (sym->scancode == 0) key |= WKC_BACKQUOTE; // yes, that key is code '0' under MorphOS :) |
|
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
|
398 |
#else |
423 | 399 |
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
|
400 |
#endif |
0 | 401 |
// META are the command keys on mac |
402 |
if (sym->mod & KMOD_META) key |= WKC_META; |
|
403 |
if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT; |
|
404 |
if (sym->mod & KMOD_CTRL) key |= WKC_CTRL; |
|
405 |
if (sym->mod & KMOD_ALT) key |= WKC_ALT; |
|
423 | 406 |
// these two lines really help porting hotkey combos. Uncomment to use -- Bjarni |
167 | 407 |
//printf("scancode character pressed %d\n", sym->scancode); |
408 |
//printf("unicode character pressed %d\n", sym->unicode); |
|
0 | 409 |
return (key << 16) + sym->unicode; |
410 |
} |
|
411 |
||
423 | 412 |
static int PollEvent(void) |
413 |
{ |
|
0 | 414 |
SDL_Event ev; |
415 |
||
416 |
if (!SDL_CALL SDL_PollEvent(&ev)) |
|
417 |
return -2; |
|
418 |
||
423 | 419 |
switch (ev.type) { |
0 | 420 |
case SDL_MOUSEMOTION: |
421 |
if (_cursor.fix_at) { |
|
422 |
int dx = ev.motion.x - _cursor.pos.x; |
|
423 |
int dy = ev.motion.y - _cursor.pos.y; |
|
424 |
if (dx != 0 || dy != 0) { |
|
425 |
_cursor.delta.x += dx; |
|
426 |
_cursor.delta.y += dy; |
|
427 |
SDL_CALL SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y); |
|
428 |
} |
|
429 |
} else { |
|
430 |
_cursor.delta.x = ev.motion.x - _cursor.pos.x; |
|
431 |
_cursor.delta.y = ev.motion.y - _cursor.pos.y; |
|
432 |
_cursor.pos.x = ev.motion.x; |
|
433 |
_cursor.pos.y = ev.motion.y; |
|
434 |
_cursor.dirty = true; |
|
435 |
} |
|
436 |
break; |
|
437 |
||
438 |
case SDL_MOUSEBUTTONDOWN: |
|
439 |
if (_rightclick_emulate && (SDL_CALL SDL_GetModState() & (KMOD_LCTRL | KMOD_RCTRL))) |
|
440 |
ev.button.button = SDL_BUTTON_RIGHT; |
|
441 |
||
423 | 442 |
switch (ev.button.button) { |
443 |
case SDL_BUTTON_LEFT: |
|
444 |
_left_button_down = true; |
|
445 |
break; |
|
446 |
case SDL_BUTTON_RIGHT: |
|
447 |
_right_button_down = true; |
|
448 |
_right_button_clicked = true; |
|
449 |
break; |
|
450 |
#if !defined(WIN32) |
|
451 |
case SDL_BUTTON_WHEELUP: |
|
452 |
_cursor.wheel--; |
|
453 |
break; |
|
454 |
case SDL_BUTTON_WHEELDOWN: |
|
455 |
_cursor.wheel++; |
|
456 |
break; |
|
457 |
#endif |
|
458 |
default: |
|
459 |
break; |
|
0 | 460 |
} |
461 |
break; |
|
462 |
||
463 |
case SDL_MOUSEBUTTONUP: |
|
464 |
if (_rightclick_emulate) { |
|
465 |
_right_button_down = false; |
|
466 |
_left_button_down = false; |
|
467 |
_left_button_clicked = false; |
|
468 |
} else if (ev.button.button == SDL_BUTTON_LEFT) { |
|
469 |
_left_button_down = false; |
|
470 |
_left_button_clicked = false; |
|
471 |
} else if (ev.button.button == SDL_BUTTON_RIGHT) { |
|
472 |
_right_button_down = false; |
|
473 |
} |
|
474 |
break; |
|
475 |
||
476 |
case SDL_QUIT: |
|
291
8339f2fe911d
(svn r297) Fix (SDL): Added a confirmation dialog when quitting the game
dominik
parents:
245
diff
changeset
|
477 |
// do not ask to quit on the main screen |
423 | 478 |
if (_game_mode != GM_MENU) |
291
8339f2fe911d
(svn r297) Fix (SDL): Added a confirmation dialog when quitting the game
dominik
parents:
245
diff
changeset
|
479 |
AskExitGame(); |
8339f2fe911d
(svn r297) Fix (SDL): Added a confirmation dialog when quitting the game
dominik
parents:
245
diff
changeset
|
480 |
else |
8339f2fe911d
(svn r297) Fix (SDL): Added a confirmation dialog when quitting the game
dominik
parents:
245
diff
changeset
|
481 |
return ML_QUIT; |
8339f2fe911d
(svn r297) Fix (SDL): Added a confirmation dialog when quitting the game
dominik
parents:
245
diff
changeset
|
482 |
break; |
0 | 483 |
|
484 |
case SDL_KEYDOWN: |
|
423 | 485 |
if ((ev.key.keysym.mod & (KMOD_ALT | KMOD_META)) && |
486 |
(ev.key.keysym.sym == SDLK_RETURN || ev.key.keysym.sym == SDLK_f)) { |
|
0 | 487 |
_fullscreen ^= true; |
488 |
GetVideoModes(); |
|
489 |
CreateMainSurface(_screen.width, _screen.height); |
|
490 |
MarkWholeScreenDirty(); |
|
491 |
} else { |
|
492 |
_pressed_key = ConvertSdlKeyIntoMy(&ev.key.keysym); |
|
493 |
} |
|
494 |
break; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
495 |
|
0 | 496 |
case SDL_VIDEORESIZE: { |
423 | 497 |
int w = clamp(ev.resize.w, 64, MAX_SCREEN_WIDTH); |
498 |
int h = clamp(ev.resize.h, 64, MAX_SCREEN_HEIGHT); |
|
499 |
ChangeResInGame(w, h); |
|
500 |
break; |
|
501 |
} |
|
0 | 502 |
} |
503 |
return -1; |
|
504 |
} |
|
505 |
||
506 |
static const char *SdlVideoStart(char **parm) |
|
507 |
{ |
|
423 | 508 |
char buf[30]; |
0 | 509 |
|
423 | 510 |
const char *s = SdlOpen(SDL_INIT_VIDEO); |
511 |
if (s != NULL) return s; |
|
0 | 512 |
|
513 |
SDL_CALL SDL_VideoDriverName(buf, 30); |
|
514 |
DEBUG(misc, 0) ("sdl: using driver '%s'", buf); |
|
515 |
||
516 |
GetVideoModes(); |
|
517 |
CreateMainSurface(_cur_resolution[0], _cur_resolution[1]); |
|
518 |
MarkWholeScreenDirty(); |
|
519 |
||
520 |
SDL_CALL SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); |
|
521 |
SDL_CALL SDL_EnableUNICODE(1); |
|
522 |
return NULL; |
|
523 |
} |
|
524 |
||
423 | 525 |
static void SdlVideoStop(void) |
0 | 526 |
{ |
527 |
SdlClose(SDL_INIT_VIDEO); |
|
528 |
} |
|
529 |
||
423 | 530 |
static int SdlVideoMainLoop(void) |
0 | 531 |
{ |
423 | 532 |
uint32 next_tick = SDL_CALL SDL_GetTicks() + 30; |
533 |
uint32 cur_ticks; |
|
534 |
uint32 pal_tick = 0; |
|
0 | 535 |
int i; |
536 |
uint32 mod; |
|
537 |
int numkeys; |
|
538 |
Uint8 *keys; |
|
539 |
||
423 | 540 |
for (;;) { |
0 | 541 |
InteractiveRandom(); // randomness |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
542 |
|
423 | 543 |
while ((i = PollEvent()) == -1) {} |
544 |
if (i >= 0) return i; |
|
0 | 545 |
|
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
546 |
if (_exit_game) return ML_QUIT; |
0 | 547 |
|
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
548 |
mod = SDL_CALL SDL_GetModState(); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
549 |
keys = SDL_CALL SDL_GetKeyState(&numkeys); |
0 | 550 |
#if defined(_DEBUG) |
423 | 551 |
if (_shift_pressed) |
0 | 552 |
#else |
423 | 553 |
if (keys[SDLK_TAB]) |
0 | 554 |
#endif |
423 | 555 |
{ |
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
|
556 |
if (!_networking) _fast_forward |= 2; |
423 | 557 |
} else if (_fast_forward & 2) { |
0 | 558 |
_fast_forward = 0; |
559 |
} |
|
560 |
||
423 | 561 |
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
|
562 |
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
|
563 |
next_tick = cur_ticks; |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
564 |
|
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
565 |
if (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 += 30; |
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 |
_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
|
569 |
_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
|
570 |
_dbg_screen_rect = !!(mod & KMOD_CAPS); |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
571 |
|
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
572 |
// determine which directional keys are down |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
573 |
_dirkeys = |
423 | 574 |
(keys[SDLK_LEFT] ? 1 : 0) | |
575 |
(keys[SDLK_UP] ? 2 : 0) | |
|
576 |
(keys[SDLK_RIGHT] ? 4 : 0) | |
|
577 |
(keys[SDLK_DOWN] ? 8 : 0); |
|
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
578 |
GameLoop(); |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
579 |
|
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
580 |
_screen.dst_ptr = _sdl_screen->pixels; |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
581 |
UpdateWindows(); |
423 | 582 |
if (++pal_tick > 4) { |
583 |
CheckPaletteAnim(); |
|
584 |
pal_tick = 1; |
|
0 | 585 |
} |
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
586 |
DrawSurfaceToScreen(); |
0 | 587 |
} else { |
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
588 |
SDL_CALL SDL_Delay(1); |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
589 |
_screen.dst_ptr = _sdl_screen->pixels; |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
590 |
DrawMouseCursor(); |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
591 |
DrawSurfaceToScreen(); |
0 | 592 |
} |
593 |
} |
|
594 |
} |
|
595 |
||
596 |
static bool SdlVideoChangeRes(int w, int h) |
|
597 |
{ |
|
598 |
// see if the mode is available |
|
599 |
if (GetAvailableVideoMode(&w, &h) != 1) |
|
600 |
return false; |
|
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
601 |
|
0 | 602 |
CreateMainSurface(w, h); |
603 |
return true; |
|
604 |
} |
|
605 |
||
606 |
const HalVideoDriver _sdl_video_driver = { |
|
607 |
SdlVideoStart, |
|
608 |
SdlVideoStop, |
|
609 |
SdlVideoMakeDirty, |
|
610 |
SdlVideoMainLoop, |
|
611 |
SdlVideoChangeRes, |
|
612 |
}; |
|
613 |
||
614 |
static void CDECL fill_sound_buffer(void *userdata, Uint8 *stream, int len) |
|
615 |
{ |
|
423 | 616 |
MxMixSamples(_mixer, stream, len / 4); |
0 | 617 |
} |
618 |
||
619 |
static char *SdlSoundStart(char **parm) |
|
620 |
{ |
|
621 |
SDL_AudioSpec spec; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
179
diff
changeset
|
622 |
|
423 | 623 |
char *s = SdlOpen(SDL_INIT_AUDIO); |
624 |
if (s != NULL) return s; |
|
625 |
||
0 | 626 |
spec.freq = GetDriverParamInt(parm, "hz", 11025); |
627 |
spec.format = AUDIO_S16SYS; |
|
628 |
spec.channels = 2; |
|
629 |
spec.samples = 512; |
|
423 | 630 |
spec.callback = fill_sound_buffer; |
0 | 631 |
SDL_CALL SDL_OpenAudio(&spec, &spec); |
632 |
SDL_CALL SDL_PauseAudio(0); |
|
633 |
return NULL; |
|
634 |
} |
|
635 |
||
423 | 636 |
static void SdlSoundStop(void) |
0 | 637 |
{ |
638 |
SDL_CALL SDL_CloseAudio(); |
|
639 |
SdlClose(SDL_INIT_AUDIO); |
|
640 |
} |
|
641 |
||
642 |
const HalSoundDriver _sdl_sound_driver = { |
|
643 |
SdlSoundStart, |
|
644 |
SdlSoundStop, |
|
645 |
}; |
|
646 |
||
647 |
||
423 | 648 |
#if 0 /* XXX what the heck is that? */ |
0 | 649 |
#include "viewport.h" |
650 |
void redsq_debug(int tile) |
|
651 |
{ |
|
652 |
_thd.redsq = tile; |
|
653 |
MarkWholeScreenDirty(); |
|
654 |
_screen.dst_ptr = _sdl_screen->pixels; |
|
655 |
UpdateWindows(); |
|
656 |
||
657 |
SdlVideoMakeDirty(0,0,_screen.width,_screen.height); |
|
658 |
DrawSurfaceToScreen(); |
|
659 |
} |
|
660 |
||
410 | 661 |
static void DbgRedraw() |
0 | 662 |
{ |
663 |
SdlVideoMakeDirty(0,0,_screen.width,_screen.height); |
|
664 |
DrawSurfaceToScreen(); |
|
665 |
} |
|
423 | 666 |
#endif |
667 |
||
0 | 668 |
#endif // WITH_SDL |