Star Travel with - speed-up-Tweak -

Post Reply
Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Star Travel with - speed-up-Tweak -

Post by Operator »

Star Travel Animation with speed up "tweak",
use the slider to see the effect...
any further speed up hints are welcome :D

Code: Select all


REM Star-Travel - with "speed-up-tweak"
REM Stars with perspective projection
REM scaled graphics in sprite "tweak" used 
REM to gain speed against resolution/details
REM sB 5.6 / iPhone4 / iOS 6.1 / by Operator
REM 
REM - use the slider to test speed/detail
REM - change the speed for fine tune
REM - change max. stars for fine tune
REM 
REM this "tweak" is useful to speed up sB,
REM specially for older devices (iPhone4)
REM if resolution/details does not matter...
REM speed-up-factor max. ~ 3
REM 
REM basically we draw all the graphics in a
REM scaled (smaller) window and then we use
REM a sprite to show the window content
REM scaled back (bigger) again, loosing
REM details but with speed gain

GRAPHICS
GRAPHICS CLEAR 0,0,0
OPTION BASE 1
OPTION ANGLE DEGREES
OPTION SPRITE POS CENTRAL

'max. stars, star speed, color of stars
max_stars = 150   'change to fine tune
speed = 160       'change to fine tune
DRAW COLOR 1,1,1

'helpers
scr_h = SCREEN_HEIGHT()
scr_w = SCREEN_WIDTH()
scr_h2 = scr_h/2
scr_w2 = scr_w/2

'slider
sl_x = scr_w*0.9
sl_y = scr_h*0.9
sl_v = 0.5
sl_s = scr_h*0.3
sl_a = -90
SLIDER "detail" VALUE sl_v AT sl_x,sl_y SIZE sl_s ANGLE sl_a
SLIDER "detail" SHOW

init:
'window to display screen content
'window scale will determine speed/resolution
'1   -> highest res - lowest speed
'0.1 -> lowest  res - highest speed

window_scl = sl_v
window_w = scr_w*window_scl
window_h = scr_h*window_scl
window_w2 = window_w/2
window_h2 = window_h/2
window_cx = window_w/2
window_cy = window_h/2

'sprite used for speed up if window_scl
'is set below 1
SPRITE "stars" BEGIN window_w,window_h
SPRITE "stars" END
SPRITE "stars" SHOW
sprite_scl = 1/window_scl 'sprite scale

'position the stars out of view axis
fac = 4
free_spot_x = fac * scr_w*window_scl
free_spot_y = fac * scr_h*window_scl
focus = 100          

x = 1 ! y = 2 ! z = 3
DIM stars(max_stars,3) '3-> x,y,z

'preload stars array with initial star 'positions x,y,z avoiding stars near the 'focal axis (screen hit avoidance)
FOR i = 1 TO max_stars
  alfa = RND(360)
  stars(i,x) = (free_spot_x + RND(10*window_w)*window_scl)*COS(alfa)
  stars(i,y) = (free_spot_y + RND(10*window_h)*window_scl)*SIN(alfa)
  stars(i,z) = 3000 + RND(4000)
NEXT i

'field used to display fps/lps and detail
FIELD "info" TEXT "" AT 0,0 SIZE scr_w,20 RO
FIELD "info" BACK ALPHA 0
FIELD "info" FONT COLOR 1,1,1


REFRESH OFF
'window clear color black with low alpha
'for fade out effect...
FILL COLOR 0,0,0
FILL ALPHA 0.1

'reset timer and loop counter
TIMER RESET
loop_count = 0


LOOP:
loop_count += 1

'if slider is changes restart the animation
IF SLIDER_CHANGED("detail") THEN
  sl_v = MAX(0.1,SLIDER_VALUE("detail"))
  sl_v = INT(10*sl_v)/10
  SLIDER "detail" VALUE sl_v
  GOTO init
END IF

