December 04, 2020, 11:00:30 AM

Author Topic: [bb] Animated Flag by jfk EO-11110 [ 1+ years ago ]  (Read 634 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Animated Flag by jfk EO-11110 [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Animated Flag
Author : jfk EO-11110
Posted : 1+ years ago

Description : Smoothly animated Flag that can be used as a Screensaver or with any Flag.

Code :
Code: BlitzBasic
  1. ; Realtime-Plasma-based Mesdeformation FLAG-SCREENSAVER Demo
  2. ; Compile to .EXE and rename to .SCR. Then copy to windowssystem
  3. ; Can be used with _any_ Flag-Picture. It will also run without a Picture.
  4. ; 2002 by CSP
  5.  
  6. ;.................................... Screensaver-specific Initialisation:
  7. ; prevent to be started more than one instance when used as a Screensaver
  8. cmd$=CommandLine$()
  9. If Len(cmd$)>1
  10.  For i=1 To Len(cmd$)-1
  11.   mi$=Upper$(Mid$(cmd$,i,2))
  12.   If mi$="/P" Or mi$="/C"
  13.    ; is a Call For Screensaver settings. nothing to set tough...
  14.    End
  15.   EndIf
  16.  Next
  17. EndIf
  18. cspfile$="csp_mutex.txt"
  19. If FileType(cspfile$)=1
  20.  csp=ReadFile(cspfile$)
  21.  timestamp=ReadLine(csp) ; read last legal Instance Timestamp
  22.  CloseFile csp
  23.  timenow=MilliSecs()
  24.  If Abs(timestamp-timenow)<1000 ; more than n secs ago?
  25.   ; no, seems To be only a Mutant
  26.   End
  27.  Else
  28.   ; yes, seems To be the first Instance, a new legal instance
  29.   csp=WriteFile(cspfile$)
  30.   WriteLine csp,Str$(MilliSecs()) ; update timestamp
  31.   CloseFile csp
  32.  EndIf
  33. Else
  34.  ; seems it runs first from this Computer/Folder
  35.   csp=WriteFile(cspfile$)
  36.   WriteLine csp,Str$(MilliSecs()) ; update timestamp
  37.   CloseFile csp
  38. EndIf
  39. ;...................eo Screensaver init
  40.  
  41. Graphics3D 800,600,16,1
  42. SetBuffer BackBuffer()
  43.  
  44. ; init plasma lookup table
  45. Dim cosinus#(640)
  46. For c = 0 To 640
  47.    cosinus#(c) = Cos((115*3.14159265358 * c) / 320) * 32 + 32
  48. Next
  49.  
  50. ;size of map
  51. Const Gridx=32
  52. Const Gridz=32
  53.  
  54. x#=0:y#=0:z#=0
  55.  
  56. SetBuffer BackBuffer()
  57.  
  58. lit=CreateLight()
  59. AmbientLight 25,25,25
  60. cam=CreateCamera()
  61.  
  62. CameraRange cam,.1,1000
  63. PositionEntity cam,16,32,-10
  64. TurnEntity cam,30,0,0
  65.  
  66. ; used for surface
  67. Const Texture_Grid=4
  68.  
  69. ; create Mesh
  70. Dim map#(gridx,gridz)
  71. mesh=Create_Map(gridx,gridz,b1,3)
  72.  
  73. ; try to load a Flag Texture
  74. flagname$="my_lovely_flag.bmp"
  75. If FileType(flagname$)=1
  76.  tex1=LoadTexture(flagname$,9) ; any texture for flag
  77.  tex2=LoadTexture(flagname$,9) ; any texture for Background
  78. Else
  79.  ; else create a placebo-flag on the fly
  80.  tex1=CreateTexture(256,256,9)
  81.  tex2=CreateTexture(256,256,9)
  82.  TextureCoords tex1,1
  83.  SetBuffer TextureBuffer(tex1)
  84.  For i=0 To 255 Step 32 ; smileys'n'stripes :)
  85.   Color 255,255,255
  86.   Rect i,0,16,256,1
  87.   Color 0,0,255
  88.   Rect i+16,0,16,256,1
  89.  Next
  90.  Color 255,0,0
  91.  Rect 0,0,127,127
  92.  Color 255,255,255
  93.  For j=0 To 127 Step 16
  94.   For i=0 To 127 Step 16
  95.    Color 255,255,255
  96.    Oval i+5,j+4,8,8,1
  97.    Color 255,0,0
  98.    Oval i+2,j+4,8,8,1
  99.    Color 255,255,255
  100.    Oval i+4,j+5,2,2,1
  101.    Oval i+4,j+9,2,2,1
  102.   Next
  103.  Next
  104.  CopyRect 0,0,256,256,0,0,TextureBuffer(tex1),TextureBuffer(tex2)
  105.  SetBuffer BackBuffer()
  106. EndIf
  107.  
  108. EntityTexture mesh,tex1,0,1
  109.  
  110. ; Background
  111. in=CreateSphere()
  112. ScaleEntity in,100,100,100
  113. FlipMesh in
  114. EntityTexture in,tex2
  115.  
  116.  
  117. ; try to load a Sprite BMP
  118. sparkfile$="bluespark.bmp"
  119. If FileType(sparkfile$)=1
  120.  spark=LoadTexture(sparkfile$,2)
  121. Else
  122.  ; else create it on the fly
  123.  spark=CreateTexture(64,64,2)
  124.  SetBuffer TextureBuffer(spark)
  125.  For i=0 To 32
  126.   r=i*8
  127.   g=i*10
  128.   b=i*i
  129.   If r>255 Then r=255
  130.   If g>255 Then g=255
  131.   If b>255 Then b=255
  132.   Color r,g,b
  133.   Oval i,  i,  64-(i*2),64-(i*2),0
  134.   Oval i+1,i,  64-(i*2),64-(i*2),0
  135.  Next
  136.  LockBuffer
  137.  For j=0 To 63
  138.   For i=0 To 63
  139.    argb=ReadPixelFast(i,j)
  140.    r=(argb Shr 16) And $ff
  141.    g=(argb Shr 8) And $ff
  142.    b=argb And $ff
  143.    a=((r+g+b)/3) Shl 24
  144.    WritePixelFast i,j,(argb And $ffffff) Or a
  145.   Next
  146.  Next
  147.  UnlockBuffer
  148.  SetBuffer BackBuffer()
  149. EndIf
  150.  
  151.  
  152. ; init Particles
  153. spritemax=200 ; number of sprites
  154. Dim sp(spritemax),spx#(spritemax),spy#(spritemax),spz#(spritemax),sps#(spritemax)
  155. For i=0 To spritemax
  156.  sp(i)=CreateSprite()
  157.  EntityTexture sp(i),spark
  158.  SpriteViewMode sp(i),1
  159.  spx#(i)=16+Rnd(10)-5
  160.  spy#(i)=27+Rnd(10)-5
  161.  spz#(i)=16+Rnd(100)-50
  162.  sps#(i)=1+Rnd(0,0.5)
  163.  PositionEntity sp(i),spx#(i),spy#(i),spz#(i)
  164. Next
  165.  
  166.         mx=MouseX()
  167.         my=MouseY()
  168.     mx2=mx
  169.         my2=my
  170. ;___________________________________MAINLOOP__________________________________
  171.  
  172. While KeyDown(1)=0 And mx2=mx And my2=my
  173.         mx2=MouseX()
  174.         my2=MouseY()
  175.         Gosub Update_Plasma
  176.         mesh=Update_Map(mesh,gridx,gridz,b1,3)
  177.         TurnEntity in,0,0,1
  178.         TurnEntity cam,0,0,1.0
  179.  
  180.         ;upate particles
  181.         For i=0 To spritemax
  182.          spz#(i)=spz#(i)-sps#(i)
  183.          If spz#(i)<-30 Then
  184.           spz#(i)=16+100
  185.          EndIf
  186.          PositionEntity sp(i),spx#(i),spy#(i),spz#(i)
  187.         Next
  188.        
  189.         UpdateWorld()
  190.         RenderWorld
  191.         Flip
  192. Wend
  193. ;__________________________________eo mainloop________________________________
  194.  
  195. FreeEntity mesh
  196. FreeEntity lit
  197. FreeEntity cam
  198. EndGraphics
  199. End
  200.  
  201. Function Create_Map(tilex,tilez,brush1,tile)
  202. .create_map
  203.         mesh=CreateMesh()
  204.         surf=CreateSurface(mesh,brush1)
  205.         wid#=Float(1)/Float(Texture_grid)
  206.         u0#=wid*Float(tile Mod texture_Grid)
  207.         v0#=wid*Float(tile/texture_grid)
  208.         u1#=u0+wid
  209.         v1#=v0
  210.         u2#=u1
  211.         v2#=v0+wid
  212.         u3#=u0
  213.         v3#=v2
  214.         u#=0
  215.         v#=0
  216.         stp#=1.0/Float(tilex)
  217.         For z#=0 To tilez-1
  218.                 u=0
  219.                 For x#=0 To tilex-1
  220.                         h1#=map(x,z)
  221.                         h2#=map(x+1,z)
  222.                         h3#=map(x+1,z+1)
  223.                         h4#=map(x,z+1)
  224.                         AddVertex surf,x,h1,z,u0,v0
  225.                         VertexTexCoords surf,cnt,u,v,0,1
  226.                        
  227.                         AddVertex surf,x+1,h2,z,u1,v1
  228.                         VertexTexCoords surf,cnt+1,u+stp,v,0,1
  229.                        
  230.                         AddVertex surf,x+1,h3,z+1,u2,v2
  231.                         VertexTexCoords surf,cnt+2,u+stp,v+stp,0,1
  232.                        
  233.                         AddVertex surf,x,h4,z+1,u3,v3
  234.                         VertexTexCoords surf,cnt+3,u,v+stp,0,1
  235.                        
  236.                         AddTriangle surf,cnt,cnt+2,cnt+1
  237.                         AddTriangle surf,cnt,cnt+3,cnt+2
  238.                         cnt=cnt+4
  239.                         u=u+stp
  240.                 Next
  241.                 v=v+stp
  242.         Next
  243.         UpdateNormals mesh
  244.         Return mesh
  245. End Function
  246.  
  247.  
  248. Function Update_Map(mesh,tilex,tilez,brush1,tile)
  249.         surf=GetSurface(mesh,1)
  250.         wid#=Float(1)/Float(Texture_grid)
  251.         u0#=wid*Float(tile Mod texture_Grid)
  252.         v0#=wid*Float(tile/texture_grid)
  253.         u1#=u0+wid
  254.         v1#=v0
  255.         u2#=u1
  256.         v2#=v0+wid
  257.         u3#=u0
  258.         v3#=v2
  259.         u#=0
  260.         v#=0
  261.         stp#=1.0/Float(tilex)
  262.         For z#=0 To tilez-1
  263.                 u=0
  264.                 For x#=0 To tilex-1
  265.                         h1#=map(x,z)
  266.                         h2#=map(x+1,z)
  267.                         h3#=map(x+1,z+1)
  268.                         h4#=map(x,z+1)
  269.                         VertexCoords surf,cnt,x,h1,z
  270.                         VertexCoords surf,cnt+1,x+1,h2,z
  271.                         VertexCoords surf,cnt+2,x+1,h3,z+1
  272.                         VertexCoords surf,cnt+3,x,h4,z+1
  273.                         cnt=cnt+4
  274.                         u=u+stp
  275.                 Next
  276.                 v=v+stp
  277.         Next
  278.         UpdateNormals mesh
  279.         Return mesh
  280. End Function
  281.  
  282. ; this will animate kinda heightmap inside array 'map(,)'
  283. .Update_Plasma
  284.  wave1% = wave1% + 8
  285.  If wave1% >= 320 Then wave1% = 0
  286.  wave2% = wave2% + 4
  287.  If wave2% >= 320 Then wave2% = 0
  288.   For yw = 0 To 329 Step 10
  289.    y10=yw/10
  290.    dw = cosinus#(yw + wave2) + cosinus#(Yw + wave2)
  291.    For xw = 0 To 329 Step 10
  292.     x10=xw/10
  293.     map(x10,y10) = 1.0 + Abs(((cosinus#(xw + wave1) + cosinus#(xw + yw) + dw) / 16) )
  294.    Next
  295.   Next
  296. Return


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal