UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on September 12, 2024, 09:01:09 AM

Title: Star Tunnel - Unrolling code for better execution.. How Demo Effects Work
Post by: kevin on September 12, 2024, 09:01:09 AM
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)

[pbcode]

    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()

[/pbcode]

  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 ) (https://www.youtube.com/watch?v=W43Hs8JmKPY&ab_channel=PlayBasic)
  Watch Interference Amiga Demo by Sanity ( 1080HD ) (https://www.youtube.com/watch?v=IXJLC5Hlvhw&ab_channel=amigaland)


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.