'sprite for speed-up-tweak
SPRITE "stars" BEGIN

  FOR i = 1 TO max_stars
    'move stars
    stars(i,z) -= speed
    
    'reposition star if they reach z=0
    IF stars(i,z) <= 0 THEN
      alfa = RND(360)
      stars(i,z) = 3000 + RND(4000)
      stars(i,x) = (free_spot_x + RND(10*window_w)*window_scl)*COS(alfa)
      stars(i,y) = (free_spot_y + RND(10*window_h)*window_scl)*SIN(alfa)
    END IF
  
    'apply perspective projection
    x_p0 = -(stars(i,x)*focus/(stars(i,z) + focus)) + window_cx
    y_p0 = -(stars(i,y)*focus/(stars(i,z) + focus)) + window_cy
    
    'create a second point in a distance of speed 
    x_p1 = -(stars(i,x)*focus/(stars(i,z)+speed + focus)) + window_cx
    y_p1 = -(stars(i,y)*focus/(stars(i,z)+speed  +focus)) + window_cy

    'draw stars
    DRAW SIZE 0.5 + 10/stars(i,z)
    DRAW LINE x_p0,y_p0 TO x_p1,y_p1
  NEXT i
  
  'clear window area, fill color black and
  'alpha set to 0.1...
  FILL RECT window_cx,window_cy SIZE window_w2,window_h2
  
  SPRITE "stars" END
  SPRITE "stars" AT scr_w2,scr_h2 SCALE sprite_scl
  
  'update the field content
  fps = INT(10*loop_count/TIME())/10
  FIELD "info" TEXT "FPS: "&fps&"  Detail: "&window_scl
  
GOTO LOOP
Attachments
image.jpg
image.jpg (139.61 KiB) Viewed 2736 times
image.jpg
image.jpg (131.42 KiB) Viewed 2736 times
image.jpg
image.jpg (106.95 KiB) Viewed 2736 times
Last edited by Operator on Wed May 18, 2016 2:02 pm, edited 1 time in total.

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Star Travel with - speed-up-Tweak -

Post by rbytes »

Very nice effect.

Regarding speed ups, I read once that it is always preferable to use variables rather than numeric values where speed is an issue. I haven't tested this, but I generally code this way.

As an example, your code:

'preload stars array with initial star 'positions x,y,z avoiding stars near the 'focal axis (screen hit avoidance)
FOR i = 1 TO max_stars
alfa = RND(360)
stars(i,x) = (free_spot_x + RND(10*window_w)*window_scl)*COS(alfa)
stars(i,y) = (free_spot_y + RND(10*window_h)*window_scl)*SIN(alfa)
stars(i,z) = 3000 + RND(4000)
NEXT i

might be written:

'preload stars array with initial star 'positions x,y,z avoiding stars near the 'focal axis (screen hit avoidance)
circ=360 ! mult=10 ! scnt=3000 ! rcnt=4000
FOR i = 1 TO max_stars
alfa = RND(circ)
stars(i,x) = (free_spot_x + RND(mult*window_w)*window_scl)*COS(alfa)
stars(i,y) = (free_spot_y + RND(mult*window_h)*window_scl)*SIN(alfa)
stars(i,z) = scnt + RND(rcnt)
NEXT i

It would be interesting to go through your loops and do this kind of conversion, and then compare your execution speed.
Maybe Mr. K could comment on whether lines coded entirely with variables are executed faster in sB.
The only thing that gets me down is gravity...

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: Star Travel with - speed-up-Tweak -

Post by Operator »

@Ricardobytes:

Thanks for commenting.

I tried sin/cos look-up-table but had
almost no speed gain... will try to pre calculate
as possible and then compare...
The main speed boost is the "tweak"..,

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: Star Travel with - speed-up-Tweak -

Post by Mr. Kibernetik »

Of course you can perform speed tests to find out what is faster.
Regarding numbers/variables usage, there is no noticeable difference in speed whatever was used.

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: Star Travel with - speed-up-Tweak -

Post by Operator »

Reduced a bit the calcs and added a sin/cos
look-up table, but there was NO noticable
speed improvement (fps).
Interestingly if I increase the stars amount
to 4000 then the speed-up gain with the
"tweak" goes down to 0 (no gain)... :?

Code: Select all


REM Star-Travel - with "speed-up-tweak"
REM Stars with perspective projection
REM scaled graphics in sprite "tweak" used 
REM to gain speed against resolution/details
REM sB 5.6 / iPhone4 / iOS 6.1 / by Operator
REM 
REM - use the slider to test speed/detail
REM - change the speed for fine tune
REM - change max. stars for fine tune
REM 
REM this "tweak" is useful to speed up sB,
REM specially for older devices (iPhone4)
REM if resolution/details does not matter...
REM speed-up-factor max. ~ 3
REM 
REM basically we draw all the graphics in a
REM scaled (smaller) window and then we use
REM a sprite to show the window content
REM scaled back (bigger) again, loosing
REM details but with speed gain
REM 
REM Update_1:
REM Now with cos/sin look-up table and
REM calcs inside main loop reduced:
REM --> no speed gain as per (fps or lps)
REM frames or loops per second
REM At about 4000 stars the speed up gain 
REM with the "tweak" is ~0 : no gain :/

GRAPHICS
GRAPHICS CLEAR 0,0,0
OPTION BASE 1
OPTION ANGLE DEGREES
OPTION SPRITE POS CENTRAL

