#include "Include\Common.dba"

   `Setup Display
      ScreenWidth#=desktop width() : ScreenHeight#=desktop height()   
      set display mode 640,480,32,1,0,0,ScreenWidth#,ScreenHeight#
      sync on : sync rate 0
      hide mouse
      autocam off
      InitCommonGlobals()

   `Set Text
      set text font "Verdana"
      set text size 15
      set text to bold
      ink rgb(255,255,255),0

   `Camera
      Aspect#=ScreenWidth#/ScreenHeight#
      Fov#=125
      Fov#=2.0*atan(tan(Fov#*0.5)/Aspect#)
      CameraDist#=-200
      position camera 0,0,-150
      set camera view 0,0,ScreenWidth#,ScreenHeight#
      set camera aspect 0,Aspect#
      set camera fov 0,Fov#
      xrotate camera 30
      color backdrop rgb(64,64,64)

   `Load Textures
      load image "Resources/water normalmap.png",1
      load image "Resources/ColdSunset.dds",2,2
      load image "Resources/flowmap.png",3

   `Load effect
      load effect "Shaders/Flowmap.fx",1,0
      load effect "Shaders/Cubemap Sky.fx",2,0
      set vector4 Vector4(1),0.25,0.25,0.25,0
      set effect constant vector 1,"AmbientColor",Vector4(1)
      set vector4 Vector4(1),1.0,1.0,1.0,0
      set effect constant vector 1,"LightColor",Vector4(1)
      set vector4 Vector4(1),1.0,-0.5,0,0
      normalize vector4 Vector4(1),Vector4(1)
      set effect constant vector 1,"LightDirection",Vector4(1)
      
   `Create Object
      make object box 1,500,10,500
      position object  1,0,-20,0 
      texture object 1,0,1
      texture object 1,1,2
      texture object 1,2,3
      set object effect 1,1

   `Create Sky
      make object sphere 2,-2000,5,5
      set object effect 2,2
      texture object 2,0,2
      
   `Timer
      global TimerVal as float
      global TimerStart as double integer
      global TimerPerf as double integer
      global TimerDelta as double float
      global TimerTarget as float
      TimerStart=PerfTimer()
      TimerTarget=16.66

   `Start loop
      do
      
   `Timer update
      TimerPerf=PerfTimer()
      TimerDelta=(TimerDelta*0.5)+((TimerPerf-TimerStart)*0.5)
      TimerStart=TimerPerf
      TimerVal=TimerDelta/(TimerTarget*10000)      

   `Control Camera
      position camera 0,0,0
      if mouseclick()=0
         xrotate camera camera angle x()-mousemovey()*0.25
         yrotate camera camera angle y()+mousemovex()*0.25
         if camera angle x()>80 then xrotate camera 80
         if camera angle x()<-60 then xrotate camera -60
      endif
      if mouseclick()>0 then CameraDist#=CameraDist#-mousemovey()*0.25
      CameraDist#=CameraDist#+mousemovez()*0.25
      if upkey()=1 then CameraDist#=CameraDist#+2.5
      if downkey()=1 then CameraDist#=CameraDist#-2.5
      if CameraDist#>-20 then CameraDist#=-20
      if CameraDist#<-300 then CameraDist#=-300
      move camera CameraDist#
      if camera position y()<-0 then position camera camera position x(),0,camera position z()

   `Change Texture
      if keystate(20)=1 and NoPress=0
         delete image 2
         inc ChangeTexture
         if ChangeTexture>3 then ChangeTexture=0
         if ChangeTexture=0 then load image "Resources/ColdSunset.dds",2,2
         if ChangeTexture=1 then load image "Resources/EpicBlueSunset.dds",2,2
         if ChangeTexture=2 then load image "Resources/Overcast.dds",2,2
         if ChangeTexture=3 then load image "Resources/MoonBurst.dds",2,2
         texture object 1,1,2
         texture object 2,0,2
         NoPress=1
      endif

   `Flow Time
      inc FlowTime#,0.01*TimerVal
      if FlowTime#>1
         FlowTime#=0
         UShift1#=UShift2#+rnd(25)+25
         VShift1#=VShift2#+rnd(25)+25
         if  UShift1#>10000 then  UShift1#=0
         if  VShift1#>10000 then  VShift1#=0
         FlowFlip=0
      endif
      if FlowTime#>0.5  and FlowFlip=0
         FlowFlip=1
         UShift2#=UShift1#+rnd(25)+25
         VShift2#=VShift1#+rnd(25)+25
         if  UShift2#>10000 then  UShift2#=0
         if  VShift2#>10000 then  VShift2#=0
      endif
      set effect constant float 1,"FlowTime",FlowTime#
      set vector4 Vector4(1), UShift1#/100,VShift1#/100,12,12
      set effect constant vector 1,"FlowUV1",Vector4(1)
      set vector4 Vector4(1),UShift2#/100,VShift2#/100,12,12
      set effect constant vector 1,"FlowUV2",Vector4(1)

   `Text
      text 20,20,"Flow Map - Fps "+str$(screen fps())   

   `End loop
      if scancode()=0 then NoPress=0
      mousemovex()=0
      mousemovey()=0
      sync
      loop

