use the slider to see the effect...
any further speed up hints are welcome
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