StarScrolling

BlitzMax Forums/BlitzMax Programming/StarScrolling

MacSven(Posted 2005) [#1]
Here is a little StarScrolling demo

Graphics 800,600,32,60

Type star1
Field x,y
End Type

Global star1_list:TList=New TList

For Local i=1 To 200

Local st1:star1=New star1
star1_list.AddLast st1
st1.x=Rnd(0,800)
st1.y=Rnd(0,600)

Next

Type star2
Field x,y
End Type

Global star2_list:TList=New TList

For Local j=1 To 150

Local st2:star2=New star2
star2_list.AddLast st2
st2.x=Rnd(0,800)
st2.y=Rnd(0,600)

Next

Type star3
Field x,y
End Type

Global star3_list:TList=New TList

For Local k=1 To 100

Local st3:star3=New star3
star3_list.AddLast st3
st3.x=Rnd(0,800)
st3.y=Rnd(0,600)

Next

Local winkel
Local beam

While Not KeyHit(KEY_ESCAPE) 'Escape

Cls

For st1:star1=EachIn star1_list
SetColor 155,155,155
Plot st1.x,st1.y
st1.x:-1
If st1.x<0 Then
st1.x=800
EndIf
Next
For st2:star2=EachIn star2_list
SetColor 175,175,175
Plot st2.x,st2.y
st2.x:-2
If st2.x<0 Then
st2.x=800
EndIf
Next
For st3:star3=EachIn star3_list
SetColor 255,255,255
Plot st3.x,st3.y
st3.x:-3
If st3.x<0 Then
st3.x=800
EndIf
Next

Flip
   
Wend


tesuji(Posted 2005) [#2]
Yay. Scrolling stars.

Here's a OO version I adapted from some B3D code of mine. It also lets you change scroll direction.



/me wonders if plot is as fast as writepixelfast ? Seems to be fast enough anyway.


MacSven(Posted 2005) [#3]
Ohh year this is cool?


DannyD(Posted 2005) [#4]
Nice ,thanks for sharing.


ImaginaryHuman(Posted 2005) [#5]
Cool. Reminds me of the earlier days of the Amiga demo scene, scrolling starfields were all the rage and very often had 3 or so layers of parallax like in this demo.


ImaginaryHuman(Posted 2005) [#6]
I have no idea if plot is as fast as writepixelfast, since I don't know about blitz3d or blitzplus ... but plot isnt' as fast as it could be. The Plot command is just a higher-level wrapper for a peice of OpenGL or DirectX code.

e.g.

glBegin(GL_POINTS)
glVertex2i(100,100)
glEnd(GL_POINTS)

Would plot a single point in OpenGL. You could put more vertexes in between the begin and end and it'd be more efficient than having a set of those for every point. You also get rid of any overhead from jumping to the routine as you call the `Plot` function.


Sveinung(Posted 2005) [#7]
Here is one more starfield routin.

'Star Demo

Graphics 800,600,0

strnr:Int = 1500 'amount of stars
spinn:Int = 20 'amount of spinn. Turn off = 0

Local rad[strnr]
Local angle[strnr]
Local spd[strnr]

For i = 0 To strnr
	rad[i]=Rnd(280) + 20
	angle[i]=Rnd(65535)
	spd[i]=Rnd(5)+1
Next

Repeat
Cls 
For i=0 To strnr
	xp=400+(Sin((2*angle[i])*(Pi/360))*rad[i])
	yp=300+(Cos((2*angle[i])*(Pi/360))*rad[i])
	
	If xp < 0 Or xp > 800 Or yp < 0 Or yp > 600
		rad[i]=Rnd(40) + 20
		angle[i]=Rnd(65535)
		spd[i]=Rnd(5)+1
	EndIf
	
	rad[i] = rad[i] + spd[i]
	angle[i] = angle[i] + spinn
	
	If spd[i]=1 Then SetColor(100,100,100)
	If spd[i]=2 Then SetColor(130,130,130)
	If spd[i]=3 Then SetColor(170,170,170)
	If spd[i]=4 Then SetColor(210,210,210)
	If spd[i]=5 Then SetColor(255,255,255)
	Plot(xp,yp)
Next

Flip()
FlushMem()
Until KeyHit(KEY_ESCAPE)
End


Sveinung


Booticus(Posted 2005) [#8]
Right on guys! Thanks for all the good code! Nice to have in my "code nuggets" folder of goodies. :)


tesuji(Posted 2005) [#9]
Nice.

There can never be enough starfield routines imo ;-), so here's another B3D conversion...




SillyPutty(Posted 2005) [#10]
very nice guys, awsome stuff


SoggyP(Posted 2005) [#11]
Hello.

What about:


Not tested but it should whup everyone else's routines I reckon.

Goodbye.


MacSven(Posted 2005) [#12]
Looks like C64 Basic Style......


SoggyP(Posted 2005) [#13]
Hello.

I don't know what you could possibly mean!!! That's class, man!

[quote]There can never be enough starfield routines imo ;-)[\quote]

Ok, perhaps there can ;oD

Goodbye.


DannyD(Posted 2005) [#14]
Thanks all.

Sveinung, your code doesn't run, out of bounds array error.


Sveinung(Posted 2005) [#15]
@DannyD
OOOPS...Sorry about that. Had no debugger on...
Should be like this
'Star Demo

Graphics 800,600,0

strnr:Int = 1500 'amount of stars
spinn:Int = 20 'amount of spinn. Turn off = 0

Local rad[strnr]
Local angle[strnr]
Local spd[strnr]

For i = 0 To strnr-1
	rad[i]=Rnd(280) + 20
	angle[i]=Rnd(65535)
	spd[i]=Rnd(5)+1
Next

Repeat
Cls 
For i=0 To strnr-1
	xp=400+(Sin((2*angle[i])*(Pi/360))*rad[i])
	yp=300+(Cos((2*angle[i])*(Pi/360))*rad[i])
	
	If xp < 0 Or xp > 800 Or yp < 0 Or yp > 600
		rad[i]=Rnd(40) + 20
		angle[i]=Rnd(65535)
		spd[i]=Rnd(5)+1
	EndIf
	
	rad[i] = rad[i] + spd[i]
	angle[i] = angle[i] + spinn
	
	If spd[i]=1 Then SetColor(100,100,100)
	If spd[i]=2 Then SetColor(130,130,130)
	If spd[i]=3 Then SetColor(170,170,170)
	If spd[i]=4 Then SetColor(210,210,210)
	If spd[i]=5 Then SetColor(255,255,255)
	Plot(xp,yp)
Next

Flip()
FlushMem()
Until KeyHit(KEY_ESCAPE)
End


Sveinung


Booticus(Posted 2005) [#16]
Dude they all rule! Thanks guys!


Filax(Posted 2005) [#17]
Nice !