Browse Source

MorphOS: Open Library in initialization and Close Library in finalization, InitLibrary stays for backward compatibility

git-svn-id: trunk@33145 -
marcus 9 years ago
parent
commit
6f60221927

+ 7 - 22
packages/morphunits/src/agraphics.pas

@@ -2193,7 +2193,7 @@ const
 
 
 var
-  GfxBase : Pointer;
+  GfxBase : Pointer = nil;
 
 
 function BltBitMap(srcBitMap : pBitMap location 'a0'; xSrc : LongInt location 'd0'; ySrc : LongInt location 'd1'; destBitMap : pBitMap location 'a1'; xDest : LongInt location 'd2'; yDest : LongInt location 'd3'; xSize : LongInt location 'd4'; ySize : LongInt location 'd5'; minterm : CARDINAL location 'd6'; mask : CARDINAL location 'd7'; tempA : pCHAR location 'a2') : LongInt;
@@ -2837,29 +2837,14 @@ const
   VERSION : string[2] = '50';
   LIBVERSION : longword = 50;
 
-var
-  graphics_exit : Pointer;
-
-procedure CloseGraphicsLibrary;
-begin
-  ExitProc := graphics_exit;
-  if GfxBase <> nil then begin
-    CloseLibrary(GfxBase);
-    GfxBase := nil;
-  end;
-end;
-
 function InitGraphicsLibrary : boolean;
 begin
-  GfxBase := nil;
-  GfxBase := OpenLibrary(GRAPHICSNAME,LIBVERSION);
-  if GfxBase <> nil then begin
-    graphics_exit := ExitProc;
-    ExitProc := @CloseGraphicsLibrary;
-    InitGraphicsLibrary:=True;
-  end else begin
-    InitGraphicsLibrary:=False;
-  end;
+  InitGraphicsLibrary := Assigned(GfxBase);
 end;
 
+initialization
+  GfxBase := OpenLibrary(GRAPHICSNAME,LIBVERSION);
+finalization
+  if Assigned(GfxBase) then
+    CloseLibrary(GfxBase);
 end.

+ 7 - 24
packages/morphunits/src/ahi.pas

@@ -27,7 +27,7 @@ interface
 uses exec, utility;
 
 var
-  AHIBase: PLibrary;
+  AHIBase: PLibrary = nil;
 
   type
 
@@ -594,37 +594,20 @@ begin
     AHI_PlayA(Audioctrl,@taglist);
 end;
 
-
 const
     { Change VERSION and LIBVERSION to proper values }
-
     VERSION : string[2] = '0';
     LIBVERSION : longword = 0;
 
-var
-  ahi_exit : Pointer;
-
-procedure CloseAHILibrary;
-begin
-  ExitProc := ahi_exit;
-  if AHIBase <> nil then begin
-    CloseLibrary(PLibrary(AHIBase));
-    AHIBase := nil;
-  end;
-end;
-
 function InitAHILibrary : boolean;
 begin
-  AHIBase := nil;
-  AHIBase := OpenLibrary(AHINAME,LIBVERSION);
-  if AHIBase <> nil then begin
-    ahi_exit := ExitProc;
-    ExitProc := @CloseAhiLibrary;
-    InitAhiLibrary:=True;
-  end else begin
-    InitAhiLibrary:=False;
-  end;
+  InitAhiLibrary:=Assigned(AHIBase);
 end;
 
+initialization
+  AHIBase := OpenLibrary(AHINAME,LIBVERSION);
+finalization
+  if Assigned(AHIBase) then
+    CloseLibrary(PLibrary(AHIBase));
 end. (* UNIT AHI *)
 

+ 7 - 25
packages/morphunits/src/asl.pas

@@ -522,7 +522,7 @@ const
 
 
 var
-  AslBase : pLibrary;
+  AslBase : pLibrary = nil;
 
 function AllocFileRequest : pFileRequester;
 SysCall AslBase 030;
@@ -573,38 +573,20 @@ begin
   AslRequestTags:=AslRequest(requester,@tagList);
 end;
 
-
 const
     { Change VERSION and LIBVERSION to proper values }
-
     VERSION : string[2] = '0';
     LIBVERSION : longword = 0;
 
-var
-  asl_exit: Pointer;
-
-procedure CloseAslLibrary;
-begin
-  ExitProc := asl_exit;
-  if AslBase <> nil then begin
-    CloseLibrary(PLibrary(AslBase));
-    AslBase := nil;
-  end;
-end;
-
 function InitAslLibrary : boolean;
 begin
