Star Tunnel - Unrolling code for better execution.. How Demo Effects Work

Started by kevin, September 12, 2024, 09:01:09 AM

Previous topic - Next topic

kevin

Star Tunnel - Unrolling code for better execution..  How Demo Effects Work

  The original version of this PlayBASIC star tunnel demo calculates the position of each star (dot) in real-time, simulating the effect of stars zooming toward the viewer. For each frame, the program computes the angle and radius of each star across multiple rings, then uses trigonometry to plot their positions. While this approach achieves the desired effect, it recalculates every position at runtime, resulting in slower performance—about half the speed of more optimized methods.

BASIC EXAMPLE (Cut'n'Paste Me)

PlayBASIC Code: [Select]
    Stars_Per_Ring = 100
Ring_Size# = 200
ProjectionX#= 400
basex#=getscreenwidth()/2
basey#=getscreenheight()/2


do
cls 0


print "Fps:"+str$(fps())

offset=mod(offset-1,20)
lockbuffer
For Z=100 to 5000 step 20

for lp=0 to Stars_Per_Ring-1
Angle#=lp*(360.0/Stars_Per_Ring)
Radius#=(Ring_Size#*ProjectionX#)/(Z+Offset)
X#=basex#+cos(Angle#)*RAdius#
Y#=basey#+sin(Angle#)*RAdius#
dot x#,y#
next

next
unlockbuffer


Sync
loop spacekey()




  To improve performance, the second version dynamically generates a new PlayBASIC program that unrolls these calculations. This version pre-computes the positions of all stars, allowing the demo to simply plot them without recalculating during each frame. This results in a significant speed boost, but at the cost of program size. The unrolled version typically generates around 500KB of code, compared to less than 1KB for the slower version. This trade-off between speed and size highlights the balance between runtime efficiency and resource usage in optimizing real-time effects.





Links:

  Unrolled Star Tunnel How Demo Effects work - PlayBASIC Source Code (2024-09-13 )
  Watch Interference Amiga Demo by Sanity ( 1080HD )


KeyWords: 
  Code Unrolling, Dynamic Programing, Amiga Atari ST, C64, Amstrad, 8bit, 16bit Machines


Full Source Code Attached Bellow

      Full Source Code Attached bellow..  Added version 2 which includes a 3rd version that draw's each star as a highlighted 2 by 2 grid of pixels.  These are alpha blended to the output buffer..  Fr a cleaning / better looking effect.