UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on April 02, 2016, 11:20:57 PM

Title: Electrix Stars
Post by: kevin on April 02, 2016, 11:20:57 PM
    Electrix 2D Stars

        Here's a quick mock up of an effect found in the winning Amiga demo from revision 2016.    The effect moves a bunch of stars, where  each star has a glow / luminance value computed from the nearest star which are then linked via alpha additive lines..  

         This version contains an ugly nested (squared) loop to find near by stars.. you can get rid of most of it by shuffling the array, which is a lot less expensive...    

 
   Video




   Source Code

[pbcode]

;*=-----------------------------------------------------------------------------=*   
;
;                                >> Exlectric Stars <<
;
;                                  By Kevin Picone
;
;                            Built Using PlayBasic V1.65 beta2
;
;                  Copyright 2016 by Kevin Picone All Rights Reserved.
;
;*=-----------------------------------------------------------------------------=*   
;                     www.UnderwareDesign.com  -  www.PLayBASIC.com
;*=-----------------------------------------------------------------------------=*   

   ; Include the Blit Image functions
     #Include "BlitImage"


   ; Compute a 16 by 9 screen size.
   ScreenWidth   =960
   ScreenHeight=ScreenWidth*(9.0/16)
   
   if ScreenWidth<>GetSCreenWidth()
      if ScreenHeight<>GetSCreenHeight()
         OPenScreen ScreenWidth,ScreenHeight,32,1   
      endif
   endif


   ; Define the basic star structure
   TYPE tStars
            X#,Y#
            Speed#
            Direction#
   EndTYPE
   
   Size=250
      
   Dim Stars(Size) as tStars

   ; fill the array with starting data
   For lp =0 to size
         Stars(lp)             = new tStars
         Stars(lp).x            =rndrange(-100,ScreenWidth+100)
         Stars(lp).y            =rndrange(-100,ScreenHeight+100)
         Stars(lp).speed      =rndrange#(2,7)
         Stars(lp).Direction   =90
   next

   ; create the screen sized FX buffer
   Screen=NewImage(ScreenWidth,ScreenHeight,2)


   ; ---------------------------------------------------------------
   Do
   ; ---------------------------------------------------------------

         ; draw to the FX screen
         rendertoimage screen
   
         ; Ink Mode to Alpha Addition
         inkmode 1+64

         ; loop through the stars
         For lp=0 to Size

               ; get th stars position
               x#      =Stars(lp).x
               y#      =Stars(lp).y
               angle#=Stars(lp).Direction
               Speed#=Stars(lp).Speed
               
               ; move the star
               x#+=sinradius(Angle#,speed#)
               y#+=cosradius(Angle#,speed#)
               if x#>(SCreenWidth+100)
                  ; wrap star to screen
                  x#-=(ScreenWidth+200)                           
                  y#=rndrange(-100,ScreenHeight+100)
                  Stars(lp).speed=rndrange#(2,7)
               endif
               
               ; set it's position
               Stars(lp).x=x#
               Stars(lp).y=y#

               ; ugly nested loop to find near by stars
               Glow#=10
               For SparkLp=lp+1 to size

                        sx#=Stars(Sparklp).x
                        sy#=Stars(Sparklp).y

                        d#=getdistance2d(x#,y#,sx#,sy#)

                        if d#<120.0
                              Glow#+=d#*(4/120.0)
                              linec x#,y#,sx#,sy#,$102030
                        endif
               next

               ; draw the glow at this point
               for GlowLP=0 to Glow#*1.2 step 4
                     circlec x#,y#,GlowLP,true,$020304         
               next
         next


         ; draw to the screen
         rendertoscreen
         ; restore nomral inkmode
         inkmode 1
         
         ; render screen image to the screen, and clear the
         ; screen image for next update
         c=64
         BlitImageAlphaPostMultColour(Screen,0,0,RGB(c,c,c))
                
         ; show the screen to the user                
         sync
   loop

[/pbcode]

  Note: I'm using todays build of PlayBASIC V1.6. but this doesn't need that to function. 



   Related Examples:

       2d Stars Example (https://www.underwaredesign.com/forums/index.php?topic=4742.0)
Title: Re: Electrix Stars
Post by: ATLUS on April 03, 2016, 05:51:39 AM
It looks very nice :)