; Make my Mimimum_Pack 
;


;; ----------------------------------------------------------------------------
;; Irrlicht Wrapper For Imperative Languages - Purebasic Examples
;; IrrlichtWrapper and FreeBasic-Example by Frank Dodd (2006)
;; Improved IrrlichtWrapper - Michael Taupitz (2007)
;; PureBasic-Examples by Michael Taupitz (neotoma) & Marius Eckardt (Thalius) 
;; ----------------------------------------------------------------------------
;; Example 04 : 3D Models - Meshes And Nodes
;; This example loads an MD2 model And adds it As a node To the scene
;; ----------------------------------------------------------------------------

EnableExplicit 

;; Include the Irr3D-Requester
XIncludeFile "Irr3DRequester.pb"


;; ////////////////////////////////////////////////////////////////////////////
;; Global variables

; irrlicht objects
Define *MD2Mesh.irr_mesh          ;Pointer to the Mesh
Define *MeshTexture.irr_texture     ;Pointer to the Texture
Define *SceneNode.irr_node        ;Pointer to the SceneNode
Define *OurCamera.irr_camera      ;Pointer to the Camera

;

;; ////////////////////////////////////////////////////////////////////////////
;; Include the Irr3D-Requester
XIncludeFile "Irr3DRequester.pb"


; The next lines are only for this example.
; We make a small list of files, pack them (to media/Emaple.pck)
; with the PureBasic-Packer-Commands.
;

; A Small Structure to hold some data
Structure sFiles 
  id.l
  name.s
EndStructure 

; A LinkedList for the File-Infos    
NewList myFiles.sFiles()

; Counting the Entzries
Global myFiles_id.l = 0

; Helper-Macro to add Files to the List
Macro addMyFile(__NAME__) 
  AddElement(myFiles())
  myFiles()\id = myFiles_id
  myFiles()\name = __NAME__
  myFiles_id+1
EndMacro


;Add 2 Files 
addMyFile("media\sydney.md2");
addMyFile("media\sydney.bmp");

; Create the Pack with the Infos from the
; List.
Procedure makePack()
  Shared myFiles()
  Define p.l
  p = CreatePack("media\Example.pck")
  ForEach myFiles()
    AddPackFile(myFiles()\name,9)
  Next  
  ClosePack()
EndProcedure

;Start this Packing
makePack()

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

; This is the very impiortant part of our FileSystem - 
; the Callback-Function. If we try to load a File from Irrlicht,
; irrlicht ask all of its FileSystems. So we get here the
; request about existing or loading a file.
; The 2. Parameter tells what irrlicht request - #False - only check if
; we can open that file. #True - Load that File.
; When request for Loading, the IrrlichtWrapper give us as 3. Parameter
; a small Structure about memory-position and length.
; we have to fill it, and Irrlicht load that File (from Memory !)
;
;Please note, that the first Parameter is a pointer to a String. You have
; to use PeekS() to get the String.
;
; The return of the Function is different for the 2 possible requeste.
;
; If we check only our filesystem about the existence of the file, irrlicht
; wants a -1 if could not find it, otherwise a id.
;
; If we have to load that file, we return #True if everything okay, otherwise
; #False


ProcedureCDLL.l FSCallback(*name, read_file.l, *efs.sExternFS)
  Shared myFiles()
  
  Define tname.s , *p, i
  
;  Debug(PeekS(*name))
 
  ; Get the Name of the File as IrrlichtRequest it. 
  tname.s = PeekS(*name)
  
  ; Check if it starts with "myfs\", to see if it is one of
  ; our files. This is here only for example. You have not to
  ; do the same, but if Irrlicht find the file in filesystem (that
  ; is checked before) it will load from FileSystem. 
  If Left (tname, 5) = "myfs\" 
    tname = Right(tname, Len(tname)-5)
  EndIf
     
;  Debug(tname)
 
  ; Walk through the List of our Files, and see if we find
  ; the Requested File
  ForEach myFiles()
    If myFiles()\name = 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("media\Example.pck")
          ; go to the wanted one
          For i = 0 To myFiles()\id
            *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))
;           Debug("len="+Str(*efs\len))
          
          ProcedureReturn #True        
        EndIf
      Else 
;        Debug("exists")
        ProcedureReturn myFiles()\id
      EndIf
    EndIf
  Next
  ProcedureReturn -1

EndProcedure

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

  ;Add our CallBack-FileSystem
  IrrCreateCallbackFileSystem(@FSCallback())


; set the window caption
  IrrSetWindowCaption( "Example : Use our Own FileSystem" )
  
  ; add credits for the model as a static text GUI object on the display
  IrrGuiAddStaticText( "This is the Model loaded via own FileSystem", 4,0,200,16, #IRR_GUI_NO_BORDER, #IRR_GUI_NO_WRAP )
  
  
  ; load a mesh, the FileName/Path has the prefix myfs\, to make sure we use our own FileSystem
  *MD2Mesh = IrrGetMesh( "myfs\media\sydney.md2" )
  
  ; load texture resources for texturing the scene nodes
  *MeshTexture = IrrGetTexture( "myfs\media\sydney.bmp" )
      
  ; add the mesh to the scene as a new node, the node is an instance of the
  ; mesh object supplied here
  *SceneNode = IrrAddMeshToScene( *MD2Mesh )
  
  ; apply a material to the node to give its surface color
  IrrSetNodeMaterialTexture( *SceneNode, *MeshTexture, 0 )
  
  ; switch off lighting effects on this model, as there are no lights in this
  ; scene the model would otherwise render pure black
  IrrSetNodeMaterialFlag( *SceneNode, #IRR_EMF_LIGHTING, #IRR_OFF )
  
  ; MD2 Animation sequences:
  ;/ #IRR_EMAT_STAND                     ; 0
  ;/ #IRR_EMAT_RUN                       ; 1
  ;/ #IRR_EMAT_ATTACK                    ; 2
  ;/ #IRR_EMAT_PAIN_A                    ; 3
  ;/ #IRR_EMAT_PAIN_B                    ; 4
  ;/ #IRR_EMAT_PAIN_C                    ; 5
  ;/ #IRR_EMAT_JUMP                      ; 6
  ;/ #IRR_EMAT_FLIP                      ; 7
  ;/ #IRR_EMAT_SALUTE                    ; 8
  ;/ #IRR_EMAT_FALLBACK                  ; 9
  ;/ #IRR_EMAT_WAVE                      ; 10
  ;/ #IRR_EMAT_POINT                     ; 11
  ;/ #IRR_EMAT_CROUCH_STAND              ; 12
  ;/ #IRR_EMAT_CROUCH_WALK               ; 13
  ;/ #IRR_EMAT_CROUCH_ATTACK             ; 14
  ;/ #IRR_EMAT_CROUCH_PAIN               ; 15
  ;/ #IRR_EMAT_CROUCH_DEATH              ; 16
  ;/ #IRR_EMAT_DEATH_FALLBACK            ; 17
  ;/ #IRR_EMAT_DEATH_FALLFORWARD         ; 18
  ;/ #IRR_EMAT_DEATH_FALLBACKSLOW        ; 19
  ;/ #IRR_EMAT_BOOM                      ; 20
  ;
  ; play an animation sequence embeded in the mesh. MD2 models have a group of
  ; preset animations for various poses and actions ( see above ). this animation sequence is
  ; the character standing idle.
  IrrPlayNodeMD2Animation( *SceneNode, #IRR_EMAT_STAND )
  
  ; add a camera into the scene, the first co-ordinates represents the 3D
  ; location of our view point into the scene the second co-ordinates specify the
  ; target point that the camera is looking at
  *OurCamera = IrrAddCamera( 50,0,0, 0,0,0 )
  
  
  ; -----------------------------------------------------------------------------
  ; while the irrlicht environment is still running
  While IrrRunning()
      ; begin the scene, erasing the canvas with sky-blue before rendering
      IrrBeginScene( 240, 255, 255 )
  
      ; draw the scene
      IrrDrawScene()
      
      ; draw the GUI
      IrrDrawGUI()
  
      ; end drawing the scene and render it
      IrrEndScene()
  Wend
  
  ; -----------------------------------------------------------------------------
  ; Stop the irrlicht engine and release resources
  IrrStop()

EndIf





;typedef bool(*FileSystemCallbackPtr)(const c8* , bool ,  ExternFS* );


End


  





; jaPBe Version=3.8.2.693
; Build=0
; FirstLine=24
; CursorPosition=9
; ExecutableFormat=Console
; DontSaveDeclare
; EOF