DEADSOFTWARE

3392c68ef45f081cd50a796f15d189122fc31401
[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 - 1] 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 inputChar: Char;
381 inputText: Boolean;
382 ticks: UInt32;
383 quit: Boolean;
385 s2lc: array [0..KEY_MAX] of char = (
386 #00, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
387 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
388 '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', #00,
389 #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, '`', '-', '=', #00,
390 #09, '[', ']', #10, ';', #39, '\', '\', ',', '.', '/', ' ', #00, #00, #00, #00,
391 #00, #00, #00, #00, #00, '/', '*', '-', '+', #00, #00, #00, #00, #00, #00, #00,
392 #00, #00, #00, #00, '@', #00, ':', #00, '=', #00, ';', #00, #00, #00, #00, #00,
393 #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00
394 );
395 s2uc: array [0..KEY_MAX] of char = (
396 #00, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
397 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ')', '!', '@', '#', '$',
398 '%', '^', '&', '*', '(', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', #00,
399 #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, '~', '_', '+', #00,
400 #09, '{', '}', #10, ':', '"', '|', '|', '<', '>', '?', ' ', #00, #00, #00, #00,
401 #00, #00, #00, #00, #00, '/', '*', '-', '+', #00, #00, #00, #00, #00, #00, #00,
402 #00, #00, #00, #00, '@', #00, ':', #00, '=', #00, ';', #00, #00, #00, #00, #00,
403 #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00, #00
404 );
406 function IsEmptyKeyboard: Boolean;
407 begin
408 result := keybeg = keyend
409 end;
411 function IsFullKeyboard: Boolean;
412 begin
413 result := (keybeg - 1) mod maxKeyBuffer = keyend
414 end;
416 function NextScancode: Integer;
417 begin
418 result := 0;
419 if not IsEmptyKeyboard then
420 begin
421 result := keyring[keybeg];
422 keybeg := (keybeg + 1) mod maxKeyBuffer
423 end
424 end;
426 procedure KeyboardWatcher (scancode: Integer); cdecl;
427 begin
428 if (keybeg - 1) mod maxKeyBuffer <> keyend then (* not IsFullKeyboard *)
429 begin
430 keyring[keyend] := scancode;
431 keyend := (keyend + 1) mod maxKeyBuffer
432 end
433 end;
434 procedure KeyboardWatcherEND; begin end;
436 /// MACRO ///
438 function SDL_PIXELFLAG(X: Cardinal): Cardinal;
439 begin
440 Result := (X shr 28) and $0F;
441 end;
443 function SDL_PIXELTYPE(X: Cardinal): Cardinal;
444 begin
445 Result := (X shr 24) and $0F;
446 end;
448 function SDL_PIXELORDER(X: Cardinal): Cardinal;
449 begin
450 Result := (X shr 20) and $0F;
451 end;
453 function SDL_PIXELLAYOUT(X: Cardinal): Cardinal;
454 begin
455 Result := (X shr 16) and $0F;
456 end;
458 function SDL_BITSPERPIXEL(X: Cardinal): Cardinal;
459 begin
460 Result := (X shr 8) and $FF;
461 end;
463 function SDL_IsPixelFormat_FOURCC(format: Variant): Boolean;
464 begin
465 Result := format and SDL_PIXELFLAG(format) <> 1;
466 end;
468 /// FUNCTIONS ////
470 (********** FILES **********)
472 function SDL_RWFromFile(const _file: PAnsiChar; const mode: PAnsiChar): PSDL_RWops;
473 begin
474 result := nil
475 end;
477 function SDL_RWFromConstMem(const mem: Pointer; size: SInt32): PSDL_RWops;
478 begin
479 result := nil
480 end;
482 (********** KEYBOARD **********)
484 function SDL_GetScancodeName(scancode: TSDL_ScanCode): PAnsiChar;
485 begin
486 result := ''
487 end;
489 function SDL_IsTextInputActive: TSDL_Bool;
490 begin
491 if inputText then
492 result := SDL_TRUE
493 else
494 result := SDL_FALSE
495 end;
497 procedure SDL_StartTextInput;
498 begin
499 inputText := True
500 end;
502 procedure SDL_StopTextInput;
503 begin
504 inputText := False
505 end;
507 (********** JOYSTICK **********)
509 function SDL_NumJoysticks: SInt32;
510 begin
511 result := 0
512 end;
514 function SDL_JoystickOpen(device_index: SInt32): PSDL_Joystick;
515 begin
516 result := nil
517 end;
519 function SDL_JoystickName(joystick: PSDL_Joystick): PAnsiChar;
520 begin
521 result := ''
522 end;
524 function SDL_JoystickNumAxes(joystick: PSDL_Joystick): SInt32;
525 begin
526 result := 0
527 end;
529 function SDL_JoystickNumButtons(joystick: PSDL_Joystick): SInt32;
530 begin
531 result := 0
532 end;
534 function SDL_JoystickNumHats(joystick: PSDL_Joystick): SInt32;
535 begin
536 result := 0
537 end;
539 procedure SDL_JoystickClose(joystick: PSDL_Joystick);
540 begin
541 end;
543 procedure SDL_JoystickUpdate;
544 begin
545 end;
547 function SDL_JoystickGetButton(joystick: PSDL_Joystick; button: SInt32): UInt8;
548 begin
549 result := 0
550 end;
552 function SDL_JoystickGetAxis(joystick: PSDL_Joystick; axis: SInt32): SInt16;
553 begin
554 result := 0
555 end;
557 function SDL_JoystickGetHat(joystick: PSDL_Joystick; hat: SInt32): UInt8;
558 begin
559 result := 0
560 end;
562 (********** TOUCH **********)
564 function SDL_GetNumTouchDevices: SInt32;
565 begin
566 result := 0
567 end;
569 (********** TIMERS **********)
571 procedure AllegroTimerCallback; cdecl;
572 begin
573 inc(ticks)
574 end;
575 procedure AllegroTimerCallbackEND; begin end;
577 function SDL_GetPerformanceCounter: UInt64;
578 begin
579 (* TODO *)
580 result := ticks;
581 end;
583 function SDL_GetPerformanceFrequency: UInt64;
584 begin
585 (* TODO *)
586 result := 1
587 end;
589 procedure SDL_Delay(ms: UInt32);
590 begin
591 rest(ms)
592 end;
594 function SDL_GetTicks: UInt32;
595 begin
596 result := ticks;
597 end;
599 (********** DISPLAY MODES **********)
601 function SDL_GetDesktopDisplayMode(displayIndex: SInt32; mode: PSDL_DisplayMode): SInt32;
602 begin
603 e_LogWritefln('SDL_GetDesktopDisplayMode %s', [displayIndex]);
604 result := -1;
605 mode.format := SDL_PIXELFORMAT_UNKNOWN; (* FIXIT *)
606 mode.refresh_rate := 0;
607 mode.driverdata := nil;
608 if get_desktop_resolution(@mode.w, @mode.h) = 0 then
609 result := 0
610 end;
612 function SDL_GetClosestDisplayMode(displayIndex: SInt32; const mode: PSDL_DisplayMode; closest: PSDL_DisplayMode): PSDL_DisplayMode;
613 begin
614 e_LogWritefln('SDL_GetClosestDisplayMode %s', [displayIndex]);
615 result := nil;
616 end;
618 function SDL_GetNumDisplayModes(displayIndex: SInt32): SInt32;
619 var m: PGFX_MODE_LIST;
620 begin
621 e_LogWritefln('SDL_GetNumDisplayModes %s', [displayIndex]);
622 result := -1;
623 m := get_gfx_mode_list(GFX_AUTODETECT);
624 if m <> nil then
625 begin
626 result := m.num_modes;
627 destroy_gfx_mode_list(m);
628 end
629 end;
631 function SDL_GetDisplayMode(displayIndex: SInt32; modeIndex: SInt32; mode: PSDL_DisplayMode): SInt32;
632 var m: PGFX_MODE_LIST;
633 begin
634 e_LogWritefln('SDL_GetDisplayMode %s %s', [displayIndex, modeIndex]);
635 result := -1;
636 m := get_gfx_mode_list(GFX_AUTODETECT);
637 if (m <> nil) and (modeIndex >= 0) and (modeIndex < m.num_modes) then
638 begin
639 mode.format := SDL_PIXELFORMAT_UNKNOWN; (* FIX IT *)
640 mode.w := m.mode[modeIndex].width;
641 mode.h := m.mode[modeIndex].height;
642 mode.refresh_rate := 0;
643 mode.driverdata := nil;
644 destroy_gfx_mode_list(m);
645 result := 0
646 end
647 end;
649 function SDL_GetDisplayDPI(displayIndex: SInt32; ddpi, hdpi, vdpi: PFloat): SInt32;
650 begin
651 result := -1
652 end;
654 (*********** WINDOW MANAGEMENT **********)
656 function SDL_CreateWindow(const title: PAnsiChar; x: SInt32; y: SInt32; w: SInt32; h: SInt32; flags: UInt32): PSDL_Window;
657 var window: PSDL_Window; mode: Integer;
658 begin
659 e_LogWritefln('SDL_CreateWindow %s %s %s %s %s %u', [title, x, y, w, h, flags]);
660 result := nil;
662 {$IF DEFINED(GO32V2)}
663 mode := GFX_AUTODETECT;
664 {$ELSE}
665 if (flags and (SDL_WINDOW_FULLSCREEN or SDL_WINDOW_FULLSCREEN_DESKTOP)) <> 0 then
666 mode := GFX_AUTODETECT_FULLSCREEN
667 else
668 mode := GFX_AUTODETECT_WINDOWED;
669 {$ENDIF}
671 set_color_depth(DEFAULT_DEPTH);
672 if set_gfx_mode(mode, w, h, 0, 0) = 0 then
673 begin
674 new(window);
675 set_window_title(title);
676 if sdl2allegro_screen <> nil then
677 destroy_bitmap(sdl2allegro_screen);
678 sdl2allegro_screen := create_system_bitmap(w, h);
679 if sdl2allegro_screen = nil then
680 sdl2allegro_screen := create_bitmap(w, h);
681 ASSERT(sdl2allegro_screen <> nil);
682 window.w := w;
683 window.h := h;
684 window.mode := mode;
685 result := window
686 end
687 end;
689 function SDL_SetWindowFullscreen(window: PSDL_Window; flags: UInt32): SInt32;
690 var mode: Integer;
691 begin
692 e_LogWritefln('SDL_SetWindowFullscreen %u', [flags]);
693 result := -1;
694 if window = nil then exit;
696 {$IF DEFINED(GO32V2)}
697 mode := GFX_AUTODETECT;
698 {$ELSE}
699 if (flags and (SDL_WINDOW_FULLSCREEN or SDL_WINDOW_FULLSCREEN_DESKTOP)) <> 0 then
700 mode := GFX_AUTODETECT_FULLSCREEN
701 else
702 mode := GFX_AUTODETECT_WINDOWED;
703 {$ENDIF}
705 set_color_depth(DEFAULT_DEPTH);
706 if set_gfx_mode(mode, window.w, window.h, 0, 0) = 0 then
707 begin
708 if sdl2allegro_screen <> nil then
709 destroy_bitmap(sdl2allegro_screen);
710 sdl2allegro_screen := create_system_bitmap(window.w, window.h);
711 if sdl2allegro_screen = nil then
712 sdl2allegro_screen := create_bitmap(window.w, window.h);
713 ASSERT(sdl2allegro_screen <> nil);
714 set_palette(desktop_palette);
715 window.mode := mode;
716 result := 0
717 end
718 end;
720 procedure SDL_SetWindowSize(window: PSDL_Window; w: SInt32; h: SInt32);
721 begin
722 e_LogWritefln('SDL_SetWindowSize %s %s', [w, h]);
723 if window = nil then exit;
724 set_color_depth(DEFAULT_DEPTH);
725 if set_gfx_mode(window.mode, w, h, 0, 0) = 0 then
726 begin
727 if sdl2allegro_screen <> nil then
728 destroy_bitmap(sdl2allegro_screen);
729 sdl2allegro_screen := create_system_bitmap(w, h);
730 if sdl2allegro_screen = nil then
731 sdl2allegro_screen := create_bitmap(w, h);
732 ASSERT(sdl2allegro_screen <> nil);
733 set_palette(desktop_palette);
734 window.w := w;
735 window.h := h;
736 end
737 end;
739 procedure SDL_DestroyWindow(window: PSDL_Window);
740 begin
741 e_LogWriteln('SDL_DestroyWindow');
742 if window = nil then exit;
743 if sdl2allegro_screen <> nil then
744 destroy_bitmap(sdl2allegro_screen);
745 sdl2allegro_screen := nil;
746 dispose(window)
747 end;
749 procedure SDL_GetWindowSize(window: PSDL_Window; w: PInt; h: PInt);
750 begin
751 e_LogWriteln('SDL_GetWindowSize');
752 if window = nil then exit;
753 if w <> nil then
754 w^ := window.w;
755 if h <> nil then
756 h^ := window.h;
757 end;
759 procedure SDL_RestoreWindow(window: PSDL_Window);
760 begin
761 e_LogWriteln('SDL_RestoreWindow');
762 if window = nil then exit;
763 (* stub *)
764 end;
766 function SDL_SetWindowGammaRamp(window: PSDL_Window; const red: PUInt16; const green: PUInt16; const blue: PUInt16): SInt32;
767 begin
768 e_LogWriteln('SDL_SetWindowGammaRamp');
769 result := -1;
770 if window = nil then exit;
771 result := 0
772 end;
774 function SDL_GetWindowGammaRamp(window: PSDL_Window; red: PUInt16; green: PUInt16; blue: PUInt16): SInt32;
775 begin
776 e_LogWriteln('SDL_GetWindowGammaRamp');
777 result := -1;
778 if window = nil then exit;
779 result := 0
780 end;
782 (********** OPENGL MANAGEMENT **********)
784 function SDL_GL_SetAttribute(attr: TSDL_GLattr; value: SInt32): SInt32;
785 begin
786 e_LogWritefln('SDL_GL_SetAttribute %s %s', [attr, value]);
787 allegro_error := 'Attribute ' + IntToStr(attr) + ' := ' + IntToStr(value) + 'not supported';
788 result := -1
789 end;
791 function SDL_GL_GetAttribute(attr: TSDL_GLattr; value: PInt): SInt32;
792 begin
793 e_LogWritefln('SDL_GL_GetAttribute %s', [attr]);
794 value^ := 0; result := -1
795 end;
797 function SDL_GL_CreateContext(window: PSDL_Window): TSDL_GLContext;
798 begin
799 e_LogWriteln('SDL_GL_CreateContext');
800 result := window
801 end;
803 function SDL_GL_MakeCurrent(window: PSDL_Window; context: TSDL_GLContext): SInt32;
804 begin
805 e_LogWriteln('SDL_GL_MakeCurrent');
806 result := 0
807 end;
809 procedure SDL_GL_DeleteContext(context: TSDL_GLContext);
810 begin
811 e_LogWriteln('SDL_GL_DeleteContext');
812 end;
814 function SDL_GL_SetSwapInterval(interval: SInt32): SInt32;
815 begin
816 e_LogWritefln('SDL_GL_SetSwapInterval %s', [interval]);
817 result := 0
818 end;
820 procedure SDL_GL_SwapWindow(window: PSDL_Window);
821 begin
822 ASSERT(sdl2allegro_screen <> nil);
823 acquire_screen;
824 blit(sdl2allegro_screen, screen, 0, 0, 0, 0, sdl2allegro_screen.w, sdl2allegro_screen.h);
825 show_video_bitmap(screen);
826 release_screen;
827 end;
829 (********** EVENTS **********)
831 function SDL_PushEvent(event: PSDL_Event): SInt32;
832 begin
833 result := 1;
834 case event.type_ of
835 SDL_QUITEV: quit := True;
836 else ASSERT(FALSE); result := 0
837 end
838 end;
840 function allegro_to_sdl_scancode (x: Integer): Integer;
841 begin
842 x := x and $7F;
843 case x of
844 KEY_A..KEY_Z: result := SDL_SCANCODE_A + (x - KEY_A);
845 KEY_0..KEY_0: result := SDL_SCANCODE_0 + (x - KEY_0);
846 KEY_0_PAD: result := SDL_SCANCODE_KP_0;
847 KEY_1_PAD..KEY_9_PAD: result := SDL_SCANCODE_KP_1 + (x - KEY_1_PAD);
848 KEY_F1..KEY_F12: result := SDL_SCANCODE_F1 + (x - KEY_F1);
849 KEY_ESC: result := SDL_SCANCODE_ESCAPE;
850 KEY_TILDE: result := SDL_SCANCODE_GRAVE;
851 KEY_MINUS: result := SDL_SCANCODE_MINUS;
852 KEY_EQUALS: result := SDL_SCANCODE_EQUALS;
853 KEY_BACKSPACE: result := SDL_SCANCODE_BACKSPACE;
854 KEY_TAB: result := SDL_SCANCODE_TAB;
855 KEY_OPENBRACE: result := SDL_SCANCODE_LEFTBRACKET;
856 KEY_CLOSEBRACE: result := SDL_SCANCODE_RIGHTBRACKET;
857 KEY_ENTER: result := SDL_SCANCODE_RETURN;
858 KEY_COLON: result := SDL_SCANCODE_SEMICOLON;
859 KEY_QUOTE: result := SDL_SCANCODE_APOSTROPHE;
860 KEY_BACKSLASH: result := SDL_SCANCODE_BACKSLASH;
861 KEY_BACKSLASH2: result := SDL_SCANCODE_NONUSHASH; (* ??? *)
862 KEY_COMMA: result := SDL_SCANCODE_COMMA;
863 (*KEY_STOP: result :=;*) (* ??? *)
864 KEY_SLASH: result := SDL_SCANCODE_SLASH;
865 KEY_SPACE: result := SDL_SCANCODE_SPACE;
866 KEY_INSERT: result := SDL_SCANCODE_INSERT;
867 KEY_DEL: result := SDL_SCANCODE_DELETE;
868 KEY_HOME: result := SDL_SCANCODE_HOME;
869 KEY_END: result := SDL_SCANCODE_END;
870 KEY_PGUP: result := SDL_SCANCODE_PAGEUP;
871 KEY_PGDN: result := SDL_SCANCODE_PAGEDOWN;
872 KEY_LEFT: result := SDL_SCANCODE_LEFT;
873 KEY_RIGHT: result := SDL_SCANCODE_RIGHT;
874 KEY_UP: result := SDL_SCANCODE_UP;
875 KEY_DOWN: result := SDL_SCANCODE_DOWN;
876 KEY_SLASH_PAD: result := SDL_SCANCODE_KP_DIVIDE;
877 KEY_ASTERISK: result := SDL_SCANCODE_KP_MULTIPLY;
878 KEY_MINUS_PAD: result := SDL_SCANCODE_KP_MINUS;
879 KEY_PLUS_PAD: result := SDL_SCANCODE_KP_PLUS;
880 KEY_DEL_PAD: result := SDL_SCANCODE_KP_PERIOD;
881 KEY_ENTER_PAD: result := SDL_SCANCODE_KP_ENTER;
882 KEY_PRTSCR: result := SDL_SCANCODE_PRINTSCREEN;
883 KEY_PAUSE: result := SDL_SCANCODE_PAUSE;
884 (*KEY_ABNT_C1: result :=;*) (* ??? *)
885 KEY_YEN: result := SDL_SCANCODE_INTERNATIONAL3;
886 KEY_KANA: result := SDL_SCANCODE_LANG3; (* ??? *)
887 (*KEY_CONVERT: result :=;*) (* ??? *)
888 (*KEY_NOCONVERT: result := ;*) (* ??? *)
889 KEY_AT: result := SDL_SCANCODE_KP_AT;
890 (*KEY_CIRCUMFLEX: result :=;*) (* ??? *)
891 KEY_COLON2: result := SDL_SCANCODE_KP_COLON;
892 (*KEY_KANJI: result :=;*) (* ??? *)
893 KEY_EQUALS_PAD: result := SDL_SCANCODE_KP_EQUALS;
894 (*KEY_BACKQUOTE: result :=;*) (* ??? *)
895 KEY_SEMICOLON: result := SDL_SCANCODE_SEMICOLON;
896 KEY_COMMAND: result := SDL_SCANCODE_LGUI;
897 (*KEY_UNKNOWN1: result :=;*)
898 (*KEY_UNKNOWN2: result :=;*)
899 (*KEY_UNKNOWN3: result :=;*)
900 (*KEY_UNKNOWN4: result :=;*)
901 (*KEY_UNKNOWN5: result :=;*)
902 (*KEY_UNKNOWN6: result :=;*)
903 (*KEY_UNKNOWN7: result :=;*)
904 (*KEY_UNKNOWN8: result :=;*)
905 KEY_LSHIFT: result := SDL_SCANCODE_LSHIFT;
906 KEY_RSHIFT: result := SDL_SCANCODE_RSHIFT;
907 KEY_LCONTROL: result := SDL_SCANCODE_LCTRL;
908 KEY_RCONTROL: result := SDL_SCANCODE_RCTRL;
909 KEY_ALT: result := SDL_SCANCODE_LALT;
910 KEY_ALTGR: result := SDL_SCANCODE_RALT;
911 KEY_LWIN: result := SDL_SCANCODE_LGUI;
912 KEY_RWIN: result := SDL_SCANCODE_RGUI;
913 KEY_MENU: result := SDL_SCANCODE_MODE; (* ??? *)
914 KEY_SCRLOCK: result := SDL_SCANCODE_SCROLLLOCK;
915 KEY_NUMLOCK: result := SDL_SCANCODE_NUMLOCKCLEAR;
916 KEY_CAPSLOCK: result := SDL_SCANCODE_CAPSLOCK;
917 else result := SDL_SCANCODE_UNKNOWN
918 end
919 end;
921 function SDL_PollEvent(event: PSDL_Event): SInt32;
922 var alscan: Integer; pressed, shift, caps: Boolean;
923 begin
924 result := 0;
926 if quit then
927 begin
928 quit := False;
929 event.type_ := SDL_QUITEV;
930 event.quit.timestamp := ticks;
931 result := 1;
932 Exit
933 end;
935 if inputText and (inputChar <> #0) then
936 begin
937 event.type_ := SDL_TEXTINPUT;
938 event.text.timestamp := ticks;
939 event.text.windowID := 0;
940 event.text.text[0] := inputChar;
941 event.text.text[1] := #0;
942 inputChar := #0;
943 result := 1;
944 Exit
945 end;
947 poll_keyboard;
948 if not IsEmptyKeyboard then
949 begin
950 alscan := NextScancode;
951 pressed := alscan and $80 = 0;
952 if pressed then
953 begin
954 shift := key_shifts and KB_SHIFT_FLAG <> 0;
955 caps := key_shifts and KB_CAPSLOCK_FLAG <> 0;
956 if shift xor caps then
957 inputChar := s2uc[alscan and $7F]
958 else
959 inputChar := s2lc[alscan and $7F];
960 event.type_ := SDL_KEYDOWN
961 end
962 else
963 begin
964 inputChar := #0;
965 event.type_ := SDL_KEYUP
966 end;
967 event.key.timestamp := ticks;
968 event.key.windowID := 0;
969 (**** df not use it?
970 if pressed then
971 event.key.state := SDL_PRESSED
972 else
973 event.key.state := SDL_RELEASED;
974 ****)
975 event.key._repeat := 0;
976 event.key.keysym.scancode := allegro_to_sdl_scancode(alscan);
977 event.key.keysym.sym := 0; (* df not use it? *)
978 event.key.keysym._mod := 0; (* df not use it? *)
979 result := 1;
980 Exit
981 end
982 end;
984 (********** MOUSE **********)
986 function SDL_ShowCursor(toggle: SInt32): SInt32;
987 begin
988 e_LogWritefln('SDL_ShowCursor %s', [toggle]);
989 (* TODO *)
990 result := 0
991 end;
993 (********** SDL **********)
995 procedure AllegroQuitCallback; cdecl;
996 begin
997 quit := True
998 end;
999 procedure AllegroQuitCallbackEND; begin end;
1001 function SDL_SetHint( const name: PChar; const value: PChar) : boolean;
1002 begin
1003 e_LogWritefln('SDL_SetHint %s %s', [name, value]);
1004 (* TODO *)
1005 result := false
1006 end;
1008 function SDL_GetError: PAnsiChar;
1009 begin
1010 result := allegro_error;
1011 end;
1013 {$IFDEF GO32V2}
1014 (* HACK: allegro crashes while init without this *)
1015 var
1016 __crt0_argv: PPchar; cvar; external;
1017 myargv: array [0..255] of Pchar;
1019 procedure FIX_ARGV;
1020 begin
1021 __crt0_argv := @myargv[0];
1022 myargv[0] := PChar(ParamStr(0));
1023 e_LogWritefln('argv[0] = %s', [myargv[0]]);
1024 end;
1025 {$ENDIF}
1027 function SDL_Init(flags: UInt32): SInt32;
1028 begin
1029 result := -1;
1030 {$IFDEF GO32V2}
1031 FIX_ARGV;
1032 {$ENDIF}
1033 if allegro_init = 0 then
1034 begin
1035 e_LogWriteln('Allegro Init: ok');
1036 {$IFDEF GO32V2}
1037 (* without this df dies with fire when swapped *)
1038 lock_data(ticks, sizeof(ticks));
1039 lock_code(@AllegroTimerCallback, PtrUInt(@AllegroTimerCallbackEND) - PtrUInt(@AllegroTimerCallback));
1040 lock_data(keybeg, sizeof(keybeg));
1041 lock_data(keyend, sizeof(keyend));
1042 lock_data(keyring, sizeof(keyring));
1043 lock_code(@KeyboardWatcher, PtrUInt(@KeyboardWatcherEND) - PtrUInt(@KeyboardWatcher));
1044 lock_data(quit, sizeof(quit));
1045 lock_code(@AllegroQuitCallback, PtrUInt(@AllegroQuitCallbackEND) - PtrUInt(@AllegroQuitCallback));
1046 {$ENDIF}
1047 install_timer;
1048 install_keyboard;
1049 keyboard_lowlevel_callback := KeyboardWatcher;
1050 set_keyboard_rate(0, 0);
1051 install_int_ex(AllegroTimerCallback, MSEC_TO_TIMER(1));
1052 set_close_button_callback(AllegroQuitCallback);
1053 result := 0
1054 end
1055 end;
1057 procedure SDL_Quit;
1058 begin
1059 set_close_button_callback(nil);
1060 remove_keyboard;
1061 remove_timer;
1062 {$IFDEF GO32V2}
1063 unlock_data(ticks, sizeof(ticks));
1064 unlock_code(@AllegroTimerCallback, PtrUInt(@AllegroTimerCallbackEND) - PtrUInt(@AllegroTimerCallback));
1065 unlock_data(keybeg, sizeof(keybeg));
1066 unlock_data(keyend, sizeof(keyend));
1067 unlock_data(keyring, sizeof(keyring));
1068 unlock_code(@KeyboardWatcher, PtrUInt(@KeyboardWatcherEND) - PtrUInt(@KeyboardWatcher));
1069 unlock_data(quit, sizeof(quit));
1070 unlock_code(@AllegroQuitCallback, PtrUInt(@AllegroQuitCallbackEND) - PtrUInt(@AllegroQuitCallback));
1071 {$ENDIF}
1072 allegro_exit
1073 end;