Main Menu

100K Unrolled Stars

Started by kevin, September 03, 2024, 08:37:32 AM

Previous topic - Next topic

kevin

This demo showcases a 2D star field, drawn by plotting 100,000 dots using an unrolled loop method. The unique aspect here is that the pixel rendering routine is essentially a sequence of hard-coded dot plots, optimized to draw 5 dots per loop iteration. This method can be easily adjusted to change the number of dots drawn per loop.

In terms of performance, the speed gains become more noticeable as the number of dots increases, making it run more efficiently, especially on slower machines. This technique can also be applied to manage objects in games, providing a useful boost in performance.

I hope someone finds this approach helpful, even if it might seem a bit niche!




PlayBASIC Code: [Select]
`  This demo draws a star playfield in 2d.  The only interesting thing about
` it, is that it's done via an unrolled method for drawing the dots. Meaning
` that the pixel render routine, is really just a list of dot plots. It's
` hard code at the moment to render 5 dots per loop, but it's easy to change
`
` Speed wise, the boost will vary, the more dots the faster it runs,
` but it also should run better on slower the machines.. You can also use
` this style of control system to manage objects in games also. Every little
` bit helps...
`
` Anyway, I hope somebody out there finds this useful.. (prolly not :)
`
` L8r,
` Kevin Picone
`
` *=---------------------------------------------------------------------=*


ScreenWidth = 1200
ScreenHeight = 600
screenDepth = 32
openscreen screenWidth,screenheight,screenDepth,1


stars=100000
gosub _Init_2D_Stars


white=rgb(255,255,255)
do

cls


Start_time=timer()

lockbuffer
gosub _DisplayAndControl_2D_Stars
unlockbuffer

End_time=timer()
starTimer=End_time-start_time


` Display Frame Stat's
ink white

s$="Fps:"+digits$(fps(),3)
s$+=" Dots:"+str$(star*groups)
s$+=" Ticks: ["+digits$(StarTimer,3)+"]"
text 0,0,s$

sync
loop spacekey()
end






` *=-------------------------------------------------------------------=*
` Init 2d Star Arrays and Constants
` *=-------------------------------------------------------------------=*


_Init_2D_Stars:

`number control planes (each plane has the same colour & speed setting)

planes=200

` PREcalc inner loop len
Groups =5
Stars2 =stars/planes
StarSegs =stars2/groups
Stars3 =(stars/groups)+1

dim StarX#(stars3 ,groups)
dim StarY(stars3 ,groups)
dim StarXBases#(planes,Starsegs)
dim StarXSpeeds#(planes)
dim StarPlaneColour(planes)

` Pre- init Star positions
star=0
Star_XSegWidth#=float(screenwidth)/(starsegs-1)

colstep#=256.0/planes
for p=1 to planes-1

z# =colstep#*p
speed# =z#*0.025
StarXSpeeds#(p)=speed#

StarPlaneColour(p)=rgb(z#,z#,z#)

XposOffset =Star_XsegWidth#*-1

for s=0 to starsegs-1
starxbases#(p,s)=Xposoffset
xposOffset+=Star_XSegWidth#

for g=1 to groups
starx#(star,g) =rnd#(Star_XSegWidth#*1.5)
stary(star,g) =rnd(screenheight)
next g

inc star

next s
next p

return





` *=-------------------------------------------------------------------=*
` Control / Render 2D Stars - Unrolled Group Controller
` *=-------------------------------------------------------------------=*


_DisplayAndControl_2D_Stars:


`Render Stars

dots=0
star=0

for P=0 to planes-1

ink StarPlaneColour(p)

xSpeed#=StarXSpeeds#(p)

for S=0 to starsegs-1

xbase#=StarXbases#(p,s)+xspeed#

if xbase#>(screenwidth+Star_XSegWidth#)
xbase# = Star_XSegWidth#*-1
for g=1 to groups
starx#(star,g)=rnd#(Star_XSegWidth#*1.5)
stary(star,g)=rnd(screenheight)
Login required to view complete source code



  Note: This is a PlayBASIC port of DARK BASIC (DBPRO) version of this code that I wrote back in 2001



kevin

20000 Unrolled Stars - How Demo Effects Actually Work

  Attached is an example of 20000 star field that is created much like classic computer demo effects are created and this is unrolling the code.  Most effects are more about how the unrolled code is created than the final onscreen effect appears.. 


  Here's the creation code for the dot plot routines...  :) 

  This program output a string of dot plots for the number of stars required within the required screen size.  This code is then pasted into the attached demo bellow.  In demo effects we'd be using machine code directly, but here we're just using high level PlayBASIC code. 

PlayBASIC Code: [Select]
    ScreenWidth    =1200
ScreenHeight=600
Dots = 20000

cr$=chr$(13)+chr$(10)
for lp=1 to Dots
X=Rnd( ScreenWidth)
Y=Rnd( ScreenHeight)
s$+= " Dot BaseX+"+str$(x)+","+str$(y)+cr$
next
#print s$

Sync
waitkey







Download

    Attached bellow.