DEADSOFTWARE

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