Page 1 of 1

Diamond Intro (testing 4.8) code

Posted: Tue Apr 14, 2015 7:24 am
by DrChip

Code: Select all

rem DIAMOND INTRO v1b 
rem iPhone 6 Plus / iOS 8.3 / SB 4.8
rem testing 4.8 tri commands :)
rem nice update to Smart Basic!! 
rem 1b - fixed typos...
rem Enjoy! 

'I wrote this to test Smart Basic's new
'features. No new ideas to create 
'this demo. I made some quick routines
'that run my vectors fast.. Although 
'they are not practical to use in game 
'design, they sure look cool!

graphics
pi=3.1415
sw=screen_width()
sh=screen_height()

gosub printupbolox
gosub precalculate


b=0
'Variable definitions..
b=0
p=0
qz=255
linesize=20 'characters in each line
a$=""
a$=a$&" THE DIAMOND INTRO  "
a$=a$&" IN SMARTBASIC 4.8  "
a$=a$&" ITS FAST AND EASY  "
a$=a$&""

s$="                                                                   "
s$=s$&"THIS INTRO TOOK SOME TIME AND IS SIMPLE. "
s$=s$&"YOU CAN SEE BEFORE YOU: A PLASMA "
s$=s$&"BACKGROUND, A 55 POINT 3D STARFIELD, A SCALLED "
s$=s$&"AND ROTATED 32 POLYGON LIGHT SOURCED VECTOR AND 3 "
s$=s$&"DIFFERENT TEXT DISPLAYS ALL HAPPENING AT ONCE, AND"
s$=s$&" AT FULL FRAME RATE WITH *NO* SLOWDOWN!!   THIS "
s$=s$&"IS ONLY POSSIBLE BECAUSE I AM DOING SOME CHEATING "
s$=s$&"WITH THE VECTOR OBJECT, THE REST OF IT IS LAME "
s$=s$&"THOUGH....  I AM STARTING TO GET TO GRIPS WITH "
s$=s$&"BASIC NOW, SO YOU CAN EXPECT TO SEE MORE DEMOS "
s$=s$&"FROM ME WITH MORE IMPRESSIVE ROUTINES THAN THIS!! "
s$=s$&"THAT'S ENOUGH TEXT I THINK...   TAKE CARE AND I'LL "
s$=s$&"SEE YOU IN MY NEXT DEMO!!! "
scrx=10
tp=0

nstar=55
zm=1
xm=1
ym=1
dim ox(nstar+1)
dim oy(nstar+1)
dim oc(nstar+1)
dim os(nstar+1)
dim x(nstar+1)
dim y(nstar+1)
dim z(nstar+1)

'Set Star positions..
mmm=1
for a=1 to nstar
  x(a)=-25+rnd(50)
  y(a)=-25+rnd(50)
  z(a)=mmm
  mmm=mmm+100/nstar
next a



loop:
refresh off
   graphics clear 0,0,0
   gosub background
   gosub dispstars
   gosub logo
   gosub scroller
   gosub drawprecalcobj

if qz<=50 then 
  draw color qz/255,qz/255,qz/255
  draw text mid$(a$,p+1,linesize) at sw/4,sh/2
end if
if qz>50 then 
  draw color qz/255,qz/255,qz/255
  draw text MID$(a$,p+1,linesize) at sw/4,sh/2
end if

refresh on
goto loop


printupbolox:
dim boll$(12)
 boll$(1)="        DRCHIP PROUDLY PRESENTS:              "
 boll$(2)="       THE  D I A M O N D  INTRO!             "
 boll$(3)="=============================================="
 boll$(4)="= ALL PROGRAMMING AND DESIGN : DRCHIP        ="
 boll$(5)="= RELEASED ON APRIL 2015 IN SMART BASIC 8.4  ="
 boll$(6)="= SMARTBASIC (IN THE APP STORE - LOOK!!).    ="
 boll$(7)="= CONTACT ME VIA THE FORUM PAGE ON THAT SITE ="
 boll$(8)="= OR IF YOU'RE REALLY  COOL YOU CAN CONTACT  ="
 boll$(9)="= ME VIA FORUM. (CODERS ONLY!)               ="
boll$(10)="=============================================="
for b=1 to 64
refresh off
graphics clear 0,0,0
yy=sh/3
for a=1 to 10
scalc=(b+(a*13))*sin(b/17)
draw color scalc/255,scalc/255,scalc/255
draw text boll$(a) at 1,yy
   yy=yy+13
next a
refresh on
pause .1
next b
return


scroller:
draw color 0,0,0
draw text mid$(s$,tp,64) at scrx,sh-22
draw color 200/255,1,1
draw text mid$(s$,tp,64) at scrx,sh-20

scrx=scrx-2
if scrx<0 then 
 tp=tp+1
 scrx=scrx+10
end if
if tp>len(s$)-1 then tp=0
return

logo:
'fill color 0,0,50/255
'fill rect 0,0 to sw,20
draw color .5,.5,.5
draw text "THE DIAMOND INTRO" at sw/6,3
draw color 1,1,1
draw text "THE DIAMOND INTRO" at sw/6,0
return

'Draw stars
'This subroutine draws the plasma and blue border 
background:
'Cycle colors
     backcount=backcount+1
     red=20+20*sin(backcount/14)
     grn=20+20*sin(backcount/15)
     blu=20+20*sin(backcount/16)

'Draw blue border lines
     fill color 15/255,15/255,1
     fill rect  0,0 to sw,sh

'Set poly palette
     fill color red/255,grn/255,blu/255
     ' fill color 20/255,grn,20/255
     ' fill color 20/255,20/255,blu/255

'draw plasma
     fill tri 20,20, sw-20,sh-20, 20,sh-20
     fill tri 20,20, sw-20,sh-20, sw-20,20

return


dispstars:
mm=mm+.1
sz=2*sin(mm/23)


for a=1 to nstar

qt=200-z(a)*2
fill color qt/255,qt/255,(qt+20)/255
xx=x(a)*25
yy=y(a)*25
xx=xx/z(a)*30
yy=yy/z(a)*30
xx=xx+sw/2
yy=yy+sh/2
zz=10-z(a)/14
fill rect xx,yy to xx+zz,yy+zz
z(a)=z(a)+sz

if z(a)<1 then z(a)=z(a)+100
if z(a)>100 then z(a)=z(a)-100
next a

qz=qz-1
if qz<=0 then p=p+linesize ! if p>len(a$) -1 then p=0
if qz<=0 then qz=255
return

'Draw the diamond from out software memory bank!
drawprecalcobj:
for a=1 to polys
'fill color rstore(b/3)*a,gstore(b/3)*(a*2),bstore(b/3)*(a*3)

fill color (a*2)/255,(a*3)/255,(a*4)/255
fill tri xstore(b),ystore(b), xstore(b+1),ystore(b+1),xstore(b+2),ystore(b+2)

b=b+3 
next a
if b>=(polys*3)*175 then b=0
return


'The rest of the listing is the precalculation routine!!!!
'This code will work out the screen co-ordinates for the
'32 polygon diamond!!


precalculate:
'Number of faces in object:

polys=32

dim xstore((polys*4)*180)
dim ystore((polys*4)*180)
dim rstore(polys*180)
dim gstore(polys*180)
dim bstore(polys*180)

'X and Y screen offset positions to center object:
xff=sw/2
yff=sh/2

'X+Y rotation values!

rotx=0.036
roty=0.036
rtx=0
rty=0

'How big do you want it???
size=8


'Permanent and temporary storage definition
'Define arrays to store data:

dim x(polys*4)
dim y(polys*5)
dim z(polys*4)
dim c(polys*4)

'Define arrays to hold transformed data:

dim tx(polys*4)
dim ty(polys*5)
dim tz(polys*4)
dim tc(polys*4)

 
' Read in the object data and place into storage arrays:
b=1
for a=1 to polys
   read x(b),y(b),z(b),x(b+1),y(b+1),z(b+1),x(b+2),y(b+2),z(b+2),c(b),c(b+1),c(b+2)
   b=b+3
next a

'Make movements
for frames=1 to 179
refresh off
graphics clear 0,0,0
draw color 1,1,1
draw text "precalculating..." at 0,0
     gosub transform
     gosub rotate
     gosub sort
     gosub draw
refresh on
next frames

return
'End of precalculation
'Sort Faces, colors and z co-ords according to depth!


sort:
for lame=1 to polys-1
b=1
for a=1 to polys-1

  dck1=tz(b)+tz(b+1)+tz(b+2)
  dck2=tz(b+3)+tz(b+4)+tz(b+5)
  if dck2>dck1 then gosub swap
  b=b+3
next a
next lame
return

swap:

  for in=0 to 2
    tmpx=tx(b+in)
    tmpy=ty(b+in)
    tmpz=tz(b+in)
    tmpc=tc(b+in)
      tx(b+in)=tx(b+in+3)
      ty(b+in)=ty(b+in+3)
      tz(b+in)=tz(b+in+3)
      tc(b+in)=tc(b+in+3)
        tx(b+in+3)=tmpx
        ty(b+in+3)=tmpy
        tz(b+in+3)=tmpz
        tc(b+in+3)=tmpc
  next in
return


'Rotate Object and apply perspective
rotate:
'New angles

rtx=rtx+rotx
rty=rty+roty
'For tidyness
crx=cos(rtx)
srx=sin(rtx)
cry=cos(rty)
sry=sin(rty)
'spin `em
b=1
for p=1 to polys
for inner=1 to 3

 nwy= crx*ty(b) + srx*tz(b)
 nwz= crx*tz(b) - srx*ty(b)

 nwx= cry*tx(b) + sry*nwz
 nwz= cry*nwz - sry*tx(b)

'Perspective+offset
 nwz=nwz+700
 tx(b)=xff+nwx*1300 /nwz
 ty(b)=yff+nwy*1300 /nwz
 tz(b)=nwz
 b=b+1
next inner
next p

return

'Pull Object into temporary storage for manipulation!!


transform:
b=1

size=12*sin(frames/55.01)

for a=1 to polys
for inner=1 to 3
 tx(b)=x(b)*size
 ty(b)=y(b)*size
 tz(b)=z(b)*size
 tc(b)=c(b)
 b=b+1
next inner
next a
 
return

'Draw the object held in the temporary array.

draw:
b=1

for a=1 to polys
xstore(mp)=tx(b)
xstore(mp+1)=tx(b+1)
xstore(mp+2)=tx(b+2)

ystore(mp)=ty(b)
ystore(mp+1)=ty(b+1)
ystore(mp+2)=ty(b+2)
rstore(cp)=tc(b)+(size*4) -100
gstore(cp)=tc(b+1)+(size*4) -100
bstore(cp)=tc(b+2)+(size*4) -100

fill color tc(b)/255,tc(b+1)/255,tc(b+2)/255
fill tri tx(b),ty(b), tx(b+1),ty(b+1), tx(b+2),ty(b+2)
b=b+3
cp=cp+1
mp=mp+3
next a

return

' 3D Object Description As Numerical Data...

object:
' data structure: x,y,z,x,y,z,x,y,z,r,g,b
' (diamond object, 32 polygons)

data -1,6,0,1,6,0,0,0,4,97,7,7
data -1,-6,0,1,-6,0,0,0,4,97,7,7
data  6,1,0,6,-1,0,0,0,4,97,7,7
data -6,1,0,-6,-1,0,0,0,4,97,7,7
data  3,5,0,1,6,0,0,0,4,57,7,7
data  3,5,0,5,3,0,0,0,4,67,7,7
data  5,3,0,6,1,0,0,0,4,77,7,7
data  -3,5,0,-1,6,0,0,0,4,87,7,7
data  -3,5,0,-5,3,0,0,0,4,97,7,7
data  -5,3,0,-6,1,0,0,0,4,107,7,7
data  3,-5,0,1,-6,0,0,0,4,117,7,7
data  3,-5,0,5,-3,0,0,0,4,127,7,7
data  5,-3,0,6,-1,0,0,0,4,137,7,7
data  -3,-5,0,-1,-6,0,0,0,4,147,7,7
data  -3,-5,0,-5,-3,0,0,0,4,157,7,7
data  -5,-3,0,-6,-1,0,0,0,4,167,7,7
data -1,6,0,1,6,0,0,0,-4,7,7,97
data -1,-6,0,1,-6,0,0,0,-4,7,7,97
data  6,1,0,6,-1,0,0,0,-4,7,7,97
data -6,1,0,-6,-1,0,0,0,-4,7,7,97
data  3,5,0,1,6,0,0,0,-4,7,7,57
data  3,5,0,5,3,0,0,0,-4,7,7,67
data  5,3,0,6,1,0,0,0,-4,7,7,77
data  -3,5,0,-1,6,0,0,0,-4,7,7,87
data  -3,5,0,-5,3,0,0,0,-4,7,7,97
data  -5,3,0,-6,1,0,0,0,-4,7,7,107
data  3,-5,0,1,-6,0,0,0,-4,7,7,117
data  3,-5,0,5,-3,0,0,0,-4,7,7,127
data  5,-3,0,6,-1,0,0,0,-4,7,7,137
data  -3,-5,0,-1,-6,0,0,0,-4,7,7,147
data  -3,-5,0,-5,-3,0,0,0,-4,7,7,157
data  -5,-3,0,-6,-1,0,0,0,-4,7,7,167

Re: Dimond Intro (testing 8.4)

Posted: Tue Apr 14, 2015 7:31 am
by Dutchman
I suppose that it is intended for SB 4.8 ??
And I saved it as 'Di(a)mond intro' :D

Re: Dimond Intro (testing 8.4)

Posted: Tue Apr 14, 2015 7:48 am
by Mr. Kibernetik
Very nice demo!

Currently we have sB version 4.8, not 8.4 :D
Also I strongly suggest you to enclose program code in "Code" tag in your posts, because otherwise it is impossible to select your posted code in Safari in iOS.

Re: Dimond Intro (testing 8.4)

Posted: Tue Apr 14, 2015 1:10 pm
by DrChip
I was rushing and it was late... Oops! "Diamond" and 4.8 is what I meant. I like the new commands! It's fast and less typing! :)

Re: Dimond Intro (testing 8.4)

Posted: Tue Apr 14, 2015 3:18 pm
by Frez
Mr. Kibernetik wrote:Very nice demo!

Currently we have sB version 4.8, not 8.4 :D
Also I strongly suggest you to enclose program code in "Code" tag in your posts, because otherwise it is impossible to select your posted code in Safari in iOS.
А у меня наоборот, код не выделяется и соответственно не копируется, будучи заключенным в тег. iOS 8.1, iPad mini. Браузеры пробовал разные.

Re: Dimond Intro (testing 8.4)

Posted: Tue Apr 14, 2015 3:30 pm
by Mr. Kibernetik
Frez wrote:А у меня наоборот, код не выделяется и соответственно не копируется, будучи заключенным в тег. iOS 8.1, iPad mini. Браузеры пробовал разные.
Верно. Хорошее замечание.