Ooops
January 26, 2021, 06:35:18 AM

### Author Topic: [bb] Automatic Starfield generator by _33 [ 1+ years ago ]  (Read 665 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Automatic Starfield generator by _33 [ 1+ years ago ]
« on: June 29, 2017, 12:28:40 AM »
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
1. ; FILENAME: func_stars.bb
2. ;----------------------------------------------------------------------------------------
3. ; starfield management
4. ;----------------------------------------------------------------------------------------
5. ; Define the type for each stars in a starfield
6. Type star_info
7.    Field ptr
8.    Field xpos#
9.    Field ypos#
10.    Field zpos#
11.    Field velocity#
12. End Type
13.
14. Global star_count% = 0
15.
16. Function AnimateStarfield(x#=0,y#=0,z#=0,occ%=6)
17.    Local overlap_x# = Rnd(0, 2500.0)
18.    Local overlap_z# = Rnd(0, 5000.0)
19.
20.    If Rand(1,occ%) = occ% Then AddStar(x#, y#, z#, overlap_x#, overlap_z#)
21.
22.    For star.star_info = Each star_info
23.        starxpos# = starxpos# + starvelocity#
24.
25.        If (starxpos#) > (x# + overlap_x# + 2500.0) Then
26.           FreeEntity starptr
27.           Delete star.star_info
28.           star_count% = star_count% - 1
29.        Else
30.           ;PositionMesh starptr, starvelocity#, 0, 0
31.           PositionEntity starptr, starxpos#, starypos#, starzpos#
32.        EndIf
33.    Next
34. End Function
35.
36.
37. Function DeleteAllStars()
38.    For star.star_info = Each star_info
39.       FreeEntity starptr
40.       Delete star.star_info
41.    Next
42.    star_count% = 0
43. End Function
44.
45.
46. Function AddStar(xref#, yref#, zref#, overlap_x#, overlap_z#)
47.    Local starsize# = Rnd(0.5,2.01)
48.    Local speed# = Rnd(2.0,8.0)
49.
50.    If starsize# > 2 Then
51.       starsize# = 18.0 - speed#
52.    EndIf
53.
54.    star.star_info = New star_info
55.
56. ;   starptr      = make_star (2 + Int starsize#)
57.    starptr       = CreateSphere(2 + Int starsize#)
58.    starxpos#     = xref# - overlap_x# - 2500.0
59.    starzpos#     = zref# - overlap_z# + 2500.0
60.    starypos#     = 650.0 + Rnd(0, 350.0)
61.    starvelocity# = speed#
62.
63.   ; EntityTexture starptr, ptr_texture(127),0,1
64.    EntityBlend starptr, 3
65.    EntityFX starptr, 1
66.    ScaleEntity starptr, starsize#, starsize#, starsize#
67.    EntityColor starptr, 255, 255, 255
68.    star_count% = star_count% + 1
69. ;   PositionEntity starptr, starxpos#, starypos#, starzpos#
70.
71. End Function

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:
Code: [Select]
`Include "func_stars.bb"Graphics3D 1024,768,32,1Light=CreateLight()cam=CreateCamera()CameraRange cam, .01, 10000land = CreatePlane()tex = create_checker_tex(64,96,64,128,192,128,1,1)EntityTexture land, texEntityAlpha land, 0.5mirror=CreateMirror() sens# = 0.25centerofworld = CreatePivot () : PositionEntity centerofworld, 0,1,0PositionEntity cam,0,1,0For i = 1 To 1000 : AnimateStarfield() : Next ; let it create and move stars for 1000 cyclesWhile 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"   FlipWendDeleteAllStars() ; deletes all star entitiesEndFunction 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_handleEnd Function `Do not forget to have the include file in the same directory as this test. [/i]