DEADSOFTWARE

Added support for win9x using allegro
[d2df-sdl.git] / src / wrappers / sdl2 / sdl2allegro.inc
1 {$INCLUDE ../shared/a_modes.inc}
2 {$SCOPEDENUMS OFF}
4 interface
6 uses Allegro;
8 {$I sdltype.inc}
9 {$I sdlpixels.inc}
10 {$I sdlscancode.inc}
12 const
13 DEFAULT_DEPTH = 8;
15 const
16 SDL_HAT_CENTERED = $00;
17 SDL_HAT_UP = $01;
18 SDL_HAT_RIGHT = $02;
19 SDL_HAT_DOWN = $04;
20 SDL_HAT_LEFT = $08;
21 SDL_HAT_RIGHTUP = SDL_HAT_RIGHT or SDL_HAT_UP;
22 SDL_HAT_RIGHTDOWN = SDL_HAT_RIGHT or SDL_HAT_DOWN;
23 SDL_HAT_LEFTUP = SDL_HAT_LEFT or SDL_HAT_UP;
24 SDL_HAT_LEFTDOWN = SDL_HAT_LEFT or SDL_HAT_DOWN;
26 const
27 AUDIO_U8 = $0008;
28 AUDIO_S8 = $8008;
29 AUDIO_U16LSB = $0010;
30 AUDIO_S16LSB = $8010;
31 AUDIO_U16MSB = $1010;
32 AUDIO_S16MSB = $9010;
33 AUDIO_U16 = AUDIO_U16LSB;
34 AUDIO_S16 = AUDIO_S16LSB;
36 const
37 SDL_TOUCH_MOUSEID = UInt32(-1);
39 type
40 PSDL_Window = ^TSDL_Window;
41 TSDL_Window = record
42 w, h, mode: Integer; (* Allegro-specific *)
43 end;
45 PSDL_Joystick = Pointer;
46 TSDL_JoystickGUID = record
47 data: array[0..15] of UInt8;
48 end;
49 TSDL_JoystickID = SInt32;
51 PSDL_RWops = ^TSDL_RWops;
53 TSize = function(context: PSDL_RWops): SInt64; cdecl;
54 TSeek = function(context: PSDL_RWops; offset: SInt64; whence: SInt32): SInt64; cdecl;
55 TRead = function(context: PSDL_RWops; ptr: Pointer; size: size_t; maxnum: size_t): size_t; cdecl;
56 TWrite = function(context: PSDL_RWops; const ptr: Pointer; size: size_t; num: size_t): size_t; cdecl;
57 TClose = function(context: PSDL_RWops): SInt32; cdecl;
59 TStdio = record
60 autoclose: TSDL_Bool;
61 fp: file;
62 end;
64 TMem = record
65 base: PUInt8;
66 here: PUInt8;
67 stop: PUInt8;
68 end;
70 TUnknown = record
71 data1: Pointer;
72 end;
74 TSDL_RWops = packed record
75 size: TSize;
76 seek: TSeek;
77 read: TRead;
78 write: TWrite;
79 close: TClose;
80 _type: UInt32;
81 case Integer of
82 0: (stdio: TStdio);
83 1: (mem: TMem);
84 2: (unknown: TUnknown);
85 {$IFDEF ANDROID}
86 //3: (androidio: TAndroidIO);
87 {$ENDIF}
88 {$IFDEF WINDOWS}
89 //3: (windowsio: TWindowsIO);
90 {$ENDIF}
91 end;
93 PSDL_TouchID = ^TSDL_TouchID;
94 TSDL_TouchID = SInt64;
96 PSDL_FingerID = ^TSDL_FingerID;
97 TSDL_FingerID = SInt64;
100 const
101 { Touch events }
102 SDL_FINGERDOWN = $700;
103 SDL_FINGERUP = $701;
104 SDL_FINGERMOTION = $702;
106 type
107 TSDL_TouchFingerEvent = record
108 type_: UInt32;
109 timestamp: UInt32;
110 touchId: TSDL_TouchID;
111 fingerId: TSDL_FingerID;
112 x, y, dx, dy: Float;
113 pressure: Float;
114 end;
117 const
118 SDL_WINDOWPOS_CENTERED_MASK = $2FFF0000;
119 SDL_WINDOWPOS_CENTERED = SDL_WINDOWPOS_CENTERED_MASK or 0;
121 type
122 PSDL_DisplayMode = ^TSDL_DisplayMode;
123 TSDL_DisplayMode = record
124 format: UInt32;
125 w: SInt32;
126 h: SInt32;
127 refresh_rate: SInt32;
128 driverdata: Pointer;
129 end;
131 TSDL_GLContext = Pointer;
133 const
134 SDL_WINDOW_FULLSCREEN = $00000001;
135 SDL_WINDOW_OPENGL = $00000002;
136 SDL_WINDOW_SHOWN = $00000004;
137 SDL_WINDOW_HIDDEN = $00000008;
138 SDL_WINDOW_BORDERLESS = $00000010;
139 SDL_WINDOW_RESIZABLE = $00000020;
140 SDL_WINDOW_MINIMIZED = $00000040;
141 SDL_WINDOW_MAXIMIZED = $00000080;
142 SDL_WINDOW_INPUT_GRABBED = $00000100;
143 SDL_WINDOW_INPUT_FOCUS = $00000200;
144 SDL_WINDOW_MOUSE_FOCUS = $00000400;
145 SDL_WINDOW_FULLSCREEN_DESKTOP = SDL_WINDOW_FULLSCREEN or $00001000;
146 SDL_WINDOW_FOREIGN = $00000800;
147 SDL_WINDOW_ALLOW_HIGHDPI = $00002000;
149 SDL_WINDOWEVENT_EXPOSED = 3;
150 SDL_WINDOWEVENT_MOVED = 4;
151 SDL_WINDOWEVENT_RESIZED = 5;
152 SDL_WINDOWEVENT_MINIMIZED = 7;
153 SDL_WINDOWEVENT_MAXIMIZED = 8;
154 SDL_WINDOWEVENT_RESTORED = 9;
155 SDL_WINDOWEVENT_FOCUS_GAINED = 12;
156 SDL_WINDOWEVENT_FOCUS_LOST = 13;
158 SDL_DISABLE = 0;
159 SDL_ENABLE = 1;
161 SDL_TEXTINPUTEVENT_TEXT_SIZE = 32;
163 SDL_FIRSTEVENT = 0;
164 SDL_COMMONEVENT = 1;
165 SDL_QUITEV = $100;
166 SDL_WINDOWEVENT = $200;
167 SDL_KEYDOWN = $300;
168 SDL_KEYUP = $301;
169 //SDL_TEXTEDITING = $302;
170 SDL_TEXTINPUT = $303;
172 type
173 TSDL_WindowEvent = record
174 type_: UInt32; // SDL_WINDOWEVENT
175 timestamp: UInt32;
176 windowID: UInt32; // The associated window
177 event: UInt8; // SDL_WindowEventID
178 padding1: UInt8;
179 padding2: UInt8;
180 padding3: UInt8;
181 data1: SInt32; // event dependent data
182 data2: SInt32; // event dependent data
183 end;
185 PSDL_Keysym = ^TSDL_Keysym;
186 TSDL_Keysym = record
187 scancode: TSDL_ScanCode; // SDL physical key code - see SDL_Scancode for details
188 sym: TSDL_KeyCode; // SDL virtual key code - see SDL_Keycode for details
189 _mod: UInt16; // current key modifiers
190 unicode: UInt32; // (deprecated) use SDL_TextInputEvent instead
191 end;
193 TSDL_KeyboardEvent = record
194 type_: UInt32; // SDL_KEYDOWN or SDL_KEYUP
195 timestamp: UInt32;
196 windowID: UInt32; // The window with keyboard focus, if any
197 state: UInt8; // SDL_PRESSED or SDL_RELEASED
198 _repeat: UInt8; // Non-zero if this is a key repeat
199 padding2: UInt8;
200 padding3: UInt8;
201 keysym: TSDL_KeySym; // The key that was pressed or released
202 end;
204 TSDL_TextInputEvent = record
205 type_: UInt32; // SDL_TEXTINPUT
206 timestamp: UInt32;
207 windowID: UInt32; // The window with keyboard focus, if any
208 text: array[0..SDL_TEXTINPUTEVENT_TEXT_SIZE] of Char; // The input text
209 end;
211 TSDL_QuitEvent = record
212 type_: UInt32; // SDL_QUIT
213 timestamp: UInt32;
214 end;
216 PSDL_Event = ^TSDL_Event;
217 TSDL_Event = record
218 case Integer of
219 0: (type_: UInt32);
220 //SDL_COMMONEVENT: (common: TSDL_CommonEvent);
221 SDL_WINDOWEVENT: (window: TSDL_WindowEvent);
222 SDL_KEYUP,
223 SDL_KEYDOWN: (key: TSDL_KeyboardEvent);
224 //SDL_TEXTEDITING: (edit: TSDL_TextEditingEvent);
225 SDL_TEXTINPUT: (text: TSDL_TextInputEvent);
226 //SDL_MOUSEMOTION: (motion: TSDL_MouseMotionEvent);
227 //SDL_MOUSEBUTTONUP,
228 //SDL_MOUSEBUTTONDOWN: (button: TSDL_MouseButtonEvent);
229 //SDL_MOUSEWHEEL: (wheel: TSDL_MouseWheelEvent);
230 //SDL_JOYAXISMOTION: (jaxis: TSDL_JoyAxisEvent);
231 //SDL_JOYBALLMOTION: (jball: TSDL_JoyBallEvent);
232 //SDL_JOYHATMOTION: (jhat: TSDL_JoyHatEvent);
233 //SDL_JOYBUTTONDOWN,
234 //SDL_JOYBUTTONUP: (jbutton: TSDL_JoyButtonEvent);
235 //SDL_JOYDEVICEADDED,
236 //SDL_JOYDEVICEREMOVED: (jdevice: TSDL_JoyDeviceEvent);
237 //SDL_CONTROLLERAXISMOTION: (caxis: TSDL_ControllerAxisEvent);
238 //SDL_CONTROLLERBUTTONUP,
239 //SDL_CONTROLLERBUTTONDOWN: (cbutton: TSDL_ControllerButtonEvent);
240 //SDL_CONTROLLERDEVICEADDED,
241 //SDL_CONTROLLERDEVICEREMOVED,
242 //SDL_CONTROLLERDEVICEREMAPPED: (cdevice: TSDL_ControllerDeviceEvent);
243 //SDL_AUDIODEVICEADDED,
244 //SDL_AUDIODEVICEREMOVED: (adevice: TSDL_AudioDeviceEvent);
245 SDL_QUITEV: (quit: TSDL_QuitEvent);
246 //SDL_USEREVENT: (user: TSDL_UserEvent);
247 //SDL_SYSWMEVENT: (syswm: TSDL_SysWMEvent);
248 SDL_FINGERDOWN,
249 SDL_FINGERUP,
250 SDL_FINGERMOTION: (tfinger: TSDL_TouchFingerEvent);
251 //SDL_MULTIGESTURE: (mgesture: TSDL_MultiGestureEvent);
252 //SDL_DOLLARGESTURE,SDL_DOLLARRECORD: (dgesture: TSDL_DollarGestureEvent);
253 //SDL_DROPFILE: (drop: TSDL_DropEvent);
254 end;
256 const
257 SDL_GL_RED_SIZE = 0;
258 SDL_GL_GREEN_SIZE = 1;
259 SDL_GL_BLUE_SIZE = 2;
260 SDL_GL_ALPHA_SIZE = 3;
261 SDL_GL_BUFFER_SIZE = 4;
262 SDL_GL_DOUBLEBUFFER = 5;
263 SDL_GL_DEPTH_SIZE = 6;
264 SDL_GL_STENCIL_SIZE = 7;
265 SDL_GL_ACCUM_RED_SIZE = 8;
266 SDL_GL_ACCUM_GREEN_SIZE = 9;
267 SDL_GL_ACCUM_BLUE_SIZE = 10;
268 SDL_GL_ACCUM_ALPHA_SIZE = 11;
269 SDL_GL_STEREO = 12;
270 SDL_GL_MULTISAMPLEBUFFERS = 13;
271 SDL_GL_MULTISAMPLESAMPLES = 14;
272 SDL_GL_ACCELERATED_VISUAL = 15;
273 SDL_GL_RETAINED_BACKING = 16;
274 SDL_GL_CONTEXT_MAJOR_VERSION = 17;
275 SDL_GL_CONTEXT_MINOR_VERSION = 18;
276 SDL_GL_CONTEXT_EGL = 19;
277 SDL_GL_CONTEXT_FLAGS = 20;
278 SDL_GL_CONTEXT_PROFILE_MASK = 21;
279 SDL_GL_SHARE_WITH_CURRENT_CONTEXT = 22;
280 SDL_GL_FRAMEBUFFER_SRGB_CAPABLE = 23;
282 type
283 TSDL_GLattr = DWord;
285 const
286 SDL_INIT_TIMER = $00000001;
287 SDL_INIT_AUDIO = $00000010;
288 SDL_INIT_VIDEO = $00000020;
289 SDL_INIT_JOYSTICK = $00000200;
290 SDL_INIT_HAPTIC = $00001000;
291 SDL_INIT_GAMECONTROLLER = $00002000; //turn on game controller also implicitly does JOYSTICK
292 SDL_INIT_NOPARACHUTE = $00100000; //Don't catch fatal signals
293 SDL_INIT_EVERYTHING = SDL_INIT_TIMER or SDL_INIT_AUDIO or
294 SDL_INIT_VIDEO or SDL_INIT_JOYSTICK or
295 SDL_INIT_HAPTIC or SDL_INIT_GAMECONTROLLER;
297 SDL_HINT_ACCELEROMETER_AS_JOYSTICK = 'SDL_ACCELEROMETER_AS_JOYSTICK';
299 var
300 sdl2allegro_screen: PBITMAP;
302 /// FUNCTIONS ///
304 function SDL_SetWindowGammaRamp(window: PSDL_Window; const red: PUInt16; const green: PUInt16; const blue: PUInt16): SInt32;
305 function SDL_GetWindowGammaRamp(window: PSDL_Window; red: PUInt16; green: PUInt16; blue: PUInt16): SInt32;
307 function SDL_NumJoysticks: SInt32;
308 function SDL_JoystickOpen(device_index: SInt32): PSDL_Joystick;
309 function SDL_JoystickName(joystick: PSDL_Joystick): PAnsiChar;
310 function SDL_JoystickNumAxes(joystick: PSDL_Joystick): SInt32;
311 function SDL_JoystickNumButtons(joystick: PSDL_Joystick): SInt32;
312 function SDL_JoystickNumHats(joystick: PSDL_Joystick): SInt32;
313 procedure SDL_JoystickClose(joystick: PSDL_Joystick);
314 procedure SDL_JoystickUpdate;
315 function SDL_JoystickGetButton(joystick: PSDL_Joystick; button: SInt32): UInt8;
316 function SDL_JoystickGetAxis(joystick: PSDL_Joystick; axis: SInt32): SInt16;
317 function SDL_JoystickGetHat(joystick: PSDL_Joystick; hat: SInt32): UInt8;
319 function SDL_GetScancodeName(scancode: TSDL_ScanCode): PAnsiChar;
321 function SDL_GetPerformanceCounter: UInt64;
322 function SDL_GetPerformanceFrequency: UInt64;
324 function SDL_RWFromFile(const _file: PAnsiChar; const mode: PAnsiChar): PSDL_RWops;
325 function SDL_RWFromConstMem(const mem: Pointer; size: SInt32): PSDL_RWops;
327 function SDL_GetDisplayDPI(displayIndex: SInt32; ddpi, hdpi, vdpi: PFloat): SInt32;
328 function SDL_IsTextInputActive: TSDL_Bool;
329 procedure SDL_StartTextInput;
330 procedure SDL_StopTextInput;
331 function SDL_GetNumTouchDevices: SInt32;
333 function SDL_GetDesktopDisplayMode(displayIndex: SInt32; mode: PSDL_DisplayMode): SInt32;
334 function SDL_GetClosestDisplayMode(displayIndex: SInt32; const mode: PSDL_DisplayMode; closest: PSDL_DisplayMode): PSDL_DisplayMode;
336 procedure SDL_DestroyWindow(window: PSDL_Window);
337 procedure SDL_GL_DeleteContext(context: TSDL_GLContext);
338 procedure SDL_RestoreWindow(window: PSDL_Window);
339 procedure SDL_SetWindowSize(window: PSDL_Window; w: SInt32; h: SInt32);
340 function SDL_CreateWindow(const title: PAnsiChar; x: SInt32; y: SInt32; w: SInt32; h: SInt32; flags: UInt32): PSDL_Window;
341 function SDL_SetWindowFullscreen(window: PSDL_Window; flags: UInt32): SInt32;
342 function SDL_GL_MakeCurrent(window: PSDL_Window; context: TSDL_GLContext): SInt32;
343 procedure SDL_GetWindowSize(window: PSDL_Window; w: PInt; h: PInt);
344 function SDL_GetNumDisplayModes(displayIndex: SInt32): SInt32;
345 function SDL_GetDisplayMode(displayIndex: SInt32; modeIndex: SInt32; mode: PSDL_DisplayMode): SInt32;
347 function SDL_ShowCursor(toggle: SInt32): SInt32;
348 procedure SDL_Delay(ms: UInt32);
349 procedure SDL_GL_SwapWindow(window: PSDL_Window);
351 function SDL_GL_CreateContext(window: PSDL_Window): TSDL_GLContext;
352 function SDL_GetTicks: UInt32;
354 function SDL_PushEvent(event: PSDL_Event): SInt32;
355 function SDL_PollEvent(event: PSDL_Event): SInt32;
356 function SDL_GL_SetSwapInterval(interval: SInt32): SInt32;
357 function SDL_GL_SetAttribute(attr: TSDL_GLattr; value: SInt32): SInt32;
358 function SDL_GL_GetAttribute(attr: TSDL_GLattr; value: PInt): SInt32;
360 function SDL_GetError: PAnsiChar;
362 function SDL_Init(flags: UInt32): SInt32;
363 procedure SDL_Quit();
364 function SDL_SetHint( const name: PChar; const value: PChar) : boolean;
366 implementation
368 uses
369 {$IFDEF GO32V2}
370 go32,
371 {$ENDIF}
372 e_Log, SysUtils, ctypes;
374 const
375 maxKeyBuffer = 64;
377 var
378 keyring: array [0..maxKeyBuffer - 1] of Integer;
379 keybeg, keyend: Integer;
380 ticks: UInt32;
381 quit: Boolean;
383 function IsEmptyKeyboard: Boolean;
384 begin
385 result := keybeg = keyend
386 end;
388 function IsFullKeyboard: Boolean;
389 begin
390 result := (keybeg - 1) mod maxKeyBuffer = keyend
391 end;
393 function NextScancode: Integer;
394 begin
395 result := 0;
396 if not IsEmptyKeyboard then
397 begin
398 result := keyring[keybeg];
399 keybeg := (keybeg + 1) mod maxKeyBuffer
400 end
401 end;
403 procedure KeyboardWatcher (scancode: Integer); cdecl;
404 begin
405 if (keybeg - 1) mod maxKeyBuffer <> keyend then (* not IsFullKeyboard *)
406 begin
407 keyring[keyend] := scancode;
408 keyend := (keyend + 1) mod maxKeyBuffer
409 end
410 end;
411 procedure KeyboardWatcherEND; begin end;
413 /// MACRO ///
415 //from "sdl_pixels.h"
417 function SDL_PIXELFLAG(X: Cardinal): Cardinal;
418 begin
419 Result := (X shr 28) and $0F;
420 end;
422 function SDL_PIXELTYPE(X: Cardinal): Cardinal;
423 begin
424 Result := (X shr 24) and $0F;
425 end;
427 function SDL_PIXELORDER(X: Cardinal): Cardinal;
428 begin
429 Result := (X shr 20) and $0F;
430 end;
432 function SDL_PIXELLAYOUT(X: Cardinal): Cardinal;
433 begin
434 Result := (X shr 16) and $0F;
435 end;
437 function SDL_BITSPERPIXEL(X: Cardinal): Cardinal;
438 begin
439 Result := (X shr 8) and $FF;
440 end;
442 function SDL_IsPixelFormat_FOURCC(format: Variant): Boolean;
443 begin
444 Result := format and SDL_PIXELFLAG(format) <> 1;
445 end;
447 /// FUNCTIONS ////
449 (********** FILES **********)
451 function SDL_RWFromFile(const _file: PAnsiChar; const mode: PAnsiChar): PSDL_RWops;
452 begin
453 result := nil
454 end;
456 function SDL_RWFromConstMem(const mem: Pointer; size: SInt32): PSDL_RWops;
457 begin
458 result := nil
459 end;
461 (********** KEYBOARD **********)
463 function SDL_GetScancodeName(scancode: TSDL_ScanCode): PAnsiChar;
464 begin
465 result := ''
466 end;
468 function SDL_IsTextInputActive: TSDL_Bool;
469 begin
470 result := SDL_FALSE
471 end;
473 procedure SDL_StartTextInput;
474 begin
475 end;
477 procedure SDL_StopTextInput;
478 begin
479 end;
481 (********** JOYSTICK **********)
483 function SDL_NumJoysticks: SInt32;
484 begin
485 result := 0
486 end;
488 function SDL_JoystickOpen(device_index: SInt32): PSDL_Joystick;
489 begin
490 result := nil
491 end;
493 function SDL_JoystickName(joystick: PSDL_Joystick): PAnsiChar;
494 begin
495 result := ''
496 end;
498 function SDL_JoystickNumAxes(joystick: PSDL_Joystick): SInt32;
499 begin
500 result := 0
501 end;
503 function SDL_JoystickNumButtons(joystick: PSDL_Joystick): SInt32;
504 begin
505 result := 0
506 end;
508 function SDL_JoystickNumHats(joystick: PSDL_Joystick): SInt32;
509 begin
510 result := 0
511 end;
513 procedure SDL_JoystickClose(joystick: PSDL_Joystick);
514 begin
515 end;
517 procedure SDL_JoystickUpdate;
518 begin
519 end;
521 function SDL_JoystickGetButton(joystick: PSDL_Joystick; button: SInt32): UInt8;
522 begin
523 result := 0
524 end;
526 function SDL_JoystickGetAxis(joystick: PSDL_Joystick; axis: SInt32): SInt16;
527 begin
528 result := 0
529 end;
531 function SDL_JoystickGetHat(joystick: PSDL_Joystick; hat: SInt32): UInt8;
532 begin
533 result := 0
534 end;
536 (********** TOUCH **********)
538 function SDL_GetNumTouchDevices: SInt32;
539 begin
540 result := 0
541 end;
543 (********** TIMERS **********)
545 procedure AllegroTimerCallback; cdecl;
546 begin
547 inc(ticks)
548 end;
549 procedure AllegroTimerCallbackEND; begin end;
551 function SDL_GetPerformanceCounter: UInt64;
552 begin
553 //e_LogWriteln('SDL_GetPerformanceCounter');
554 (* TODO *)
555 result := ticks;
556 end;
558 function SDL_GetPerformanceFrequency: UInt64;
559 begin
560 //e_LogWriteln('SDL_GetPerformanceFrequency');
561 (* TODO *)
562 result := 1
563 end;
565 procedure SDL_Delay(ms: UInt32);
566 begin
567 //e_LogWriteln('SDL_Delay');
568 rest(ms)
569 end;
571 function SDL_GetTicks: UInt32;
572 begin
573 //e_LogWriteln('SDL_GetTicks');
574 result := ticks;
575 end;
577 (********** DISPLAY MODES **********)
579 function SDL_GetDesktopDisplayMode(displayIndex: SInt32; mode: PSDL_DisplayMode): SInt32;
580 begin
581 e_LogWritefln('SDL_GetDesktopDisplayMode %s', [displayIndex]);
582 result := -1;
583 mode.format := SDL_PIXELFORMAT_UNKNOWN; (* FIXIT *)
584 mode.refresh_rate := 0;
585 mode.driverdata := nil;
586 if get_desktop_resolution(@mode.w, @mode.h) = 0 then
587 result := 0
588 end;
590 function SDL_GetClosestDisplayMode(displayIndex: SInt32; const mode: PSDL_DisplayMode; closest: PSDL_DisplayMode): PSDL_DisplayMode;
591 begin
592 e_LogWritefln('SDL_GetClosestDisplayMode %s', [displayIndex]);
593 result := nil;
594 end;
596 function SDL_GetNumDisplayModes(displayIndex: SInt32): SInt32;
597 var m: PGFX_MODE_LIST;
598 begin
599 e_LogWritefln('SDL_GetNumDisplayModes %s', [displayIndex]);
600 result := -1;
601 m := get_gfx_mode_list(GFX_AUTODETECT);
602 if m <> nil then
603 begin
604 result := m.num_modes;
605 destroy_gfx_mode_list(m);
606 end
607 end;
609 function SDL_GetDisplayMode(displayIndex: SInt32; modeIndex: SInt32; mode: PSDL_DisplayMode): SInt32;
610 var m: PGFX_MODE_LIST;
611 begin
612 e_LogWritefln('SDL_GetDisplayMode %s %s', [displayIndex, modeIndex]);
613 result := -1;
614 m := get_gfx_mode_list(GFX_AUTODETECT);
615 if (m <> nil) and (modeIndex >= 0) and (modeIndex < m.num_modes) then
616 begin
617 mode.format := SDL_PIXELFORMAT_UNKNOWN; (* FIX IT *)
618 mode.w := m.mode[modeIndex].width;
619 mode.h := m.mode[modeIndex].height;
620 mode.refresh_rate := 0;
621 mode.driverdata := nil;
622 destroy_gfx_mode_list(m);
623 result := 0
624 end
625 end;
627 function SDL_GetDisplayDPI(displayIndex: SInt32; ddpi, hdpi, vdpi: PFloat): SInt32;
628 begin
629 result := -1
630 end;
632 (*********** WINDOW MANAGEMENT **********)
634 function SDL_CreateWindow(const title: PAnsiChar; x: SInt32; y: SInt32; w: SInt32; h: SInt32; flags: UInt32): PSDL_Window;
635 var window: PSDL_Window; mode: Integer;
636 begin
637 e_LogWritefln('SDL_CreateWindow %s %s %s %s %s %u', [title, x, y, w, h, flags]);
638 result := nil;
640 {$IF DEFINED(GO32V2)}
641 mode := GFX_AUTODETECT;
642 {$ELSE}
643 if (flags and (SDL_WINDOW_FULLSCREEN or SDL_WINDOW_FULLSCREEN_DESKTOP)) <> 0 then
644 mode := GFX_AUTODETECT_FULLSCREEN
645 else
646 mode := GFX_AUTODETECT_WINDOWED;
647 {$ENDIF}
649 set_color_depth(DEFAULT_DEPTH);
650 if set_gfx_mode(mode, w, h, 0, 0) = 0 then
651 begin
652 new(window);
653 set_window_title(title);
654 if sdl2allegro_screen <> nil then
655 destroy_bitmap(sdl2allegro_screen);
656 sdl2allegro_screen := create_system_bitmap(w, h);
657 if sdl2allegro_screen = nil then
658 sdl2allegro_screen := create_bitmap(w, h);
659 ASSERT(sdl2allegro_screen <> nil);
660 window.w := w;
661 window.h := h;
662 window.mode := mode;
663 result := window
664 end
665 end;
667 function SDL_SetWindowFullscreen(window: PSDL_Window; flags: UInt32): SInt32;
668 var mode: Integer;
669 begin
670 e_LogWritefln('SDL_SetWindowFullscreen %u', [flags]);
671 result := -1;
672 if window = nil then exit;
674 {$IF DEFINED(GO32V2)}
675 mode := GFX_AUTODETECT;
676 {$ELSE}
677 if (flags and (SDL_WINDOW_FULLSCREEN or SDL_WINDOW_FULLSCREEN_DESKTOP)) <> 0 then
678 mode := GFX_AUTODETECT_FULLSCREEN
679 else
680 mode := GFX_AUTODETECT_WINDOWED;
681 {$ENDIF}
683 set_color_depth(DEFAULT_DEPTH);
684 if set_gfx_mode(mode, window.w, window.h, 0, 0) = 0 then
685 begin
686 if sdl2allegro_screen <> nil then
687 destroy_bitmap(sdl2allegro_screen);
688 sdl2allegro_screen := create_system_bitmap(window.w, window.h);
689 if sdl2allegro_screen = nil then
690 sdl2allegro_screen := create_bitmap(window.w, window.h);
691 ASSERT(sdl2allegro_screen <> nil);
692 set_palette(desktop_palette);
693 window.mode := mode;
694 result := 0
695 end
696 end;
698 procedure SDL_SetWindowSize(window: PSDL_Window; w: SInt32; h: SInt32);
699 begin
700 e_LogWritefln('SDL_SetWindowSize %s %s', [w, h]);
701 if window = nil then exit;
702 set_color_depth(DEFAULT_DEPTH);
703 if set_gfx_mode(window.mode, w, h, 0, 0) = 0 then
704 begin
705 if sdl2allegro_screen <> nil then
706 destroy_bitmap(sdl2allegro_screen);
707 sdl2allegro_screen := create_system_bitmap(w, h);
708 if sdl2allegro_screen = nil then
709 sdl2allegro_screen := create_bitmap(w, h);
710 ASSERT(sdl2allegro_screen <> nil);
711 set_palette(desktop_palette);
712 window.w := w;
713 window.h := h;
714 end
715 end;
717 procedure SDL_DestroyWindow(window: PSDL_Window);
718 begin
719 e_LogWriteln('SDL_DestroyWindow');
720 if window = nil then exit;
721 if sdl2allegro_screen <> nil then
722 destroy_bitmap(sdl2allegro_screen);
723 sdl2allegro_screen := nil;
724 dispose(window)
725 end;
727 procedure SDL_GetWindowSize(window: PSDL_Window; w: PInt; h: PInt);
728 begin
729 e_LogWriteln('SDL_GetWindowSize');
730 if window = nil then exit;
731 if w <> nil then
732 w^ := window.w;
733 if h <> nil then
734 h^ := window.h;
735 end;
737 procedure SDL_RestoreWindow(window: PSDL_Window);
738 begin
739 e_LogWriteln('SDL_RestoreWindow');
740 if window = nil then exit;
741 (* stub *)
742 end;
744 function SDL_SetWindowGammaRamp(window: PSDL_Window; const red: PUInt16; const green: PUInt16; const blue: PUInt16): SInt32;
745 begin
746 e_LogWriteln('SDL_SetWindowGammaRamp');
747 result := -1;
748 if window = nil then exit;
749 result := 0
750 end;
752 function SDL_GetWindowGammaRamp(window: PSDL_Window; red: PUInt16; green: PUInt16; blue: PUInt16): SInt32;
753 begin
754 e_LogWriteln('SDL_GetWindowGammaRamp');
755 result := -1;
756 if window = nil then exit;
757 result := 0
758 end;
760 (********** OPENGL MANAGEMENT **********)
762 function SDL_GL_MakeCurrent(window: PSDL_Window; context: TSDL_GLContext): SInt32;
763 begin
764 e_LogWriteln('SDL_GL_MakeCurrent');
765 result := 0
766 end;
768 procedure SDL_GL_DeleteContext(context: TSDL_GLContext);
769 begin
770 e_LogWriteln('SDL_GL_DeleteContext');
771 end;
773 procedure SDL_GL_SwapWindow(window: PSDL_Window);
774 var res: cint;
775 begin
776 //e_LogWriteln('SDL_GL_SwapWindow');
777 ASSERT(sdl2allegro_screen <> nil);
778 acquire_screen;
779 blit(sdl2allegro_screen, screen, 0, 0, 0, 0, sdl2allegro_screen.w, sdl2allegro_screen.h);
780 res := show_video_bitmap(screen);
781 release_screen;
782 // ASSERT(res = 0);
783 end;
785 function SDL_GL_CreateContext(window: PSDL_Window): TSDL_GLContext;
786 begin
787 e_LogWriteln('SDL_GL_CreateContext');
788 result := window
789 end;
791 function SDL_GL_SetSwapInterval(interval: SInt32): SInt32;
792 begin
793 e_LogWritefln('SDL_GL_SetSwapInterval %s', [interval]);
794 result := 0
795 end;
797 function SDL_GL_SetAttribute(attr: TSDL_GLattr; value: SInt32): SInt32;
798 begin
799 e_LogWritefln('SDL_GL_SetAttribute %s %s', [attr, value]);
800 result := -1;
801 end;
803 function SDL_GL_GetAttribute(attr: TSDL_GLattr; value: PInt): SInt32;
804 begin
805 e_LogWritefln('SDL_GL_GetAttribute %s', [attr]);
806 value^ := 0; result := -1
807 end;
809 (********** EVENTS **********)
811 function SDL_PushEvent(event: PSDL_Event): SInt32;
812 begin
813 result := 1;
814 case event.type_ of
815 SDL_QUITEV: quit := True;
816 else ASSERT(FALSE); result := 0
817 end
818 end;
820 function allegro_to_sdl_scancode (x: Integer): Integer;
821 begin
822 x := x and $7F;
823 case x of
824 KEY_A..KEY_Z: result := SDL_SCANCODE_A + (x - KEY_A);
825 KEY_0..KEY_0: result := SDL_SCANCODE_0 + (x - KEY_0);
826 KEY_0_PAD: result := SDL_SCANCODE_KP_0;
827 KEY_1_PAD..KEY_9_PAD: result := SDL_SCANCODE_KP_1 + (x - KEY_1_PAD);
828 KEY_F1..KEY_F12: result := SDL_SCANCODE_F1 + (x - KEY_F1);
829 KEY_ESC: result := SDL_SCANCODE_ESCAPE;
830 KEY_TILDE: result := SDL_SCANCODE_GRAVE;
831 KEY_MINUS: result := SDL_SCANCODE_MINUS;
832 KEY_EQUALS: result := SDL_SCANCODE_EQUALS;
833 KEY_BACKSPACE: result := SDL_SCANCODE_BACKSPACE;
834 KEY_TAB: result := SDL_SCANCODE_TAB;
835 KEY_OPENBRACE: result := SDL_SCANCODE_LEFTBRACKET;
836 KEY_CLOSEBRACE: result := SDL_SCANCODE_RIGHTBRACKET;
837 KEY_ENTER: result := SDL_SCANCODE_RETURN;
838 KEY_COLON: result := SDL_SCANCODE_SEMICOLON;
839 KEY_QUOTE: result := SDL_SCANCODE_APOSTROPHE;
840 KEY_BACKSLASH: result := SDL_SCANCODE_BACKSLASH;
841 KEY_BACKSLASH2: result := SDL_SCANCODE_NONUSHASH; (* ??? *)
842 KEY_COMMA: result := SDL_SCANCODE_COMMA;
843 (*KEY_STOP: result :=;*) (* ??? *)
844 KEY_SLASH: result := SDL_SCANCODE_SLASH;
845 KEY_SPACE: result := SDL_SCANCODE_SPACE;
846 KEY_INSERT: result := SDL_SCANCODE_INSERT;
847 KEY_DEL: result := SDL_SCANCODE_DELETE;
848 KEY_HOME: result := SDL_SCANCODE_HOME;
849 KEY_END: result := SDL_SCANCODE_END;
850 KEY_PGUP: result := SDL_SCANCODE_PAGEUP;
851 KEY_PGDN: result := SDL_SCANCODE_PAGEDOWN;
852 KEY_LEFT: result := SDL_SCANCODE_LEFT;
853 KEY_RIGHT: result := SDL_SCANCODE_RIGHT;
854 KEY_UP: result := SDL_SCANCODE_UP;
855 KEY_DOWN: result := SDL_SCANCODE_DOWN;
856 KEY_SLASH_PAD: result := SDL_SCANCODE_KP_DIVIDE;
857 KEY_ASTERISK: result := SDL_SCANCODE_KP_MULTIPLY;
858 KEY_MINUS_PAD: result := SDL_SCANCODE_KP_MINUS;
859 KEY_PLUS_PAD: result := SDL_SCANCODE_KP_PLUS;
860 KEY_DEL_PAD: result := SDL_SCANCODE_KP_PERIOD;
861 KEY_ENTER_PAD: result := SDL_SCANCODE_KP_ENTER;
862 KEY_PRTSCR: result := SDL_SCANCODE_PRINTSCREEN;
863 KEY_PAUSE: result := SDL_SCANCODE_PAUSE;
864 (*KEY_ABNT_C1: result :=;*) (* ??? *)
865 KEY_YEN: result := SDL_SCANCODE_INTERNATIONAL3;
866 KEY_KANA: result := SDL_SCANCODE_LANG3; (* ??? *)
867 (*KEY_CONVERT: result :=;*) (* ??? *)
868 (*KEY_NOCONVERT: result := ;*) (* ??? *)
869 KEY_AT: result := SDL_SCANCODE_KP_AT;
870 (*KEY_CIRCUMFLEX: result :=;*) (* ??? *)
871 KEY_COLON2: result := SDL_SCANCODE_KP_COLON;
872 (*KEY_KANJI: result :=;*) (* ??? *)
873 KEY_EQUALS_PAD: result := SDL_SCANCODE_KP_EQUALS;
874 (*KEY_BACKQUOTE: result :=;*) (* ??? *)
875 KEY_SEMICOLON: result := SDL_SCANCODE_SEMICOLON;
876 KEY_COMMAND: result := SDL_SCANCODE_LGUI;
877 (*KEY_UNKNOWN1: result :=;*)
878 (*KEY_UNKNOWN2: result :=;*)
879 (*KEY_UNKNOWN3: result :=;*)
880 (*KEY_UNKNOWN4: result :=;*)
881 (*KEY_UNKNOWN5: result :=;*)
882 (*KEY_UNKNOWN6: result :=;*)
883 (*KEY_UNKNOWN7: result :=;*)
884 (*KEY_UNKNOWN8: result :=;*)
885 KEY_LSHIFT: result := SDL_SCANCODE_LSHIFT;
886 KEY_RSHIFT: result := SDL_SCANCODE_RSHIFT;
887 KEY_LCONTROL: result := SDL_SCANCODE_LCTRL;
888 KEY_RCONTROL: result := SDL_SCANCODE_RCTRL;
889 KEY_ALT: result := SDL_SCANCODE_LALT;
890 KEY_ALTGR: result := SDL_SCANCODE_RALT;
891 KEY_LWIN: result := SDL_SCANCODE_LGUI;
892 KEY_RWIN: result := SDL_SCANCODE_RGUI;
893 KEY_MENU: result := SDL_SCANCODE_MODE; (* ??? *)
894 KEY_SCRLOCK: result := SDL_SCANCODE_SCROLLLOCK;
895 KEY_NUMLOCK: result := SDL_SCANCODE_NUMLOCKCLEAR;
896 KEY_CAPSLOCK: result := SDL_SCANCODE_CAPSLOCK;
897 else result := SDL_SCANCODE_UNKNOWN
898 end
899 end;
901 function SDL_PollEvent(event: PSDL_Event): SInt32;
902 var alscan: Integer;
903 begin
904 result := 0;
906 if quit then
907 begin
908 quit := False;
909 event.type_ := SDL_QUITEV;
910 event.quit.timestamp := ticks;
911 result := 1;
912 Exit
913 end;
915 poll_keyboard;
916 if not IsEmptyKeyboard then
917 begin
918 alscan := NextScancode;
919 if alscan and $80 = 0 then
920 event.type_ := SDL_KEYDOWN
921 else
922 event.type_ := SDL_KEYUP;
923 event.key.timestamp := ticks;
924 event.key.windowID := 0;
925 (**** df not use it?
926 if alscan and $80 = 0 then
927 event.key.state := SDL_PRESSED
928 else
929 event.key.state := SDL_RELEASED;
930 ****)
931 event.key.keysym.scancode := allegro_to_sdl_scancode(alscan);
932 event.key.keysym.sym := 0; (* df not use it? *)
933 event.key.keysym._mod := 0; (* df not use it? *)
934 result := 1;
935 Exit
936 end
937 end;
939 (********** MOUSE **********)
941 function SDL_ShowCursor(toggle: SInt32): SInt32;
942 begin
943 e_LogWritefln('SDL_ShowCursor %s', [toggle]);
944 result := 0
945 end;
947 (********** SDL **********)
949 procedure AllegroQuitCallback; cdecl;
950 begin
951 quit := True
952 end;
953 procedure AllegroQuitCallbackEND; begin end;
955 function SDL_SetHint( const name: PChar; const value: PChar) : boolean;
956 begin
957 e_LogWritefln('SDL_SetHint %s %s', [name, value]);
958 result := false
959 end;
961 function SDL_GetError: PAnsiChar;
962 begin
963 e_LogWritefln('SDL_GetError => %s', [allegro_error]);
964 result := allegro_error;
965 end;
967 {$IFDEF GO32V2}
968 (* HACK: allegro crashes while init without this *)
969 var
970 __crt0_argv: PPchar; cvar; external;
971 myargv: array [0..255] of Pchar;
973 procedure FIX_ARGV;
974 begin
975 __crt0_argv := @myargv[0];
976 myargv[0] := PChar(ParamStr(0));
977 e_LogWritefln('argv[0] = %s', [myargv[0]]);
978 end;
979 {$ENDIF}
981 function SDL_Init(flags: UInt32): SInt32;
982 begin
983 e_LogWritefln('SDL_Init %u', [flags]);
984 result := -1;
985 {$IFDEF GO32V2}
986 FIX_ARGV;
987 {$ENDIF}
988 if allegro_init = 0 then
989 begin
990 e_LogWriteln('Allegro Init: ok');
991 {$IFDEF GO32V2}
992 (* without this df dies with fire when swapped *)
993 lock_data(ticks, sizeof(ticks));
994 lock_code(@AllegroTimerCallback, PtrUInt(@AllegroTimerCallbackEND) - PtrUInt(@AllegroTimerCallback));
995 lock_data(keybeg, sizeof(keybeg));
996 lock_data(keyend, sizeof(keyend));
997 lock_data(keyring, sizeof(keyring));
998 lock_code(@KeyboardWatcher, PtrUInt(@KeyboardWatcherEND) - PtrUInt(@KeyboardWatcher));
999 lock_data(quit, sizeof(quit));
1000 lock_code(@AllegroQuitCallback, PtrUInt(@AllegroQuitCallbackEND) - PtrUInt(@AllegroQuitCallback));
1001 {$ENDIF}
1002 install_timer;
1003 install_keyboard;
1004 keyboard_lowlevel_callback := KeyboardWatcher;
1005 set_keyboard_rate(0, 0);
1006 install_int_ex(AllegroTimerCallback, MSEC_TO_TIMER(1));
1007 set_close_button_callback(AllegroQuitCallback);
1008 result := 0
1009 end
1010 end;
1012 procedure SDL_Quit;
1013 begin
1014 e_LogWriteln('SDL_Quit');
1015 set_close_button_callback(nil);
1016 remove_keyboard;
1017 remove_timer;
1018 {$IFDEF GO32V2}
1019 unlock_data(ticks, sizeof(ticks));
1020 unlock_code(@AllegroTimerCallback, PtrUInt(@AllegroTimerCallbackEND) - PtrUInt(@AllegroTimerCallback));
1021 unlock_data(keybeg, sizeof(keybeg));
1022 unlock_data(keyend, sizeof(keyend));
1023 unlock_data(keyring, sizeof(keyring));
1024 unlock_code(@KeyboardWatcher, PtrUInt(@KeyboardWatcherEND) - PtrUInt(@KeyboardWatcher));
1025 unlock_data(quit, sizeof(quit));
1026 unlock_code(@AllegroQuitCallback, PtrUInt(@AllegroQuitCallbackEND) - PtrUInt(@AllegroQuitCallback));
1027 {$ENDIF}
1028 allegro_exit
1029 end;