Code: Select all
;Thank you very much team.
;The coolest release to date!
InitEngine3D()
InitSprite()
InitKeyboard()
InitMouse()
DeclareModule _TrainYard
  
  Declare.i SetResolution()  
  Declare.i RunDemo()
  
EndDeclareModule
Module _TrainYard
  
  #CameraSpeed = 5
  
  Structure _CamData
    id.i
    x.f
    y.f
    z.f
  EndStructure
  
  Structure _MeshData
    id.i
    mes.i
    tex.i
    mat.i
  EndStructure
  
  
  Declare.i ReturnTexture(Index.i = 0)
  
  Global Dim train._MeshData(20)
  Global Dim wagon._MeshData(15)
  Global Dim ground._MeshData(4)
  
  Global cam._CamData
  Global Win.i, Light.i
  Global node.i, smokenode.i
  Global smoke._MeshData
  Global station._MeshData
  
  Procedure.i SetResolution()
    Win = OpenWindow(#PB_Any, 0, 0, 1027, 768, "Chugga chugga choo chooooo!", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
    Scr = OpenWindowedScreen(WindowID(Win), 0, 0, 1024, 768,#False, 0, 0, #PB_Screen_SmartSynchronization)
    cam\id = CreateCamera(#PB_Any, 0, 0, 100, 100)
    node = CreateNode(#PB_Any, 0, 0, 0)
    CameraBackColor(cam\id, $F48735)
    MoveCamera(cam\id, 0, 5, -50)
    cam\x = 0
    cam\y = 5
    cam\z = -120
    CameraLookAt(cam\id, 0, 0, 0)
    CameraRange(cam\id, 0, 300)
    Light = CreateLight(#PB_Any, $FFFFFF, 50, 50, 50,#PB_Light_Directional)
    AttachNodeObject(node, LightID(Light))
  EndProcedure
  
  Procedure.i MakeWorld()
    With ground(0)
      \mes = CreatePlane(#PB_Any, 500, 500, 20, 20, 1, 1)
      \mat = ReturnTexture(3)
      ScaleMaterial(\mat, 0.1, 0.1)
      \id  = CreateEntity(#PB_Any, MeshID(\mes), MaterialID(\mat), 0, -9, 0)
    EndWith
    With ground(1)
      \mes = CreateCube(#PB_Any, 2)
      \mat = ReturnTexture(4)
      \id  = CreateEntity(#PB_Any, MeshID(\mes), MaterialID(\mat), 0, -8.7, 4)
      ScaleEntity(\id, 260,0.2,0.2)
    EndWith
    With ground(2)
      \mes = CreateCube(#PB_Any, 2)
      \mat = ReturnTexture(4)
      \id  = CreateEntity(#PB_Any, MeshID(\mes), MaterialID(\mat), 0, -8.7, -4)
      ScaleEntity(\id, 260,0.2,0.2)
    EndWith
  EndProcedure
  
  
  Procedure.i MakeTrain()
    With train(0) 
      \Mes = CreateCylinder(#PB_Any, 3, 20, 8, 1, 0)
      \Mat = ReturnTexture(1)
      RotateMaterial(\Mat, -90,#PB_Material_Fixed)
      \id  = CreateEntity(#PB_Any, MeshID(\Mes), MaterialID(\mat), 0, -1.5, 0)
      RotateEntity(\id, 0, 0, 90)
    EndWith
    With train(1)
      \mes = CreateSphere(#PB_Any, 2.7, 10, 10)
      \mat = ReturnTexture(1)
      \id  = CreateEntity(#PB_Any, MeshID(\mes), MaterialID(\mat), 8.7, -1.5, 0)
    EndWith
    With train(2)
      \mes = CreateCube(#PB_Any, 2)
      \mat = ReturnTexture(0)
      ScaleMaterial(\mat, 1, 4)
      \id  = CreateEntity(#PB_Any, MeshID(\mes), MaterialID(\mat), -10.1, 0, 0)
      ScaleEntity(\id, 8, 5, 4)
    EndWith
    With train(3)
      \mes = CreateCube(#PB_Any, 2)
      \mat = ReturnTexture(1)
      ScaleMaterial(\mat, 1, 4)
      \id  = CreateEntity(#PB_Any, MeshID(\mes), MaterialID(\mat), -25.1, 2.2, 0)
      ScaleEntity(\id, 7, 7, 5)
    EndWith
    With train(4)
      \mes = CreateCylinder(#PB_Any, 1, 7, 8, 0, 0)
      \mat = ReturnTexture(1)
      \id  = CreateEntity(#PB_Any, MeshID(\mes), MaterialID(\mat), 8, 4, 0)
    EndWith
    With train(5)
      ;Base
      \mes = CreateCube(#PB_Any, 1)
      \mat = ReturnTexture(1)
      ScaleMaterial(\mat, 1, 4)
      \id  = CreateEntity(#PB_Any, MeshID(\mes), MaterialID(\mat), -14, -5, 0)
      ScaleEntity(\id, 48, 0.5, 9)
    EndWith
    
    With train(6)
      ;Wheel
      \mes = CreateSphere(#PB_Any, 2, 10, 10)
      \mat = ReturnTexture(5)
      ScaleMaterial(\mat, 1, 4)
      \id  = CreateEntity(#PB_Any, MeshID(\mes), MaterialID(\mat), 5, -7.2, -3.4)
      ScaleEntity(\id, 0.8, 0.8, 0.2)
    EndWith
    
    train(7)\id = CopyEntity(train(6)\id, #PB_Any)
    MoveEntity(train(7)\id, 0, -7.2, -3.4)
    
    train(8)\id = CopyEntity(train(6)\id, #PB_Any)
    MoveEntity(train(8)\id, -9, -7.2, -3.4)
    
    train(9)\id = CopyEntity(train(6)\id, #PB_Any)
    MoveEntity(train(9)\id, -14, -7.2, -3.4)
    
    train(10)\id = CopyEntity(train(6)\id, #PB_Any)
    MoveEntity(train(10)\id, -23, -7.2, -3.4)
    
    train(11)\id = CopyEntity(train(6)\id, #PB_Any)
    MoveEntity(train(11)\id, -28, -7.2, -3.4)
    
    train(12)\id = CopyEntity(train(6)\id, #PB_Any)
    MoveEntity(train(12)\id, 5, -7.2, 3.4)
    RotateEntity(train(12)\id, 180, 0, 0)
    
    train(13)\id = CopyEntity(train(6)\id, #PB_Any)
    MoveEntity(train(13)\id, 0, -7.2, 3.4)
    RotateEntity(train(13)\id, 180, 0, 0)
    
    train(14)\id = CopyEntity(train(6)\id, #PB_Any)
    MoveEntity(train(14)\id, -9, -7.2, 3.4)
    RotateEntity(train(14)\id, 180, 0, 0)
    
    train(15)\id = CopyEntity(train(6)\id, #PB_Any)
    MoveEntity(train(15)\id, -14, -7.2, 3.4)
    RotateEntity(train(15)\id, 180, 0, 0)
    
    train(16)\id = CopyEntity(train(6)\id, #PB_Any)
    MoveEntity(train(16)\id, -23, -7.2, 3.4)
    RotateEntity(train(16)\id, 180, 0, 0)
    
    train(17)\id = CopyEntity(train(6)\id, #PB_Any)
    MoveEntity(train(17)\id, -28, -7.2, 3.4)
    RotateEntity(train(17)\id, 180, 0, 0)
    
  EndProcedure
  
  
  Procedure.i MakeStation()
    Protected fn.i
    
    fn = LoadFont(#PB_Any, "Arial", 36, #PB_Font_Bold)
    
    With station
      \tex = CreateTexture(#PB_Any, 512, 512)
      StartDrawing(TextureOutput(\tex))
      Box(0, 0, 512, 512, $908E92)
      DrawingFont(FontID(fn))
      DrawingMode(#PB_2DDrawing_Transparent)
      DrawText(150, 50, "STATION", $2D4345)
      DrawText(50, 100, "PURE BASICVILLE",$2D4345)
      DrawingMode(#PB_2DDrawing_AlphaBlend)
      For x = 0 To 512 Step 64
        Box(x, 150, 56, 100, $E1F19F59)
      Next x
      DrawingMode(#PB_2DDrawing_Outlined)
      For x = 0 To 512 Step 64
        Box(x, 150, 56, 100, $207290)
        Box(x+1, 150, 55, 99, $207290)
      Next x
      StopDrawing()
      \mes = CreatePlane(#PB_Any, 20, 20, 2, 2, 1, 1)
      \mat = CreateMaterial(#PB_Any, TextureID(\tex))
      \id  = CreateEntity(#PB_Any, MeshID(\mes), MaterialID(\mat), 0, 10, 10)
      RotateEntity(\id, -90, 0, 0)
    EndWith
    FreeFont(fn)
  EndProcedure
  
  Procedure.i ReturnTexture(Index.i = 0)
    Protected tex.i, mat.i, fn.i
    tex = CreateTexture(#PB_Any, 512, 512)
    Select Index
      Case 0 
        fn = LoadFont(#PB_Any, "MS Sans Serif", 16, #PB_Font_Bold)
        StartDrawing(TextureOutput(tex))
        DrawingFont(FontID(fn))
        DrawingMode(#PB_2DDrawing_Gradient)
        BackColor($777777)
        FrontColor($AAAAAA)
        BoxedGradient(0, 0, 512, 12)
        Box(0, 0, 512,512)
        DrawingMode(#PB_2DDrawing_Transparent)
        DrawText(140, 226, "WWW.PUREBASIC.COM",$FFFFFF)
        DrawText(50, 266, "THE BEST PROGRAMMING LANGUAGE EVER!",$FFFFFF)
        StopDrawing()
        FreeFont(fn)
      Case 1 
        StartDrawing(TextureOutput(tex))
        DrawingMode(#PB_2DDrawing_Gradient)
        BackColor($777777)
        FrontColor($AAAAAA)
        BoxedGradient(0, 0, 512, 512)
        Box(0, 0, 512,512)
        StopDrawing()
      Case 2 ; Bark - poor imitation
        StartDrawing(TextureOutput(tex))
        DrawingMode(#PB_2DDrawing_AlphaBlend)
        Box(0, 0, 512, 512, $FF095688)
        For x = 0 To 200
          LineXY(Random(511,2), Random(511,2), Random(511,2), Random(511,2),$E1041635)
        Next x
        StopDrawing()
      Case 3 ; Ground
        StartDrawing(TextureOutput(tex))
        Box(0, 0, 512, 512, $546F7C)
        FrontColor($444540)
        BackColor($AAAAAA)
        DrawingMode(#PB_2DDrawing_Gradient)
        For z = 0 To 56000
          x.i = Random(509,2)
          y.i = Random(509,2)
          r.i = Random(3,1)
          CircularGradient(x, y, r)
          Circle(x, y, r)
        Next z
        StopDrawing()
      Case 4 ; Rails
        StartDrawing(TextureOutput(tex))
        Box(0, 0, 512, 512, $4D4B53)
        FrontColor($2042A1)
        BackColor($2E19A1)
        DrawingMode(#PB_2DDrawing_Gradient)
        For z = 0 To 6000
          x.i = Random(509,2)
          y.i = Random(509,2)
          r.i = Random(3,1)
          CircularGradient(x, y, r)
          Circle(x, y, r)
        Next z
        StopDrawing()
      Case 5
        StartDrawing(TextureOutput(tex))
        Box(0, 0, 512, 512, $000000)
        For x = 0 To 512 Step 16
          LineXY(0, x, 512, 512 - x, $C6C6C6)
        Next x
        StopDrawing()
      Case 6
        StartDrawing(TextureOutput(tex))
        Box(0, 0, 64, 64, $000000)
        DrawingMode(#PB_2DDrawing_Gradient)
        FrontColor($AAAAAA)
        BackColor($333333)
        CircularGradient(32, 32, 15)
        Circle(32, 32, 15)
        StopDrawing()
    EndSelect
        
    mat = CreateMaterial(#PB_Any, TextureID(tex))
    ProcedureReturn mat
    
  EndProcedure
  
  Procedure.i DoSmoke()
    smokenode = CreateNode(#PB_Any, 0, 0, 0)
    smoke\id = CreateParticleEmitter(#PB_Any, 1, 1, 1, #PB_Particle_Point)
    smoke\mat = ReturnTexture(6)
    MaterialBlendingMode(smoke\mat, #PB_Material_Color)
    ParticleEmissionRate(smoke\id, 200)
    ParticleEmitterDirection(smoke\id, 0, 1, 0)
    ParticleMaterial(smoke\id, MaterialID(smoke\mat))
    ParticleTimeToLive(smoke\id, 0.3, 1)
    ParticleVelocity(smoke\id, 0.2, 0.5)
    MoveParticleEmitter(smoke\id,  -5, -5, -1, #PB_Absolute|#PB_Local)
    AttachEntityObject(train(4)\id, "Empty here produces an error", ParticleEmitterID(smoke\id)) 
    ;Word must be present or an error occur. ("something")
  EndProcedure
  
  Procedure.i RunDemo()
    Protected Exit.i = 0
    WorldShadows(#PB_Shadow_Additive)
    ret = MakeWorld()
    ret = MakeTrain()
    ret = DoSmoke()
    ret = MakeStation()
    
    Repeat
      
      Repeat
        ev = WindowEvent()
        If ev = #PB_Event_CloseWindow
          exit = 1
        EndIf
      Until ev = 0
      
      If ExamineKeyboard()       
        
        If KeyboardPushed(#PB_Key_A)
          cam\x = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_D)
          cam\x = #CameraSpeed 
        Else
          cam\x = 0
        EndIf
        
        If KeyboardPushed(#PB_Key_W)
          cam\y = -#CameraSpeed 
        ElseIf KeyboardPushed(#PB_Key_S)
          cam\y = #CameraSpeed 
        Else
          cam\y = 0
        EndIf
        
      EndIf
      MoveCamera(cam\id, CameraX(cam\id), CameraY(cam\id), CameraZ(cam\id), #PB_Absolute)
      MoveCamera(cam\id, cam\x, 0, cam\y)
      
      RenderWorld()
      
      FlipBuffers()
      
    Until KeyboardPushed(#PB_Key_Escape) Or Exit = 1
    
  EndProcedure
EndModule
_TrainYard::SetResolution()
_TrainYard::RunDemo()