;; ----------------------------------------------------------------------------
;; Irrlicht Wrapper for Purebasic   Examples
;; ----------------------------------------------------------------------------
;; Example Irrklang_3DSound
;;This example will show how to play sounds using irrKlang.
;; It will play a looped background music and a sound every
;; time the user presses a key.
;; ----------------------------------------------------------------------------

;EnableExplicit

;At first we need to Include the right importfile
XIncludeFile #PB_Compiler_Home + "Includes\IrrlichtWrapper_Include.pbi"

;Now doing the nessecary defines
Define Quit.l = #False                             ; Flag for quitting
Define *music_soundsource.l   = #Null     ; Pointer to a SoundSource
Define *fx_soundsource.l   = #Null            ; Pointer to a SoundSource
Define KeyPressed$ =""                          ; Key

Define *music_sound.l = #Null                  ;Pointer to a Sound


;make a simple console programm !
If OpenConsole()
  
  ; Now lets start with irrKlang 3D sound engine example 01, demonstrating simple 2D sound.
  ; start up the sound engine using IrrKlangStart(). you can specify several
  ; Options as parameters when invoking that function, but for This example, the default
  ; parameters are enough.
  
  ; start the sound engine With Default parameters
  If IrrKlangStart()
    ;IrrKLangSetSoundVolume(1.0)
    ;at first we add the Sounds as SoundSource to the engine
    ;We need the pointer to the SoundSource to start a Sound. Also we can
    ;define some initial parameters for playing this SoundSource.
    *music_soundsource = IrrKlangAddSound("T:\Dev\Libraries\irrKlang\irrKlang-1.0\media/getout.ogg",ESM_NO_STREAMING,#False)
    *fx1_soundsource = IrrKlangAddSound(".\media\bell.wav")
    *fx2_soundsource = IrrKlangAddSound(".\media\explosion.wav")
    
    
    ; Now play some sound stream As music in 3d space, looped.
    ; We are setting the last parameter named 'track' To 'true' To
    ; make irrKlang Return A pointer To the played sound. (This is also returned
    ;I f the parameter 'startPaused' is set To true, by the way). 
    *music_sound = IrrKlangPlay3D(*music_soundsource, 0,0,0,  #True, #False, #True)
    
    ; the following Step isn't necessary, but to adjust the distance where
    ; the 3D sound can be heard, We set some nicer minimum distance
    ; (the Default min distance is 1, For A small object). the minimum
    ; distance simply is the distance in which the sound gets played
    ; at maximum volume.
    
    If *music_sound <> #Null
      IrrKlangSoundSetMinDistance(*music_sound, 5.0)
    EndIf
    
        
        
        PrintN("Playing streamed sound in 3D.");
        PrintN("Press ESCAPE to quit, any other key to play sound at random position.");
        PrintN("")
        PrintN("+ = Listener position");
        PrintN("o = Playing sound");
        
        
        posOnCircle.f = 0;
        radius.f = 5;
        
        Define pos3d_x.f,pos3d_y.f,pos3d_z.f
   
        ;in A loop, wait Until user presses 'q' To exit Or another key To
        ;play another sound.
        Repeat
          
          ; Each Step We calculate the Position of the 3D music.
          ; For This example, We let the
          ; music Position rotate on A circle:
            
          posOnCircle + 0.04;
          
          pos3d_x = radius * Cos(posOnCircle)
          pos3d_y = 0
          pos3d_z = radius * Sin(posOnCircle * 0.5)
            
          ; After We know the positions, We need To let irrKlang know about the
          ; listener position (always position (0,0,0), facing forward in This example)
          ; And let irrKlang know about our calculated 3D music Position
            
          IrrKlangListenerPosition(0,0,0,  0,0,1,  0,0,0,  0,0,0)
          
          If *music_sound <> #Null
            IrrKlangSoundSetPosition(*music_sound, pos3d_x, pos3d_y, pos3d_z)
          EndIf
           
          
        ; Now Print the Position of the sound in A nice way To the console
        ; And also Print the play Position
          
          stringForDisplay.s = "          +         "
          charpos.l = Int((pos3d_x + radius) / radius * 10.0);
          If charpos >= 0 And charpos < 20
            stringForDisplay = Left(stringForDisplay,charpos) + "o" + Right(stringForDisplay, Len(stringForDisplay)-charpos-1)
          EndIf
          If *music_sound <> #Null
            playPos.l = IrrKlangSoundGetPlayPosition(*music_sound) 
          EndIf
          Print(Chr(13) + "x:("+stringForDisplay+") 3dpos: " + StrF(pos3d_x) +","+StrF(pos3d_y) +","+StrF(pos3d_z) + ", playPos:"+Str(playPos/60000))
          
          ; 
          ; Char stringForDisplay[] = "          +         ";
          ; int charpos = (int)((pos3d.X + radius) / radius * 10.0f);
          ; If (charpos >= 0 && charpos < 20)
            ; stringForDisplay[charpos] = 'o';
            ; int playPos = music ? music->getPlayPosition() : 0;
            ; 
            ; printf("\rx:(%s)   3dpos: %.1f %.1f %.1f, playpos:%d:%.2d    ",
            ; stringForDisplay, pos3d.X, pos3d.Y, pos3d.Z,
            ; playPos/60000, (playPos%60000)/1000 );
            
            
            
          
          KeyPressed$ = Inkey()
      
          If KeyPressed$ <> "" And KeyPressed$ <> Chr(27)
            
            
            
            ; play random sound at some random Position.
            ; Note that when calling play3D(), no pointer is returned because We didn't
            ; specify the sound To start paused Or To track it (As We did above
            ; With the music), so We also don't need to call drop().
            
            pos_x.f = (Random(100)/radius*2)-radius
            If Random(1000) > 500
              *fx_soundsource =  *fx1_soundsource
            Else
              *fx_soundsource =  *fx2_soundsource
            EndIf 
            soundName.s = IrrKlangSSGetName(*fx_soundsource)
            IrrKlangPlay3D(*fx_soundsource, pos_x,0,0 , #False, #False, #False)
            PrintN("playing "+soundName+" at "+StrF(pos_x,1)+","+StrF(0,1)+","+StrF(0,1))
        
      ElseIf KeyPressed$ = Chr(27)
        Quit=#True
      Else
        Delay(20) ; We save some time for other Application in our system ;-)
      EndIf
      
    Until Quit
    
  Else
    PrintN("Could not startup engine")
    End
  EndIf
  
  IrrKlangStop()
EndIf

End
  


; jaPBe Version=3.7.5.643
; Build=1
; Language=0x0000 Language Neutral
; FirstLine=114
; CursorPosition=136
; ExecutableFormat=Console
; Executable=T:\projects\IrrlichtWrapperSVN\IrrlichtWrapper_1_05\PureBasic\Examples\IrrlichtWrapper\IrrKlang\Irrklang_§dSound.exe
; DontSaveDeclare
; IDE Options = PureBasic 4.10 Beta 3 (Windows - x86)
; ExecutableFormat = Console
; CursorPosition = 42
; FirstLine = 26
; Folding = -