Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADD: Support for dynamic linking first version that can be compiled u… #149

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
40 changes: 40 additions & 0 deletions units/sdl.inc
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,74 @@ const
* Unless the SDL_INIT_NOPARACHUTE flag is set, it will install cleanup
* signal handlers for some commonly ignored fatal signals (like SIGSEGV).
*}
{$ifdef SDL_RUNTIME_LOADING}
Type
TSDL_Init_func = function(flags: TSDL_Init): cint; cdecl;
Var
SDL_Init : TSDL_Init_func = Nil;
{$else}

function SDL_Init(flags: TSDL_Init): cint; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_Init' {$ENDIF} {$ENDIF};
{$endif}

{**
* This function initializes specific SDL subsystems
*}
{$ifdef SDL_RUNTIME_LOADING}
Type
TSDL_InitSubSystem_func = function(flags: TSDL_Init): cint; cdecl;
Var
SDL_InitSubSystem : TSDL_InitSubSystem_func = Nil;
{$else}

function SDL_InitSubSystem(flags: TSDL_Init): cint; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_InitSubSystem' {$ENDIF} {$ENDIF};
{$endif}

{**
* This function cleans up specific SDL subsystems
*}
{$ifdef SDL_RUNTIME_LOADING}
Type
TSDL_QuitSubSystem_proc = procedure(flags: TSDL_Init); cdecl;
Var
SDL_QuitSubSystem : TSDL_QuitSubSystem_proc = Nil;
{$else}

procedure SDL_QuitSubSystem(flags: TSDL_Init); cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_QuitSubSystem' {$ENDIF} {$ENDIF};
{$endif}

{**
* This function returns a mask of the specified subsystems which have
* previously been initialized.
*
* If flags is 0, it returns a mask of all initialized subsystems.
*}
{$ifdef SDL_RUNTIME_LOADING}
Type
TSDL_WasInit_func = function(flags: TSDL_Init): cuint32; cdecl;
Var
SDL_WasInit : TSDL_WasInit_func = Nil;
{$else}

function SDL_WasInit(flags: TSDL_Init): cuint32; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_WasInit' {$ENDIF} {$ENDIF};
{$endif}

{**
* This function cleans up all initialized subsystems. You should
* call it upon all exit conditions.
*}
{$ifdef SDL_RUNTIME_LOADING}
Type
TSDL_Quit_proc = procedure(); cdecl;
Var
SDL_Quit : TSDL_Quit_proc = Nil;
{$else}

procedure SDL_Quit(); cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_Quit' {$ENDIF} {$ENDIF};
{$endif}

47 changes: 44 additions & 3 deletions units/sdl2.pas
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@

{$I jedi.inc}

(*
* If you get a compiler error with missing file
* just create a file namend "sdl2_cfg.inc" in your project folder and
* insert the following content:
*
* ---------- Content of file ----------

{*
* set this define if you want to use runtime loading instead of static linking
* ! Attention !
* Not all functions are "ported" yet, so use is on own risk
* port missing functions.
*}
{.$DEFINE SDL_RUNTIME_LOADING}

* ---------- End content of file ----------
*
* ! Attention !
* If you use the runtime loading feature, don't forget to call the SDL_LoadLib
* function.
*)

{$I sdl2_cfg.inc}

interface

{$IFDEF WINDOWS}
Expand Down Expand Up @@ -169,6 +193,11 @@ interface
{$I sdlsystem.inc} // 2.24.0
{$I sdl.inc} // 2.0.14

{$ifdef SDL_RUNTIME_LOADING}
Function SDL_LoadLib(LibFilename: String): Boolean;
Procedure SDL_UnLoadLib();
{$endif}

implementation

