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 |
|
|
191 |
static void DrawSurfaceToScreen()
|
|
192 |
{
|
|
193 |
int n;
|
|
194 |
|
|
195 |
if(_pal_last_dirty != -1) {
|
|
196 |
UpdatePalette(_pal_first_dirty, _pal_last_dirty + 1);
|
|
197 |
_pal_last_dirty = -1;
|
|
198 |
}
|
|
199 |
|
|
200 |
if ((n=_num_dirty_rects) != 0) {
|
|
201 |
_num_dirty_rects = 0;
|
|
202 |
if (n > MAX_DIRTY_RECTS)
|
|
203 |
SDL_CALL SDL_UpdateRect(_sdl_screen, 0, 0, 0, 0);
|
|
204 |
else {
|
|
205 |
SDL_CALL SDL_UpdateRects(_sdl_screen, n, _dirty_rects);
|
|
206 |
}
|
|
207 |
}
|
|
208 |
}
|
|
209 |
|
|
210 |
static int CDECL compare_res(const void *pa, const void *pb)
|
|
211 |
{
|
|
212 |
int x = ((uint16*)pa)[0] - ((uint16*)pb)[0];
|
|
213 |
if (x) return x;
|
|
214 |
return ((uint16*)pa)[1] - ((uint16*)pb)[1];
|
|
215 |
}
|
|
216 |
|
|
217 |
static const uint16 default_resolutions[][2] = {
|
|
218 |
{640,480},
|
|
219 |
{800,600},
|
|
220 |
{1024,768},
|
|
221 |
{1152,864},
|
|
222 |
{1280,960},
|
|
223 |
{1280,1024},
|
|
224 |
{1400,1050},
|
|
225 |
{1600,1200},
|
|
226 |
};
|
|
227 |
|
|
228 |
static void GetVideoModes(void) {
|
|
229 |
int i;
|
|
230 |
SDL_Rect **modes;
|
|
231 |
|
|
232 |
modes = SDL_CALL SDL_ListModes(NULL, SDL_SWSURFACE + (_fullscreen ? SDL_FULLSCREEN : 0));
|
|
233 |
|
|
234 |
_all_modes = (int)modes == -1;
|
|
235 |
|
|
236 |
if(modes == NULL) {
|
|
237 |
error("sdl: no modes available");
|
|
238 |
} else if((int) modes == -1) {
|
|
239 |
// all modes available, put some default ones here
|
|
240 |
memcpy(_resolutions, default_resolutions, sizeof(default_resolutions));
|
|
241 |
_num_resolutions = sizeof(default_resolutions) / (sizeof(uint16)*2);
|
|
242 |
} else {
|
|
243 |
int n = 0;
|
|
244 |
|
|
245 |
for(i = 0; modes[i] != NULL; i++) {
|
|
246 |
int w = modes[i]->w;
|
|
247 |
int h = modes[i]->h;
|
|
248 |
if (IS_INT_INSIDE(w, 640, MAX_SCREEN_WIDTH+1) &&
|
|
249 |
IS_INT_INSIDE(h, 480, MAX_SCREEN_HEIGHT+1) &&
|
|
250 |
w%8 == 0 && h%8 == 0) { // disable screen resolutions which are not multiples of 8
|
|
251 |
_resolutions[n][0] = w;
|
|
252 |
_resolutions[n][1] = h;
|
|
253 |
if (++n == sizeof(_resolutions) / (sizeof(uint16)*2)) break;
|
|
254 |
}
|
|
255 |
}
|
|
256 |
_num_resolutions = n;
|
|
257 |
qsort(_resolutions, n, sizeof(uint16)*2, compare_res);
|
|
258 |
}
|
|
259 |
}
|
|
260 |
|
|
261 |
static int GetAvailableVideoMode(int *w, int *h)
|
|
262 |
{
|
|
263 |
int i;
|
|
264 |
|
|
265 |
|
|
266 |
// all modes available?
|
|
267 |
if (_all_modes)
|
|
268 |
return 1;
|
|
269 |
|
|
270 |
// is the wanted mode among the available modes?
|
|
271 |
for(i = 0; i != _num_resolutions; i++) {
|
|
272 |
if(*w == _resolutions[i][0] && *h == _resolutions[i][1])
|
|
273 |
return 1;
|
|
274 |
}
|
|
275 |
|
|
276 |
// use the default mode
|
|
277 |
*w = _resolutions[0][0];
|
|
278 |
*h = _resolutions[0][1];
|
|
279 |
return 2;
|
|
280 |
}
|
|
281 |
|
|
282 |
static bool CreateMainSurface(int w, int h)
|
|
283 |
{
|
|
284 |
SDL_Surface *newscreen;
|
|
285 |
bool sizechange;
|
|
286 |
|
|
287 |
GetAvailableVideoMode(&w, &h);
|
|
288 |
|
|
289 |
sizechange = (_screen.width != w || _screen.height != h);
|
|
290 |
_screen.pitch = _screen.width = w;
|
|
291 |
_screen.height = h;
|
|
292 |
|
|
293 |
DEBUG(misc, 0) ("sdl: using mode %dx%d", w, h);
|
|
294 |
|
|
295 |
newscreen = SDL_CALL SDL_SetVideoMode(w, h, 8, SDL_SWSURFACE + SDL_HWPALETTE + (_fullscreen?SDL_FULLSCREEN:SDL_RESIZABLE));
|
|
296 |
if(newscreen == NULL)
|
|
297 |
return false;
|
|
298 |
|
|
299 |
_sdl_screen = newscreen;
|
|
300 |
InitPalette();
|
|
301 |
|
|
302 |
SDL_CALL SDL_WM_SetCaption("OpenTTD", "OpenTTD");
|
|
303 |
SDL_CALL SDL_ShowCursor(0);
|
|
304 |
|
|
305 |
// if(sizechange)
|
|
306 |
GameSizeChanged();
|
|
307 |
|
|
308 |
return true;
|
|
309 |
}
|
|
310 |
|
|
311 |
typedef struct {
|
|
312 |
uint16 vk_from;
|
|
313 |
byte vk_count;
|
|
314 |
byte map_to;
|
|
315 |
} VkMapping;
|
|
316 |
|
|
317 |
#define AS(x,z) {x,0,z}
|
|
318 |
#define AM(x,y,z,w) {x,y-x,z}
|
|
319 |
|
|
320 |
static const VkMapping _vk_mapping[] = {
|
|
321 |
// Pageup stuff + up/down
|
|
322 |
AM(SDLK_PAGEUP,SDLK_PAGEDOWN, WKC_PAGEUP, WKC_PAGEDOWN),
|
|
323 |
AS(SDLK_UP, WKC_UP),
|
|
324 |
AS(SDLK_DOWN, WKC_DOWN),
|
|
325 |
AS(SDLK_LEFT, WKC_LEFT),
|
|
326 |
AS(SDLK_RIGHT, WKC_RIGHT),
|
|
327 |
|
|
328 |
AS(SDLK_HOME, WKC_HOME),
|
|
329 |
AS(SDLK_END, WKC_END),
|
|
330 |
|
|
331 |
AS(SDLK_INSERT, WKC_INSERT),
|
|
332 |
AS(SDLK_DELETE, WKC_DELETE),
|
|
333 |
|
|
334 |
// Map letters & digits
|
|
335 |
AM(SDLK_a,SDLK_z,'A','Z'),
|
|
336 |
AM(SDLK_0,SDLK_9,'0','9'),
|
|
337 |
|
|
338 |
AS(SDLK_AMPERSAND, WKC_AMPERSAND), // mainly for french keyboards
|
|
339 |
|
|
340 |
AS(SDLK_ESCAPE, WKC_ESC),
|
|
341 |
AS(SDLK_BACKSPACE, WKC_BACKSPACE),
|
|
342 |
|
|
343 |
AS(SDLK_SPACE, WKC_SPACE),
|
|
344 |
AS(SDLK_RETURN, WKC_RETURN),
|
|
345 |
AS(SDLK_TAB, WKC_TAB),
|
|
346 |
|
|
347 |
// Function keys
|
|
348 |
AM(SDLK_F1, SDLK_F12, WKC_F1, WKC_F12),
|
|
349 |
|
|
350 |
// Numeric part.
|
|
351 |
// What is the virtual keycode for numeric enter??
|
|
352 |
AM(SDLK_KP0,SDLK_KP9, WKC_NUM_0, WKC_NUM_9),
|
|
353 |
AS(SDLK_KP_DIVIDE, WKC_NUM_DIV),
|
|
354 |
AS(SDLK_KP_MULTIPLY, WKC_NUM_MUL),
|
|
355 |
AS(SDLK_KP_MINUS, WKC_NUM_MINUS),
|
|
356 |
AS(SDLK_KP_PLUS, WKC_NUM_PLUS),
|
|
357 |
AS(SDLK_KP_ENTER, WKC_NUM_ENTER),
|
|
358 |
AS(SDLK_KP_PERIOD, WKC_NUM_DECIMAL),
|
|
359 |
{0}
|
|
360 |
};
|
|
361 |
|
|
362 |
static uint32 ConvertSdlKeyIntoMy(SDL_keysym *sym)
|
|
363 |
{
|
|
364 |
const VkMapping *map = _vk_mapping - 1;
|
|
365 |
uint from;
|
|
366 |
uint key = sym->sym;
|
|
367 |
for(;;) {
|
|
368 |
map++;
|
|
369 |
from = map->vk_from;
|
|
370 |
if (from == 0) {
|
|
371 |
key = 0;
|
|
372 |
break;
|
|
373 |
}
|
|
374 |
if ((uint)(key - from) <= map->vk_count) {
|
|
375 |
key = key - from + map->map_to;
|
|
376 |
break;
|
|
377 |
}
|
|
378 |
}
|
|
379 |
|
|
380 |
// META are the command keys on mac
|
|
381 |
if (sym->mod & KMOD_META) key |= WKC_META;
|
|
382 |
if (sym->mod & KMOD_SHIFT) key |= WKC_SHIFT;
|
|
383 |
if (sym->mod & KMOD_CTRL) key |= WKC_CTRL;
|
|
384 |
if (sym->mod & KMOD_ALT) key |= WKC_ALT;
|
|
385 |
return (key << 16) + sym->unicode;
|
|
386 |
}
|
|
387 |
|
|
388 |
static int PollEvent() {
|
|
389 |
SDL_Event ev;
|
|
390 |
|
|
391 |
if (!SDL_CALL SDL_PollEvent(&ev))
|
|
392 |
return -2;
|
|
393 |
|
|
394 |
switch(ev.type) {
|
|
395 |
case SDL_MOUSEMOTION:
|
|
396 |
if (_cursor.fix_at) {
|
|
397 |
int dx = ev.motion.x - _cursor.pos.x;
|
|
398 |
int dy = ev.motion.y - _cursor.pos.y;
|
|
399 |
if (dx != 0 || dy != 0) {
|
|
400 |
_cursor.delta.x += dx;
|
|
401 |
_cursor.delta.y += dy;
|
|
402 |
SDL_CALL SDL_WarpMouse(_cursor.pos.x, _cursor.pos.y);
|
|
403 |
}
|
|
404 |
} else {
|
|
405 |
_cursor.delta.x = ev.motion.x - _cursor.pos.x;
|
|
406 |
_cursor.delta.y = ev.motion.y - _cursor.pos.y;
|
|
407 |
_cursor.pos.x = ev.motion.x;
|
|
408 |
_cursor.pos.y = ev.motion.y;
|
|
409 |
_cursor.dirty = true;
|
|
410 |
}
|
|
411 |
break;
|
|
412 |
|
|
413 |
case SDL_MOUSEBUTTONDOWN:
|
|
414 |
if (_rightclick_emulate && (SDL_CALL SDL_GetModState() & (KMOD_LCTRL | KMOD_RCTRL)))
|
|
415 |
ev.button.button = SDL_BUTTON_RIGHT;
|
|
416 |
|
|
417 |
if (ev.button.button == SDL_BUTTON_LEFT) {
|
|
418 |
_left_button_down = true;
|
|
419 |
} else if (ev.button.button == SDL_BUTTON_RIGHT) {
|
|
420 |
_right_button_down = true;
|
|
421 |
_right_button_clicked = true;
|
|
422 |
}
|
|
423 |
#if !defined(WIN32)
|
|
424 |
else if (ev.button.button == SDL_BUTTON_WHEELUP) {
|
|
425 |
_cursor.wheel--;
|
|
426 |
} else if (ev.button.button == SDL_BUTTON_WHEELDOWN) {
|
|
427 |
_cursor.wheel++;
|
|
428 |
}
|
|
429 |
#endif
|
|
430 |
break;
|
|
431 |
|
|
432 |
case SDL_MOUSEBUTTONUP:
|
|
433 |
if (_rightclick_emulate) {
|
|
434 |
_right_button_down = false;
|
|
435 |
_left_button_down = false;
|
|
436 |
_left_button_clicked = false;
|
|
437 |
} else if (ev.button.button == SDL_BUTTON_LEFT) {
|
|
438 |
_left_button_down = false;
|
|
439 |
_left_button_clicked = false;
|
|
440 |
} else if (ev.button.button == SDL_BUTTON_RIGHT) {
|
|
441 |
_right_button_down = false;
|
|
442 |
}
|
|
443 |
break;
|
|
444 |
|
|
445 |
case SDL_QUIT:
|
|
446 |
return ML_QUIT;
|
|
447 |
|
|
448 |
case SDL_KEYDOWN:
|
|
449 |
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))) {
|
|
450 |
_fullscreen ^= true;
|
|
451 |
GetVideoModes();
|
|
452 |
CreateMainSurface(_screen.width, _screen.height);
|
|
453 |
MarkWholeScreenDirty();
|
|
454 |
} else {
|
|
455 |
_pressed_key = ConvertSdlKeyIntoMy(&ev.key.keysym);
|
|
456 |
}
|
|
457 |
break;
|
|
458 |
|
|
459 |
case SDL_VIDEORESIZE: {
|
|
460 |
int w, h;
|
|
461 |
w = ev.resize.w;
|
|
462 |
h = ev.resize.h;
|
|
463 |
|
|
464 |
w = clamp(w & ~0x7, 64, MAX_SCREEN_WIDTH);
|
|
465 |
h = clamp(h & ~0x7, 64, MAX_SCREEN_HEIGHT);
|
|
466 |
|
|
467 |
ChangeResInGame(w, h);
|
|
468 |
|
|
469 |
break;
|
|
470 |
}
|
|
471 |
}
|
|
472 |
return -1;
|
|
473 |
}
|
|
474 |
|
|
475 |
static const char *SdlVideoStart(char **parm)
|
|
476 |
{
|
|
477 |
char buf[30];
|
|
478 |
|
|
479 |
{char *s;if ((s = SdlOpen(SDL_INIT_VIDEO)) != NULL) return s;}
|
|
480 |
|
|
481 |
SDL_CALL SDL_VideoDriverName(buf, 30);
|
|
482 |
DEBUG(misc, 0) ("sdl: using driver '%s'", buf);
|
|
483 |
|
|
484 |
GetVideoModes();
|
|
485 |
CreateMainSurface(_cur_resolution[0], _cur_resolution[1]);
|
|
486 |
MarkWholeScreenDirty();
|
|
487 |
|
|
488 |
SDL_CALL SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
|
|
489 |
SDL_CALL SDL_EnableUNICODE(1);
|
|
490 |
return NULL;
|
|
491 |
}
|
|
492 |
|
|
493 |
static void SdlVideoStop()
|
|
494 |
{
|
|
495 |
SdlClose(SDL_INIT_VIDEO);
|
|
496 |
}
|
|
497 |
|
|
498 |
static int SdlVideoMainLoop()
|
|
499 |
{
|
|
500 |
uint32 next_tick = SDL_CALL SDL_GetTicks() + 30, cur_ticks;
|
|
501 |
int i;
|
|
502 |
uint32 mod;
|
|
503 |
int numkeys;
|
|
504 |
Uint8 *keys;
|
|
505 |
|
|
506 |
while (true) {
|
|
507 |
InteractiveRandom(); // randomness
|
|
508 |
|
|
509 |
while ((i=PollEvent()) == -1) {}
|
|
510 |
if (i>=0) return i;
|
|
511 |
|
|
512 |
mod = SDL_CALL SDL_GetModState();
|
|
513 |
_ctrl_pressed = !!(mod & (KMOD_LCTRL | KMOD_RCTRL));
|
|
514 |
_shift_pressed = !!(mod & (KMOD_LSHIFT | KMOD_RSHIFT));
|
|
515 |
|
|
516 |
|
|
517 |
keys = SDL_CALL SDL_GetKeyState(&numkeys);
|
|
518 |
|
|
519 |
_dirkeys =
|
|
520 |
(keys[SDLK_LEFT] ? 1 : 0) +
|
|
521 |
(keys[SDLK_UP] ? 2 : 0) +
|
|
522 |
(keys[SDLK_RIGHT] ? 4 : 0) +
|
|
523 |
(keys[SDLK_DOWN] ? 8 : 0);
|
|
524 |
|
|
525 |
GameLoop();
|
|
526 |
|
|
527 |
_screen.dst_ptr = _sdl_screen->pixels;
|
|
528 |
UpdateWindows();
|
|
529 |
|
|
530 |
if ((cur_ticks=SDL_CALL SDL_GetTicks()) > next_tick) {
|
|
531 |
next_tick = cur_ticks;
|
|
532 |
} else {
|
|
533 |
DrawSurfaceToScreen();
|
|
534 |
}
|
|
535 |
|
|
536 |
#if defined(_DEBUG)
|
|
537 |
if (_shift_pressed) {
|
|
538 |
#else
|
|
539 |
if (keys[SDLK_TAB]) {
|
|
540 |
#endif
|
|
541 |
_fast_forward |= 2;
|
|
542 |
} else if (_fast_forward&2) {
|
|
543 |
_fast_forward = 0;
|
|
544 |
}
|
|
545 |
|
|
546 |
if (!_fast_forward || _pause) {
|
|
547 |
int d = next_tick - SDL_CALL SDL_GetTicks();
|
|
548 |
if (d > 0) {
|
|
549 |
SDL_CALL SDL_Delay(d);
|
|
550 |
}
|
|
551 |
} else {
|
|
552 |
next_tick = SDL_CALL SDL_GetTicks();
|
|
553 |
}
|
|
554 |
next_tick += 30;
|
|
555 |
|
|
556 |
if (_exit_game)
|
|
557 |
return ML_QUIT;
|
|
558 |
}
|
|
559 |
}
|
|
560 |
|
|
561 |
static bool SdlVideoChangeRes(int w, int h)
|
|
562 |
{
|
|
563 |
// see if the mode is available
|
|
564 |
if (GetAvailableVideoMode(&w, &h) != 1)
|
|
565 |
return false;
|
|
566 |
|
|
567 |
CreateMainSurface(w, h);
|
|
568 |
return true;
|
|
569 |
}
|
|
570 |
|
|
571 |
const HalVideoDriver _sdl_video_driver = {
|
|
572 |
SdlVideoStart,
|
|
573 |
SdlVideoStop,
|
|
574 |
SdlVideoMakeDirty,
|
|
575 |
SdlVideoMainLoop,
|
|
576 |
SdlVideoChangeRes,
|
|
577 |
};
|
|
578 |
|
|
579 |
static void CDECL fill_sound_buffer(void *userdata, Uint8 *stream, int len)
|
|
580 |
{
|
|
581 |
MxMixSamples(_mixer, stream, len >> 2);
|
|
582 |
}
|
|
583 |
|
|
584 |
static char *SdlSoundStart(char **parm)
|
|
585 |
{
|
|
586 |
SDL_AudioSpec spec;
|
|
587 |
|
|
588 |
{char *s;if ((s = SdlOpen(SDL_INIT_AUDIO)) != NULL) return s;}
|
|
589 |
spec.freq = GetDriverParamInt(parm, "hz", 11025);
|
|
590 |
spec.format = AUDIO_S16SYS;
|
|
591 |
spec.channels = 2;
|
|
592 |
spec.samples = 512;
|
|
593 |
*(void**)&spec.callback = fill_sound_buffer;
|
|
594 |
SDL_CALL SDL_OpenAudio(&spec, &spec);
|
|
595 |
SDL_CALL SDL_PauseAudio(0);
|
|
596 |
return NULL;
|
|
597 |
}
|
|
598 |
|
|
599 |
static void SdlSoundStop()
|
|
600 |
{
|
|
601 |
SDL_CALL SDL_CloseAudio();
|
|
602 |
SdlClose(SDL_INIT_AUDIO);
|
|
603 |
}
|
|
604 |
|
|
605 |
const HalSoundDriver _sdl_sound_driver = {
|
|
606 |
SdlSoundStart,
|
|
607 |
SdlSoundStop,
|
|
608 |
};
|
|
609 |
|
|
610 |
|
|
611 |
#include "viewport.h"
|
|
612 |
void redsq_debug(int tile)
|
|
613 |
{
|
|
614 |
_thd.redsq = tile;
|
|
615 |
MarkWholeScreenDirty();
|
|
616 |
_screen.dst_ptr = _sdl_screen->pixels;
|
|
617 |
UpdateWindows();
|
|
618 |
|
|
619 |
SdlVideoMakeDirty(0,0,_screen.width,_screen.height);
|
|
620 |
DrawSurfaceToScreen();
|
|
621 |
}
|
|
622 |
|
|
623 |
void DbgRedraw()
|
|
624 |
{
|
|
625 |
SdlVideoMakeDirty(0,0,_screen.width,_screen.height);
|
|
626 |
DrawSurfaceToScreen();
|
|
627 |
}
|
|
628 |
#endif // WITH_SDL
|