-  AslBase := nil;
-  AslBase := OpenLibrary(ASLNAME,LIBVERSION);
-  if AslBase <> nil then begin
-    asl_exit := ExitProc;
-    ExitProc := @CloseAslLibrary;
-    InitAslLibrary:=True;
-  end else begin
-    InitAslLibrary:=False;
-  end;
+  InitAslLibrary:=Assigned(AslBase);
 end;
 
-
+initialization
+  AslBase := OpenLibrary(ASLNAME,LIBVERSION);
+finalization
+  if Assigned(AslBase) then
+    CloseLibrary(PLibrary(AslBase));
 end. (* UNIT ASL *)
 

+ 7 - 23
packages/morphunits/src/cgxvideo.pas

@@ -38,7 +38,7 @@ const
   CGXVIDEONAME = 'cgxvideo.library';
 
 var
-  CGXVideoBase: PLibrary;
+  CGXVideoBase: PLibrary = nil;
 
 type
   TVLayerHandle = Pointer;
@@ -202,33 +202,17 @@ implementation
 
 const
     { Change VERSION and LIBVERSION to proper values }
-
     VERSION : string[2] = '0';
     LIBVERSION : longword = 0;
 
-var
-    cgxvideo_exit : Pointer;
-
-procedure CloseCGXVideoLibrary;
-begin
-    ExitProc := cgxvideo_exit;
-    if CGXVideoBase <> nil then begin
-        CloseLibrary(CGXVideoBase);
-        CGXVideoBase := nil;
-    end;
-end;
-
 function InitCGXVideoLibrary: boolean;
 begin
-    CGXVideoBase := nil;
-    CGXVideoBase := OpenLibrary(CGXVIDEONAME,LIBVERSION);
-    if CGXVideoBase <> nil then begin
-        cgxvideo_exit := ExitProc;
-        ExitProc := @CloseCGXVideoLibrary;
-        InitCGXVideoLibrary := true;
-    end else begin
-        InitCGXVideoLibrary := false;
-    end;
+  InitCGXVideoLibrary := Assigned(CGXVideoBase);
 end;
 
+initialization
+  CGXVideoBase := OpenLibrary(CGXVIDEONAME,LIBVERSION);
+finalization
+  if Assigned(CGXVideoBase) then
+    CloseLibrary(CGXVideoBase);
 end.

+ 7 - 23
packages/morphunits/src/cybergraphics.pas

@@ -25,7 +25,7 @@ INTERFACE
 USES
   exec,agraphics,utility;
 
-VAR CyberGfxBase : pLibrary;
+VAR CyberGfxBase : pLibrary = nil;
 
 const
     CYBERGRAPHICSNAME : PChar = 'cybergraphics.library';
@@ -397,33 +397,17 @@ end;
 
 const
     { Change VERSION and LIBVERSION to proper values }
-
     VERSION : string[2] = '0';
     LIBVERSION : longword = 0;
 
-var
-    cybergfx_exit : Pointer;
-
-procedure CloseCyberGfxLibrary;
-begin
-    ExitProc := cybergfx_exit;
-    if CyberGfxBase <> nil then begin
-        CloseLibrary(CyberGfxBase);
-        CyberGfxBase := nil;
-    end;
-end;
-
 function InitCyberGfxLibrary: boolean;
 begin
-    CyberGfxBase := nil;
-    CyberGfxBase := OpenLibrary(CYBERGFXNAME,LIBVERSION);
-    if CyberGfxBase <> nil then begin
-        cybergfx_exit := ExitProc;
-        ExitProc := @CloseCyberGfxLibrary;
-        InitCyberGfxLibrary := true;
-    end else begin
-        InitCyberGfxLibrary := false;
-    end;
+  InitCyberGfxLibrary := Assigned(CyberGfxBase);
 end;
 
+initialization
+  CyberGfxBase := OpenLibrary(CYBERGFXNAME,LIBVERSION);
+finalization
+  if Assigned(CyberGfxBase) then
+    CloseLibrary(CyberGfxBase);
 END. (* UNIT CYBERGRAPHICS *)

+ 8 - 26
packages/morphunits/src/datatypes.pas

@@ -1250,8 +1250,8 @@ Type
 
 { *************************************************************************** }
 
-var 
-  DataTypesBase : PLibrary;
+var
+  DataTypesBase : PLibrary = nil;
 
 const
   DATATYPESNAME : PChar = 'datatypes.library';
@@ -1439,37 +1439,19 @@ begin
   DoDTDomain:=DoDTDomainA(o, win, req, rport, which, domain, @attrs);
 end;
 
-
 const
   { Change VERSION and LIBVERSION to proper values }
   VERSION : string[2] = '50';
   LIBVERSION : longword = 50;
 
-var
-  datatypes_exit : Pointer;
-
-
-procedure CloseDatatypesLibrary;
-begin
-  ExitProc := datatypes_exit;
-  if DatatypesBase <> nil then begin
-    CloseLibrary(PLibrary(DatatypesBase));
-    DatatypesBase := nil;
-  end;
-end;
-
 function InitDatatypesLibrary : boolean;
 begin
-  DatatypesBase := nil;
-  DatatypesBase := OpenLibrary(DATATYPESNAME,LIBVERSION);
-  if DatatypesBase <> nil then begin
-    datatypes_exit := ExitProc;
-    ExitProc := @CloseDatatypesLibrary;
-    InitDatatypesLibrary:=True;
-  end else begin
-    InitDatatypesLibrary:=False;
-  end;
+  InitDatatypesLibrary := Assigned(DatatypesBase);
 end;
 
-
+initialization
+  DatatypesBase := OpenLibrary(DATATYPESNAME,LIBVERSION);
+finalization
+  if Assigned(DatatypesBase) then
+    CloseLibrary(PLibrary(DatatypesBase));
 end. { UNIT DATATYPES }

+ 7 - 23
packages/morphunits/src/diskfont.pas

@@ -116,7 +116,7 @@ Type
 const
     DISKFONTNAME : PChar = 'diskfont.library';
 
-VAR DiskfontBase : pLibrary;
+VAR DiskfontBase : pLibrary = nil;
 
 FUNCTION AvailFonts(buffer : pCHAR location 'a0'; bufBytes : LONGINT location 'd0'; flags : LONGINT location 'd1') : LONGINT; syscall DiskfontBase 036;
 PROCEDURE DisposeFontContents(fontContentsHeader : pFontContentsHeader location 'a1'); syscall DiskfontBase 048;
@@ -133,33 +133,17 @@ IMPLEMENTATION
 
 const
     { Change VERSION and LIBVERSION to proper values }
-
     VERSION : string[2] = '0';
     LIBVERSION : longword = 0;
 
-var
-    diskfont_exit : Pointer;
-
-procedure CloseDiskFontLibrary;
-begin
-    ExitProc := diskfont_exit;
-    if DiskFontBase <> nil then begin
-        CloseLibrary(DiskFontBase);
-        DiskFontBase := nil;
-    end;
-end;
-
 function InitDiskFontLibrary: boolean;
 begin
-    DiskFontBase := nil;
-    DiskFontBase := OpenLibrary(DISKFONTNAME,LIBVERSION);
-    if DiskFontBase <> nil then begin
-        diskfont_exit := ExitProc;
-        ExitProc := @CloseDiskFontLibrary;
-        InitDiskFontLibrary := true;
-    end else begin
-        InitDiskFontLibrary := false;
-    end;
+  InitDiskFontLibrary := Assigned(DiskFontBase);
 end;
 
+initialization
+  DiskFontBase := OpenLibrary(DISKFONTNAME,LIBVERSION);
+finalization
+  if Assigned(DiskFontBase) then
+    CloseLibrary(DiskFontBase);
 END.

+ 7 - 23
packages/morphunits/src/gadtools.pas

@@ -435,7 +435,7 @@ Type
 
 
 VAR
-    GadToolsBase : pLibrary;
+    GadToolsBase : pLibrary = nil;
 
 FUNCTION CreateContext(glistptr : pGadget location 'a0'): pGadget; syscall GadToolsBase 114;
 FUNCTION CreateGadgetA(kind : ULONG location 'd0'; gad : pGadget location 'a0'; const ng : pNewGadget location 'a1'; const taglist : pTagItem location 'a2') : pGadget; syscall GadToolsBase 030;
@@ -476,35 +476,19 @@ end;
 
 const
     { Change VERSION and LIBVERSION to proper values }
-
     VERSION : string[2] = '0';
     LIBVERSION : longword = 0;
 
-var
-    GadTools_Exit : Pointer;
-
-procedure CloseGadToolsLibrary;
-begin
-    ExitProc := gadtools_exit;
-    if GadToolsBase <> nil then begin
-        CloseLibrary(GadToolsBase);
-        GadToolsBase := nil;
-    end;
-end;
-
 function InitGadToolsLibrary: Boolean;
 begin