(*
Expand All @@ -183,7 +212,12 @@ implementation
{$ELSE}
AnsiStrings
{$ENDIF}
;
{$ifdef SDL_RUNTIME_LOADING}
, dynlibs
{$endif}
;

{$I sdl_runtime_linking.inc}

// Macros from "sdl_version.h"
procedure SDL_VERSION(out x: TSDL_Version);
Expand Down Expand Up @@ -219,13 +253,13 @@ function SDL_Button(X: cint): cint;
{$IFDEF WINDOWS}
//from "sdl_thread.h"

function SDL_CreateThread(fn: TSDL_ThreadFunction; name: PAnsiChar;
function SDL_CreateThread2(fn: TSDL_ThreadFunction; name: PAnsiChar;
data: Pointer): PSDL_Thread; overload;
begin
Result := SDL_CreateThread(fn,name,data,nil,nil);
end;

function SDL_CreateThreadWithStackSize(fn: TSDL_ThreadFunction;
function SDL_CreateThreadWithStackSize2(fn: TSDL_ThreadFunction;
name: PAnsiChar; const stacksize: csize_t; data: Pointer
): PSDL_Thread; overload;
begin
Expand Down Expand Up @@ -480,4 +514,11 @@ function SDL_GameControllerAddMappingsFromFile(const FilePath: PAnsiChar
Result := SDL_GameControllerAddMappingsFromRW(SDL_RWFromFile(FilePath, 'rb'), 1)
end;


{$IFDEF SDL_RUNTIME_LOADING}

Finalization
SDL_UnLoadLib();
{$ENDIF}

end.
1,149 changes: 1,149 additions & 0 deletions units/sdl_runtime_linking.inc

Large diffs are not rendered by default.

78 changes: 74 additions & 4 deletions units/sdlatomic.inc
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,43 @@ type
{**
* Try to lock a spin lock by setting it to a non-zero value.
*}
{$ifdef SDL_RUNTIME_LOADING}
Type
TSDL_AtomicTryLock_func = function (lock: PSDL_SpinLock): TSDL_bool; cdecl;
Var
SDL_AtomicTryLock : TSDL_AtomicTryLock_func = Nil;
{$else}
function SDL_AtomicTryLock(lock: PSDL_SpinLock): TSDL_bool; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_AtomicTryLock' {$ENDIF} {$ENDIF};
{$endif}

{**
* Lock a spin lock by setting it to a non-zero value.
*}
{$ifdef SDL_RUNTIME_LOADING}
Type
TSDL_AtomicLock_func = function (lock: PSDL_SpinLock): TSDL_bool; cdecl;
Var
SDL_AtomicLock : TSDL_AtomicLock_func = Nil;
{$else}
function SDL_AtomicLock(lock: PSDL_SpinLock): TSDL_bool; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_AtomicLock' {$ENDIF} {$ENDIF};
{$endif}

{**
* Unlock a spin lock by setting it to 0.
*
* Always returns immediately.
*}
{$ifdef SDL_RUNTIME_LOADING}
Type
TSDL_AtomicUnlock_proc = procedure (lock: PSDL_SpinLock); cdecl;
Var
SDL_AtomicUnlock : TSDL_AtomicUnlock_proc = Nil;
{$else}
procedure SDL_AtomicUnlock(lock: PSDL_SpinLock); cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_AtomicUnlock' {$ENDIF} {$ENDIF};
{$endif}

{**
* The compiler barrier prevents the compiler from reordering
Expand All @@ -54,30 +75,58 @@ type
{**
* Set an atomic variable to a new value if it is currently an old value.
*}
function SDL_AtomicCAS(atomic: PSDL_Atomic; oldValue, newValue: cint): TSDL_bool; cdecl;
{$ifdef SDL_RUNTIME_LOADING}
Type
TSDL_AtomicCAS_func = function (atomic: PSDL_Atomic; oldValue, newValue: cint): TSDL_bool; cdecl;
Var
SDL_AtomicCAS : TSDL_AtomicCAS_func = Nil;
{$else}
function SDL_AtomicCAS(atomic: PSDL_Atomic; oldValue, newValue: cint): TSDL_bool; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_AtomicCAS' {$ENDIF} {$ENDIF};
{$endif}

{**
* Set an atomic variable to a new value and return the old one.
*
* This function also acts as a full memory barrier.
*}
function SDL_AtomicSet(atomic: PSDL_Atomic; value: cint): cint; cdecl;
{$ifdef SDL_RUNTIME_LOADING}
Type
TSDL_AtomicSet_func = function (atomic: PSDL_Atomic; value: cint): cint; cdecl;
Var
SDL_AtomicSet : TSDL_AtomicSet_func = Nil;
{$else}
function SDL_AtomicSet(atomic: PSDL_Atomic; value: cint): cint; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_AtomicSet' {$ENDIF} {$ENDIF};
{$endif}

{**
* Get the value of an atomic variable.
*}
function SDL_AtomicGet(atomic: PSDL_Atomic): cint; cdecl;
{$ifdef SDL_RUNTIME_LOADING}
Type
TSDL_AtomicGet_func = function (atomic: PSDL_Atomic): cint; cdecl;
Var
SDL_AtomicGet : TSDL_AtomicGet_func = Nil;
{$else}
function SDL_AtomicGet(atomic: PSDL_Atomic): cint; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_AtomicGet' {$ENDIF} {$ENDIF};
{$endif}

{**
* Add to an atomic variable, and return the old value.
*
* This function also acts as a full memory barrier.
*}
function SDL_AtomicAdd(atomic: PSDL_Atomic; value: cint): cint; cdecl;
{$ifdef SDL_RUNTIME_LOADING}
Type
TSDL_AtomicAdd_func = function (atomic: PSDL_Atomic; value: cint): cint; cdecl;
Var
SDL_AtomicAdd : TSDL_AtomicAdd_func = Nil;
{$else}
function SDL_AtomicAdd(atomic: PSDL_Atomic; value: cint): cint; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_AtomicAdd' {$ENDIF} {$ENDIF};
{$endif}

{**
* Increment an atomic variable used as a reference count.
Expand All @@ -92,18 +141,39 @@ function SDL_AtomicDecRef(atomic: PSDL_Atomic): Boolean;
{**
* Set a pointer to a new value if it is currently an old value.
*}
{$ifdef SDL_RUNTIME_LOADING}
Type
TSDL_AtomicCASPtr_func = function (ptr: PPointer; oldValue, newValue: Pointer): TSDL_bool; cdecl;
Var
SDL_AtomicCASPtr : TSDL_AtomicCASPtr_func = Nil;
{$else}
function SDL_AtomicCASPtr(ptr: PPointer; oldValue, newValue: Pointer): TSDL_bool; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_AtomicCASPtr' {$ENDIF} {$ENDIF};
{$endif}

{**
* Set a pointer to a new value atomically, and return the old value.
*}
{$ifdef SDL_RUNTIME_LOADING}
Type
TSDL_AtomicSetPtr_func = function (ptr: PPointer; value: Pointer): Pointer; cdecl;
Var
SDL_AtomicSetPtr : TSDL_AtomicSetPtr_func = Nil;
{$else}
function SDL_AtomicSetPtr(ptr: PPointer; value: Pointer): Pointer; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_AtomicSetPtr' {$ENDIF} {$ENDIF};
{$endif}

{**
* Get the value of a pointer atomically.
*}
{$ifdef SDL_RUNTIME_LOADING}
Type
TSDL_AtomicGetPtr_func = function (ptr: PPointer): Pointer; cdecl;
Var
SDL_AtomicGetPtr : TSDL_AtomicGetPtr_func = Nil;
{$else}
function SDL_AtomicGetPtr(ptr: PPointer): Pointer; cdecl;
external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_AtomicGetPtr' {$ENDIF} {$ENDIF};
{$endif}

Loading