; -------------------------------------------------------
;  UnitTest for the IrrlichtWrapper
; -------------------------------------------------------
;
;  Testing the FileSystem-Functions of the IrrlichtWrapper (Irrlicht)

;#PB_Compiler_Home + 
XIncludeFile "T:\Dev\PB4\Includes\IrrlichtWrapper_Include.pbi"

;

; Test in normal + unicode mode only.
; Thread tests are not needed as the ObjectManager library takes care of all
; thread issues and there are no further global datastructures in this library.
;
PureUnitOptions()

; -------------------------------------------------------
;- Setup
; -------------------------------------------------------


; This ist the Structure we have To fill, If we open
; a File.
Structure sExternFS
  memory.l
  len.l
EndStructure


;This is the CallBack-Function
ProcedureCDLL.l FSCallback(*name, read_file.l, *efs.sExternFS)

  Dim Files.s(2)
  Files(0) = "myfs\media\sydney.md2"
  Files(1) = "myfs\media\sydney.bmp"

 
  Define tname.s , *p, i
  
;  Debug(PeekS(*name))
 
  ; Get the Name of the File as IrrlichtRequest it. 
  tname.s = PeekS(*name,-1,#PB_Ascii)
  

  UnitDebug("name="+tname)
 
  ; Walk through the List of our Files, and see if we find
  ; the Requested File
  For ii = 0 To 1
    If Files(ii) = tname
;      Debug("found " + tname)
      ; If we found that file, we check if we have to load it      
      ; or only to say we have it....
      If read_file  
;        Debug("read")

        ;Check if the Structure is there
        If (*efs <> #Null)
          ; open the Pack-File
          OpenPack("Example.pck")
          ; go to the wanted one
          For i = 0 To ii
            *p = NextPackFile()
          Next
          
          ; copy memory and fill the structure for Irrlicht          
          
          *efs\memory = AllocateMemory(PackFileSize())
          *efs\len = PackFileSize();
          CopyMemory(*p, *efs\memory, PackFileSize())
 
          ClosePack()         
          
;           Debug("memory="+Str(*efs\memory))
UnitDebug("memory="+Str(*efs\memory))
;           Debug("len="+Str(*efs\len))
UnitDebug("len="+Str(*efs\len))
          ProcedureReturn #True        
        EndIf
      Else 
;        Debug("exists")
        ProcedureReturn ii
      EndIf
    EndIf
  Next
  ProcedureReturn -1

EndProcedure



;We should start with initialising Irrlicht

ProcedureUnitStartup startup()

  Define p.l
  p = CreatePack("Example.pck")
  
    AddPackFile("..\media\sydney.md2",9)
    AddPackFile("..\media\sydney.bmp",9)
  
  ClosePack()


  
  Assert( IrrStart( #IRR_EDT_DIRECT3D9, 640, 480, #IRR_WINDOWED, #IRR_NO_SHADOWS, #True) )

EndProcedureUnit


ProcedureUnitShutdown shutdown()

  Assert( IrrStop() )  

EndProcedureUnit


ProcedureUnit Test_IrrAddZipFile()
  Assert( IrrAddZipFile("icons.zip", #True, #True) )
  
  *texture = IrrGetTexture("open.bmp")
  Assert(*texture <> #Null, "open.bmp wurde nicht im zip gefunden !" )
  
EndProcedureUnit




 
 
; ProcedureUnit Test_IrrAddPakFile()
;   Assert( IrrAddPakFile("data.pk3", #True, #True) )
;   
;   *texture = IrrGetTexture("irrlichtlogo.bmp")
;   Assert(*texture <> #Null, "irrlichtlogo.bmp wurde nicht im pak gefunden !" )
;   
; EndProcedureUnit


ProcedureUnit Test_IrrAddUnzipFile()
  Assert( IrrAddUnZipFile("dir_test", #True, #True) )
  
  *texture = IrrGetTexture("irrlichtlogoaligned.jpg")
  Assert(*texture <> #Null, "irrlichtlogoaligned.jpg wurde nicht im dir gefunden !" )
  
EndProcedureUnit


ProcedureUnit Test_IrrGetWorkingDirectory()

  *name = IrrGetWorkingDirectory()
  Assert( *name )
    
  AssertString( PeekS(*name,-1,#PB_Ascii)+"\", GetCurrentDirectory() )
  
EndProcedureUnit


ProcedureUnit Test_IrrChangeWorkingDirectory()

  *name = IrrGetWorkingDirectory()
  Assert( *name )
  old_name.s = PeekS(*name,-1,#PB_Ascii)
  
  Assert( IrrChangeWorkingDirectory("c:\temp\") )

  *name = IrrGetWorkingDirectory()
  Assert( *name )  
  new_name.s = PeekS(*name,-1,#PB_Ascii)

;  UnitDebug("new_name="+new_name)
  AssertString(new_name, "c:\temp")

  Assert( IrrChangeWorkingDirectory(old_name ) )
       
EndProcedureUnit




ProcedureUnit Test_IrrCreateCallbackFileSystem()


  ;Add our CallBack-FileSystem
  Assert( IrrCreateCallbackFileSystem( @FSCallback() ) )
  
  ; load a mesh, the FileName/Path has the prefix myfs\, to make sure we use our own FileSystem
  Assert( IrrGetMesh( "myfs\media\sydney.md2" ) )
  
  ; load texture resources for texturing the scene nodes
  Assert( IrrGetTexture( "myfs\media\sydney.bmp") )
 
 
       
EndProcedureUnit


; IDE Options = PureBasic 4.10 Beta 3 (Windows - x86)
; CursorPosition = 80
; FirstLine = 47
; Folding = -
; CompileSourceDirectory