[bb] Automatic Starfield generator by _33 [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:40

Previous topic - Next topic

BlitzBot

Title : Automatic Starfield generator
Author : _33
Posted : 1+ years ago

Description : It's a simple function set that I made a while back to help me get an easy to maintain starfield.  Every frame, you call AnimateStarfield().  You can put some parms to adjust exactly where you want it to be.  Usually your x and z are the same as where your view is.  The starfield always go in the same direction.  I didn't make it too advanced.  And there's a function to delete the starfield, of course.

Code :
Code (blitzbasic) Select
; FILENAME: func_stars.bb
;----------------------------------------------------------------------------------------
; starfield management
;----------------------------------------------------------------------------------------
; Define the type for each stars in a starfield
Type star_info
   Field ptr
   Field xpos#
   Field ypos#
   Field zpos#
   Field velocity#
End Type

Global star_count% = 0

Function AnimateStarfield(x#=0,y#=0,z#=0,occ%=6)
   Local overlap_x# = Rnd(0, 2500.0)
   Local overlap_z# = Rnd(0, 5000.0)

   If Rand(1,occ%) = occ% Then AddStar(x#, y#, z#, overlap_x#, overlap_z#)

   For star.star_info = Each star_info
       starxpos# = starxpos# + starvelocity#

       If (starxpos#) > (x# + overlap_x# + 2500.0) Then
          FreeEntity starptr
          Delete star.star_info
          star_count% = star_count% - 1
       Else
          ;PositionMesh starptr, starvelocity#, 0, 0
          PositionEntity starptr, starxpos#, starypos#, starzpos#
       EndIf
   Next
End Function


Function DeleteAllStars()
   For star.star_info = Each star_info
      FreeEntity starptr
      Delete star.star_info
   Next
   star_count% = 0
End Function


Function AddStar(xref#, yref#, zref#, overlap_x#, overlap_z#)
   Local starsize# = Rnd(0.5,2.01)
   Local speed# = Rnd(2.0,8.0)

   If starsize# > 2 Then
      starsize# = 18.0 - speed#
   EndIf

   star.star_info = New star_info

;   starptr      = make_star (2 + Int starsize#)
   starptr       = CreateSphere(2 + Int starsize#)
   starxpos#     = xref# - overlap_x# - 2500.0
   starzpos#     = zref# - overlap_z# + 2500.0
   starypos#     = 650.0 + Rnd(0, 350.0)
   starvelocity# = speed#

  ; EntityTexture starptr, ptr_texture(127),0,1
   EntityBlend starptr, 3
   EntityFX starptr, 1
   ScaleEntity starptr, starsize#, starsize#, starsize#
   EntityColor starptr, 255, 255, 255
   star_count% = star_count% + 1
;   PositionEntity starptr, starxpos#, starypos#, starzpos#

End Function


Comments :


Boiled Sweets(Posted 1+ years ago)

 And an example of how to use the functions please?


_33(Posted 1+ years ago)

 <div class="quote"> Every frame, you call AnimateStarfield(). You can put some parms to adjust exactly where you want it to be. </div>Then call DeleteAllStars() to delete the starfield (useful when you leave the "in game" scene).CODE EXAMPLE:
Include "func_stars.bb"
Graphics3D 1024,768,32,1
Light=CreateLight()
cam=CreateCamera()
CameraRange cam, .01, 10000
land = CreatePlane()
tex = create_checker_tex(64,96,64,128,192,128,1,1)
EntityTexture land, tex
EntityAlpha land, 0.5
mirror=CreateMirror()
sens# = 0.25
centerofworld = CreatePivot () : PositionEntity centerofworld, 0,1,0
PositionEntity cam,0,1,0
For i = 1 To 1000 : AnimateStarfield() : Next ; let it create and move stars for 1000 cycles
While Not KeyDown(1)
   x# = x + sens : z# = z + sens
   PositionEntity centerofworld,Sin(x),1,Cos(z)
   PointEntity cam, centerofworld
   AnimateStarfield() ; cycling through the starfield algorythm
   RenderWorld ()
   Color 0,255,0
   Text 0,0, "PRESS [ESC] TO EXIT"
   Flip
Wend
DeleteAllStars() ; deletes all star entities
End

Function create_checker_tex(red1,green1,blue1,red2,green2,blue2,scale_u#,scale_v#)
   texture_handle = CreateTexture(32,32,256)
   SetBuffer TextureBuffer(texture_handle)
   Color red1,green1,blue1 : Rect 0,0,32,32 : Color red2,green2,blue2 : Rect 0,0,16,16,1 : Rect 16,16,16,16,1
   ScaleTexture texture_handle,scale_u#,scale_v#
   SetBuffer BackBuffer()
   Return texture_handle
End Function
Do not forget to have the include file in the same directory as this test. [/i]