Creating a decent star field

Mithrandel
03-14-2002, 10:28 PM
For a 2d space game (ala Escape Velocity), I figure I'm probably going to need a starscape in the background. It would be nice if I could have multiple layers (thus some stars would move faster than others)...

Unfortunatly, I can't think of a decent way to do this. Or rather, I can't think of a decent way to do this that won't make the average computer sputter and die because it has to do around 4000 floating point operations per rendered frame.

Here's what I have now. Notice that I'm only creating 500 stars on four different levels... but I'm going to need a heck of a lot more of stars if a player is to travel around the map and not suddenly come to a "Oh! You don't have enough system resources, so we're stiffing you on the background!" point.

Does anyone have any different ideas?

And on a less interesting note, just how does one "Set ArrayVariables() = Nothing", thus cleaning out your memory? 0.0*


Option Explicit
Option Base 1

Type tBGStar
posX As Double 'number of pixels from center - from -2,147,483,648 to 2,147,483,647
posY As Double 'number of pixels from center - from -2,147,483,648 to 2,147,483,647
layer As Byte 'What layer is this star on - from 0 to 3
End Type

Global StarField(500) As tBGStar

Public Sub CreateStarField()
Dim layer As Byte, i As Byte, Random As Byte
For layer = 0 To 3
For i = 1 To 125
With StarField(i + 125 * layer)
.layer = layer
Random = (Int(Rnd() * 4))
Select Case Random
Case 0
.posX = 0 + Rnd() * 1000
.posY = 0 + Rnd() * 1000
Case 1
.posX = 0 - Rnd() * 1000
.posY = 0 + Rnd() * 1000
Case 2
.posX = 0 - Rnd() * 1000
.posY = 0 - Rnd() * 1000
Case 3
.posX = 0 + Rnd() * 1000
.posY = 0 - Rnd() * 1000
End Select
End With
Next i
Next layer
End Sub

Public Sub MoveStarField()
Dim i As Integer, Heading As Single, Speed As Single
Speed = Ships(1).Speed
Heading = Ships(1).Heading
For i = 1 To UBound(StarField)
With StarField(i)
.posX = .posX - Speed * Sin(Heading) * (1 / (.layer + 2))
.posY = .posY + Speed * Cos(Heading) * (1 / (.layer + 2))
End With
Next i
End Sub

Public Sub DisplaySprites()

Dim SrcRect As RECT
Dim DestRect As RECT
Dim i As Integer

'Set up the source rectangle
For i = 1 To UBound(Ships)

With SrcRect
.Bottom = Ships(i).Height
.Left = 0
.Right = Ships(i).Width
.Top = 0
End With

Call BackBuffer.BltFast(370, 270, Ship(Ships(i).Facing), SrcRect, DDBLTFAST_WAIT Or DDBLTFAST_SRCCOLORKEY)
Next i
If ProjectileCount = 0 Then GoTo NoWeapons
For i = 1 To UBound(Projectiles)

With SrcRect
.Bottom = Projectiles(i).Height
.Left = 0
.Right = Projectiles(i).Width
.Top = 0
End With

Call BackBuffer.BltFast(Projectiles(i).posX, Projectiles(i).posY, Weapon(0), SrcRect, DDBLTFAST_WAIT Or DDBLTFAST_SRCCOLORKEY)
Next i
NoWeapons:

End Sub

Public Sub DestroyStarField()
'Set StarField() = Nothing
End Sub

Kitaiko
03-14-2002, 11:01 PM
look at this (http://www.visualbasicforum.com/showthread.php?threadid=18422&highlight=star) thread

andreww
03-15-2002, 12:19 AM
here, imade this before

Kitaiko
03-15-2002, 12:27 AM
yeah, its in the link

Squirm
03-15-2002, 03:09 AM
You can delete an array with the Erase statement.

Pookie
03-15-2002, 04:48 AM
I thought the erase statement doesn't delete an array but clears it's contents?

(Unless it's a dynamic array of course)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum