stars in cube demo

Post Reply
DrChip
Posts: 167
Joined: Wed Oct 22, 2014 3:26 pm
My devices: iPhone 4 to 6+,iPad mini to iPad air 2

stars in cube demo

Post by DrChip »

rem STAR IN CUBE DEMO
rem just having some fun....
rem a bug in my scroller. :(
rem Enjoy!

gosub initialize

'The Main Loop. These Calls Are Repeated Until (infinity)
do
refresh off
draw color 10,10,size*5

graphics clear 10/255,size*5/255,10/255
gosub rotate
gosub draw


'Display Text.. Do Scroll

fill color 70/255,50/255,70/255
fill rect 0,0 to sw,22
fill rect 0,sh-22 to sw,sh
draw color 0,0,0
draw text mid$(s$,p,67) at scx+2,sh-23
draw text "-= STAR IN CUBE DEMO =-" at 20,0
draw color 1,1,1
draw text "-= STAR IN CUBE DEMO =-" at 22,2
draw text mid$(s$,p,67) at scx,sh-22
scx=scx-2
if scx<-10 then
scx=scx+10
p=p+1
if p>len(s$) then p=0
end if
refresh on
until 1=2


draw:

'Draw The object!!!
b=1
mm=mm+.1
size=5+(3*sin(mm/10))
for a=1 to 42
z(a)=z(a)+4
if z(a)>50 then z(a)=z(a)-100
sz=((80-tz(a))/10)+(size-5)
fill color 30/255,sz*5/255,sz*10/255
fill rect tx(a),ty(a) to tx(a)+sz,ty(a)+sz
next a
draw color 155,255,255
draw line tx(polys-7),ty(polys-7) to tx(polys-6),ty(polys-6)
draw line tx(polys-6),ty(polys-6) to tx(polys-5),ty(polys-5)
draw line tx(polys-5),ty(polys-5) to tx(polys-4),ty(polys-4)
draw line tx(polys-4),ty(polys-4) to tx(polys-7),ty(polys-7)

draw line tx(polys-3),ty(polys-3) to tx(polys-2),ty(polys-2)
draw line tx(polys-2),ty(polys-2) to tx(polys-1),ty(polys-1)
draw line tx(polys-1),ty(polys-1) to tx(polys),ty(polys)
draw line tx(polys),ty(polys) to tx(polys-3),ty(polys-3)

draw line tx(polys-7),ty(polys-7) to tx(polys-3),ty(polys-3)
draw line tx(polys-6),ty(polys-6) to tx(polys-2),ty(polys-2)
draw line tx(polys-5),ty(polys-5) to tx(polys-1),ty(polys-1)
draw line tx(polys-4),ty(polys-4) to tx(polys),ty(polys)

return

rotate:

'Rotate And Scale Each Point - Store Result

for a=1 to polys
x1=x(a)
y1=y(a)
z1=z(a)

'X,Y,Z rotations!
xx=x1
yy=y1*cs(xr)+z1*sn(xr)
zz=z1*cs(xr)-y1*sn(xr)
y1=yy
x1=xx*cs(yr)-zz*sn(yr)
z1=xx*sn(yr)+zz*cs(yr)
zz=z1
xx=x1*cs(zr)-y1*sn(zr)
yy=x1*sn(zr)+y1*cs(zr)

'Apply Perspective!
xx=size*(xx/((zz/155)+1))+sw/2
yy=size*(yy/((zz/155)+1))+sh/2
tx(a)=xx
ty(a)=yy
tz(a)=zz
next a
'Rotation Incriments. Have a Play with these!

xr=xr+1 ' X rotation per frame
yr=yr+1 ' Y rotation per frame
zr=zr+2 ' Z rotation per frame

if xr>=720 then xr=xr-720
if yr>=720 then yr=yr-720
if zr>=720 then zr=zr-720
return


initialize:

'Open Gfx Screen!
graphics

'Define the necessary variables!
sw=screen_width()
sh=screen_height()
pi=3.1415
s$=" "
s$=s$&"Welcome to the -= STAR IN CUBE DEMO =- Just "
s$=s$&" some fun with graphical insanity made in "
s$=s$&"the SMART BASIC for a change.. I hope that "
s$=s$&"you enjoy it... This is a different idea for a "
s$=s$&"starfield and it is very similar to an Amiga demo "
s$=s$&"I did a long time ago :o) Not much to say this "
s$=s$&"time except that it's all real time (no cheating!)"
s$=s$&" That's all folks!! "

p=0 ' Text pointer
scx=0 ' Scroll text scroll offset variable

polys=58 ' The amount of polygons in the object
dim x(polys+1) ' Original X co-ordinate store
dim y(polys+1) ' Original Y co-ordinate store
dim z(polys+1) ' Original Z co-ordinate store
dim tx(polys+1) ' Transformed X co-ordinate store
dim ty(polys+1) ' Transformed Y co-ordinate store
dim tz(polys+1) ' Transformed Z co-ordinate store

'Define Sine Tables!!
dim cs(721)
dim sn(721)
for ang=0 to 720
cs(ang)=cos(ang*(pi/360))
sn(ang)=sin(ang*(pi/360))
next ang

'Generate star positions and Read in the object!

for a=1 to polys
x(a)=-50+rnd(100)
y(a)=-50+rnd(100)
z(a)=-50+rnd(100)
if a>50 then
read x(a),y(a),z(a)
end if
next a

'Cube: 8 Points
data -50,-50,-50,50,-50,-50,50,50,-50,-50,50,-50
data -50,-50,50,50,-50,50,50,50,50,-50,50,50
return
Attachments
image.jpg
image.jpg (167.34 KiB) Viewed 1398 times
image.jpg
image.jpg (163.49 KiB) Viewed 1398 times

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: stars in cube demo

Post by Mr. Kibernetik »

Speed optimization and no bug in scroller:

Code: Select all

rem STAR IN CUBE DEMO 
rem just having some fun....
rem Enjoy!

gosub initialize
refresh off

'The Main Loop. These Calls Are Repeated Until (infinity)
do
draw color 10,10,size*5

graphics clear 10/255,size*5/255,10/255
gosub rotate
gosub draw


'Display Text.. Do Scroll 

fill color 70/255,50/255,70/255
fill rect 0,0 to sw,22
fill rect 0,sh-22 to sw,sh
draw color 0,0,0
draw text mid$(s$,p,67) at scx+2,sh-23
draw text "-= STAR IN CUBE DEMO =-" at 20,0
draw color 1,1,1
draw text "-= STAR IN CUBE DEMO =-" at 22,2
draw text mid$(s$,p,67) at scx,sh-22
scx=scx-2
if scx<-10 then
scx=scx+10
p=p+1
if p=len(s$) then p=0
end if
refresh
until 1=2


draw:

'Draw The object!!!
b=1
mm=mm+.1
size=5+(3*sin(mm/10))
for a=1 to 42
z(a)=z(a)+4
if z(a)>50 then z(a)=z(a)-100
sz=((80-tz(a))/20)+(size-5)
fill color 30/255,sz*10/255,sz*20/255
fill rect tx(a),ty(a) size sz
next a
draw color 155,255,255
draw line tx(polys-7),ty(polys-7) to tx(polys-6),ty(polys-6)
draw line to tx(polys-5),ty(polys-5)
draw line to tx(polys-4),ty(polys-4)
draw line to tx(polys-7),ty(polys-7)

draw line tx(polys-3),ty(polys-3) to tx(polys-2),ty(polys-2)
draw line to tx(polys-1),ty(polys-1)
draw line to tx(polys),ty(polys)
draw line to tx(polys-3),ty(polys-3)

draw line tx(polys-7),ty(polys-7) to tx(polys-3),ty(polys-3)
draw line tx(polys-6),ty(polys-6) to tx(polys-2),ty(polys-2)
draw line tx(polys-5),ty(polys-5) to tx(polys-1),ty(polys-1)
draw line tx(polys-4),ty(polys-4) to tx(polys),ty(polys)

return

rotate:

'Rotate And Scale Each Point - Store Result

for a=1 to polys
x1=x(a)
y1=y(a)
z1=z(a)

'X,Y,Z rotations!
xx=x1
yy=y1*cs(xr)+z1*sn(xr)
zz=z1*cs(xr)-y1*sn(xr)
y1=yy
x1=xx*cs(yr)-zz*sn(yr)
z1=xx*sn(yr)+zz*cs(yr)
zz=z1
xx=x1*cs(zr)-y1*sn(zr)
yy=x1*sn(zr)+y1*cs(zr)

'Apply Perspective! 
xx=size*(xx/((zz/155)+1))+sw/2
yy=size*(yy/((zz/155)+1))+sh/2
tx(a)=xx
ty(a)=yy
tz(a)=zz
next a
'Rotation Incriments. Have a Play with these! 

xr=xr+1 ' X rotation per frame
yr=yr+1 ' Y rotation per frame
zr=zr+2 ' Z rotation per frame

if xr>=720 then xr=xr-720
if yr>=720 then yr=yr-720
if zr>=720 then zr=zr-720
return


initialize:

'Open Gfx Screen!
graphics

'Define the necessary variables! 
sw=screen_width()
sh=screen_height()
pi=3.1415
s$="                                          "
s$&="                                         "
s$=s$&"Welcome to the -= STAR IN CUBE DEMO =- Just "
s$=s$&" some fun with graphical insanity made in "
s$=s$&"the SMART BASIC for a change.. I hope that "
s$=s$&"you enjoy it... This is a different idea for a "
s$=s$&"starfield and it is very similar to an Amiga demo "
s$=s$&"I did a long time ago :o) Not much to say this "
s$=s$&"time except that it's all real time (no cheating!)"
s$=s$&" That's all folks!! "

p=0 ' Text pointer
scx=0 ' Scroll text scroll offset variable

polys=58 ' The amount of polygons in the object
dim x(polys+1) ' Original X co-ordinate store
dim y(polys+1) ' Original Y co-ordinate store
dim z(polys+1) ' Original Z co-ordinate store
dim tx(polys+1) ' Transformed X co-ordinate store
dim ty(polys+1) ' Transformed Y co-ordinate store
dim tz(polys+1) ' Transformed Z co-ordinate store

'Define Sine Tables!!
dim cs(721)
dim sn(721)
for ang=0 to 720
cs(ang)=cos(ang*(pi/360))
sn(ang)=sin(ang*(pi/360))
next ang

'Generate star positions and Read in the object!

for a=1 to polys
x(a)=-50+rnd(100)
y(a)=-50+rnd(100)
z(a)=-50+rnd(100)
if a>50 then 
read x(a),y(a),z(a)
end if
next a

'Cube: 8 Points
data -50,-50,-50,50,-50,-50,50,50,-50,-50,50,-50
data -50,-50,50,50,-50,50,50,50,50,-50,50,50
return

Post Reply