;; ----------------------------------------------------------------------------
;; Irrlicht Wrapper For Imperative Languages - Purebasic Examples
;; IrrlichtWrapper and FreeBasic-Example by Frank Dodd (2006)
;; Improved IrrlichtWrapper and PureBasic-Example by Michael Taupitz (2006)
;; ----------------------------------------------------------------------------
;; Example 17 : Selecting Nodes With the Mouse
;; This example allows you To move the mouse around the display And To Select
;; nodes on the screen by hovering over them.
;; ----------------------------------------------------------------------------

;

XIncludeFile #PB_Compiler_Home + "Includes\IrrlichtWrapper_Include.pbi"

Structure videomodeStruct 
  mode.l
  dimension.IRR_DIM2D
  depth.l 
EndStructure 


Structure vmodeSettingsStruct 
  videomode.videomodeStruct 
  device.l
  fullscreen.l 
  stencilshadows.l
  antialiasing.l 
  vSync.l
  useFpu.l
EndStructure

Enumeration
  #IRR3DREQ_LAUNCH
  #IRR3DREQ_CANCEL
  #IRR3DREQ_NONE
EndEnumeration
  

Define irr3DRequesterSetting.vmodeSettingsStruct

UseJPEGImageDecoder()

Procedure.l Irr3DRequester(doStart.l = #True) 
  Shared irr3DRequesterSetting
  
  Define MainWindow.l   ; Id of the Window
  Define comboDriver.l   ; Id of the ComboBox for Drivers
  Define comboMode.l   ; Id of the combobox for ScreenModes
  
  ;Ids of the Checkboxes
  Define cbFullscreen.l, cbSize.l, cbAnitialias.l, cbStencilshadows.l, cbVSync.l, cbFPU.l
  
  ;Ids of the Buttons
  Define btnCancel.l, btnOk.l
  
  Define drivercount.l, DriverId.l, DriverName.s    ; Infos about the Drivers...
  
  Define irr.l  ; Pointer to the Irrlicht-Engine
  
  Define vcount.l   ; Counter for the Videomode-List

  Define i.l  ; simple Variable for For-Loops
  
  Define Quit.l = #IRR3DREQ_NONE    ; Quit-Flag

  
  Define Event.l  ; Window-Event
  
  NewList videomodes.videomodeStruct()

  
  ; #### Here Starts the Requester !
  
  OpenPreferences("Irr3D.prefs")
    irr3DRequesterSetting\videomode\mode = ReadPreferenceLong  ("ScreenMode"        , -1)
    irr3DRequesterSetting\device = ReadPreferenceLong  ("Device"        , 4)
    irr3DRequesterSetting\Fullscreen = ReadPreferenceLong  ("FullScreen"        , #False)
    irr3DRequesterSetting\stencilshadows = ReadPreferenceLong  ("StencilShadows", #False)
    irr3DRequesterSetting\antialiasing = ReadPreferenceLong  ("AntiAlias", #False)
    irr3DRequesterSetting\vsync = ReadPreferenceLong  ("VSync", #False)
    irr3DRequesterSetting\useFPu = ReadPreferenceLong  ("useFpu", #False)
  ClosePreferences()
  
  MainWindow.l = OpenWindow(#PB_Any, 503, 382, 380, 236, "Irr3DRequester",  #PB_Window_SystemMenu  | #PB_Window_TitleBar )
  If MainWindow 
    If CreateGadgetList(WindowID(MainWindow))      
      ImageGadget(#PB_Any, 0, 0, 70, 40, ImageID(CatchImage(#PB_Any,?logo)))  ; TODO
      comboDriver.l = ComboBoxGadget(#PB_Any, 120, 90, 240, 200)
      comboMode.l = ComboBoxGadget(#PB_Any, 120, 120, 240, 200)      
      TextGadget(#PB_Any, 10, 90, 90, 20, "Driver")
      TextGadget(#PB_Any, 10, 120, 100, 20, "Videomode")
      
      cbFullscreen.l = CheckBoxGadget(#PB_Any,10,150,120,20,"Fullscreen")
      SetGadgetState(cbFullscreen,irr3DRequesterSetting\Fullscreen)

      cbStencilshadows.l = CheckBoxGadget(#PB_Any,140,150,120,20,"Stencilshadows")
      SetGadgetState(cbStencilshadows,irr3DRequesterSetting\stencilshadows)

      cbAnitialias.l = CheckBoxGadget(#PB_Any,250,150,120,20,"AntiAlias")
      SetGadgetState(cbAnitialias,irr3DRequesterSetting\antialiasing)

      cbVSync.l = CheckBoxGadget(#PB_Any,140,170,120,20,"VSync")
      SetGadgetState(cbVSync,irr3DRequesterSetting\vsync)

      cbFPU.l = CheckBoxGadget(#PB_Any,10,170,120,20,"use FPU")
      SetGadgetState(cbFPU,irr3DRequesterSetting\useFPu)
      
      
      btnOk.l = ButtonGadget(#PB_Any, 280, 200, 90, 30, "OK")
      btnCancel.l = ButtonGadget(#PB_Any, 10, 200, 100, 30, "Cancel")      
    EndIf  
    
    ; Now we use the Irrlicht-Device NULL to get all possible Screenmodes      
    irr.l = IrrStart(#IRR_EDT_NULL, 0, 0, #IRR_WINDOWED, #IRR_NO_SHADOWS, #IRR_IGNORE_EVENTS);
    If irr
      vcount.l = IrrGetVideoModeCount()
      For i = 0 To vcount-1 
        AddElement(videomodes())    
        videomodes()\mode = i 
        videomodes()\depth = IrrGetVideoModeDepth(i)
        
        IrrGetVideoModeResolution(i, @videomodes()\dimension)
      Next 
      ;With the Screenmodes, we can populate the Comob-Boxes  
      ForEach videomodes() 
        AddGadgetItem(comboMode,videomodes()\mode, Str(videomodes()\dimension\Width)+"x"+Str(videomodes()\dimension\Height)+" "+Str(videomodes()\depth)+" bit")
      Next  
      ; Select previous mode
      SetGadgetState(comboMode,irr3DRequesterSetting\videomode\mode)
    
      Restore drivers
      Read DriverId.l
      drivercount.l =0
      While DriverId <> -1
        Read DriverName.s
        AddGadgetItem(comboDriver,drivercount,DriverName) 
        If DriverId = irr3DRequesterSetting\device
          SetGadgetState(comboDriver,drivercount);
        EndIf 

        Read DriverId.l
        drivercount+1
      Wend
;-ToDo Why not make a CloseDevice() after requestein NullDevice ?      
;      IrrStop()
    Else
      MessageRequester("Problem !","Not able to create the NULL-Device with Irrlicht !",#PB_MessageRequester_Ok)
      ProcedureReturn #False; Not Sucessfull 
    EndIf

    
    SetActiveWindow(MainWindow)
    Repeat
      Event = WaitWindowEvent()
        
      Select Event
        
        Case #PB_Event_Gadget
          
          Select EventGadget()            
            Case btnOk
              Quit = #IRR3DREQ_LAUNCH
            
            Case btnCancel
              Quit = #IRR3DREQ_CANCEL
          
          EndSelect 
             
        Case #PB_Event_CloseWindow
          Quit = #IRR3DREQ_CANCEL        
      EndSelect 
    Until Quit <> #IRR3DREQ_NONE    
  Else
    MessageRequester("Problem !","Not able to create the Irr3DRequester-Window !",#PB_MessageRequester_Ok)
    ProcedureReturn #False; Not Sucessfull 
  EndIf
  
  If Quit = #IRR3DREQ_LAUNCH
    ;find Videomode
    irr3DRequesterSetting\videomode\mode = GetGadgetState(comboMode);
    ForEach videomodes()
      If videomodes()\mode = irr3DRequesterSetting\videomode\mode
        irr3DRequesterSetting\videomode\dimension\Width = videomodes()\dimension\Width
        irr3DRequesterSetting\videomode\dimension\Height = videomodes()\dimension\Height
        irr3DRequesterSetting\videomode\depth = videomodes()\depth
        Break;
      EndIf
    Next
    ; find drive-ID
    Restore drivers
    Read DriverId.l
    While DriverId <> -1
      Read DriverName.s
      If DriverName = GetGadgetText(comboDriver)
        irr3DRequesterSetting\device = DriverId
        Break;
      EndIf
      Read DriverId.l
    Wend
    
    irr3DRequesterSetting\fullscreen = GetGadgetState(cbFullscreen)
    irr3DRequesterSetting\stencilshadows = GetGadgetState(cbStencilshadows)
    irr3DRequesterSetting\antialiasing = GetGadgetState(cbAnitialias)
    irr3DRequesterSetting\vSync = GetGadgetState(cbVSync)
    irr3DRequesterSetting\useFpu = GetGadgetState(cbFPU)
  
  
    CreatePreferences("Irr3D.prefs")
      WritePreferenceLong("ScreenMode",irr3DRequesterSetting\videomode\mode) 
      WritePreferenceLong("Device",irr3DRequesterSetting\device) 
      WritePreferenceLong("FullScreen",irr3DRequesterSetting\Fullscreen) 
      WritePreferenceLong("StencilShadows",irr3DRequesterSetting\stencilshadows) 
      WritePreferenceLong("AntiAlias",irr3DRequesterSetting\antialiasing) 
      WritePreferenceLong("VSync",irr3DRequesterSetting\vSync) 
      WritePreferenceLong("useFpu",irr3DRequesterSetting\useFpu) 
    ClosePreferences()    

    CloseWindow(MainWindow)
    ;Should we start the correct device ?
    If doStart = #True 
      
      ProcedureReturn IrrStartEx(irr3DRequesterSetting\device,irr3DRequesterSetting\videomode\dimension\width,irr3DRequesterSetting\videomode\dimension\height,irr3DRequesterSetting\Fullscreen, irr3DRequesterSetting\stencilshadows,#True, irr3DRequesterSetting\videomode\depth, irr3DRequesterSetting\antialiasing, irr3DRequesterSetting\useFpu,irr3DRequesterSetting\vSync,0)      
    
    EndIf

     ProcedureReturn #True ; Successfull, but not started the Irrlicht-Device
  EndIf
  
  CloseWindow(MainWindow)
  ProcedureReturn #False; Not Sucessfull or Canceled
    
EndProcedure 



DataSection 
drivers:
  Data.l #IRR_EDT_DIRECT3D8
  Data.s "DirectX 8"

  Data.l #IRR_EDT_DIRECT3D9
  Data.s "DirectX 9"
  
  Data.l #IRR_EDT_OPENGL
  Data.s "OpenGl"
  
  Data.l #IRR_EDT_SOFTWARE
  Data.s "Software"

  Data.l #IRR_EDT_BURNINGSVIDEO
  Data.s "BurningsVideo"
  
  Data.l -1

  logo: IncludeBinary "media\WrapperLogo.jpg"
  
EndDataSection
; IDE Options = PureBasic v4.02 (Windows - x86)
; CursorPosition = 87
; FirstLine = 68
; jaPBe Version=3.7.2.629
; Build=0
; FirstLine=201
; CursorPosition=247
; ExecutableFormat=Windows
; DontSaveDeclare
; IDE Options = PureBasic 4.10 Beta 3 (Windows - x86)
; ExecutableFormat = Console
; CursorPosition = 149
; FirstLine = 106
; Folding = -