author | darkvater |
Tue, 07 Sep 2004 23:41:09 +0000 | |
changeset 179 | 003096efeb9d |
parent 170 | 8b184d95b4fe |
child 193 | 0a7025304867 |
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 |
||
26 |
#define SDL_CALL |
|
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)(); |
|
54 |
SDL_Surface *(SDLCALL *SDL_SetVideoMode)(int,int,int,Uint32); |
|
55 |
int (SDLCALL *SDL_EnableKeyRepeat)(int,int); |
|
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" |
|
63 |
static const char sdl_files[] = |
|
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 |
||
98 |
static char *LoadSdlDLL() |
|
99 |
{ |
|
100 |
if (_proc.SDL_Init != NULL) |
|
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) |
|
125 |
{ char *s = LoadSdlDLL(); if (s) return s; } |
|
126 |
#endif |
|
127 |
if (_sdl_usage++ == 0) { |
|
128 |
if (SDL_CALL SDL_Init(x) == -1) |
|
129 |
return SDL_CALL SDL_GetError(); |
|
130 |
} else if (x) { |
|
131 |
if (SDL_CALL SDL_InitSubSystem(x) == -1) |
|
132 |
return SDL_CALL SDL_GetError(); |
|
133 |
} |
|
134 |
||
135 |
#ifdef UNIX |
|
136 |
signal(SIGABRT, SdlAbort); |
|
137 |
#endif |
|
138 |
||
139 |
return NULL; |
|
140 |
} |
|
141 |
||
142 |
static void SdlClose(uint32 x) |
|
143 |
{ |
|
144 |
if (x) |
|
145 |
SDL_CALL SDL_QuitSubSystem(x); |
|
146 |
if (--_sdl_usage == 0) { |
|
147 |
SDL_CALL SDL_Quit(); |
|
148 |
#ifdef UNIX |
|
149 |
#ifndef __MORPHOS__ |
|
150 |
signal(SIGABRT, SIG_DFL); |
|
151 |
#else |
|
152 |
signal(SIGABRT, (void (*)(int))0 ); |
|
153 |
#endif |
|
154 |
#endif |
|
155 |
} |
|
156 |
} |
|
157 |
||
158 |
static void SdlVideoMakeDirty(int left, int top, int width, int height) |
|
159 |
{ |
|
160 |
// printf("(%d,%d)-(%d,%d)\n", left, top, width, height); |
|
161 |
// _pixels_redrawn += width*height; |
|
162 |
if (_num_dirty_rects < MAX_DIRTY_RECTS) { |
|
163 |
_dirty_rects[_num_dirty_rects].x = left; |
|
164 |
_dirty_rects[_num_dirty_rects].y = top; |
|
165 |
_dirty_rects[_num_dirty_rects].w = width; |
|
166 |
_dirty_rects[_num_dirty_rects].h = height; |
|
167 |
} |
|
168 |
_num_dirty_rects++; |
|
169 |
} |
|
170 |
||
171 |
static SDL_Color pal[256]; |
|
172 |
||
173 |
static void UpdatePalette(uint start, uint end) { |
|
174 |
uint i; |
|
175 |
byte *b; |
|
176 |
||
177 |
for(i = start, b = _cur_palette + start * 3; i != end; i++, b += 3) { |
|
178 |
pal[i].r = b[0]; |
|
179 |
pal[i].g = b[1]; |
|
180 |
pal[i].b = b[2]; |
|
181 |
pal[i].unused = b[3]; |
|
182 |
} |
|
183 |
||
184 |
SDL_CALL SDL_SetColors(_sdl_screen, pal, start, end); |
|
185 |
} |
|
186 |
||
187 |
static void InitPalette(void) { |
|
188 |
UpdatePalette(0, 256); |
|
189 |
} |
|
190 |
||
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
191 |
static void CheckPaletteAnim() |
0 | 192 |
{ |
193 |
if(_pal_last_dirty != -1) { |
|
194 |
UpdatePalette(_pal_first_dirty, _pal_last_dirty + 1); |
|
195 |
_pal_last_dirty = -1; |
|
196 |
} |
|
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
197 |
} |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
198 |
|
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
199 |
static void DrawSurfaceToScreen() |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
200 |
{ |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
201 |
int n; |
0 | 202 |
|
203 |
if ((n=_num_dirty_rects) != 0) { |
|
204 |
_num_dirty_rects = 0; |
|
205 |
if (n > MAX_DIRTY_RECTS) |
|
206 |
SDL_CALL SDL_UpdateRect(_sdl_screen, 0, 0, 0, 0); |
|
207 |
else { |
|
208 |
SDL_CALL SDL_UpdateRects(_sdl_screen, n, _dirty_rects); |
|
209 |
} |
|
210 |
} |
|
211 |
} |
|
212 |
||
213 |
static int CDECL compare_res(const void *pa, const void *pb) |
|
214 |
{ |
|
215 |
int x = ((uint16*)pa)[0] - ((uint16*)pb)[0]; |
|
216 |
if (x) return x; |
|
217 |
return ((uint16*)pa)[1] - ((uint16*)pb)[1]; |
|
218 |
} |
|
219 |
||
220 |
static const uint16 default_resolutions[][2] = { |
|
221 |
{640,480}, |
|
222 |
{800,600}, |
|
223 |
{1024,768}, |
|
224 |
{1152,864}, |
|
225 |
{1280,960}, |
|
226 |
{1280,1024}, |
|
227 |
{1400,1050}, |
|
228 |
{1600,1200}, |
|
229 |
}; |
|
230 |
||
231 |
static void GetVideoModes(void) { |
|
232 |
int i; |
|
233 |
SDL_Rect **modes; |
|
234 |
||
235 |
modes = SDL_CALL SDL_ListModes(NULL, SDL_SWSURFACE + (_fullscreen ? SDL_FULLSCREEN : 0)); |
|
236 |
||
237 |
_all_modes = (int)modes == -1; |
|
119
82c0f4713cad
(svn r120) Fix: (SDL) same resolution was displayed more than once in game options
dominik
parents:
60
diff
changeset
|
238 |
|
82c0f4713cad
(svn r120) Fix: (SDL) same resolution was displayed more than once in game options
dominik
parents:
60
diff
changeset
|
239 |
if(!modes) { |
0 | 240 |
error("sdl: no modes available"); |
241 |
} else if((int) modes == -1) { |
|
242 |
// all modes available, put some default ones here |
|
243 |
memcpy(_resolutions, default_resolutions, sizeof(default_resolutions)); |
|
244 |
_num_resolutions = sizeof(default_resolutions) / (sizeof(uint16)*2); |
|
245 |
} else { |
|
128
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
246 |
int n = 0; |
119
82c0f4713cad
(svn r120) Fix: (SDL) same resolution was displayed more than once in game options
dominik
parents:
60
diff
changeset
|
247 |
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
|
248 |
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
|
249 |
int h = modes[i]->h; |
0 | 250 |
if (IS_INT_INSIDE(w, 640, MAX_SCREEN_WIDTH+1) && |
251 |
IS_INT_INSIDE(h, 480, MAX_SCREEN_HEIGHT+1) && |
|
252 |
w%8 == 0 && h%8 == 0) { // disable screen resolutions which are not multiples of 8 |
|
128
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
253 |
int j; |
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
254 |
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
|
255 |
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
|
256 |
break; |
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
257 |
if (j == n) { |
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
258 |
_resolutions[n][0] = w; |
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
259 |
_resolutions[n][1] = h; |
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
260 |
if (++n == sizeof(_resolutions) / (sizeof(uint16)*2)) break; |
28ebd80f6521
(svn r129) Fix: no more same multiple resolutions due to incorrect SDL implementation (Tron)
dominik
parents:
119
diff
changeset
|
261 |
} |
0 | 262 |
} |
263 |
} |
|
264 |
_num_resolutions = n; |
|
265 |
qsort(_resolutions, n, sizeof(uint16)*2, compare_res); |
|
266 |
} |
|
267 |
} |
|
268 |
||
269 |
static int GetAvailableVideoMode(int *w, int *h) |
|
270 |
{ |
|
271 |
int i; |
|
272 |
||
273 |
||
274 |
// all modes available? |
|
275 |
if (_all_modes) |
|
276 |
return 1; |
|
277 |
||
278 |
// is the wanted mode among the available modes? |
|
279 |
for(i = 0; i != _num_resolutions; i++) { |
|
280 |
if(*w == _resolutions[i][0] && *h == _resolutions[i][1]) |
|
281 |
return 1; |
|
282 |
} |
|
283 |
||
284 |
// use the default mode |
|
285 |
*w = _resolutions[0][0]; |
|
286 |
*h = _resolutions[0][1]; |
|
287 |
return 2; |
|
288 |
} |
|
289 |
||
290 |
static bool CreateMainSurface(int w, int h) |
|
291 |
{ |
|
292 |
SDL_Surface *newscreen; |
|
293 |
bool sizechange; |
|
294 |
||
295 |
GetAvailableVideoMode(&w, &h); |
|
296 |
||
297 |
sizechange = (_screen.width != w || _screen.height != h); |
|
298 |
_screen.pitch = _screen.width = w; |
|
299 |
_screen.height = h; |
|
300 |
||
301 |
DEBUG(misc, 0) ("sdl: using mode %dx%d", w, h); |
|
302 |
||
36
c1b3b884584e
(svn r37) Do not change to HWSURFACE since it's not working!
darkvater
parents:
34
diff
changeset
|
303 |
// DO NOT CHANGE TO HWSURFACE, IT DOES NOT WORK |
34
a1bbde8e1f6e
(svn r35) -Fix Change SDL_HWSURFACE back to SDL_SWSURFACE (Diablo-3D); better performance
darkvater
parents:
18
diff
changeset
|
304 |
newscreen = SDL_CALL SDL_SetVideoMode(w, h, 8, SDL_SWSURFACE + SDL_HWPALETTE + (_fullscreen?SDL_FULLSCREEN:SDL_RESIZABLE)); |
0 | 305 |
if(newscreen == NULL) |
306 |
return false; |
|
307 |
||
308 |
_sdl_screen = newscreen; |
|
309 |
InitPalette(); |
|
310 |
||
311 |
SDL_CALL SDL_WM_SetCaption("OpenTTD", "OpenTTD"); |
|
312 |
SDL_CALL SDL_ShowCursor(0); |
|
313 |
||
314 |
// if(sizechange) |
|
315 |
GameSizeChanged(); |
|
316 |
||
317 |
return true; |
|
318 |
} |
|
319 |
||
320 |
typedef struct { |
|
321 |
uint16 vk_from; |
|
322 |
byte vk_count; |
|
323 |
byte map_to; |
|
324 |
} VkMapping; |
|
325 |
||
326 |
#define AS(x,z) {x,0,z} |
|
327 |
#define AM(x,y,z,w) {x,y-x,z} |
|
328 |
||
329 |
static const VkMapping _vk_mapping[] = { |
|
330 |
// Pageup stuff + up/down |
|
331 |
AM(SDLK_PAGEUP,SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN), |
|
332 |
AS(SDLK_UP, WKC_UP), |
|
333 |
AS(SDLK_DOWN, WKC_DOWN), |
|
334 |
AS(SDLK_LEFT, WKC_LEFT), |
|
335 |
AS(SDLK_RIGHT, WKC_RIGHT), |
|
336 |
||
337 |
AS(SDLK_HOME, WKC_HOME), |
|
338 |
AS(SDLK_END, WKC_END), |
|
339 |
||
340 |
AS(SDLK_INSERT, WKC_INSERT), |
|
341 |
AS(SDLK_DELETE, WKC_DELETE), |
|
342 |
||
343 |
// Map letters & digits |
|
344 |
AM(SDLK_a,SDLK_z,'A','Z'), |
|
345 |
AM(SDLK_0,SDLK_9,'0','9'), |
|
346 |
||
347 |
AS(SDLK_ESCAPE, WKC_ESC), |
|
348 |
AS(SDLK_BACKSPACE, WKC_BACKSPACE), |
|
349 |
||
350 |
AS(SDLK_SPACE, WKC_SPACE), |
|
351 |
AS(SDLK_RETURN, WKC_RETURN), |
|
352 |
AS(SDLK_TAB, WKC_TAB), |
|
353 |
||
354 |
// Function keys |
|
355 |
AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12), |
|
356 |
||
357 |
// Numeric part. |
|
358 |
// What is the virtual keycode for numeric enter?? |
|
359 |
AM(SDLK_KP0,SDLK_KP9, WKC_NUM_0, WKC_NUM_9), |
|
360 |
AS(SDLK_KP_DIVIDE, WKC_NUM_DIV), |
|
361 |
AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL), |
|
362 |
AS(SDLK_KP_MINUS, WKC_NUM_MINUS), |
|
363 |
AS(SDLK_KP_PLUS, WKC_NUM_PLUS), |
|
364 |
AS(SDLK_KP_ENTER, WKC_NUM_ENTER), |
|
365 |
AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL), |
|
179
003096efeb9d
(svn r180) -Fix: some more warning fixes for C99 (Tron)
darkvater
parents:
170
diff
changeset
|
366 |
{0, 0, 0} |
0 | 367 |
}; |
368 |
||
369 |
static uint32 ConvertSdlKeyIntoMy(SDL_keysym *sym) |
|
370 |
{ |
|
371 |
const VkMapping *map = _vk_mapping - 1; |
|
372 |
uint from; |
|
373 |
uint key = sym->sym; |
|
374 |
for(;;) { |
|
375 |
map++; |
|
376 |
from = map->vk_from; |
|
377 |
if (from == 0) { |
|
378 |
key = 0; |
|
379 |
break; |
|
380 |
} |
|
381 |
if ((uint)(key - from) <= map->vk_count) { |
|
382 |
key = key - from + map->map_to; |
|
383 |
break; |
|
384 |
} |
|
385 |
} |
|
129
df1a60bc0d70
(svn r130) Change: hotkey for console is Backquote (the key left to '1', regardless of keyboard layout
dominik
parents:
128
diff
changeset
|
386 |
|
df1a60bc0d70
(svn r130) Change: hotkey for console is Backquote (the key left to '1', regardless of keyboard layout
dominik
parents:
128
diff
changeset
|
387 |
// 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
|
388 |
#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
|
389 |
if (sym->scancode == 41) key |= WKC_BACKQUOTE; |
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
|
390 |
#else |
167 | 391 |
#if defined(__APPLE__) |
392 |
if (sym->scancode == 10) key |= WKC_BACKQUOTE; |
|
393 |
#else |
|
170
8b184d95b4fe
(svn r171) MorphOS: added hotkey for console (tokai)
bjarni
parents:
167
diff
changeset
|
394 |
#if defined(__MORPHOS__) |
8b184d95b4fe
(svn r171) MorphOS: added hotkey for console (tokai)
bjarni
parents:
167
diff
changeset
|
395 |
if (sym->scancode == 0) key |= WKC_BACKQUOTE; // yes, that key is code '0' under MorphOS :) |
8b184d95b4fe
(svn r171) MorphOS: added hotkey for console (tokai)
bjarni
parents:
167
diff
changeset
|
396 |
#else |
8b184d95b4fe
(svn r171) MorphOS: added hotkey for console (tokai)
bjarni
parents:
167
diff
changeset
|
397 |
if (sym->scancode == 49) key |= WKC_BACKQUOTE; |
8b184d95b4fe
(svn r171) MorphOS: added hotkey for console (tokai)
bjarni
parents:
167
diff
changeset
|
398 |
#endif |
167 | 399 |
#endif |
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; |
|
167 | 406 |
// these two lines really helps porting hotkey combos. Uncomment to use -- Bjarni |
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 |
||
412 |
static int PollEvent() { |
|
413 |
SDL_Event ev; |
|
414 |
||
415 |
if (!SDL_CALL SDL_PollEvent(&ev)) |
|
416 |
return -2; |
|
417 |
||
418 |
switch(ev.type) { |
|
419 |
case SDL_MOUSEMOTION: |
|
420 |
if (_cursor.fix_at) { |
|
421 |
int dx = ev.motion.x - _cursor.pos.x; |
|
422 |
int dy = ev.motion.y - _cursor.pos.y; |
|
423 |
if (dx != 0 || dy != 0) { |
|
424 |
_cursor.delta.x += dx; |
|
425 |
_cursor.delta.y += dy; |
|
426 |
SDL_CALL SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y); |
|
427 |
} |
|
428 |
} else { |
|
429 |
_cursor.delta.x = ev.motion.x - _cursor.pos.x; |
|
430 |
_cursor.delta.y = ev.motion.y - _cursor.pos.y; |
|
431 |
_cursor.pos.x = ev.motion.x; |
|
432 |
_cursor.pos.y = ev.motion.y; |
|
433 |
_cursor.dirty = true; |
|
434 |
} |
|
435 |
break; |
|
436 |
||
437 |
case SDL_MOUSEBUTTONDOWN: |
|
438 |
if (_rightclick_emulate && (SDL_CALL SDL_GetModState() & (KMOD_LCTRL | KMOD_RCTRL))) |
|
439 |
ev.button.button = SDL_BUTTON_RIGHT; |
|
440 |
||
441 |
if (ev.button.button == SDL_BUTTON_LEFT) { |
|
442 |
_left_button_down = true; |
|
443 |
} else if (ev.button.button == SDL_BUTTON_RIGHT) { |
|
444 |
_right_button_down = true; |
|
445 |
_right_button_clicked = true; |
|
446 |
} |
|
447 |
#if !defined(WIN32) |
|
448 |
else if (ev.button.button == SDL_BUTTON_WHEELUP) { |
|
449 |
_cursor.wheel--; |
|
450 |
} else if (ev.button.button == SDL_BUTTON_WHEELDOWN) { |
|
451 |
_cursor.wheel++; |
|
452 |
} |
|
453 |
#endif |
|
454 |
break; |
|
455 |
||
456 |
case SDL_MOUSEBUTTONUP: |
|
457 |
if (_rightclick_emulate) { |
|
458 |
_right_button_down = false; |
|
459 |
_left_button_down = false; |
|
460 |
_left_button_clicked = false; |
|
461 |
} else if (ev.button.button == SDL_BUTTON_LEFT) { |
|
462 |
_left_button_down = false; |
|
463 |
_left_button_clicked = false; |
|
464 |
} else if (ev.button.button == SDL_BUTTON_RIGHT) { |
|
465 |
_right_button_down = false; |
|
466 |
} |
|
467 |
break; |
|
468 |
||
469 |
case SDL_QUIT: |
|
470 |
return ML_QUIT; |
|
471 |
||
472 |
case SDL_KEYDOWN: |
|
473 |
if ((((ev.key.keysym.sym == SDLK_RETURN) || (ev.key.keysym.sym == SDLK_f)) && (ev.key.keysym.mod & KMOD_ALT)) || (((ev.key.keysym.sym == SDLK_RETURN) || (ev.key.keysym.sym == SDLK_f)) && (ev.key.keysym.mod & KMOD_META))) { |
|
474 |
_fullscreen ^= true; |
|
475 |
GetVideoModes(); |
|
476 |
CreateMainSurface(_screen.width, _screen.height); |
|
477 |
MarkWholeScreenDirty(); |
|
478 |
} else { |
|
479 |
_pressed_key = ConvertSdlKeyIntoMy(&ev.key.keysym); |
|
480 |
} |
|
481 |
break; |
|
482 |
||
483 |
case SDL_VIDEORESIZE: { |
|
484 |
int w, h; |
|
485 |
w = ev.resize.w; |
|
486 |
h = ev.resize.h; |
|
487 |
||
488 |
w = clamp(w & ~0x7, 64, MAX_SCREEN_WIDTH); |
|
489 |
h = clamp(h & ~0x7, 64, MAX_SCREEN_HEIGHT); |
|
490 |
||
491 |
ChangeResInGame(w, h); |
|
492 |
||
493 |
break; |
|
494 |
} |
|
495 |
} |
|
496 |
return -1; |
|
497 |
} |
|
498 |
||
499 |
static const char *SdlVideoStart(char **parm) |
|
500 |
{ |
|
501 |
char buf[30]; |
|
502 |
||
503 |
{char *s;if ((s = SdlOpen(SDL_INIT_VIDEO)) != NULL) return s;} |
|
504 |
||
505 |
SDL_CALL SDL_VideoDriverName(buf, 30); |
|
506 |
DEBUG(misc, 0) ("sdl: using driver '%s'", buf); |
|
507 |
||
508 |
GetVideoModes(); |
|
509 |
CreateMainSurface(_cur_resolution[0], _cur_resolution[1]); |
|
510 |
MarkWholeScreenDirty(); |
|
511 |
||
512 |
SDL_CALL SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); |
|
513 |
SDL_CALL SDL_EnableUNICODE(1); |
|
514 |
return NULL; |
|
515 |
} |
|
516 |
||
517 |
static void SdlVideoStop() |
|
518 |
{ |
|
519 |
SdlClose(SDL_INIT_VIDEO); |
|
520 |
} |
|
521 |
||
522 |
static int SdlVideoMainLoop() |
|
523 |
{ |
|
60 | 524 |
uint32 next_tick = SDL_CALL SDL_GetTicks() + 30, cur_ticks, pal_tick = 0; |
0 | 525 |
int i; |
526 |
uint32 mod; |
|
527 |
int numkeys; |
|
528 |
Uint8 *keys; |
|
529 |
||
530 |
while (true) { |
|
531 |
InteractiveRandom(); // randomness |
|
532 |
||
533 |
while ((i=PollEvent()) == -1) {} |
|
534 |
if (i>=0) return i; |
|
535 |
||
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
536 |
if (_exit_game) return ML_QUIT; |
0 | 537 |
|
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
538 |
mod = SDL_CALL SDL_GetModState(); |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
539 |
keys = SDL_CALL SDL_GetKeyState(&numkeys); |
0 | 540 |
#if defined(_DEBUG) |
541 |
if (_shift_pressed) { |
|
542 |
#else |
|
543 |
if (keys[SDLK_TAB]) { |
|
544 |
#endif |
|
545 |
_fast_forward |= 2; |
|
546 |
} else if (_fast_forward&2) { |
|
547 |
_fast_forward = 0; |
|
548 |
} |
|
549 |
||
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
550 |
cur_ticks=SDL_CALL SDL_GetTicks(); |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
551 |
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
|
552 |
next_tick = cur_ticks; |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
553 |
|
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
554 |
if (cur_ticks == next_tick) { |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
555 |
next_tick += 30; |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
556 |
|
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
557 |
_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
|
558 |
_shift_pressed = !!(mod & (KMOD_LSHIFT | KMOD_RSHIFT)); |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
559 |
|
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
560 |
// determine which directional keys are down |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
561 |
_dirkeys = |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
562 |
(keys[SDLK_LEFT] ? 1 : 0) + |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
563 |
(keys[SDLK_UP] ? 2 : 0) + |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
564 |
(keys[SDLK_RIGHT] ? 4 : 0) + |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
565 |
(keys[SDLK_DOWN] ? 8 : 0); |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
566 |
GameLoop(); |
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 |
_screen.dst_ptr = _sdl_screen->pixels; |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
569 |
UpdateWindows(); |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
570 |
if (++ pal_tick > 4){ |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
571 |
CheckPaletteAnim(); |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
572 |
pal_tick = 1; |
0 | 573 |
} |
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
574 |
DrawSurfaceToScreen(); |
0 | 575 |
} else { |
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
576 |
SDL_CALL SDL_Delay(1); |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
577 |
_screen.dst_ptr = _sdl_screen->pixels; |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
578 |
DrawMouseCursor(); |
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
579 |
DrawSurfaceToScreen(); |
0 | 580 |
} |
581 |
} |
|
582 |
} |
|
583 |
||
584 |
static bool SdlVideoChangeRes(int w, int h) |
|
585 |
{ |
|
586 |
// see if the mode is available |
|
587 |
if (GetAvailableVideoMode(&w, &h) != 1) |
|
588 |
return false; |
|
18
7428dd7c5732
(svn r19) SDL performance fix fo palette animation and mouse jumping (jirif)
dominik
parents:
0
diff
changeset
|
589 |
|
0 | 590 |
CreateMainSurface(w, h); |
591 |
return true; |
|
592 |
} |
|
593 |
||
594 |
const HalVideoDriver _sdl_video_driver = { |
|
595 |
SdlVideoStart, |
|
596 |
SdlVideoStop, |
|
597 |
SdlVideoMakeDirty, |
|
598 |
SdlVideoMainLoop, |
|
599 |
SdlVideoChangeRes, |
|
600 |
}; |
|
601 |
||
602 |
static void CDECL fill_sound_buffer(void *userdata, Uint8 *stream, int len) |
|
603 |
{ |
|
604 |
MxMixSamples(_mixer, stream, len >> 2); |
|
605 |
} |
|
606 |
||
607 |
static char *SdlSoundStart(char **parm) |
|
608 |
{ |
|
609 |
SDL_AudioSpec spec; |
|
610 |
||
611 |
{char *s;if ((s = SdlOpen(SDL_INIT_AUDIO)) != NULL) return s;} |
|
612 |
spec.freq = GetDriverParamInt(parm, "hz", 11025); |
|
613 |
spec.format = AUDIO_S16SYS; |
|
614 |
spec.channels = 2; |
|
615 |
spec.samples = 512; |
|
616 |
*(void**)&spec.callback = fill_sound_buffer; |
|
617 |
SDL_CALL SDL_OpenAudio(&spec, &spec); |
|
618 |
SDL_CALL SDL_PauseAudio(0); |
|
619 |
return NULL; |
|
620 |
} |
|
621 |
||
622 |
static void SdlSoundStop() |
|
623 |
{ |
|
624 |
SDL_CALL SDL_CloseAudio(); |
|
625 |
SdlClose(SDL_INIT_AUDIO); |
|
626 |
} |
|
627 |
||
628 |
const HalSoundDriver _sdl_sound_driver = { |
|
629 |
SdlSoundStart, |
|
630 |
SdlSoundStop, |
|
631 |
}; |
|
632 |
||
633 |
||
634 |
#include "viewport.h" |
|
635 |
void redsq_debug(int tile) |
|
636 |
{ |
|
637 |
_thd.redsq = tile; |
|
638 |
MarkWholeScreenDirty(); |
|
639 |
_screen.dst_ptr = _sdl_screen->pixels; |
|
640 |
UpdateWindows(); |
|
641 |
||
642 |
SdlVideoMakeDirty(0,0,_screen.width,_screen.height); |
|
643 |
DrawSurfaceToScreen(); |
|
644 |
} |
|
645 |
||
646 |
void DbgRedraw() |
|
647 |
{ |
|
648 |
SdlVideoMakeDirty(0,0,_screen.width,_screen.height); |
|
649 |
DrawSurfaceToScreen(); |
|
650 |
} |
|
651 |
#endif // WITH_SDL |