flares.bb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  1. ; Flares (C) 2001 Warren Skaalrud - SoftSkull Productions :)
  2. ; If you use this code email me at [email protected] so I know :)
  3. ; Unrestricted FREE use - just leave this text block in.
  4. n_Flares=1
  5. SeedRnd MilliSecs()
  6. Global GxWidth=800
  7. Global GxHeight=600
  8. Global GxDepth=16
  9. Global GxWindowed=0
  10. ;particle effects
  11. Global particlecount
  12. Global snaps$="FlareSnap"
  13. Type particle ; not everything is used in this application
  14. Field x# ; x location of particle
  15. Field y# ; y location of particle
  16. Field z# ; z location of particle
  17. Field vx# ; x movement
  18. Field vy# ; y movement
  19. Field vz# ; z movement
  20. Field startlife ; For determining alpha levels
  21. Field life ; 0=death
  22. Field red ; red colour component
  23. Field green ; green colour component
  24. Field blue ; blue colour component
  25. Field gravity# ; Amount of gravity to exert on particle
  26. Field kind
  27. Field stage
  28. Field SEffect ;special effect
  29. Field flare
  30. Field alpha#
  31. End Type
  32. Type GfxMode
  33. Field width,height,depth,Windowed
  34. End Type
  35. ; for use with normalizing etc.
  36. Type vector
  37. Field x#
  38. Field y#
  39. Field z#
  40. Field u#
  41. Field v#
  42. End Type
  43. ; fire effects
  44. Type Flare
  45. Field entity,speed#
  46. Field vx#,vy#,vz#
  47. Field life,lifespan
  48. Field kind
  49. End Type
  50. Type fpss ; fps history
  51. Field value
  52. End Type
  53. Type timer ; timers
  54. Field millisec
  55. Field time
  56. End Type
  57. Type ini ; stores the ini information
  58. Field info$
  59. End Type
  60. Global displayinfo=True
  61. Global setpoint=140 ;fps
  62. Global Bearing.vector=New vector
  63. ;Findscreen Globals
  64. Global s3dx# ;3d upper left x,y in the 3D plane
  65. Global s3dy# ;
  66. Global Scale3DX# ; The ratio of screen location per 3D space
  67. Global Scale3DY#
  68. Global snap ;snapshot counter
  69. ;fps function
  70. Global fpscount
  71. Global fpstotal
  72. Global fpstime
  73. ;mouse
  74. Global mx=MouseX()
  75. Global my=MouseY()
  76. SoftStart3D()
  77. AmbientLight 100,100,100
  78. SetBuffer BackBuffer()
  79. centre=CreateSphere()
  80. PositionEntity centre,0,0,0
  81. HideEntity centre
  82. Global camera=CreateCamera()
  83. PositionEntity camera,0,0,-10
  84. PointEntity camera,centre
  85. CameraRange camera,1,1000
  86. MoveMouse GraphicsWidth()/2,GraphicsHeight()/2
  87. ;----------------------------------------
  88. findscreen()
  89. Global light=CreateLight(camera)
  90. ;TurnEntity light,45,45,0
  91. Global sphere=SoftLoadSprite("graphics\redspark.bmp")
  92. SpriteViewMode sphere,1
  93. EntityShininess sphere,0.5
  94. ScaleSprite sphere,12,0.4
  95. ;ScaleSprite sphere,12,0.1
  96. HideEntity sphere
  97. Global player=CreateSphere(8)
  98. EntityColor player,100,255,255
  99. EntityShininess player,0.5
  100. EntityAlpha player,0.95
  101. ScaleEntity player,1,1,2
  102. HideEntity player
  103. ;PositionEntity player,0,0,-24
  104. ;flashlight=CreateLight(2,player)
  105. ;PositionEntity flashlight,0,0,0
  106. ;LightRange flashlight,100000
  107. ;LightColor flashlight,100,255,255
  108. ;LightConeAngles flashlight,0,180
  109. an#=0
  110. an_step#=360.0/n_Flares
  111. For k=1 To n_Flares
  112. ;AddFlare(0)
  113. Next
  114. val#=0
  115. rot#=0
  116. speed#=0
  117. While Not KeyHit(1)
  118. val=multiwrap(val+speed,0,359)
  119. fps=fps(20) ;get delayed fps
  120. mx=MouseX()
  121. my=MouseY()
  122. md1=MouseDown(1)
  123. md2=MouseDown(2)
  124. PositionEntity player,Screen3DX(mx),Screen3DY(my),-1
  125. If KeyDown(75) rot=multiwrap(rot-1,0,359) ; num 4
  126. If KeyDown(77) rot=multiwrap(rot+1,0,359) ; num 6
  127. If KeyDown(78) setpoint=setpoint+1; Num +
  128. If KeyDown(74) ; Num -
  129. If setpoint>10
  130. setpoint=setpoint-1
  131. EndIf
  132. EndIf
  133. If KeyDown(76) ; num 5
  134. PositionEntity camera,0,0,-10
  135. PointEntity camera,centre
  136. EndIf
  137. If KeyDown(72) Then speed=speed+0.1
  138. If KeyDown(80) Then speed=speed-0.1
  139. If KeyDown(200) Then MoveEntity camera,0,0,1
  140. If KeyDown(208) Then MoveEntity camera,0,0,-1
  141. If KeyDown(205) Then TurnEntity camera,0,-1,0
  142. If KeyDown(203) Then TurnEntity camera,0,1,0
  143. If particlecount<setpoint
  144. Addparticles(1,screen3dx(mx),screen3dy(my),0,Rnd(-0.5,0.5),Rnd(0,1),Rnd(-0.5,0.5),10000,0,0,1)
  145. Else
  146. If particlecount>setpoint
  147. p.particle=First particle
  148. FreeEntity p\flare
  149. Delete p
  150. particlecount=particlecount-1
  151. EndIf
  152. EndIf
  153. spiralize(val#,rot#)
  154. UpdateWorld
  155. RenderWorld
  156. If timer(200)
  157. If KeyDown(57)
  158. displayinfo=1-displayinfo
  159. EndIf
  160. EndIf
  161. If displayinfo
  162. Color 255,255,255
  163. Text 0,00,"FPS : "+fps
  164. Color 100,100,100
  165. Text GraphicsWidth()/2,0,"Flares : "+particlecount,True
  166. Text GraphicsWidth()/2,20,"Space to hide/unhide",True
  167. Text 0,GraphicsHeight()-40,"Arrows to Navigate : Num 8/2 to Roll flares"
  168. Text 0,GraphicsHeight()-20,"SetPoint: "+setpoint+" Flares : Numpad +/- to change setpoint"
  169. Text GraphicsWidth()-StringWidth("(C) 2001 SoftSkull Productions"),GraphicsHeight()-20,"(C) 2001 SoftSkull Productions"
  170. Text GraphicsWidth()-StringWidth("Num 4/6 Rotate | PrntScrn = Snap"),0,"Num 4/6 Rotate | PrntScrn = Snap"
  171. EndIf
  172. Flip
  173. key=GetKey()
  174. If key
  175. show=key
  176. EndIf
  177. Select key
  178. Case 42 ;
  179. While fileexists(snaps+snap+".bmp")
  180. snap=snap+1
  181. Wend
  182. SaveBuffer (FrontBuffer(),snaps+snap+".bmp")
  183. snap=snap+1
  184. End Select
  185. Wend
  186. WriteINI("startup.ini")
  187. EndGraphics
  188. End
  189. Function SoftStart3D()
  190. ; Retreive default values
  191. LoadINI("startup.ini")
  192. GxWidth=GetINIVal("gxwidth",800)
  193. GxHeight=GetINIVal("gxheight",600)
  194. GxDepth=GetINIVal("gxdepth",16)
  195. GxWindowed=GetINIVal("gxwindowed",0)
  196. If GxWidth=0 Or GxHeight=0 Or (GxDepth=0 And GxWindowed=1) ; prevent errors
  197. GxWidth=800 ;back to defaults - if this doesnt exist in the modes list
  198. GxHeight=600 ;it will go to setup mode
  199. GxDepth=16
  200. GxWindowed=0
  201. EndIf
  202. win$="Full Screen"
  203. If GxWindowed
  204. win="Windowed"
  205. EndIf
  206. Print "Starting Mode="+gxwidth+"x"+gxheight+"-"+GxDepth+" Bit :"+win
  207. Print "<Spacebar> To configure Graphics Mode"
  208. setup=False
  209. time=MilliSecs()
  210. Repeat
  211. If KeyDown(57) ;spacebar
  212. Setup=True
  213. EndIf
  214. Until setup Or (MilliSecs()-time>3000)
  215. Cls
  216. Locate 0,0
  217. Print "Make sure your monitor can support your selection"
  218. If CountGfxDrivers()>1
  219. Print "Display drivers:"
  220. For k=1 To CountGfxDrivers()
  221. Print k+":"+GfxDriverName$(k)
  222. Next
  223. Repeat
  224. driver=Input$( "Display driver (1-"+CountGfxDrivers()+"):" )
  225. Until driver>=1 And driver<=CountGfxDrivers()
  226. SetGfxDriver driver
  227. EndIf
  228. Print "Display modes:"
  229. cnt=0
  230. column=0:ct=FontHeight()
  231. For k=1 To CountGfxModes()
  232. If GfxMode3D(k)
  233. t.GfxMode=New GfxMode
  234. t\width=GfxModeWidth(k)
  235. t\height=GfxModeHeight(k)
  236. t\depth=GfxModeDepth(k)
  237. ; indicate currently selected mode
  238. char$=":"
  239. If t\width=GxWidth And t\height=GxHeight And t\depth=GxDepth
  240. char$=">" ; found the currently selected mode
  241. If Not setup ; if we are not changing that... GO!
  242. Graphics3D GxWidth,GxHeight,GxDepth,GxWindowed
  243. Return
  244. EndIf
  245. EndIf
  246. b3d$="2D"
  247. If GfxMode3D(k)
  248. b3d$="3D"
  249. EndIf
  250. cnt=cnt+1
  251. ct=ct+FontHeight()
  252. If ct>260
  253. ct=FontHeight()*2
  254. column=column+145
  255. EndIf
  256. Locate column,ct
  257. Print cnt+char+t\width+","+t\height+","+t\depth+":"+b3d$
  258. EndIf
  259. Next
  260. If Not cnt
  261. ; Doom
  262. Print "No 3D Graphics modes available!"
  263. Print "Press any key..."
  264. WaitKey
  265. EndGraphics
  266. End
  267. EndIf
  268. Repeat
  269. mode=Input$( "Display Mode (1-"+cnt+"):" )
  270. Until mode>=1 And mode<=cnt
  271. t.GfxMode=First GfxMode
  272. While mode>1
  273. t=After t
  274. mode=mode-1
  275. Wend
  276. GxWidth=t\width
  277. GxHeight=t\height
  278. GxDepth=t\depth
  279. GxWindowed=0
  280. Graphics3D GxWidth,GxHeight,GxDepth,GxWindowed
  281. ChangeINI("gxWidth",GxWidth)
  282. ChangeINI("gxHeight",GxHeight)
  283. ChangeINI("gxDepth",GxDepth)
  284. ChangeINI("gxwindowed",GxWindowed)
  285. SetBuffer BackBuffer()
  286. Delete Each GfxMode
  287. End Function
  288. Function LoadINI(file$)
  289. ; This is a one time call... It places the contents into the ini buffer
  290. If FileExists(file)
  291. ini=ReadFile(file)
  292. While Not Eof(ini)
  293. b.ini=New ini
  294. b\info=Lower$(ReadLine$(ini))
  295. Wend
  296. CloseFile INI
  297. EndIf
  298. End Function
  299. Function GetINIVal(search$,defvalue$) ; Extract a configuration item from the Search
  300. search=Lower(search)
  301. For i.ini=Each ini
  302. If Instr(i\info,search)=1
  303. spot=Instr(i\info,"=") ; check to see if theres a second half
  304. If spot>0 And spot<Len(i\info) ; otherwise there is no value (nan)
  305. value$=Right$(i\info,Len(i\info)-spot)
  306. Return value
  307. EndIf
  308. EndIf
  309. Next
  310. ; value does not exist
  311. If defvalue
  312. ChangeINI(search$,defvalue$); - add the Default
  313. Return search+"="+defvalue
  314. EndIf
  315. End Function
  316. Function ChangeINI(search$,value$)
  317. ;Changes Not permanent until WriteINI is called
  318. search=Lower(search)
  319. For i.ini=Each ini
  320. If Instr(i\info,search)>0
  321. i\info$=search+"="+value
  322. Return
  323. EndIf
  324. Next
  325. ; if we are here its because no ini entry for this was found
  326. i.ini=New ini
  327. i\info$=search+"="+value
  328. End Function
  329. Function WriteINI(file$)
  330. If First ini = Null
  331. ; No INI information to write
  332. Else
  333. If FileExists(file)
  334. DeleteFile file
  335. EndIf
  336. ini=WriteFile(file)
  337. For i.ini=Each ini
  338. WriteLine ini,i\info
  339. Next
  340. CloseFile ini
  341. EndIf
  342. End Function
  343. Function FileExists(file$)
  344. result=FileType(file$)
  345. If result
  346. Return True
  347. Else
  348. Return False
  349. EndIf
  350. End Function
  351. Function fps(buffer)
  352. ;(c) 2001 SoftSkull Productions
  353. ;0 = averaged 1=instant (tends to be hard to read)
  354. fps#=MilliSecs()-fpstime
  355. If fps=0
  356. fps=1
  357. EndIf
  358. fps=1000.0/fps ;instantaneous fps (Buffer=0)
  359. fpstime=MilliSecs()
  360. If buffer>0
  361. f.fpss=New fpss
  362. f\value=fps
  363. fpstotal=0
  364. fpscount=0
  365. For l.fpss=Each fpss
  366. If fpscount>buffer ;
  367. Delete First fpss
  368. Else
  369. fpscount=fpscount+1 ;count the number of calls
  370. fpstotal=fpstotal+l\value;fps ;total amount
  371. EndIf
  372. Next
  373. fps=fpstotal/fpscount
  374. EndIf
  375. Return fps
  376. End Function
  377. Function timer(time)
  378. ;(c) 2001 SoftSkull Productions
  379. cond=False
  380. found=False
  381. For t.timer=Each timer
  382. If t\time=time
  383. found=True
  384. elapse=MilliSecs()-t\millisec
  385. If elapse>t\time
  386. cond=True
  387. t\millisec=t\millisec+t\time
  388. EndIf
  389. EndIf
  390. Next
  391. If found=False
  392. t.timer=New timer
  393. t\time=time
  394. t\MilliSec=MilliSecs()
  395. EndIf
  396. Return cond
  397. End Function
  398. Function findscreen()
  399. ;(c) 2001 SoftSkull Productions
  400. CameraProject(camera,0,0,0)
  401. sx#=ProjectedX()
  402. sy#=ProjectedY()
  403. CameraProject(camera,1,1,0)
  404. ex#=ProjectedX()
  405. ey#=ProjectedY()
  406. Scale3DX#=ex-sx ;ratio of x movement
  407. Scale3DX=1.0/Scale3DX ;inverted ratio of x movement
  408. Scale3DY#=ey-sy ;ratio of y movement
  409. Scale3DY=1.0/Scale3DY ;inverted ratio of y movement
  410. s3dx#=GraphicsWidth()/-2.0 ; offset the pointer
  411. s3dx=s3dx*Scale3DX
  412. s3dy#=GraphicsHeight()/-2.0 ; offset the pointer
  413. s3dy=s3dy*Scale3DY
  414. CameraProject(camera,s3dx,s3dy,0)
  415. End Function
  416. Function Screen3DX#(x#) ;used to move 3d objects to 2d coordinates
  417. ;(c) 2001 SoftSkull Productions
  418. ret#=S3dX+(x*Scale3dX)
  419. Return ret
  420. End Function
  421. Function Screen3DY#(y#)
  422. ;(c) 2001 SoftSkull Productions
  423. Ret#=S3dY+(y*Scale3dY)
  424. Return Ret
  425. End Function
  426. Function multiwrap#(x#,low#,high#)
  427. range=(high-low)+1
  428. If x>high
  429. x=low+((x-low) Mod range) ;high
  430. Else
  431. If x<low
  432. x=high-(Abs(x) Mod range)+1 ;low
  433. EndIf
  434. EndIf
  435. Return x
  436. End Function
  437. Function softLoadSprite(fname$)
  438. If FileType(fname$)
  439. file=LoadSprite(fname$)
  440. Return file
  441. Else
  442. RuntimeError "Sprite file <" + fname$ + "> not found."
  443. EndIf
  444. End Function
  445. Function addparticles(num,x#,y#,z#,vx#,vy#,vz#,life,life_spread,grvty#,kind)
  446. For t=1 To num
  447. particlecount=particlecount+1
  448. a.particle=New particle
  449. a\x=x
  450. a\y=y
  451. a\z=z
  452. a\vx=vx+Rnd(-0.5,0.5)
  453. a\vy=vy+Rnd(-0.5,0.5)
  454. a\vz=vz+Rnd(-0.5,0.5)
  455. a\life=life-Rnd(life_spread)
  456. a\startlife=a\life
  457. a\red=Rnd(150)+105
  458. a\green=Rnd(150)+105
  459. a\blue=Rnd(50)+100
  460. a\gravity=grvty
  461. a\kind=kind
  462. a\stage=Rnd(5)
  463. Select a\kind
  464. Case 1
  465. a\flare=CopyEntity(sphere)
  466. a\alpha=1.0
  467. PositionEntity a\flare,a\x,a\y,a\z
  468. EntityAlpha a\flare,0.9
  469. EntityColor a\flare,a\red,a\green,a\blue
  470. Default
  471. ;
  472. End Select
  473. Next
  474. End Function
  475. ;move the particles around
  476. Function spiralize(start#,rot#)
  477. radius#=15.0
  478. prate#=14400.0/particlecount
  479. rrate#=radius/particlecount
  480. pos#=start
  481. poz#=0
  482. cx#=0
  483. cy#=0
  484. For p.particle=Each particle
  485. pos=multiwrap(pos+prate,0,359)
  486. pox#=cx+Sin(pos)*radius
  487. poy#=cy+Cos(pos)*radius
  488. poz#=poz#+1
  489. radius=radius-rrate
  490. PositionEntity p\flare,pox,poy,poz
  491. RotateSprite p\flare,-pos+rot
  492. Next
  493. End Function
  494. ;My Thanks to:
  495. ;=-===========================
  496. ; Maths Functions
  497. ;
  498. ; (c)David Bird
  499. ; [email protected]
  500. ;
  501. ; Types Universal
  502. Function GetBearingVector(ent1,ent2)
  503. Bearing\x#=EntityX(ent2)-EntityX(ent1)
  504. Bearing\y#=EntityY(ent2)-EntityY(ent1)
  505. Bearing\z#=EntityZ(ent2)-EntityZ(ent1)
  506. Normalise(Bearing)
  507. End Function
  508. Function Mag#(x#,y#,z#)
  509. Return Sqr(x^2+y^2+z^2)
  510. End Function
  511. Function Normalise(a.vector)
  512. l# = Mag(a\x,a\y,a\z)
  513. a\x=a\x/l
  514. a\y=a\y/l
  515. a\z=a\z/l
  516. Return
  517. End Function