Improved Starfield !!

BlitzMax Forums/BlitzMax Programming/Improved Starfield !!

I'm trying to get a starfield background to look a little more interesting. Can anyone suggest a way to make them twinkle, or glow, or something that might look better? (and yes, i know stars don't twinkle in space).

In the example below; stars are game objects of a random colour simply plotted to the screen.

Any idea's?

Graphics 640,480

Tstar.generate()

While Not AppTerminate() And Not KeyHit(KEY_ESCAPE)
	TGameObject.updateall() 
	TGameObject.drawall( True )
Wend

Type TGameObject
	Global list:TList = CreateList()
	Field x#,y#
	Field r% , g% , b%
	'----------------------------------------
	Method draw() 
	End Method
	'----------------------------------------
	Method update()
	End Method
	'----------------------------------------
	Function drawAll( clear% = False )
		If clear Then Cls
		For item:TGameObject = EachIn list
			item.draw() 
		Next
		Flip
	End Function
	'----------------------------------------
	Function updateAll() 
		For item:TGameObject = EachIn list
			item.update() 
		Next
	End Function
End Type

Type TStar Extends TGameObject
	'----------------------------------------
	Method Create:TStar() 
		x = Rand( 0, GraphicsWidth() )
		y = Rand( 0, GraphicsHeight() ) 
		r = Rand(128 , 255)
		g = Rand(128 , 255) 
		b = Rand(128 , 255)
		ListAddLast( list,Self )
	End Method
	'----------------------------------------
	Method draw() 
		SetColor( r,g,b )
		Plot( x,y )
	End Method
	'----------------------------------------
	Method update()
	End Method
	'----------------------------------------
	Function generate()
		For star% = 1 To 1000
			New TStar.Create() 
		Next
	End Function
End Type


Draw a slightly larger alpha and light blended version over the top of the star or do the same with a 'twinkle' image.

I spin my stars and scale them up and down slightly. Looks nice.

Stars look different the closer the are. A random size might help but try this kind of effect if you can:

(the fact it's about a neutron star isn't significant, it was just the first google image result that fitted my point :P)

And no ' in ideas.

I would probably just animate the color that you set before drawing each star, perhaps from a precomputed array of slightly randomized colors.

@tonyg: Do you have a sample?

@sswift: Scaling them randomly makes them twinkle nicely.

@Czar Flavius: I wish I could figure out how to produce that!

@ImaginaryHuman: Strangely that didn't make the stars flicker much!

I've coded in a random speed for each star relative to the players ship to give a perception of depth, and incorporated sswift's scaling idea for the twinkle.

Improvements anyone?

'# v2.00
Graphics 640 , 480

Tstar.generate()

While Not AppTerminate() And Not KeyHit(KEY_ESCAPE)
	TGameObject.updateall() 
	TGameObject.drawall( True )
Wend

Type TGameObject
	Global list:TList = CreateList()
	Field x# , y#
	Field r% , g% , b%
	Field scale#
	Field speed#
	'----------------------------------------
	Method draw() 
	End Method
	'----------------------------------------
	Method update()
	End Method
	'----------------------------------------
	Function drawAll( clear% = False )
		If clear Then Cls
		For item:TGameObject = EachIn list
			item.draw() 
		Next
		Flip
	End Function
	'----------------------------------------
	Function updateAll() 
		For item:TGameObject = EachIn list
			item.update() 
		Next
	End Function
End Type

Type TStar Extends TGameObject
	'----------------------------------------
	Method Create:TStar() 
		x = Rand( 0, GraphicsWidth() )
		y = Rand( 0, GraphicsHeight() ) 
		r = Rand(128 , 255)
		g = Rand(128 , 255) 
		b = Rand(128 , 255)
		speed = RndFloat()
		ListAddLast( list,Self )
	End Method
	'----------------------------------------
	Method draw() 
		SetColor( r , g , b ) 
		SetScale( scale, scale )
		DrawOval( x-1,y-1,3,3 )
	End Method
	'----------------------------------------
	Method update()
		'# Movement
		y:+ speed
		If y>GraphicsHeight() Then y=0
		'# Twinkle
		scale = 0.2 + RndFloat()
	End Method
	'----------------------------------------
	Function generate()
		For star% = 1 To 100
			New TStar.Create() 
		Next
	End Function
