Page 2 of 2

Re: Pouring rain

Posted: Sun Jul 06, 2014 4:49 pm
by Kuron
Awesome work DK_PETER! And the rearview mirror is a nice touch. So good to see somebody else interested in using PureBasic for games.

Re: Pouring rain

Posted: Sun Jul 06, 2014 5:24 pm
by davido
@DK_PETER,

Excellent. Thank you for sharing.

Re: Pouring rain

Posted: Thu Oct 16, 2014 12:56 pm
by dige
@DK_PETER: well done, thx for sharing. What about the li.jpg? And it seems UseJPEGImageDecoder() have to be added?

Re: Pouring rain

Posted: Thu Oct 16, 2014 4:13 pm
by DK_PETER
dige wrote:@DK_PETER: well done, thx for sharing. What about the li.jpg? And it seems UseJPEGImageDecoder() have to be added?
You can add your own image or try the code below, which creates the lightning image by code.
Added basic thunder clouds. I'm pretty busy so nothing more will be added or changed.

EDIT: Submitted wrong code..Here's the last one I made.

Code: Select all

;Pouring rain (test5)
;Added thunder clouds
;-----------------------------
;By DK_PETER
;REQUIRES PB 5.30


EnableExplicit

InitEngine3D()
InitSprite()
InitMouse()
InitKeyboard()