-    InitGadToolsLibrary := False;
-    GadToolsBase := nil;
-    GadToolsBase := OpenLibrary(GADTOOLSNAME,LIBVERSION);
-    if GadToolsBase <> nil then
-    begin
-        GadTools_Exit := ExitProc;
-        ExitProc := @CloseGadToolsLibrary;
-        InitGadToolsLibrary := True;
-    end;
+  InitGadToolsLibrary := Assigned(GadToolsBase);
 end;
 
+initialization
+  GadToolsBase := OpenLibrary(GADTOOLSNAME,LIBVERSION);
+finalization
+  if Assigned(GadToolsBase) then
+    CloseLibrary(GadToolsBase);
 END. (* UNIT GADTOOLS *)
 
 

+ 7 - 23
packages/morphunits/src/get9.pas

@@ -22,7 +22,7 @@ interface
 uses exec;
 
 var
-  Get9Base: Pointer;
+  Get9Base: Pointer = nil;
 
 const
   GET9NAME : PChar = 'get9.library';
@@ -34,33 +34,17 @@ function InitGet9Library : boolean;
 
 implementation
 
-var
-  Get9_exit : Pointer;
-
 const
   LIBVERSION=1;
 
-procedure CloseGet9Library;
-begin
-  ExitProc := Get9_exit;
-  if Get9Base <> nil then begin
-    CloseLibrary(PLibrary(Get9Base));
-    Get9Base := nil;
-  end;
-end;
-
 function InitGet9Library : boolean;
 begin
-  Get9Base := nil;
-  Get9Base := OpenLibrary(GET9NAME,LIBVERSION);
-  if Get9Base <> nil then begin
-    Get9_exit := ExitProc;
-    ExitProc := @CloseGet9Library;
-    InitGet9Library:=True;
-  end else begin
-    InitGet9Library:=False;
-  end;
+  InitGet9Library := Assigned(Get9Base);
 end;
 
-begin
+initialization
+  Get9Base := OpenLibrary(GET9NAME,LIBVERSION);
+finalization
+  if Assigned(Get9Base) then
+    CloseLibrary(PLibrary(Get9Base));
 end.

+ 7 - 23
packages/morphunits/src/iffparse.pas

@@ -223,7 +223,7 @@ CONST
  IFFOFFSET_END       = 1;
  IFFOFFSET_CURRENT   = 2;
 
-VAR IFFParseBase : pLibrary;
+VAR IFFParseBase : pLibrary = nil;
 
 FUNCTION AllocIFF : pIFFHandle; syscall IFFParseBase 030;
 FUNCTION AllocLocalItem(typ : LONGINT location 'd0'; id : LONGINT location 'd1'; ident : LONGINT location 'd2'; dataSize : LONGINT location 'd3') : pLocalContextItem; syscall IFFParseBase 186;
@@ -287,33 +287,17 @@ end;
 
 const
     { Change VERSION and LIBVERSION to proper values }
-
     VERSION : string[2] = '0';
     LIBVERSION : longword = 0;
 
-var
-    iffparse_exit : Pointer;
-
-procedure CloseIFFParseLibrary;
-begin
-    ExitProc := iffparse_exit;
-    if IFFParseBase <> nil then begin
-        CloseLibrary(IFFParseBase);
-        IFFParseBase := nil;
-    end;
-end;
-
 function InitIFFParseLibrary: boolean;
 begin
-    IFFParseBase := nil;
-    IFFParseBase := OpenLibrary(IFFPARSENAME,LIBVERSION);
-    if IFFParseBase <> nil then begin
-        iffparse_exit := ExitProc;
-        ExitProc := @CloseIFFParseLibrary;
-        InitIFFParseLibrary := true;
-    end else begin
-        InitIFFParseLibrary := false;
-    end;
+   InitIFFParseLibrary := Assigned(IFFParseBase);
 end;
 
+initialization
+  IFFParseBase := OpenLibrary(IFFPARSENAME,LIBVERSION);
+finalization
+  if Assigned(IFFParseBase) then
+    CloseLibrary(IFFParseBase);
 END. (* UNIT IFFPARSE *)

+ 7 - 23
packages/morphunits/src/intuition.pas

@@ -4042,7 +4042,7 @@ const
   INTUITIONNAME : PChar = 'intuition.library';
 
 var
