Code archives/3D Graphics - Effects/Automatic Starfield generator

This code has been declared by its author to be Public Domain code.

Download source code

Automatic Starfield generator by _332007
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.
; 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
       star\xpos# = star\xpos# + star\velocity#

       If (star\xpos#) > (x# + overlap_x# + 2500.0) Then
          FreeEntity star\ptr
          Delete star.star_info
          star_count% = star_count% - 1
       Else
          ;PositionMesh star\ptr, star\velocity#, 0, 0
          PositionEntity star\ptr, star\xpos#, star\ypos#, star\zpos#
       EndIf
   Next
End Function


Function DeleteAllStars()
   For star.star_info = Each star_info
      FreeEntity star\ptr
      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

;   star\ptr      = make_star (2 + Int starsize#)
   star\ptr       = CreateSphere(2 + Int starsize#)
   star\xpos#     = xref# - overlap_x# - 2500.0
   star\zpos#     = zref# - overlap_z# + 2500.0
   star\ypos#     = 650.0 + Rnd(0, 350.0) 
   star\velocity# = speed#

  ; EntityTexture star\ptr, ptr_texture(127),0,1
   EntityBlend star\ptr, 3
   EntityFX star\ptr, 1
   ScaleEntity star\ptr, starsize#, starsize#, starsize#
   EntityColor star\ptr, 255, 255, 255
   star_count% = star_count% + 1
;   PositionEntity star\ptr, star\xpos#, star\ypos#, star\zpos#

End Function

Comments

Boiled Sweets2007
And an example of how to use the functions please?


_332007
Every frame, you call AnimateStarfield(). You can put some parms to adjust exactly where you want it to be.


Then call DeleteAllStars() to delete the starfield (useful when you leave the "in game" scene).

CODE EXAMPLE:


Do not forget to have the include file in the same directory as this test.


Code Archives Forum