home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 63 / CDACTUAL63.iso / Aplicaciones / DarkBasic / DemoDarkBasic.exe / help / examples / basic3d / exam13.dba < prev    next >
Encoding:
Text File  |  2000-04-09  |  14.9 KB  |  750 lines

  1. Rem * Title  : 3D Model Viewer
  2. Rem * Author : DBS-LB
  3. Rem * Date   : 1st Sept 99
  4. rem ============================================
  5. rem DARK BASIC EXAMPLE PROGRAM 13
  6. rem ============================================
  7. rem This program demonstrates viewing 3D models
  8. rem --------------------------------------------
  9. backdrop off
  10. draw to back
  11. sync on
  12.  
  13. rem Main Browser Arrays
  14. dim browsefile$(1000)
  15. dim browsetype(1000)
  16. dim browsedata(5) : rem 1=Max
  17. dim pagedata(5) : rem 1=Pos / 2=Icon / 3=OldIcon / 4=SecondPage
  18.  
  19. rem Grab all images
  20. prepare_images()
  21.  
  22. rem Blank browser screen
  23. blank_browser()
  24.  
  25. rem Initialize global variables
  26. gosub _setup_browser
  27.  
  28. rem Main Loop
  29. do
  30.  
  31. rem If in second-page mode
  32. if pagedata(4)>0
  33.     if mouseclick()=0 and spflag=2 then spflag=3
  34.     if mouseclick()=1 and spflag=3
  35.         stop_secondview()
  36.         spflag=1
  37.     endif
  38. else
  39.     if mouseclick()=0 then spflag=0
  40.     if spflag=0 then gosub _handle_buttons
  41. endif
  42.  
  43. rem Handle sustained scroll
  44. if scroll<>0
  45.     if scroll>0
  46.         scroll=scroll-25
  47.         pagedata(1)=pagedata(1)-25
  48.     else
  49.         scroll=scroll+25
  50.         pagedata(1)=pagedata(1)+25
  51.     endif
  52.     updateflag=1
  53. endif
  54.  
  55. rem Update central view
  56. if pagedata(4)=0
  57.     if updateflag>0 then gosub _update_screen : updateflag=0
  58. else
  59.     view_updatesecondpage()
  60. endif
  61.  
  62. rem Update potential message bar
  63. if messagedelay>0
  64.     messagedelay=messagedelay-1
  65.     if messagedelay>0
  66.         draw_projectfolder(messagebar$)
  67.     else
  68.         draw_projectfolder(projectfolder$)
  69.     endif
  70. endif
  71.  
  72. rem Terminate countdown
  73. if termcount>0
  74.     termcount=termcount-1
  75.     if termcount=0 then exit
  76. endif
  77.  
  78. rem Update screen
  79. sync
  80.  
  81. rem End Main Loop
  82. loop
  83. end
  84.  
  85. rem ** Subroutines
  86.  
  87. _handle_buttons:
  88.  
  89. rem Handle icon selection
  90. if mouseclick()=1
  91.     if isbuttondown=0
  92.         isbuttondown=1
  93.         updateflag=detect_iconselect()
  94.     endif
  95. else
  96.     isbuttondown=0
  97. endif
  98.  
  99. rem View / Stop
  100. if detect_button(111)=1 then spflag=view_selection()
  101. if detect_button(112)=1 then stop_selection()
  102.  
  103. rem Up / Down
  104. if detect_button(113)=1 and scroll=0
  105.     if pagedata(1)>0 then scroll=100
  106. endif
  107. if detect_button(114)=1 and scroll=0
  108.     if pagedata(1)<(((browsedata(1)-14)/5)*100) then scroll=-100
  109. endif
  110.  
  111. rem Add
  112. if detect_button(115)=1
  113.     if mouseclick()=1 and ambuttondown=0
  114.         ambuttondown=1
  115.         if projectexists=1
  116.             if pagedata(2)>0
  117.                 destfile$=projectfolder$
  118.                 if right$(destfile$,1)<>"\" then destfile$=destfile$+"\"
  119.                 destfile$=destfile$+browsefile$(pagedata(2))
  120.                 if file exist(destfile$)=0
  121.     
  122.                     rem Copy Media File to Dest File
  123.                     mediafile$=mediafolder$
  124.                     if right$(mediafile$,1)<>"\" then mediafile$=mediafile$+"\"
  125.                     mediafile$=mediafile$+browsefile$(pagedata(2))
  126.                     copy file mediafile$,destfile$
  127.                     messagebar$="MEDIA FILE "+browsefile$(pagedata(2))+" COPIED TO PROJECT FOLDER!"
  128.                     messagedelay=200
  129.     
  130.                 else
  131.                     messagebar$="FILE ALREADY EXISTS IN PROJECT FOLDER!"
  132.                     messagedelay=100
  133.                 endif
  134.             else
  135.                 messagebar$="YOU MUST SELECT AN ICON TO ADD!"
  136.                 messagedelay=150
  137.             endif
  138.         else
  139.             messagebar$="YOU MUST SELECT A PROJECT FROM THE EDITOR FIRST!"
  140.             messagedelay=200
  141.         endif
  142.     endif
  143. else
  144.     ambuttondown=0
  145. endif
  146.  
  147. rem OK
  148. if mouseclick()=1 and termcount=0
  149.     if detect_button(116)=1 then termflag=1
  150. else
  151.     detect_button(116)
  152.     if termflag=1 and mousex()>=12+(5*104) and mousex()<12+(5*104)+94 and mousey()>=399 and mousey()<446
  153.         termcount=5
  154.     endif
  155.     termflag=0
  156. endif
  157.  
  158. rem Parent Folder
  159. if detect_button(117)=1
  160.     if mouseclick()=1 and pfbuttondown=0
  161.         pfbuttondown=1
  162.         goto_parentdir()
  163.         updateflag=1
  164.     endif
  165. else
  166.     pfbuttondown=0
  167. endif
  168.  
  169. return
  170.  
  171. _update_screen:
  172.  
  173.     rem Update browse view area
  174.     update_view(updateflag)
  175.  
  176.     rem Update string prompts
  177.     mediafolder$=get dir$()
  178.     draw_browsefolder(mediafolder$)
  179.     draw_projectfolder(projectfolder$)
  180.  
  181. return
  182.  
  183. _setup_browser:
  184.  
  185.     rem Field strings
  186.     projectexists=0
  187.     mediafolder$=get dir$()
  188.     if mid$(cl$(),2)<>":"
  189.         projectfolder$="NO PROJECT SELECTED."
  190.     else
  191.         projectfolder$=cl$()
  192.         projectexists=1
  193.     endif
  194.  
  195.     rem States
  196.     pagedata(1)=0
  197.     pagedata(2)=0
  198.     pagedata(3)=0
  199.     pagedata(4)=0
  200.  
  201.     rem Temp variables
  202.     scroll=0
  203.     updateflag=1
  204.     spflag=0
  205.  
  206.     rem Scan initial directory
  207.     scan_directory()
  208.  
  209. return
  210.  
  211. rem ** Functions
  212.  
  213. function view_selection()
  214.  
  215.     rem If icon selected
  216.     if pagedata(2)>0
  217.  
  218.         rem Determine icon type number
  219.         file$=browsefile$(pagedata(2))
  220.         typeid=find_filetype(file$)
  221.  
  222.         if typeid=1 then view_anim(file$)
  223.         if typeid=2 then view_bitmap(file$)
  224.         if typeid=3 then load sound file$,1 : loop sound 1
  225.         if typeid=4 then load music file$,1 : loop music 1
  226.         if typeid=5 then view_object(file$)
  227.  
  228.     endif
  229.  
  230. endfunction 2
  231.  
  232. function view_bitmap(file$)
  233.  
  234.     rem Original bitmap
  235.     load bitmap file$,5
  236.     set current bitmap 0
  237.     pagedata(4)=1
  238.  
  239. endfunction
  240.  
  241. function view_anim(file$)
  242.  
  243.     rem Original animation
  244.     load animation file$,1
  245.     width=animation width(1)
  246.     height=animation height(1)
  247.     if height>320
  248.         aspect#=height/320.0
  249.         height=320
  250.         width=width/aspect#
  251.     endif
  252.     x1=(640-width)/2
  253.     x2=x1+width-1
  254.     y1=64+((320-height)/2)
  255.     y2=y1+height-1
  256.     play animation 1,0,x1,y1,x2,y2
  257.     pagedata(4)=2
  258.  
  259. endfunction
  260.  
  261. function view_object(file$)
  262.  
  263.     rem Original object model
  264.     load object file$,1
  265.     if total object frames(1)>1 then loop object 1
  266.  
  267.     rem Grab top and bottom panels
  268.     get image 11,0,0,640,64
  269.     get image 12,0,64+320,640,479
  270.     
  271.     pagedata(4)=3
  272.  
  273. endfunction
  274.  
  275. function view_updatesecondpage()
  276.  
  277.     rem Text setting
  278.     set text size 20
  279.     set text to bold
  280.     set text transparent
  281.     set text font "Arial"
  282.  
  283.     if pagedata(4)=1
  284.         rem Original bitmap
  285.         width=bitmap width(5)
  286.         height=bitmap height(5)
  287.         if height>320
  288.             aspect#=height/320.0
  289.             height=320
  290.             width=width/aspect#
  291.         endif
  292.     
  293.         rem Region to place bitmap
  294.         x1=(640-width)/2
  295.         x2=x1+width-1
  296.         y1=64+((320-height)/2)
  297.         y2=y1+height-1
  298.     
  299.         rem Paste bitmap to visual
  300.         ink rgb(0,0,128),0 : box 0,64,639,64+319
  301.         copy bitmap 5,0,0,bitmap width(5)-1,bitmap height(5)-1,0,x1,y1,x2,y2
  302.     
  303.         rem Print Additional information
  304.         text$=browsefile$(pagedata(2))+" ("
  305.         text$=text$+str$(bitmap width(5))+"x"
  306.         text$=text$+str$(bitmap height(5))+"x"
  307.         text$=text$+str$(bitmap depth(5))+"bit)"
  308.         ink rgb(255,255,255),0
  309.         center text 320,360,text$
  310.     endif
  311.     if pagedata(4)>=2
  312.         ink rgb(0,0,128),0 : box 0,64,639,64+319
  313.         text$=browsefile$(pagedata(2))
  314.         ink rgb(255,255,255),0
  315.         center text 320,360,text$
  316.     endif
  317.     if pagedata(4)=3
  318.         yrotate object 1,wrapvalue(object angle y(1)+2)
  319.         paste image 11,0,0
  320.         paste image 12,0,64+320
  321.         text$=browsefile$(pagedata(2))
  322.         ink rgb(255,255,255),0
  323.         center text 320,360,text$
  324.     endif
  325.  
  326. endfunction
  327.  
  328. function stop_selection()
  329.  
  330.     rem Deactivate everything...
  331.     stop_secondview()
  332.     if sound exist(1)=1 then delete sound 1
  333.     if music exist(1)=1 then delete music 1
  334.  
  335. endfunction
  336.  
  337. function stop_secondview()
  338.  
  339.     rem Deactivate second page view
  340.     if pagedata(4)>0
  341.         if animation exist(1)=1 then delete animation 1
  342.         if bitmap exist(5)=1 then delete bitmap 5
  343.         if object exist(1)=1 then delete object 1
  344.     endif
  345.     pagedata(4)=0
  346.  
  347. endfunction
  348.  
  349. function scan_directory()
  350.  
  351.     rem Scan directory to get files
  352.     bi=0 : find first : find next : find next
  353.     do
  354.         rem gather directory list
  355.         bi=bi+1
  356.         browsetype(bi)=get file type()
  357.         if browsetype(bi)=-1 or bi>=999 then exit
  358.         browsefile$(bi)=get file name$()
  359.         find next
  360.         draw_projectfolder("Scanning "+browsefile$(bi)+"...")
  361.         sync
  362.     loop
  363.     total=bi-1
  364.     browsedata(1)=total
  365.  
  366.     rem Bubble sort files so folders at the top
  367.     for a=1 to total
  368.         for b=1 to total
  369.             if a<>b and browsetype(a)>browsetype(b)
  370.                 ct=browsetype(a)
  371.                 cf$=browsefile$(a)
  372.                 browsetype(a)=browsetype(b)
  373.                 browsefile$(a)=browsefile$(b)
  374.                 browsetype(b)=ct
  375.                 browsefile$(b)=cf$
  376.                 draw_projectfolder("Sorting "+cf$+"...")
  377.                 sync
  378.             endif
  379.         next b
  380.     next a
  381.  
  382. endfunction
  383.  
  384. function goto_parentdir()
  385.  
  386.     currentdir$=get dir$()
  387.     c=len(currentdir$)
  388.     if mid$(currentdir$,c)="\" then c=c-1
  389.     while mid$(currentdir$,c)<>"\" and c>0 : c=c-1 : endwhile
  390.     if c>0 then currentdir$=left$(currentdir$,c)
  391.     set dir currentdir$
  392.     scan_directory()
  393.     pagedata(1)=0
  394.     pagedata(2)=0
  395.  
  396. endfunction
  397.  
  398. function goto_folder(foldername$)
  399.  
  400.     currentdir$=get dir$()
  401.     if len(currentdir$)>3 then currentdir$=currentdir$+"\"
  402.     currentdir$=currentdir$+foldername$
  403.     set dir currentdir$
  404.     scan_directory()
  405.     pagedata(1)=0
  406.     pagedata(2)=0
  407.  
  408. endfunction
  409.  
  410. function update_view(updatelevel)
  411.  
  412.     rem Either full update or update icon only
  413.     if updatelevel=1
  414.  
  415.         rem Fill view with backdrop
  416.         view_fillbackdrop()
  417.     
  418.         rem Put contents
  419.         view_fillcontents(0)
  420.     
  421.     else
  422.  
  423.         rem Overwrite selected icon only
  424.         view_fillcontents(1)
  425.  
  426.     endif
  427.  
  428.     rem Copy to visible bitmap
  429.     set current bitmap 2
  430.     get image 1,0,0,640,320
  431.     set current bitmap 0
  432.     paste image 1,0,64
  433.  
  434. endfunction
  435.  
  436. function view_fillcontents(updateselecticononly)
  437.  
  438.     rem Work out how far down page we are
  439.     pagestartline=pagedata(1)/100
  440.  
  441.     rem Point to first file in current directory list
  442.     fileindex=1+(pagestartline*5)
  443.     
  444.     rem Draw forth row if scrolling
  445.     if pagedata(1)=(pagestartline*100) then rows=2 else rows=3
  446.  
  447.     rem Reduce pointer by advancement step
  448.     pointer=pagedata(1)-(pagestartline*100)
  449.  
  450.     rem Set text style
  451.     set text size 15
  452.     set text font "Arial"
  453.     set text transparent
  454.     ink rgb(0,0,0),rgb(255,255,255)
  455.  
  456.     rem Draw contents from page-pointer
  457.     set current bitmap 2
  458.     for y=0 to rows
  459.         for x=0 to 4
  460.  
  461.             rem If icon is to be displayed
  462.             update=0
  463.             if updateselecticononly=0 then update=1
  464.             if updateselecticononly=1 and (fileindex=pagedata(2) or fileindex=pagedata(3)) then update=1
  465.             if update=1
  466.                 if fileindex<=browsedata(1)
  467.     
  468.                     rem Icon Details
  469.                     type=1-browsetype(fileindex)
  470.                     if fileindex=pagedata(2) then light=1 else light=0
  471.                     bx=38+(x*116)
  472.                     by=11+(y*100)-pointer
  473.     
  474.                     rem Display Icon
  475.                     paste sprite 1+(type*2)+light,bx,by
  476.  
  477.                     rem Determine media type
  478.                     if type=1
  479.  
  480.                         rem Discover extension of filename
  481.                         file$=browsefile$(fileindex)
  482.                         typegfx=find_filetype(file$)
  483.                         if typegfx>0
  484.                             paste image 140+typegfx,bx+6,by+5
  485.                         endif
  486.  
  487.                         rem Place text under icon
  488.                         set current bitmap 3
  489.                         set text to normal
  490.                         ink rgb(32,32,32),rgb(255,255,255)
  491.                         cls : center text 46,0,browsefile$(fileindex)
  492.                         get image 1,0,0,92,16
  493.                         set current bitmap 2
  494.                         paste image 1,bx+4,by+76
  495.  
  496.                     else
  497.  
  498.                         rem Place text inside icon
  499.                         set current bitmap 3
  500.                         set text to bold
  501.                         ink rgb(32,32,32),15126048
  502.                         cls : center text 46,0,browsefile$(fileindex)
  503.                         get image 1,0,0,92,16
  504.                         set current bitmap 2
  505.                         paste image 1,bx+4,by+32
  506.  
  507.                     endif
  508.  
  509.                 endif
  510.             endif
  511.  
  512.             fileindex=fileindex+1
  513.     
  514.         next x
  515.     next y
  516.     set current bitmap 0
  517.  
  518. endfunction
  519.  
  520. function find_filetype(file$)
  521.  
  522.     ext$="_"
  523.     t=len(file$)
  524.     while t>1
  525.         if mid$(file$,t)="." then exit
  526.         t=t-1
  527.     endwhile
  528.     if t>1 and t<len(file$)
  529.         t=t+1
  530.         for n=t to len(file$)
  531.             ext$=ext$+mid$(file$,n)
  532.         next n
  533.     endif
  534.     ext$=lower$(ext$)
  535.  
  536.     typegfx=0
  537.     if ext$="_avi" then typegfx=1
  538.     if ext$="_bmp" then typegfx=2
  539.     if ext$="_wav" then typegfx=3
  540.     if ext$="_mid" then typegfx=4
  541.     if ext$="_3ds" then typegfx=5
  542.     if ext$="_x" then typegfx=5
  543.  
  544. endfunction typegfx
  545.  
  546. function view_fillbackdrop()
  547.  
  548.     rem Work out scroll position of backdrop
  549.     fullpage=(pagedata(1)/320)
  550.     scrolly=pagedata(1)-(fullpage*320)
  551.  
  552.     rem use second bitmap for CONSTRUCTION
  553.     set current bitmap 2
  554.     paste image 131,0,0-scrolly
  555.     paste image 131,0,320-scrolly
  556.     set current bitmap 0
  557.  
  558. endfunction
  559.  
  560. function detect_iconselect()
  561.  
  562.     if mouseclick()=1
  563.  
  564.         rem Find overlap with an icon
  565.         action=0
  566.         for y=0 to 2
  567.             for x=0 to 4
  568.                 bx=38+(x*116)
  569.                 by=11+(y*100)
  570.                 if mousex()>=bx and mousex()<=bx+96
  571.                     if mousey()>=64+by and mousey()<=64+by+96
  572.                         pagestartline=pagedata(1)/100
  573.                         fileindex=1+(pagestartline*5)+(y*5)+x
  574.                         if fileindex<=browsedata(1)
  575.                             if browsetype(fileindex)=1
  576.                                 action=1
  577.                             else
  578.                                 action=2
  579.                             endif
  580.                         endif
  581.                     endif
  582.                 endif
  583.             next x
  584.         next y
  585.  
  586.         rem perform action if overlap occuring
  587.         if action>0
  588.             if action=1
  589.                 goto_folder(browsefile$(fileindex))
  590.                 updatelevel=1
  591.             endif
  592.             if action=2
  593.                 pagedata(3)=pagedata(2)
  594.                 pagedata(2)=fileindex
  595.                 updatelevel=2
  596.             endif
  597.         endif
  598.  
  599.     endif
  600.  
  601. endfunction updatelevel
  602.  
  603. function detect_button(id)
  604.  
  605.     rem Calculate coordinate for button
  606.     if id>=111 and id<=116
  607.         t=id-111 : x1=12+(t*104) : x2=x1+94 : y1=399 : y2=446
  608.     endif
  609.     if id=117 then x1=453 : y1=43 : x2=635 : y2=62
  610.  
  611.     rem Show depressed clicking it
  612.     depressed=0
  613.     if mousex()>=x1 and mousex()<x2 and mousey()>=y1 and mousey()<y2
  614.         if mouseclick()=1
  615.             depressed=1
  616.         endif
  617.     endif
  618.  
  619.     rem Show 'indented' if depressed
  620.     if depressed=1
  621.         paste image id,x1+1,y1+1
  622.     else
  623.         paste image id+10,x1,y1
  624.     endif
  625.  
  626. endfunction depressed
  627.  
  628. function blank_browser()
  629.  
  630.     rem Copy browser to screen
  631.     copy bitmap 1,0
  632.  
  633.     rem Clear fields
  634.     clear_browsefolder()
  635.     clear_projectfolder()
  636.  
  637. endfunction
  638.  
  639. function clear_browsefolder()
  640.     ink rgb(255,255,255),0 : box 5,44,449,60
  641. endfunction
  642.  
  643. function draw_browsefolder(string$)
  644.  
  645.     clear_browsefolder()
  646.     ink rgb(128,128,128),0
  647.     set text size 15
  648.     set text font "System"
  649.     set text transparent
  650.  
  651.     rem Ensure size remains within box
  652.     prompt$=string$
  653.     while text width(prompt$)>440
  654.         newa$=""
  655.         for t=2 to len(prompt$)
  656.             newa$=newa$+mid$(prompt$,t)
  657.         next t
  658.         prompt$=newa$
  659.     endwhile
  660.     text 8,44,prompt$
  661.  
  662. endfunction
  663.  
  664. function clear_projectfolder()
  665.     ink 0,0 : box 0,463,639,479
  666. endfunction
  667.  
  668. function draw_projectfolder(string$)
  669.  
  670.     clear_projectfolder()
  671.     ink rgb(255,255,0),0
  672.     set text size 15
  673.     set text font "System"
  674.     set text transparent
  675.     text 0,463,string$
  676.  
  677. endfunction
  678.  
  679. function prepare_images()
  680.  
  681.     rem Load images bitmap
  682.     load bitmap "browser.bmp",1
  683.  
  684.     rem Grab folder icons 101,102
  685.     i=101
  686.     for y=0 to 1
  687.         get image i+y,0,64+(y*96),100,64+96+(y*96)
  688.     next y
  689.  
  690.     rem Grab file icons 103,104
  691.     i=103
  692.     for y=0 to 1
  693.         get image i+y,100,64+(y*96),200,64+96+(y*96)
  694.     next y
  695.  
  696.     rem Create transparent sprites for icons
  697.     for t=1 to 4
  698.         set sprite t,1,1
  699.         sprite t,-200,-200,100+t
  700.     next t
  701.  
  702.     rem Grab dimmed control button
  703.     get image 105,200,64,294,111
  704.  
  705.     rem Grab backdrop filler
  706.     get image 106,294,64,294+160,64+160
  707.  
  708.     rem Grab control buttons
  709.     i=111
  710.     for x=0 to 5
  711.         get image i+x,12+(x*104),399,106+(x*104),446
  712.         get image i+x+10,12+(x*104),399,106+(x*104)+1,446+1
  713.     next x        
  714.  
  715.     rem Grab parent button
  716.     get image 117,453,43,635,62
  717.     get image 117+10,453,43,635+1,62+1
  718.  
  719.     rem Grab icon-type gfx 141-145
  720.     i=141
  721.     for y=0 to 4
  722.         get image i+y,454,64+(y*62),454+89,64+62+(y*62)
  723.     next y
  724.  
  725.     rem Clear icons using backdrop
  726.     for y=0 to 1
  727.         for x=0 to 3
  728.             paste image 106,x*160,64+(y*160)
  729.         next x
  730.     next y
  731.     get image 131,0,64,639,64+320
  732.  
  733.     rem Create second bitmap for view work
  734.     create bitmap 2,640,320
  735.  
  736.     rem Create third bitmap to truncate text
  737.     create bitmap 3,92,16
  738.  
  739.     rem Return to visual bitmap 
  740.     set current bitmap 0
  741.  
  742. endfunction
  743.  
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.