'max. stars, star speed, color of stars
max_stars = 150   'change to fine tune
speed = 160       'change to fine tune
DRAW COLOR 1,1,1

'helpers
scr_h = SCREEN_HEIGHT()
scr_w = SCREEN_WIDTH()
scr_h2 = scr_h/2
scr_w2 = scr_w/2

'slider
sl_x = scr_w*0.9
sl_y = scr_h*0.9
sl_v = 0.5
sl_s = scr_h*0.3
sl_a = -90
SLIDER "detail" VALUE sl_v AT sl_x,sl_y SIZE sl_s ANGLE sl_a
SLIDER "detail" SHOW

init:
'window to display screen content
'window scale will determine speed/resolution
'1   -> highest res - lowest speed
'0.1 -> lowest  res - highest speed

window_scl = sl_v
window_w = scr_w*window_scl
window_h = scr_h*window_scl
window_w10 = 10*window_w
window_h10 = 10*window_h
window_w2 = window_w/2
window_h2 = window_h/2
window_cx = window_w/2
window_cy = window_h/2

'sprite used for speed up if window_scl
'is set below 1
SPRITE "stars" BEGIN window_w,window_h
SPRITE "stars" END
SPRITE "stars" SHOW
sprite_scl = 1/window_scl 'sprite scale

'position the stars out of view axis
fac = 4
free_spot_x = fac * scr_w*window_scl
free_spot_y = fac * scr_h*window_scl
focus = 100          

x = 1 ! y = 2 ! z = 3
DIM stars(max_stars,3) '3-> x,y,z

'preload stars array with initial star 'positions x,y,z avoiding stars near the 'focal axis (screen hit avoidance)
FOR i = 1 TO max_stars
  alfa = RND(360)
  stars(i,x) = (free_spot_x + RND(window_w10))*COS(alfa)
  stars(i,y) = (free_spot_y + RND(window_h10))*SIN(alfa)
  stars(i,z) = 3000 + RND(4000)
NEXT i

'make a cos/sin look-up table
DIM si(360) ! DIM co(360)
FOR i = 1 TO 360
  si(i) = SIN(i)
  co(i) = COS(i)
NEXT i

'field used to display fps/lps and detail
FIELD "info" TEXT "" AT 0,0 SIZE scr_w,20 RO
FIELD "info" BACK ALPHA 0
FIELD "info" FONT COLOR 1,1,1


REFRESH OFF
'window clear color black with low alpha
'for fade out effect...
FILL COLOR 0,0,0
FILL ALPHA 0.1

'reset timer and loop counter
TIMER RESET
loop_count = 0


LOOP:
loop_count += 1

'if slider is changes restart the animation
IF SLIDER_CHANGED("detail") THEN
  sl_v = MAX(0.1,SLIDER_VALUE("detail"))
  sl_v = INT(10*sl_v)/10
  SLIDER "detail" VALUE sl_v
  GOTO init
END IF

'sprite for speed-up-tweak
SPRITE "stars" BEGIN

FOR i = 1 TO max_stars
  'move stars
  stars(i,z) -= speed
    
  'reposition star if they reach z=0
  IF stars(i,z) <= 0 THEN
    alfa = RND(360)+1
    stars(i,z) = 3000 + RND(4000)
    stars(i,x) = (free_spot_x + RND(window_w10))*co(alfa)
    stars(i,y) = (free_spot_y + RND(window_h10))*si(alfa)
  END IF
  
  'apply perspective projection
  x_p0 = -(stars(i,x)*focus/(stars(i,z) + focus)) + window_cx
  y_p0 = -(stars(i,y)*focus/(stars(i,z) + focus)) + window_cy
    
  'create a second point in a distance of speed 
  x_p1 = -(stars(i,x)*focus/(stars(i,z)+speed + focus)) + window_cx
  y_p1 = -(stars(i,y)*focus/(stars(i,z)+speed + focus)) + window_cy

  'draw stars
  DRAW SIZE 0.5 + 10/stars(i,z)
  DRAW LINE x_p0,y_p0 TO x_p1,y_p1
NEXT i
  
'clear window area, fill color black and
'alpha set to 0.1...
FILL RECT window_cx,window_cy SIZE window_w2,window_h2
  
SPRITE "stars" END
SPRITE "stars" AT scr_w2,scr_h2 SCALE sprite_scl
  
'update the field content
fps = INT(10*loop_count/TIME())/10
FIELD "info" TEXT "FPS: "&fps&"  Detail: "&window_scl
  
GOTO LOOP

Post Reply