End Type


It's really the same suggestion as SSwift :
Graphics 640,480
SeedRnd MilliSecs()
Tstar.generate()

While Not AppTerminate() And Not KeyHit(KEY_ESCAPE)
	TGameObject.updateall() 
	TGameObject.drawall( True )
Wend

Type TGameObject
	Global list:TList = CreateList()
	Field x#,y#
	Field r% , g% , b%, size%
	'----------------------------------------
	Method draw() 
	End Method
	'----------------------------------------
	Method update()
	End Method
	'----------------------------------------
	Function drawAll( clear% = False )
		If clear Then Cls
		For item:TGameObject = EachIn list
			item.draw() 
		Next
		Flip
	End Function
	'----------------------------------------
	Function updateAll() 
		For item:TGameObject = EachIn list
			item.update() 
		Next
	End Function
End Type

Type TStar Extends TGameObject
	'----------------------------------------
	Method Create:TStar() 
		x = Rand( 0, GraphicsWidth() )
		y = Rand( 0, GraphicsHeight() ) 
		r = Rand(128 , 255)
		g = Rand(128 , 255) 
		b = Rand(128 , 255)
		size = Rand(2,8)
		ListAddLast( list,Self )
	End Method
	'----------------------------------------
	Method draw() 
		SetHandle size/2,size/2
		SetScale 1.0,1.0
		SetBlend maskblend
		SetAlpha 1.0
		SetColor( r,g,b )
		DrawOval x,y,size,size
		myrnd:Float=Rnd(0.8,1.4)
		SetScale myrnd,myrnd
		SetBlend lightblend
		SetAlpha rnd(0.2,0.8)
		DrawOval x,y,size,size
	End Method
	'----------------------------------------
	Method update()
	End Method
	'----------------------------------------
	Function generate()
		For star% = 1 To 1000
			New TStar.Create() 
		Next
	End Function
End Type


Thought it would look better than that though.

@tonyg: Modified your version slightly.


Graphics 640,480
SeedRnd MilliSecs()
Tstar.generate()

While Not AppTerminate() And Not KeyHit(KEY_ESCAPE)
	TGameObject.updateall() 
	TGameObject.drawall( True )
Wend

Type TGameObject
	Global list:TList = CreateList()
	Field x#,y#
	Field r% , g% , b%, size%
	'----------------------------------------
	Method draw() 
	End Method
	'----------------------------------------
	Method update()
	End Method
	'----------------------------------------
	Function drawAll( clear% = False )
		If clear Then Cls
		For item:TGameObject = EachIn list
			item.draw() 
		Next
		Flip
	End Function
	'----------------------------------------
	Function updateAll() 
		For item:TGameObject = EachIn list
			item.update() 
		Next
	End Function
End Type

Type TStar Extends TGameObject
	'----------------------------------------
	Method Create:TStar() 
		x = Rand( 0, GraphicsWidth() )
		y = Rand( 0, GraphicsHeight() ) 
		r = Rand(128 , 255)
		g = Rand(128 , 255) 
		b = Rand(128 , 255)
		size = Rand(1,4)
		ListAddLast( list,Self )
	End Method
	'----------------------------------------
	Method draw() 
		SetHandle size/2,size/2
		SetScale 1.0,1.0
		SetBlend maskblend
		SetAlpha 1.0
		SetColor( r,g,b )
		DrawOval x,y,size,size

		SetHandle (size+2)/2,(size+2)/2
		myrnd:Float=Rnd(0.8,1.4)
		SetScale myrnd,myrnd
		SetBlend lightblend
		SetAlpha Rnd(0.2,0.8)
		DrawOval x,y,size+2,size+2
	End Method
	'----------------------------------------
	Method update()
	End Method
	'----------------------------------------
	Function generate()
		For star% = 1 To 1000
			New TStar.Create() 
		Next
	End Function
End Type