-  intuitionbase : PIntuitionBase;
+  intuitionbase : PIntuitionBase = nil;
 
 
 procedure OpenIntuition;
@@ -4693,31 +4693,15 @@ const
   VERSION : string[2] = '50';
   LIBVERSION : longword = 50;
 
-var
-  intuition_exit : Pointer;
-
-procedure CloseIntuitionLibrary;
-begin
-  ExitProc := intuition_exit;
-  if IntuitionBase <> nil then begin
-    CloseLibrary(PLibrary(IntuitionBase));
-    IntuitionBase := nil;
-  end;
-end;
-
 function InitIntuitionLibrary : boolean;
 begin
-  IntuitionBase := nil;
-  IntuitionBase := OpenLibrary(INTUITIONNAME,LIBVERSION);
-  if IntuitionBase <> nil then begin
-    intuition_exit := ExitProc;
-    ExitProc := @CloseIntuitionLibrary;
-    InitIntuitionLibrary:=True;
-  end else begin
-    InitIntuitionLibrary:=False;
-  end;
+  InitIntuitionLibrary := Assigned(IntuitionBase);
 end;
 
-
+initialization
+  IntuitionBase := OpenLibrary(INTUITIONNAME,LIBVERSION);
+finalization
+  if Assigned(IntuitionBase) then
+    CloseLibrary(PLibrary(IntuitionBase));
 end. (* UNIT INTUITION *)
 

+ 9 - 24
packages/morphunits/src/keymap.pas

@@ -85,13 +85,13 @@ Const
     DP_2DINDEXMASK      = $0f;  { mask for index for 1st of two dead keys }
     DP_2DFACSHIFT       = 4;    { shift for factor for 1st of two dead keys }
 
-var 
-  KeymapBase : pLibrary;
+var
+  KeymapBase : pLibrary = nil;
 
 const
     KEYMAPNAME : PChar = 'keymap.library';
 
-procedure SetKeyMapDefault(CONST keyMap : pKeyMap location 'a0'); 
+procedure SetKeyMapDefault(CONST keyMap : pKeyMap location 'a0');
 SysCall KeymapBase 030;
 
 function AskKeyMapDefault : pKeyMap;
@@ -113,29 +113,14 @@ const
   VERSION : string[2] = '50';
   LIBVERSION : longword = 50;
 
-var
-  keymap_exit : Pointer;
-
-procedure CloseKeymapLibrary;
-begin
-  ExitProc := keymap_exit;
-  if KeymapBase <> nil then begin
-    CloseLibrary(PLibrary(KeymapBase));
-    KeymapBase := nil;
-  end;
-end;
-
 function InitKeymapLibrary : boolean;
 begin
-  KeymapBase := nil;
-  KeymapBase := OpenLibrary(KEYMAPNAME,LIBVERSION);
-  if KeymapBase <> nil then begin
-    keymap_exit := ExitProc;
-    ExitProc := @CloseKeymapLibrary;
-    InitKeymapLibrary:=True;
-  end else begin
-    InitKeymapLibrary:=False;
-  end;
+  InitKeymapLibrary := Assigned(KeymapBase);
 end;
 
+initialization
+  KeymapBase := OpenLibrary(KEYMAPNAME,LIBVERSION);
+finalization
+  if Assigned(KeymapBase) then
+    CloseLibrary(PLibrary(KeymapBase));
 end.

+ 7 - 24
packages/morphunits/src/layers.pas

@@ -72,7 +72,7 @@ const
  LAYERSNAME : PChar = 'layers.library';
 
 var
-  LayersBase : PLibrary;
+  LayersBase : PLibrary = nil;
 
 procedure InitLayers(li : pLayer_Info location 'a0');
 SysCall LayersBase 030;
@@ -194,7 +194,6 @@ function CreateBehindLayerTags(li : pLayer_Info; bm : pBitMap; x0 : LongInt; y0
 { Helper func }
 function InitLayersLibrary : boolean;
 
-
 implementation
 
 const
@@ -202,30 +201,14 @@ const
   VERSION : string[2] = '50';
   LIBVERSION : longword = 50;
 
-var
-  layers_exit : Pointer;
-
-procedure CloseLayersLibrary;
-begin
-  ExitProc := layers_exit;
-  if LayersBase <> nil then begin
-    CloseLibrary(LayersBase);
-    LayersBase := nil;
-  end;
-end;
-
 function InitLayersLibrary : boolean;
 begin
-  LayersBase := nil;
-  LayersBase := OpenLibrary(LAYERSNAME,LIBVERSION);
-  if LayersBase <> nil then begin
-    layers_exit := ExitProc;
-    ExitProc := @CloseLayersLibrary;
-    InitLayersLibrary:=True;
-  end else begin
-    InitLayersLibrary:=False;
-  end;
+  InitLayersLibrary := Assigned(LayersBase);
 end;
 
-
+initialization
+  LayersBase := OpenLibrary(LAYERSNAME,LIBVERSION);
+finalization
+  if Assigned(LayersBase) then
+    CloseLibrary(LayersBase);
 end.

+ 7 - 23
packages/morphunits/src/mui.pas

@@ -133,7 +133,7 @@ uses exec, intuition,utility,agraphics{,iffparse};
 {$WARNING IffParse required, look for FIX ME!!!}
 
 var
-  MUIMasterBase : pLibrary;
+  MUIMasterBase : pLibrary = nil;
 
 const
   MUIMASTER_NAME  : PChar = 'muimaster.library';
@@ -3814,35 +3814,19 @@ begin
   MUIset:=SetAttrsA(obj,@args);
 end;
 
-
 const
   { Change VERSION and LIBVERSION to proper values }
   VERSION : string[2] = '19';
   LIBVERSION : longword = MUIMASTER_VMIN;
 
-var
-  muimaster_exit : Pointer;
-
-procedure CloseMUIMasterLibrary;
-begin
-  ExitProc := muimaster_exit;
-  if MUIMasterBase <> nil then begin
-    CloseLibrary(MUIMasterBase);
-    MUIMasterBase := nil;
-  end;
-end;
-
 function InitMUIMasterLibrary : boolean;
 begin
-  MUIMasterBase := nil;
-  MUIMasterBase := OpenLibrary(MUIMASTER_NAME,LIBVERSION);
-  if MUIMasterBase <> nil then begin
-    muimaster_exit := ExitProc;
-    ExitProc := @CloseMUIMasterLibrary;
-    InitMUIMasterLibrary:=True;
-  end else begin
-    InitMUIMasterLibrary:=False;
-  end;
+  InitMUIMasterLibrary := Assigned(MUIMasterBase);
 end;
 
+initialization
+  MUIMasterBase := OpenLibrary(MUIMASTER_NAME,LIBVERSION);
+finalization
+  if Assigned(MUIMasterBase) then
+    CloseLibrary(MUIMasterBase);
 end.

+ 12 - 28
packages/morphunits/src/tinygl.pas

@@ -29,14 +29,14 @@ const
   TINYGLNAME : PChar = 'tinygl.library';
 
 var
-  TinyGLBase: Pointer;
-  tglContext: Pointer;
+  TinyGLBase: Pointer = nil;
+  tglContext: Pointer = nil;
 
 function InitTinyGLLibrary : boolean;
 
 implementation
 
-function _GLInit: Pointer; 
+function _GLInit: Pointer;
 syscall sysvbase TinyGLBase 640;
 
 procedure _GLClose(gcl: pointer);
@@ -47,34 +47,18 @@ const
   VERSION : string[2] = '50';
   LIBVERSION : longword = 50;
 
-var
-  tinygl_exit : Pointer;
-
-procedure CloseTinyGLLibrary;
+function InitTinyGLLibrary : boolean;
 begin
-  ExitProc := tinygl_exit;
-  if TinyGLBase <> nil then begin
-    if tglContext <> nil then begin
-      _GLClose(tglContext);
-      tglContext := nil;
-    end;
-    CloseLibrary(PLibrary(TinyGLBase));
-    TinyGLBase := nil;
-  end;
+  InitTinyGLLibrary := Assigned(tglContext) and Assigned(TinyGLBase);
 end;
 
-function InitTinyGLLibrary : boolean;
-begin
-  TinyGLBase := nil;
+initialization
   TinyGLBase := OpenLibrary(TINYGLNAME,LIBVERSION);
-  if TinyGLBase <> nil then begin
-    tinygl_exit := ExitProc;
-    ExitProc := @CloseTinyGLLibrary;
+  if Assigned(TinyGLBase) then
     tglContext := _GLInit;
-    InitTinyGLLibrary := True;
-  end else begin
-    InitTinyGLLibrary := False;
-  end;
-end;
-
+finalization
+  if Assigned(tglContext) then
+    _GLClose(tglContext);
+  if Assigned(TinyGLBase) then
+    CloseLibrary(PLibrary(TinyGLBase));
 end.