DEADSOFTWARE

fix 16/32 bit and float wav formats for openal
[d2df-sdl.git] / src / lib / sdl2 / sdl2.pas
1 {$MODE DELPHI}
2 unit SDL2;
4 {
5 Simple DirectMedia Layer
6 Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
8 Pascal-Header-Conversion
9 Copyright (C) 2012-2014 Tim Blume aka End/EV1313
11 SDL.pas is based on the files:
12 "sdl.h",
13 "sdl_audio.h",
14 "sdl_blendmode.h",
15 "sdl_clipboard.h",
16 "sdl_cpuinfo.h",
17 "sdl_events.h",
18 "sdl_error.h",
19 "sdl_filesystem.h",
20 "sdl_gamecontroller.h",
21 "sdl_gesture.h",
22 "sdl_haptic.h",
23 "sdl_hints.h",
24 "sdl_joystick.h",
25 "sdl_keyboard.h",
26 "sdl_keycode.h",
27 "sdl_loadso.h",
28 "sdl_log.h",
29 "sdl_pixels.h",
30 "sdl_power.h",
31 "sdl_main.h",
32 "sdl_messagebox.h",
33 "sdl_mouse.h",
34 "sdl_mutex.h",
35 "sdl_rect.h",
36 "sdl_render.h",
37 "sdl_rwops.h",
38 "sdl_scancode.h",
39 "sdl_shape.h",
40 "sdl_stdinc.h",
41 "sdl_surface.h",
42 "sdl_system.h",
43 "sdl_syswm.h",
44 "sdl_thread.h",
45 "sdl_timer.h",
46 "sdl_touch.h",
47 "sdl_version.h",
48 "sdl_video.h",
49 "sdl_types.h"
51 I will not translate:
52 "sdl_opengl.h",
53 "sdl_opengles.h"
54 "sdl_opengles2.h"
56 cause there's a much better OpenGL-Header avaible at delphigl.com:
58 the dglopengl.pas
60 Parts of the SDL.pas are from the SDL-1.2-Headerconversion from the JEDI-Team,
61 written by Domenique Louis and others.
63 I've changed the names of the dll for 32 & 64-Bit, so theres no conflict
64 between 32 & 64 bit Libraries.
66 This software is provided 'as-is', without any express or implied
67 warranty. In no case will the authors be held liable for any damages
68 arising from the use of this software.
70 Permission is granted to anyone to use this software for any purpose,
71 including commercial applications, and to alter it and redistribute it
72 freely, subject to the following restrictions:
74 1. The origin of this software must not be misrepresented; you must not
75 claim that you wrote the original software. If you use this software
76 in a product, an acknowledgment in the product documentation would be
77 appreciated but is not required.
78 2. Altered source versions must be plainly marked as such, and must not be
79 misrepresented as being the original software.
80 3. This notice may not be removed or altered from any source distribution.
82 Special Thanks to:
84 - DelphiGL.com - Community
85 - Domenique Louis and everyone else from the JEDI-Team
86 - Sam Latinga and everyone else from the SDL-Team
87 }
89 {
90 Changelog:
91 ----------
92 ? 31.01.2016: updated sdlevents.inc to SDL2 2.0.4, adressing issue #24 (thx to imantsg)
93 ? 16.01.2016: Delphi 6+ bugfixes/compatibility. Thx to Peter Karpov for identifiying and testing.
94 v.1.80-stable; 09.10.2014: added sdl_cpuinfo.h and sdl_clipboard.h
95 v.1.74-stable; 10.11.2013: added sdl_gamecontroller.h
96 v.1.73-stable; 08.11.2013: added sdl_hints.h and some keystate helpers
97 thx to Cybermonkey!
98 v.1.72-stable; 23.09.2013: fixed bug with procedures without parameters
99 (they must have brakets)
100 v.1.70-stable; 17.09.2013: added "sdl_messagebox.h" and "sdl_haptic.h"
101 v.1.63-stable; 16.09.2013: added libs sdl2_image and sdl2_ttf and added sdl_audio.h
102 v.1.62-stable; 03.09.2013: fixed.
103 v.1.61-stable; 02.09.2013: now it should REALLY work with Mac...
104 v.1.60-stable; 01.09.2013: now it should work with Delphi XE4 for Windows and
105 MacOS and of course Lazarus. thx to kotai :D
106 v.1.55-Alpha; 24.08.2013: fixed bug with SDL_GetEventState thx to d.l.i.w.
107 v.1.54-Alpha; 24.08.2013: added sdl_loadso.h
108 v.1.53-Alpha; 24.08.2013: renamed *really* and fixed linux comp.
109 v.1.52-Alpha; 24.08.2013: renamed sdl.pas to sdl2.pas
110 v.1.51-Alpha; 24.08.2013: added sdl_platform.h
111 v.1.50-Alpha; 24.08.2013: the header is now modular. thx for the hint from d.l.i.w.
112 v.1.40-Alpha; 13.08.2013: Added MacOS compatibility (thx to stoney-fd)
113 v.1.34-Alpha; 05.08.2013: Added missing functions from sdl_thread.h
114 v.1.33-Alpha; 31.07.2013: Added missing units for Linux. thx to Cybermonkey
115 v.1.32-Alpha; 31.07.2013: Fixed three bugs, thx to grieferatwork
116 v.1.31-Alpha; 30.07.2013: Added "sdl_power.h"
117 v.1.30-Alpha; 26.07.2013: Added "sdl_thread.h" and "sdl_mutex.h"
118 v.1.25-Alpha; 29.07.2013: Added Makros for SDL_RWops
119 v.1.24-Alpha; 28.07.2013: Fixed bug with RWops and size_t
120 v.1.23-Alpha; 27.07.2013: Fixed two bugs, thx to GrieferAtWork
121 v.1.22-Alpha; 24.07.2013: Added "sdl_shape.h" and TSDL_Window
122 (and ordered the translated header list ^^)
123 v.1.21-Alpha; 23.07.2013: Added TSDL_Error
124 v.1.20-Alpha; 19.07.2013: Added "sdl_timer.h"
125 v.1.10-Alpha; 09.07.2013: Added "sdl_render.h"
126 v.1.00-Alpha; 05.07.2013: Initial Alpha-Release.
129 {$DEFINE SDL}
131 {$I jedi.inc}
133 interface
135 {$IFDEF WINDOWS}
136 uses
137 Windows;
138 {$ENDIF}
140 {$IF DEFINED(UNIX)}
141 {$IF NOT DEFINED(HAIKU) AND NOT DEFINED(ANDROID) AND NOT DEFINED(DARWIN)}
142 uses
143 X,
144 XLib;
145 {$ENDIF}
146 {$ENDIF}
148 {$IFDEF DARWIN}
149 uses
150 CocoaAll;
151 {$ENDIF}
153 const
155 {$IFDEF WINDOWS}
156 SDL_LibName = 'SDL2.dll';
157 {$ENDIF}
159 {$IF DEFINED(HAIKU)}
160 SDL_LibName = 'libSDL2.so.0';
161 {$ELSEIF DEFINED(UNIX)}
162 {$IFDEF DARWIN}
163 SDL_LibName = 'libSDL2.dylib';
164 {$ELSE}
165 {$IFDEF FPC}
166 SDL_LibName = 'libSDL2.so';
167 {$ELSE}
168 SDL_LibName = 'libSDL2.so.0';
169 {$ENDIF}
170 {$ENDIF}
171 {$ENDIF}
173 {$IFDEF MACOS}
174 SDL_LibName = 'SDL2';
175 {$IFDEF FPC}
176 {$linklib libSDL2}
177 {$ENDIF}
178 {$ENDIF}
180 {$I sdltype.inc}
181 {$I sdlversion.inc}
182 {$I sdlerror.inc}
183 {$I sdlplatform.inc}
184 {$I sdlpower.inc}
185 {$I sdlthread.inc}
186 {$I sdlmutex.inc}
187 {$I sdltimer.inc}
188 {$I sdlpixels.inc}
189 {$I sdlrect.inc}
190 {$I sdlrwops.inc}
191 {$I sdlaudio.inc}
192 {$I sdlblendmode.inc}
193 {$I sdlsurface.inc}
194 {$I sdlshape.inc}
195 {$I sdlvideo.inc}
196 {$I sdlhints.inc}
197 {$I sdlloadso.inc}
198 {$I sdlmessagebox.inc}
199 {$I sdlrenderer.inc}
200 {$I sdlscancode.inc}
201 {$I sdlkeyboard.inc}
202 {$I sdlmouse.inc}
203 {$I sdljoystick.inc}
204 {$I sdlgamecontroller.inc}
205 {$I sdlhaptic.inc}
206 {$I sdltouch.inc}
207 {$I sdlgesture.inc}
208 {$I sdlsyswm.inc}
209 {$I sdlevents.inc}
210 {$I sdlclipboard.inc}
211 {$I sdlcpuinfo.inc}
212 {$I sdlfilesystem.inc}
213 {$I sdllog.inc}
214 {$I sdlsystem.inc}
215 {$I sdlstdinc.inc}
216 {$I sdl.inc}
218 implementation
220 //from "sdl_version.h"
221 procedure SDL_VERSION(Out x: TSDL_Version);
222 begin
223 x.major := SDL_MAJOR_VERSION;
224 x.minor := SDL_MINOR_VERSION;
225 x.patch := SDL_PATCHLEVEL;
226 end;
228 function SDL_VERSIONNUM(X,Y,Z: UInt32): Cardinal;
229 begin
230 Result := X*1000 + Y*100 + Z;
231 end;
233 function SDL_COMPILEDVERSION: Cardinal;
234 begin
235 Result := SDL_VERSIONNUM(SDL_MAJOR_VERSION,
236 SDL_MINOR_VERSION,
237 SDL_PATCHLEVEL);
238 end;
240 function SDL_VERSION_ATLEAST(X,Y,Z: Cardinal): Boolean;
241 begin
242 Result := SDL_COMPILEDVERSION >= SDL_VERSIONNUM(X,Y,Z);
243 end;
245 //from "sdl_mouse.h"
246 function SDL_Button(button: SInt32): SInt32;
247 begin
248 Result := 1 shl (button - 1);
249 end;
251 {$IFDEF WINDOWS}
252 //from "sdl_thread.h"
254 function SDL_CreateThread(fn: TSDL_ThreadFunction; name: PAnsiChar; data: Pointer): PSDL_Thread; overload;
255 begin
256 Result := SDL_CreateThread(fn,name,data,nil,nil);
257 end;
259 {$ENDIF}
261 //from "sdl_rect.h"
262 function SDL_RectEmpty(X: TSDL_Rect): Boolean;
263 begin
264 Result := (X.w <= 0) or (X.h <= 0);
265 end;
267 function SDL_RectEquals(A: TSDL_Rect; B: TSDL_Rect): Boolean;
268 begin
269 Result := (A.x = B.x) and (A.y = B.y) and (A.w = B.w) and (A.h = B.h);
270 end;
272 //from "sdl_rwops.h"
274 function SDL_RWsize(ctx: PSDL_RWops): SInt64;
275 begin
276 Result := ctx^.size(ctx);
277 end;
279 function SDL_RWseek(ctx: PSDL_RWops; offset: SInt64; whence: SInt32): SInt64;
280 begin
281 Result := ctx^.seek(ctx,offset,whence);
282 end;
284 function SDL_RWtell(ctx: PSDL_RWops): SInt64;
285 begin
286 Result := ctx^.seek(ctx, 0, RW_SEEK_CUR);
287 end;
289 function SDL_RWread(ctx: PSDL_RWops; ptr: Pointer; size: size_t; n: size_t): size_t;
290 begin
291 Result := ctx^.read(ctx, ptr, size, n);
292 end;
294 function SDL_RWwrite(ctx: PSDL_RWops; ptr: Pointer; size: size_t; n: size_t): size_t;
295 begin
296 Result := ctx^.write(ctx, ptr, size, n);
297 end;
299 function SDL_RWclose(ctx: PSDL_RWops): SInt32;
300 begin
301 Result := ctx^.close(ctx);
302 end;
304 //from "sdl_audio.h"
306 function SDL_LoadWAV(_file: PAnsiChar; spec: PSDL_AudioSpec; audio_buf: PPUInt8; audio_len: PUInt32): PSDL_AudioSpec;
307 begin
308 Result := SDL_LoadWAV_RW(SDL_RWFromFile(_file, 'rb'), 1, spec, audio_buf, audio_len);
309 end;
311 function SDL_AUDIO_BITSIZE(x: Cardinal): Cardinal;
312 begin
313 Result := x and SDL_AUDIO_MASK_BITSIZE;
314 end;
316 function SDL_AUDIO_ISFLOAT(x: Cardinal): Cardinal;
317 begin
318 Result := x and SDL_AUDIO_MASK_DATATYPE;
319 end;
321 function SDL_AUDIO_ISBIGENDIAN(x: Cardinal): Cardinal;
322 begin
323 Result := x and SDL_AUDIO_MASK_ENDIAN;
324 end;
326 function SDL_AUDIO_ISSIGNED(x: Cardinal): Cardinal;
327 begin
328 Result := x and SDL_AUDIO_MASK_SIGNED;
329 end;
331 function SDL_AUDIO_ISINT(x: Cardinal): Cardinal;
332 begin
333 Result := not SDL_AUDIO_ISFLOAT(x);
334 end;
336 function SDL_AUDIO_ISLITTLEENDIAN(x: Cardinal): Cardinal;
337 begin
338 Result := not SDL_AUDIO_ISLITTLEENDIAN(x);
339 end;
341 function SDL_AUDIO_ISUNSIGNED(x: Cardinal): Cardinal;
342 begin
343 Result := not SDL_AUDIO_ISSIGNED(x);
344 end;
346 //from "sdl_pixels.h"
348 function SDL_PIXELFLAG(X: Cardinal): Cardinal;
349 begin
350 Result := (X shr 28) and $0F;
351 end;
353 function SDL_PIXELTYPE(X: Cardinal): Cardinal;
354 begin
355 Result := (X shr 24) and $0F;
356 end;
358 function SDL_PIXELORDER(X: Cardinal): Cardinal;
359 begin
360 Result := (X shr 20) and $0F;
361 end;
363 function SDL_PIXELLAYOUT(X: Cardinal): Cardinal;
364 begin
365 Result := (X shr 16) and $0F;
366 end;
368 function SDL_BITSPERPIXEL(X: Cardinal): Cardinal;
369 begin
370 Result := (X shr 8) and $FF;
371 end;
373 function SDL_IsPixelFormat_FOURCC(format: Variant): Boolean;
374 begin
375 {* The flag is set to 1 because 0x1? is not in the printable ASCII range *}
376 Result := format and SDL_PIXELFLAG(format) <> 1;
377 end;
379 //from "sdl_surface.h"
380 function SDL_LoadBMP(_file: PAnsiChar): PSDL_Surface;
381 begin
382 Result := SDL_LoadBMP_RW(SDL_RWFromFile(_file, 'rb'), 1);
383 end;
385 function SDL_SaveBMP(Const surface:PSDL_Surface; Const filename:AnsiString):sInt32;
386 begin
387 Result := SDL_SaveBMP_RW(surface, SDL_RWFromFile(PAnsiChar(filename), 'wb'), 1)
388 end;
390 {**
391 * Evaluates to true if the surface needs to be locked before access.
392 *}
393 function SDL_MUSTLOCK(Const S:PSDL_Surface):Boolean;
394 begin
395 Result := ((S^.flags and SDL_RLEACCEL) <> 0)
396 end;
398 //from "sdl_video.h"
399 function SDL_WindowPos_IsUndefined(X: Variant): Variant;
400 begin
401 Result := (X and $FFFF0000) = SDL_WINDOWPOS_UNDEFINED_MASK;
402 end;
404 function SDL_WindowPos_IsCentered(X: Variant): Variant;
405 begin
406 Result := (X and $FFFF0000) = SDL_WINDOWPOS_CENTERED_MASK;
407 end;
409 //from "sdl_events.h"
411 function SDL_GetEventState(type_: UInt32): UInt8;
412 begin
413 Result := SDL_EventState(type_, SDL_QUERY);
414 end;
416 // from "sdl_timer.h"
417 function SDL_TICKS_PASSED(Const A, B:UInt32):Boolean;
418 begin
419 Result := ((Int64(B) - Int64(A)) <= 0)
420 end;
422 // from "sdl_gamecontroller.h"
423 {**
424 * Load a set of mappings from a file, filtered by the current SDL_GetPlatform()
425 *}
426 function SDL_GameControllerAddMappingsFromFile(Const FilePath:PAnsiChar):SInt32;
427 begin
428 Result := SDL_GameControllerAddMappingsFromRW(SDL_RWFromFile(FilePath, 'rb'), 1)
429 end;
431 end.