sdl2.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. unit sdl2;
  2. {
  3. SDL2-for-Pascal
  4. =================
  5. Pascal units for SDL2 - Simple Direct MediaLayer, Version 2
  6. Copyright (C) 2020-2023 PGD Community
  7. Maintainers: M. J. Molski and suve
  8. Visit: https://github.com/PascalGameDevelopment/SDL2-for-Pascal
  9. Simple DirectMedia Layer
  10. Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
  11. Visit: http://libsdl.org
  12. SDL2-for-Pascal is based upon:
  13. Pascal-Header-Conversion
  14. Copyright (C) 2012-2020 Tim Blume aka End/EV1313
  15. JEDI-SDL : Pascal units for SDL
  16. Copyright (C) 2000 - 2004 Dominique Louis <[email protected]>
  17. sdl2.pas is based on the C header files in the include folder
  18. of the original Simple DirectMedia Layer repository.
  19. See: https://github.com/libsdl-org/SDL
  20. OpenGL header files are not translated:
  21. "sdl_opengl.h",
  22. "sdl_opengles.h"
  23. "sdl_opengles2.h"
  24. There is a much better OpenGL-Header avaible at delphigl.com: dglopengl.pas
  25. See: https://github.com/SaschaWillems/dglOpenGL
  26. This software is provided 'as-is', without any express or implied
  27. warranty. In no case will the authors be held liable for any damages
  28. arising from the use of this software.
  29. Permission is granted to anyone to use this software for any purpose,
  30. including commercial applications, and to alter it and redistribute it
  31. freely, subject to the following restrictions:
  32. 1. The origin of this software must not be misrepresented; you must not
  33. claim that you wrote the original software. If you use this software
  34. in a product, an acknowledgment in the product documentation would be
  35. appreciated but is not required.
  36. 2. Altered source versions must be plainly marked as such, and must not be
  37. misrepresented as being the original software.
  38. 3. This notice may not be removed or altered from any source distribution.
  39. Special Thanks to:
  40. - Tim Blume and everyone else contributing to the "Pascal-Header-Conversion"
  41. - DelphiGL.com - Community
  42. - Domenique Louis and everyone else from the JEDI-Team
  43. - Sam Latinga and everyone else from the SDL-Team
  44. }
  45. {$DEFINE SDL}
  46. {$I jedi.inc}
  47. {$I ImGuiPasDef.inc}
  48. interface
  49. {$IFDEF WINDOWS}
  50. uses
  51. {$IFDEF FPC}
  52. ctypes,
  53. {$ENDIF}
  54. Windows;
  55. {$ENDIF}
  56. {$IF DEFINED(UNIX) AND NOT DEFINED(ANDROID)}
  57. uses
  58. {$IFDEF FPC}
  59. ctypes,
  60. UnixType,
  61. {$ENDIF}
  62. {$IFDEF DARWIN}
  63. CocoaAll;
  64. {$ELSE}
  65. X,
  66. XLib;
  67. {$ENDIF}
  68. {$ENDIF}
  69. {$IF DEFINED(UNIX) AND DEFINED(ANDROID) AND DEFINED(FPC)}
  70. uses
  71. ctypes,
  72. UnixType;
  73. {$ENDIF}
  74. {$IfNDef DYNAMIC_LINK}
  75. {$LinkLib 'libSDL2.a'}
  76. {$ELSE}
  77. const
  78. {$IFDEF WINDOWS}
  79. SDL_LibName = 'SDL2.dll';
  80. {$ENDIF}
  81. {$IFDEF UNIX}
  82. {$IFDEF DARWIN}
  83. SDL_LibName = 'libSDL2.dylib';
  84. {$IFDEF FPC}
  85. {$LINKLIB libSDL2}
  86. {$ENDIF}
  87. {$ELSE}
  88. {$IFDEF FPC}
  89. SDL_LibName = 'libSDL2.so';
  90. {$ELSE}
  91. SDL_LibName = 'libSDL2.so.0';
  92. {$ENDIF}
  93. {$MESSAGE HINT 'Known MESA bug may generate float-point exception in software graphics mode! See https://github.com/PascalGameDevelopment/SDL2-for-Pascal/issues/56 for reference.'}
  94. {$ENDIF}
  95. {$ENDIF}
  96. {$IFDEF MACOS}
  97. SDL_LibName = 'SDL2';
  98. {$IFDEF FPC}
  99. {$linklib libSDL2}
  100. {$ENDIF}
  101. {$ENDIF}
  102. {$EndIf}
  103. {$DEFINE WANT_CWCHAR_T}
  104. {$I ctypes.inc} // C data types
  105. {SDL2 version of the represented header file}
  106. {$I sdlstdinc.inc}
  107. {$I sdlversion.inc} // 2.0.14
  108. {$I sdlerror_c.inc} // 2.0.14
  109. {$I sdlerror.inc} // 2.0.14
  110. {$I sdlplatform.inc} // 2.0.14
  111. {$I sdlpower.inc} // 2.0.14
  112. {$I sdlthread.inc}
  113. {$I sdlatomic.inc} // 2.0.20
  114. {$I sdlmutex.inc} // 2.26.5
  115. {$I sdltimer.inc} // 2.0.18
  116. {$I sdlpixels.inc} // 2.26.5
  117. {$I sdlrect.inc} // 2.24.0
  118. {$I sdlrwops.inc} // 2.0.14
  119. {$I sdlaudio.inc} // 2.26.3
  120. {$I sdlblendmode.inc} // 2.0.14
  121. {$I sdlsurface.inc} // 2.0.14
  122. {$I sdlvideo.inc} // 2.24.0
  123. {$I sdlshape.inc} // 2.24.0
  124. {$I sdlhints.inc} // 2.26.0
  125. {$I sdlloadso.inc} // 2.24.1
  126. {$I sdlmessagebox.inc} // 2.0.14
  127. {$I sdlrenderer.inc} // 2.0.22
  128. {$I sdlscancode.inc} // 2.26.2
  129. {$I sdlkeycode.inc} // 2.26.2
  130. {$I sdlkeyboard.inc} // 2.24.1
  131. {$I sdlmouse.inc} // 2.0.24
  132. {$I sdlguid.inc} // 2.24.0
  133. {$I sdljoystick.inc} // 2.24.0
  134. {$I sdlsensor.inc} // 2.26.0
  135. {$I sdlgamecontroller.inc} // 2.24.0
  136. {$I sdlhaptic.inc} // 2.26.2
  137. {$I sdlhidapi.inc} // 2.0.18
  138. {$I sdltouch.inc} // 2.24.0
  139. {$I sdlgesture.inc} // 2.26.2
  140. {$I sdlsyswm.inc}
  141. {$I sdlevents.inc} // 2.24.0
  142. {$I sdllocale.inc} // 2.0.14
  143. {$I sdlclipboard.inc} // 2.24.1
  144. {$I sdlcpuinfo.inc} // 2.0.14
  145. {$I sdlfilesystem.inc} // 2.24.1
  146. {$I sdllog.inc} // 2.0.14
  147. {$I sdlmisc.inc} // 2.0.14
  148. {$I sdlsystem.inc} // 2.24.0
  149. {$I sdl.inc} // 2.0.14
  150. implementation
  151. (*
  152. * We need an strlen() implementation for some operations on C-strings.
  153. * FPC ships one in the Strings unit; Delphi has one in the AnsiStrings unit.
  154. * Since FPC defines "DELPHI" when building in Delphi-compatibility mode,
  155. * check if "FPC" is defined to determine which compiler is used.
  156. *)
  157. uses
  158. {$IFDEF FPC}
  159. Strings
  160. {$ELSE}
  161. AnsiStrings
  162. {$ENDIF}
  163. ;
  164. // Macros from "sdl_version.h"
  165. procedure SDL_VERSION(out x: TSDL_Version);
  166. begin
  167. x.major := SDL_MAJOR_VERSION;
  168. x.minor := SDL_MINOR_VERSION;
  169. x.patch := SDL_PATCHLEVEL;
  170. end;
  171. function SDL_VERSIONNUM(X,Y,Z: cuint8): Cardinal;
  172. begin
  173. Result := X*1000 + Y*100 + Z;
  174. end;
  175. function SDL_COMPILEDVERSION: Cardinal;
  176. begin
  177. Result := SDL_VERSIONNUM(SDL_MAJOR_VERSION,
  178. SDL_MINOR_VERSION,
  179. SDL_PATCHLEVEL);
  180. end;
  181. function SDL_VERSION_ATLEAST(X,Y,Z: cuint8): Boolean;
  182. begin
  183. Result := SDL_COMPILEDVERSION >= SDL_VERSIONNUM(X,Y,Z);
  184. end;
  185. //Macros from "sdl_mouse.h"
  186. function SDL_Button(X: cint): cint;
  187. begin
  188. Result := 1 shl (X - 1);
  189. end;
  190. {$IFDEF WINDOWS}
  191. //from "sdl_thread.h"
  192. function SDL_CreateThread(fn: TSDL_ThreadFunction; name: PAnsiChar; data: Pointer): PSDL_Thread; overload;
  193. begin
  194. Result := SDL_CreateThread(fn,name,data,nil,nil);
  195. end;
  196. {$ENDIF}
  197. //from "sdl_rect.h"
  198. function SDL_PointInRect(const p: PSDL_Point; const r: PSDL_Rect): Boolean;
  199. begin
  200. Result :=
  201. (p^.x >= r^.x) and (p^.x < (r^.x + r^.w))
  202. and
  203. (p^.y >= r^.y) and (p^.y < (r^.y + r^.h))
  204. end;
  205. function SDL_RectEmpty(const r: PSDL_Rect): Boolean;
  206. begin
  207. Result := (r = NIL) or (r^.w <= 0) or (r^.h <= 0);
  208. end;
  209. function SDL_RectEquals(const a, b: PSDL_Rect): Boolean;
  210. begin
  211. Result := (a^.x = b^.x) and (a^.y = b^.y) and (a^.w = b^.w) and (a^.h = b^.h);
  212. end;
  213. function SDL_PointInFRect(const p: PSDL_FPoint; const r: PSDL_FRect): Boolean;
  214. begin
  215. Result :=
  216. (p^.x >= r^.x) and (p^.x < (r^.x + r^.w))
  217. and
  218. (p^.y >= r^.y) and (p^.y < (r^.y + r^.h))
  219. end;
  220. function SDL_FRectEmpty(const r: PSDL_FRect): Boolean;
  221. begin
  222. Result := (r = NIL) or (r^.w <= cfloat(0.0)) or (r^.h <= cfloat(0.0))
  223. end;
  224. function SDL_FRectEqualsEpsilon(const a, b: PSDL_FRect; const epsilon: cfloat): Boolean;
  225. begin
  226. Result :=
  227. (a <> NIL) and
  228. (b <> NIL) and
  229. (
  230. (a = b)
  231. or
  232. (
  233. (SDL_fabsf(a^.x - b^.x) <= epsilon)
  234. and
  235. (SDL_fabsf(a^.y - b^.y) <= epsilon)
  236. and
  237. (SDL_fabsf(a^.w - b^.w) <= epsilon)
  238. and
  239. (SDL_fabsf(a^.h - b^.h) <= epsilon)
  240. )
  241. )
  242. end;
  243. function SDL_FRectEquals(const a, b: PSDL_FRect): Boolean; Inline;
  244. begin
  245. Result := SDL_FRectEqualsEpsilon(a, b, SDL_FLT_EPSILON)
  246. end;
  247. //from "sdl_atomic.h"
  248. function SDL_AtomicIncRef(atomic: PSDL_Atomic): cint;
  249. begin
  250. Result := SDL_AtomicAdd(atomic, 1)
  251. end;
  252. function SDL_AtomicDecRef(atomic: PSDL_Atomic): Boolean;
  253. begin
  254. Result := SDL_AtomicAdd(atomic, -1) = 1
  255. end;
  256. procedure SDL_CompilerBarrier();
  257. {$IFDEF FPC}
  258. begin
  259. ReadWriteBarrier()
  260. {$ELSE}
  261. var
  262. lock: TSDL_SpinLock;
  263. begin
  264. lock := 0;
  265. SDL_AtomicLock(@lock);
  266. SDL_AtomicUnlock(@lock)
  267. {$ENDIF}
  268. end;
  269. //from "sdl_audio.h"
  270. function SDL_LoadWAV(file_: PAnsiChar; spec: PSDL_AudioSpec; audio_buf: ppcuint8; audio_len: pcuint32): PSDL_AudioSpec;
  271. begin
  272. Result := SDL_LoadWAV_RW(SDL_RWFromFile(file_, 'rb'), 1, spec, audio_buf, audio_len);
  273. end;
  274. function SDL_AUDIO_BITSIZE(x: Cardinal): Cardinal;
  275. begin
  276. Result := x and SDL_AUDIO_MASK_BITSIZE;
  277. end;
  278. function SDL_AUDIO_ISFLOAT(x: Cardinal): Cardinal;
  279. begin
  280. Result := x and SDL_AUDIO_MASK_DATATYPE;
  281. end;
  282. function SDL_AUDIO_ISBIGENDIAN(x: Cardinal): Cardinal;
  283. begin
  284. Result := x and SDL_AUDIO_MASK_ENDIAN;
  285. end;
  286. function SDL_AUDIO_ISSIGNED(x: Cardinal): Cardinal;
  287. begin
  288. Result := x and SDL_AUDIO_MASK_SIGNED;
  289. end;
  290. function SDL_AUDIO_ISINT(x: Cardinal): Cardinal;
  291. begin
  292. Result := not SDL_AUDIO_ISFLOAT(x);
  293. end;
  294. function SDL_AUDIO_ISLITTLEENDIAN(x: Cardinal): Cardinal;
  295. begin
  296. Result := not SDL_AUDIO_ISLITTLEENDIAN(x);
  297. end;
  298. function SDL_AUDIO_ISUNSIGNED(x: Cardinal): Cardinal;
  299. begin
  300. Result := not SDL_AUDIO_ISSIGNED(x);
  301. end;
  302. //from "sdl_pixels.h"
  303. function SDL_PIXELFLAG(X: cuint32): cuint32;
  304. begin
  305. Result := (X shr 28) and $0F;
  306. end;
  307. function SDL_PIXELTYPE(X: cuint32): cuint32;
  308. begin
  309. Result := (X shr 24) and $0F;
  310. end;
  311. function SDL_PIXELORDER(X: cuint32): cuint32;
  312. begin
  313. Result := (X shr 20) and $0F;
  314. end;
  315. function SDL_PIXELLAYOUT(X: cuint32): cuint32;
  316. begin
  317. Result := (X shr 16) and $0F;
  318. end;
  319. function SDL_BITSPERPIXEL(X: cuint32): cuint32;
  320. begin
  321. Result := (X shr 8) and $FF;
  322. end;
  323. function SDL_ISPIXELFORMAT_FOURCC(format: Variant): Boolean;
  324. begin
  325. Result := (format and (SDL_PIXELFLAG(format) <> 1));
  326. end;
  327. // Macros from "sdl_surface.h"
  328. function SDL_LoadBMP(_file: PAnsiChar): PSDL_Surface;
  329. begin
  330. Result := SDL_LoadBMP_RW(SDL_RWFromFile(_file, 'rb'), 1);
  331. end;
  332. function SDL_SaveBMP(const surface: PSDL_Surface; const filename: AnsiString
  333. ): cint;
  334. begin
  335. Result := SDL_SaveBMP_RW(surface, SDL_RWFromFile(PAnsiChar(filename), 'wb'), 1)
  336. end;
  337. {**
  338. * Evaluates to true if the surface needs to be locked before access.
  339. *}
  340. function SDL_MUSTLOCK(const S: PSDL_Surface): Boolean;
  341. begin
  342. Result := ((S^.flags and SDL_RLEACCEL) <> 0)
  343. end;
  344. // Macros from "sdl_shape.h"
  345. function SDL_SHAPEMODEALPHA(mode: TWindowShapeMode): Boolean;
  346. begin
  347. Result := (mode = ShapeModeDefault) or (mode = ShapeModeBinarizeAlpha) or (mode = ShapeModeReverseBinarizeAlpha);
  348. end;
  349. // from "sdl_stdinc.h"
  350. // Note: We're using FPC's Strings.strlen() here, not SDL_strlen().
  351. function SDL_iconv_utf8_locale(Const str: PAnsiChar): PAnsiChar; cdecl;
  352. begin
  353. Result := SDL_iconv_string('', 'UTF-8', str, strlen(str)+1)
  354. end;
  355. function SDL_iconv_utf8_ucs2(Const str: PAnsiChar): pcUint16; cdecl;
  356. begin
  357. Result := pcUint16(SDL_iconv_string('UCS-2-INTERNAL', 'UTF-8', str, strlen(str)+1))
  358. end;
  359. function SDL_iconv_utf8_ucs4(Const str: PAnsiChar): pcUint32; cdecl;
  360. begin
  361. Result := pcUint32(SDL_iconv_string('UCS-4-INTERNAL', 'UTF-8', str, strlen(str)+1))
  362. end;
  363. //from "sdl_video.h"
  364. function SDL_WINDOWPOS_UNDEFINED_DISPLAY(X: Variant): Variant;
  365. begin
  366. Result := (SDL_WINDOWPOS_UNDEFINED_MASK or X);
  367. end;
  368. function SDL_WINDOWPOS_ISUNDEFINED(X: Variant): Variant;
  369. begin
  370. Result := (X and $FFFF0000) = SDL_WINDOWPOS_UNDEFINED_MASK;
  371. end;
  372. function SDL_WINDOWPOS_CENTERED_DISPLAY(X: Variant): Variant;
  373. begin
  374. Result := (SDL_WINDOWPOS_CENTERED_MASK or X);
  375. end;
  376. function SDL_WINDOWPOS_ISCENTERED(X: Variant): Variant;
  377. begin
  378. Result := (X and $FFFF0000) = SDL_WINDOWPOS_CENTERED_MASK;
  379. end;
  380. //from "sdl_events.h"
  381. function SDL_GetEventState(type_: TSDL_EventType): cuint8;
  382. begin
  383. Result := SDL_EventState(type_, SDL_QUERY);
  384. end;
  385. // from "sdl_timer.h"
  386. function SDL_TICKS_PASSED(const A, B: cint32): Boolean;
  387. begin
  388. Result := ((B - A) <= 0);
  389. end;
  390. // from "sdl_gamecontroller.h"
  391. {**
  392. * Load a set of mappings from a file, filtered by the current SDL_GetPlatform()
  393. *}
  394. function SDL_GameControllerAddMappingsFromFile(const FilePath: PAnsiChar
  395. ): cint32;
  396. begin
  397. Result := SDL_GameControllerAddMappingsFromRW(SDL_RWFromFile(FilePath, 'rb'), 1)
  398. end;
  399. end.