DeclareModule Rain
  Declare.i SetResolution(Width.i = 1024, Height.i = 768, FullScreen.i = #False)
  Declare.i RunDemo()
EndDeclareModule

Module Rain
 
  Structure RainData
    id.i
    mat.i
    tex.i[2]
    x.f
    y.f
    z.f
  EndStructure
 
  Structure _MeshData
    mesh.i
    id.i
    mat.i
    tex.i
    id2.i
    node.i
  EndStructure
  
  Structure ObjectData
    rain.RainData
    splash.RainData
    road._MeshData
    street._MeshData
    clouds._MeshData
    clouds2._MeshData
    List build._MeshData()
    Winscreen.i
    steer.i
    lig.RainData
    wat._MeshData
    lg.i[2]
  EndStructure
  
  Structure thunderclouds
    mat.i
    tex.i
  EndStructure
  
  
  Declare.i DoPouringRain()
  Declare.i DoStreetAndRoad()
  Declare.i DoBuildings()
  Declare.i DoClouds()
  Declare.i CreateBuildingTexture()
  Declare.i RandomI(min.i, Max.i, Res.i = 100000)
  Declare.i CheckBuild()
  Declare.i DoSteering()
  Declare.i DoLightning()
  Declare.i CreateTheLightning()
  Declare.i DoFakePuddleSurface()
  Declare.i DoLights()
  
  
  Global Win.i, Scr.i, Cam.i, Cam2.i, FS.i,TM.i
  Global Thund.thunderclouds
  Global ob.ObjectData
  
  Procedure.i SetResolution(Width.i = 1024, Height.i = 768, FullScreen.i = #False)
    FS = FullScreen
    If FullScreen = #False
      Win = OpenWindow(#PB_Any, 0, 0, Width, Height, "Driving miss Daisy", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
      Scr = OpenWindowedScreen(WindowID(Win), 0 ,0 , Width, Height, #False, 0, 0, #PB_Screen_SmartSynchronization)
    Else
      Scr = OpenScreen(1024, 768, 32, "Driving miss Daisy",#PB_Screen_SmartSynchronization, 60)
    EndIf
    Cam = CreateCamera(#PB_Any, 0, 0, 100, 100)
    If ScreenWidth()=1024
      Cam2 = CreateCamera(#PB_Any,45,21,10,5)
    Else
      Cam2 = CreateCamera(#PB_Any,45,15,10,5)
    EndIf
    MoveCamera(Cam, 50, 12, 120)
    MoveCamera(Cam2, 50, 15, -320)
    RotateCamera(Cam2, 180,0,180)
    CameraBackColor(Cam, $323234)
    CameraBackColor(Cam2, $323234)
    Fog($323234, 150, 500, 2000)
    Add3DArchive("\",#PB_3DArchive_FileSystem)
  EndProcedure
 
  Procedure.i RandomI(min.i, Max.i, Res.i = 100000)
    ProcedureReturn (Min + (Max - Min) * Random(Res) / Res)
  EndProcedure
 
  Procedure.i DoPouringRain()
    With ob\rain
      \id = CreateParticleEmitter(#PB_Any, 2000, 600, 2000, #PB_Particle_Box, 0, 0, 0)
      \tex[0] = CreateTexture(#PB_Any, 1 , 12)
      StartDrawing(TextureOutput(\tex[0]))
      LineXY(0, 0, 0, 10, $AAAAAA)
      StopDrawing()
      \mat = CreateMaterial(#PB_Any, TextureID(\tex[0]))
      ParticleMaterial(\id, MaterialID(\mat))
      ParticleTimeToLive(\id, 4, 6)
      ParticleVelocity(\id, 200, 400)
      ParticleEmitterDirection(\id, 0, -1, 0)
      ParticleSize(\id, 0.03, 150)
      ParticleEmissionRate(\id, 1500)
      ParticleSpeedFactor(\id, 4)
    EndWith
   
    With ob\splash
      \id = CreateParticleEmitter(#PB_Any, 2000, 2000, 5, #PB_Particle_Box, 0, 5, 0)
      \tex[0] = CreateTexture(#PB_Any, 8 , 10)
      StartDrawing(TextureOutput(\tex[0]))
      LineXY(4,10,2,2,$FFFFFF)
      LineXY(4,10,4,2,$FFFFFF)
      LineXY(4,10,6,2,$FFFFFF)
      StopDrawing()
      \mat = CreateMaterial(#PB_Any, TextureID(\tex[0]))
      MaterialBlendingMode(\mat, #PB_Material_Color)
      ParticleMaterial(\id, MaterialID(\mat))
      ParticleTimeToLive(\id, 0.1, 0.1)
      ParticleVelocity(\id, 0.01, 0.01)
      ParticleEmitterDirection(\id, 0, 1, 0)
      ParticleSize(\id, 3, 0.7)
      ParticleEmissionRate(\id, 2700)
      ParticleSpeedFactor(\id, 2)
    EndWith
    ProcedureReturn #True
  EndProcedure
 
  Procedure.i DoStreetAndRoad()
    Protected tex.i
    With ob\road
      \mesh = CreatePlane(#PB_Any, 1000, 1000, 10, 10, 1, 1)
      \tex  = CreateTexture(#PB_Any, 512, 512)
      StartDrawing(TextureOutput(\tex))
      Box(0, 0, 512, 512, $484545)
      For x = 0 To 10000
        Plot(Random(511,1), Random(511,1), $737273)
      Next x
      Box(10,0,50,512, $C1C2BF)
      Box(502,0,50,512, $C1C2BF)
      For y = 0 To 9 Step 100
        Box(256, y, 80, 200, $C1C2BF)
      Next y
      StopDrawing()
      \mat = CreateMaterial(#PB_Any, TextureID(\tex))
      ScaleMaterial(\mat, 0.01, 0.01)
      MaterialFilteringMode(\mat, #PB_Material_Trilinear)
      ScrollMaterial(\mat, 0.6,0, #PB_Material_Fixed)
      ScrollMaterial(\mat, 0,-1, #PB_Material_Animated)
      \id = CreateEntity(#PB_Any, MeshID(\mesh), MaterialID(\mat), 0, 0, 0)
      ScaleEntity(\id, 8, 1, 8)
    EndWith
   
    With ob\street
      \mesh = CreateCube(#PB_Any, 0.5)
      \tex  = CreateTexture(#PB_Any, 1000,1000)
      StartDrawing(TextureOutput(\tex))
      Box(0,0, 50, 1000, $978F8A)
      Box(950, 0, 50, 1000, $978F8A)
      Box(51,0, 900, 1000, $59554C)
      StopDrawing()
      \mat = CreateMaterial(#PB_Any, TextureID(\tex))
      \id  = CreateEntity(#PB_Any, MeshID(\mesh), MaterialID(\mat), -300, -20, 0)
      ScaleEntity(\id, 700,100, 10000)
      \id2 = CopyEntity(\id, #PB_Any)
      MoveEntity(\id2, 300,-20,0)
    EndWith
    ProcedureReturn #True
  EndProcedure
 
  Procedure.i CreateBuildingTexture()
    Protected tex.i, ColorGrey.i
    tex = CreateTexture(#PB_Any, 512, 512)
    StartDrawing(TextureOutput(tex))
    ColorGrey = Random(100,40)
    Box(0, 0, 512, 512, RGB(ColorGrey, ColorGrey, ColorGrey))
    Box(0, 0, 512, 30, $8B9075)
    For y = 60 To 460 Step 20
      For x = 0 To 512 Step 20
        ColorGrey = Random(255)
        Box(x,y, 10, 10, RGB(ColorGrey, ColorGrey, ColorGrey))
      Next x
    Next y
    Box(0, 480, 512, 32, $8B9075)
    For x = 10 To 512 Step 80
      Box(x, 484, 15, 30, $555555)
    Next x
    StopDrawing()
    ProcedureReturn tex
  EndProcedure
 
  Procedure.i DoBuildings()
    Protected  scalex.i, scaley.i, scalez.i
    For z = 0 To 5
      AddElement(ob\build())
      With ob\build()
        \mesh = CreateCube(#PB_Any, 150)
        \tex  = CreateBuildingTexture()
        \mat  = CreateMaterial(#PB_Any, TextureID(\tex))
        \id   = CreateEntity(#PB_Any, MeshID(\mesh),MaterialID(\mat), -205, 78, 0-z*300)
        \id2  = CopyEntity(\id, #PB_Any)
        MoveEntity(\id2, 205, 78, 0-z*300)
      EndWith
    Next z
    ProcedureReturn #True
  EndProcedure
 
  Procedure.i CreateTheLightning()
    Protected tx.i
    tx = CreateTexture(#PB_Any, 100,300)
    StartDrawing(TextureOutput(tx))
    Box(0, 0, 100, 300, $0)
    LineXY(25,0, 50, 100, $FFFFFF)
    LineXY(24,0, 49, 100, $E0A60F)
    LineXY(26,0, 51, 100, $E0A60F)
   
    LineXY(50,100, 90, 200, $FFFFFF)
    LineXY(49,100, 89, 200, $E0A60F)
    LineXY(51,100, 91, 200, $E0A60F)
   
    LineXY(90,200, 20, 300, $FFFFFF)
    LineXY(89,200, 19, 300, $E0A60F)
    LineXY(91,200, 21, 300, $E0A60F)
   
    LineXY(50,100, 10, 200, $FFFFFF)
    LineXY(49,100, 9, 200, $E0A60F)
    LineXY(51,100, 11, 200, $E0A60F)
   
    LineXY(10,200, 60, 300, $FFFFFF)
    LineXY(9,200, 59, 300, $E0A60F)
    LineXY(11,200, 61, 300, $E0A60F)

    StopDrawing()
    ProcedureReturn tx
  EndProcedure
 
  Procedure.i DoLightning()
    ob\lig\id=CreateParticleEmitter(#PB_Any, 200,400,140,#PB_Particle_Box,0,100,-200)
    ob\lig\tex[0] = CreateTheLightning()
    ob\lig\mat = CreateMaterial(#PB_Any, TextureID(ob\lig\tex[0]))
    MaterialBlendingMode(ob\lig\mat,#PB_Material_Add)
    ParticleMaterial(ob\lig\id,MaterialID(ob\lig\mat))
    ParticleColorRange(ob\lig\id, $F1FFDD,$FFFFFF)
    ParticleEmissionRate(ob\lig\id,3)
    ParticleTimeToLive(ob\lig\id, 0.1,0.2)
    ParticleSize(ob\lig\id, 7,300)
    ParticleVelocity(ob\lig\id,0,0)
    ParticleEmitterDirection(ob\lig\id,0,-1,0)
  EndProcedure
 
  Procedure.i DoClouds()
    Protected tex.i, Col.i
    With ob\clouds
      \mesh = CreatePlane(#PB_Any, 5000, 5000, 10, 10, 1, 1)
      \tex  = CreateTexture(#PB_Any, 1024, 1024)
      StartDrawing(TextureOutput(\tex))
      DrawingMode(#PB_2DDrawing_AlphaBlend)
      For y = 0 To 4000
        ypos = Random(922,101)
        xpos = Random(922, 101)
        For x = 0 To 200
          Col.i = Random(100,40)
          Plot(RandomI(xpos-100, xpos+100), RandomI(ypos-100, ypos+100),RGBA(Col,Col,Col, Random(150,50)))
        Next x
      Next y
      StopDrawing()
     
      tex  = CreateTexture(#PB_Any, 1024, 1024)
      StartDrawing(TextureOutput(tex))
      DrawingMode(#PB_2DDrawing_AlphaBlend)
      For y = 0 To 4000
        xpos = Random(922, 101)
        ypos = Random(922, 101)
        For x = 0 To 200
          Col.i = Random(100,40)
          Plot(RandomI(xpos-100, xpos+100), RandomI(ypos-100, ypos+100),RGBA(Col,Col,Col, Random(150,50)))
        Next x
      Next y
      StopDrawing()
     
      \mat = CreateMaterial(#PB_Any, TextureID(\tex))
       ScaleMaterial(\mat, 5, 5)
      ScrollMaterial(\mat, -0.0002, 0.0002, #PB_Material_Animated)
      AddMaterialLayer(\mat, TextureID(tex),#PB_Material_Add)
      ScaleMaterial(\mat, 20, 20, 1)
      ScrollMaterial(\mat, 0.0002, 0.0002, #PB_Material_Animated, 1)
      \id = CreateEntity(#PB_Any, MeshID(\mesh), MaterialID(\mat), 0, 200, 200)
      RotateEntity(\id, 180, 0, 0)

      thund\tex = CreateTexture(#PB_Any, 1024, 1024)
      StartDrawing(TextureOutput(thund\tex))
      Box(0, 0, 1024,1024,$333333)
      For x = 0 To 1150
        Plot(Random(1023, 6), Random(1023,6), $555555)
      Next x
      StopDrawing()
     
      Thund\mat = CopyMaterial(\mat, #PB_Any)
      AddMaterialLayer(thund\mat, TextureID(thund\tex),#PB_Material_Add)
      ScaleMaterial(thund\mat, 10,10)
    EndWith
    ProcedureReturn #True
  EndProcedure
 
  Procedure.i CheckBuild()
    ForEach ob\build()
      If EntityZ(ob\build()\id,#PB_Absolute) > 680
        MoveEntity(ob\build()\id, -205, 78, -1600, #PB_Absolute)
        MoveEntity(ob\build()\id2, 205, 78, -1600, #PB_Absolute)
      Else
        MoveEntity(ob\build()\id, 0, 0, 1, #PB_Relative)
        MoveEntity(ob\build()\id2, 0, 0, 1, #PB_Relative)
      EndIf
    Next
  EndProcedure
 
  Procedure.i DoMyWindScreen()
    Protected w.i = ScreenWidth():h.i=ScreenHeight()
    ob\Winscreen = CreateSprite(#PB_Any, ScreenWidth(), ScreenHeight(),#PB_Sprite_AlphaBlending)
    StartDrawing(SpriteOutput(ob\Winscreen))
    DrawingMode(#PB_2DDrawing_Outlined)
    LineXY(100,140,80,ScreenHeight()-100,$111111)
    LineXY(ScreenWidth()-100,140,ScreenWidth()-80,ScreenHeight()-100,$111111)
    LineXY(100,140,ScreenWidth()-100,140,$111111)
    LineXY(80,ScreenHeight()-100,ScreenWidth()-80,ScreenHeight()-100,$111111)
    LineXY(0, 140, 60, 180,$111111)
    LineXY(60, 180, 70, ScreenHeight()-100,$111111)
    LineXY(70, ScreenHeight()-100, 0, ScreenHeight()-80,$111111)
    LineXY(ScreenWidth()-60, 180, ScreenWidth()-70, ScreenHeight()-100,$111111)
    LineXY(ScreenWidth()-70, ScreenHeight()-100, ScreenWidth(), ScreenHeight()-80,$111111)
    LineXY(ScreenWidth(), 140, ScreenWidth()-60, 180,$111111)
    FillArea(0,0,$111111,$3A5164)
    DrawingMode(#PB_2DDrawing_Default)
    Box(ScreenWidth()/2-5,140,10,20,$3A5164)
     StopDrawing()
    TransparentSpriteColor(ob\Winscreen, 0)
  EndProcedure
 
  Procedure.i DoSteering()
    Protected w.i, h.i
    w = ScreenWidth()/3
    h = ScreenHeight()/3
    ob\steer = CreateSprite(#PB_Any, w, h)
    StartDrawing(SpriteOutput(ob\steer))
    DrawingMode(#PB_2DDrawing_Outlined )
    Box(0, 0, w, h, $000000)
    Circle(w/2, h/2, h/2, $222222)
    Circle(w/2, h/2, h/2-25, $222222)
    FillArea(w/2,2, $222222,$222212)
    DrawingMode(#PB_2DDrawing_Default)
    Box(w/4, h/2-30, w - w/2, h/3, $222212)
    StopDrawing()
    TransparentSpriteColor(ob\steer, 0)
    ProcedureReturn #True
  EndProcedure
 
  Procedure.i DoFakePuddleSurface()
    With ob\wat
      \tex = CreateTexture(#PB_Any, 512, 512)
      StartDrawing(TextureOutput(\tex))
      DrawingMode(#PB_2DDrawing_AlphaBlend)
      For x = 0 To 50
        Ellipse(Random(500,12),Random(500,12), Random(12,2),Random(12,2), $FF656565)
      Next x
      StopDrawing()
      \mesh = CreatePlane(#PB_Any, 500, 4000, 1, 1, 1, 1)
      \mat = CreateMaterial(#PB_Any, TextureID(\tex))
      MaterialBlendingMode(\mat, #PB_Material_Color)
      ScrollMaterial(\mat, 0,-0.02, #PB_Material_Animated)
      \id = CreateEntity(#PB_Any, MeshID(\mesh), MaterialID(\mat), 0, 7, 0)
    EndWith
    ProcedureReturn #True
  EndProcedure
  
  Procedure.i DoLights()
    ob\lg[0] = CreateLight(#PB_Any, $999999, -100, 20, 120, #PB_Light_Directional)
    ob\lg[1] = CreateLight(#PB_Any, $999999, 100, 20, 120, #PB_Light_Directional)
    LightLookAt(ob\lg[0], -100,10,100)
    LightLookAt(ob\lg[1], 100,10,100)
    LightAttenuation(ob\lg[0], 0.0004, 0.000007)
    LightAttenuation(ob\lg[1], 0.0004, 0.000007)
  EndProcedure
    
  Procedure.i RunDemo()
    Protected NewOn.i, NewOf.i, Quit.i, CloudsOn.i, em.i
    Quit = 0
    ret = DoPouringRain()
    ret = DoStreetAndRoad()
    ret = DoBuildings()
    ret = DoClouds()
    ret = DoMyWindScreen()
    ret = DoSteering()
    ret = DoLightning()
    ret = DoFakePuddleSurface()
    ret = DoLights()
    
    em = ElapsedMilliseconds()
    CloudsOn = 0
    NewOn = 100
    NewOf = 20
   
    Repeat
     
      If FS = #False
        Repeat
          ev = WindowEvent()
          If ev = #PB_Event_CloseWindow
            Quit = 1
          EndIf
        Until  ev = 0
      EndIf
     
      ret = CheckBuild()
      If (ElapsedMilliseconds() - em > NewOn) And CloudsOn = 0
        SetEntityMaterial(ob\clouds\id, MaterialID(thund\mat))
        em = ElapsedMilliseconds()
        NewOn = Random(4000,50)
        CloudsOn = 1
      EndIf
      If (ElapsedMilliseconds() - em > NewOf) And CloudsOn = 1
        SetEntityMaterial(ob\clouds\id, MaterialID(ob\clouds\mat))
        em = ElapsedMilliseconds()
        NewOf = Random(100,10)
        CloudsOn = 0
      EndIf
       
      RenderWorld()
     
      ExamineKeyboard()

      DisplayTransparentSprite(ob\Winscreen, 0, 0)
      DisplayTransparentSprite(ob\steer, ScreenWidth()/80, ScreenHeight() - (SpriteHeight(ob\steer)/2)-30)
      FlipBuffers()
     
    Until KeyboardPushed(#PB_Key_Escape) Or Quit = 1
   
  EndProcedure
 
EndModule

Rain::SetResolution(1920,1080) ;or (1024,768)
Rain::RunDemo()