main.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. function SpriteStressToy::create( %this )
  23. {
  24. // Set the sandbox drag mode availability.
  25. Sandbox.allowManipulation( pan );
  26. // Set the manipulation mode.
  27. Sandbox.useManipulation( pan );
  28. // Turn-off the full metrics.
  29. setMetricsOption( false );
  30. // Turn-on the FPS metrics only.
  31. setFPSMetricsOption( true );
  32. // Configure the toy.
  33. SpriteStressToy.SpriteSceneLayer = 10;
  34. SpriteStressToy.MaxSprite = 100;
  35. SpriteStressToy.SpriteCreateRate = 50;
  36. SpriteStressToy.SpriteCreatePeriod = 100;
  37. SpriteStressToy.SpriteSize = 8;
  38. SpriteStressToy.RandomAngle = false;
  39. SpriteStressToy.RenderMode = "Static";
  40. SpriteStressToy.RenderSortMode = "off";
  41. // Add the configuration options.
  42. addNumericOption("Sprite Maximum", 1, 100000, 100, "setSpriteMaximum", SpriteStressToy.MaxSprite, true, "Sets the maximum number of sprites to create." );
  43. addNumericOption("Sprite Size", 1, 75, 1, "setSpriteSize", SpriteStressToy.SpriteSize, true, "Sets the size of the sprites to create." );
  44. addFlagOption("Random Angle", "setSpriteRandomAngle", SpriteStressToy.RandomAngle, true, "Whether to place the sprites at a random angle or not." );
  45. addNumericOption("Sprite Create Period (ms)", 10, 1000, 10, "setSpriteCreatePeriod", SpriteStressToy.SpriteCreatePeriod, true, "Sets the time interval for creating bursts of sprites. Bigger values create the maximum sprites quicker." );
  46. addSelectionOption( "Static,Static (Composite),Animated,Animated (Composite)", "Render Mode", 4, "setRenderMode", true, "Sets the type of object used in the stress test." );
  47. addSelectionOption( "Off,New,Old,X,Y,Z,-X,-Y,-Z", "Render Sort Mode", 9, "setRenderSortMode", false, "Sets the render sorting mode controlling the order of rendering." );
  48. // Reset the toy.
  49. SpriteStressToy.reset();
  50. }
  51. //-----------------------------------------------------------------------------
  52. function SpriteStressToy::destroy( %this )
  53. {
  54. }
  55. //-----------------------------------------------------------------------------
  56. function SpriteStressToy::reset( %this )
  57. {
  58. // Reset sprite count.
  59. SpriteStressToy.SpriteCount = 0;
  60. // Calculate sprite bounds.
  61. SpriteStressToy.HalfSize = SpriteStressToy.SpriteSize * 0.5;
  62. SpriteStressToy.MinX = -50 + SpriteStressToy.HalfSize;
  63. SpriteStressToy.MaxX = 50 - SpriteStressToy.HalfSize;
  64. SpriteStressToy.MinY = -37.5 + SpriteStressToy.HalfSize;
  65. SpriteStressToy.MaxY = 37.5 - SpriteStressToy.HalfSize;
  66. // Clear the scene.
  67. SandboxScene.clear();
  68. // Update the render sort mode.
  69. %this.updateRenderSortMode();
  70. // Create background.
  71. %this.createBackground();
  72. // Create sprite count overlay.
  73. %this.createSpriteCountOverlay();
  74. // Handle static sprites.
  75. if ( SpriteStressToy.RenderMode $= "Static" )
  76. {
  77. // Create the static sprites.
  78. SpriteStressToy.startTimer( "createStaticSprites", SpriteStressToy.SpriteCreatePeriod );
  79. return;
  80. }
  81. // Handle static composite sprites.
  82. if ( SpriteStressToy.RenderMode $= "Static (Composite)" )
  83. {
  84. // Create the static composite sprites.
  85. SpriteStressToy.startTimer( "createStaticCompositeSprites", SpriteStressToy.SpriteCreatePeriod );
  86. return;
  87. }
  88. // Handle animated sprites.
  89. if ( SpriteStressToy.RenderMode $= "Animated" )
  90. {
  91. // Create the animated sprites.
  92. SpriteStressToy.startTimer( "createAnimatedSprites", SpriteStressToy.SpriteCreatePeriod );
  93. return;
  94. }
  95. // Handle animated composite sprites.
  96. if ( SpriteStressToy.RenderMode $= "Animated (Composite)" )
  97. {
  98. // Create the animated composite sprites.
  99. SpriteStressToy.startTimer( "createAnimatedCompositeSprites", SpriteStressToy.SpriteCreatePeriod );
  100. return;
  101. }
  102. // Error.
  103. error( "SpriteStressToy::reset() - Unknown render mode." );
  104. }
  105. //-----------------------------------------------------------------------------
  106. function SpriteStressToy::createBackground( %this )
  107. {
  108. // Create the sprite.
  109. %object = new Scroller();
  110. // Set the sprite as "static" so it is not affected by gravity.
  111. %object.setBodyType( static );
  112. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  113. // Set the position.
  114. %object.Position = "0 0";
  115. // Set the size.
  116. %object.Size = "100 75";
  117. // Set to the furthest background layer.
  118. %object.SceneLayer = 31;
  119. // Set an image.
  120. %object.Image = "ToyAssets:checkered";
  121. %object.ScrollX = 4;
  122. %object.ScrollY = 3;
  123. %object.RepeatX = 4;
  124. %object.RepeatY = 3;
  125. // Set the blend color.
  126. %object.BlendColor = LightSteelBlue;
  127. // Add the sprite to the scene.
  128. SandboxScene.add( %object );
  129. }
  130. //-----------------------------------------------------------------------------
  131. function SpriteStressToy::createSpriteCountOverlay( %this )
  132. {
  133. // Create the image font.
  134. %object = new ImageFont();
  135. // Set the overlay font object.
  136. SpriteStressToy.OverlayFontObject = %object;
  137. // Set the sprite as "static" so it is not affected by gravity.
  138. %object.setBodyType( static );
  139. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  140. // Set the position.
  141. %object.Position = "-50 -35";
  142. // Set the size.
  143. %object.FontSize = 2;
  144. // Set the text alignment.
  145. %object.TextAlignment = Left;
  146. // Set to the nearest layer.
  147. %object.SceneLayer = 0;
  148. // Set a font image.
  149. %object.Image = "ToyAssets:fancyFont";
  150. // Set the blend color.
  151. %object.BlendColor = White;
  152. // Add the sprite to the scene.
  153. SandboxScene.add( %object );
  154. // Update the overlay.
  155. %this.updateSpriteCountOverlay();
  156. }
  157. //-----------------------------------------------------------------------------
  158. function SpriteStressToy::updateSpriteCountOverlay( %this )
  159. {
  160. // Update sprite count overlay.
  161. SpriteStressToy.OverlayFontObject.Text = SpriteStressToy.SpriteCount;
  162. }
  163. //-----------------------------------------------------------------------------
  164. function SpriteStressToy::updateRenderSortMode( %this )
  165. {
  166. // Set the sprite layer sort mode.
  167. SandboxScene.setLayerSortMode( SpriteStressToy.SpriteSceneLayer, SpriteStressToy.RenderSortMode );
  168. // Finish if no composite-sprite object.
  169. if ( !isObject(SpriteStressToy.CompositeSpriteObject) )
  170. return;
  171. // Set the batch sort mode.
  172. SpriteStressToy.CompositeSpriteObject.SetBatchSortMode( SpriteStressToy.RenderSortMode );
  173. }
  174. //-----------------------------------------------------------------------------
  175. function SpriteStressToy::createStaticSprites( %this )
  176. {
  177. // Finish if max sprites reached.
  178. if ( SpriteStressToy.SpriteCount >= SpriteStressToy.MaxSprite )
  179. {
  180. // Stop the timer.
  181. SpriteStressToy.stopTimer();
  182. return;
  183. }
  184. // Create the sprites at the specified rate.
  185. for( %n = 0; %n < SpriteStressToy.SpriteCreateRate; %n++ )
  186. {
  187. // Create the sprite.
  188. %object = new Sprite();
  189. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  190. // The sprite is static i.e. it won't move.
  191. %object.BodyType = static;
  192. // Set the position.
  193. %object.Position = getRandom(SpriteStressToy.MinX, SpriteStressToy.MaxX) SPC getRandom(SpriteStressToy.MinY, SpriteStressToy.MaxY);
  194. // Set to just in-front of the background layer.
  195. %object.SceneLayer = SpriteStressToy.SpriteSceneLayer;
  196. // Set a random scene-layer depth.
  197. %object.SceneLayerDepth = getRandom(-100,100);
  198. // Set the size of the sprite.
  199. %object.Size = SpriteStressToy.SpriteSize;
  200. // Set a random angle if selected.
  201. if ( SpriteStressToy.RandomAngle )
  202. %object.Angle = getRandom(0,360);
  203. // Set the sprite to use an image. This is known as "static" image mode.
  204. %object.Image = "ToyAssets:TD_Knight_CompSprite";
  205. // We don't really need to do this as the frame is set to zero by default.
  206. %object.Frame = getRandom(0,63);
  207. // Add the sprite to the scene.
  208. SandboxScene.add( %object );
  209. // Increase sprite count.
  210. SpriteStressToy.SpriteCount++;
  211. // Finish if max sprites reached.
  212. if ( SpriteStressToy.SpriteCount == SpriteStressToy.MaxSprite )
  213. {
  214. // Update the overlay.
  215. %this.updateSpriteCountOverlay();
  216. // Stop the timer.
  217. SpriteStressToy.stopTimer();
  218. return;
  219. }
  220. }
  221. // Update the overlay.
  222. %this.updateSpriteCountOverlay();
  223. }
  224. //-----------------------------------------------------------------------------
  225. function SpriteStressToy::createAnimatedSprites( %this )
  226. {
  227. // Finish if max sprites reached.
  228. if ( SpriteStressToy.SpriteCount >= SpriteStressToy.MaxSprite )
  229. {
  230. // Stop the timer.
  231. SpriteStressToy.stopTimer();
  232. return;
  233. }
  234. // Create the sprites at the specified rate.
  235. for( %n = 0; %n < SpriteStressToy.SpriteCreateRate; %n++ )
  236. {
  237. // Create the sprite.
  238. %object = new Sprite();
  239. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  240. // The sprite is static i.e. it won't move.
  241. %object.BodyType = static;
  242. // Set the position.
  243. %object.Position = getRandom(SpriteStressToy.MinX, SpriteStressToy.MaxX) SPC getRandom(SpriteStressToy.MinY, SpriteStressToy.MaxY);
  244. // Set to just in-front of the background layer.
  245. %object.SceneLayer = SpriteStressToy.SpriteSceneLayer;
  246. // Set a random scene-layer depth.
  247. %object.SceneLayerDepth = getRandom(-100,100);
  248. // Set the size of the sprite.
  249. %object.Size = SpriteStressToy.SpriteSize;
  250. // Set a random angle if selected.
  251. if ( SpriteStressToy.RandomAngle )
  252. %object.Angle = getRandom(0,360);
  253. // Set the sprite to use an animation. This is known as "animated" image mode.
  254. %object.Animation = "ToyAssets:TD_Knight_MoveSouth";
  255. // Add the sprite to the scene.
  256. SandboxScene.add( %object );
  257. // Increase sprite count.
  258. SpriteStressToy.SpriteCount++;
  259. // Finish if max sprites reached.
  260. if ( SpriteStressToy.SpriteCount == SpriteStressToy.MaxSprite )
  261. {
  262. // Update the overlay.
  263. %this.updateSpriteCountOverlay();
  264. // Stop the timer.
  265. SpriteStressToy.stopTimer();
  266. return;
  267. }
  268. }
  269. // Update the overlay.
  270. %this.updateSpriteCountOverlay();
  271. }
  272. //-----------------------------------------------------------------------------
  273. function SpriteStressToy::createStaticCompositeSprites( %this )
  274. {
  275. // Finish if max sprites reached.
  276. if ( SpriteStressToy.SpriteCount >= SpriteStressToy.MaxSprite )
  277. {
  278. // Stop the timer.
  279. SpriteStressToy.stopTimer();
  280. return;
  281. }
  282. // Do we have a composite sprite yet?
  283. if ( !isObject(SpriteStressToy.CompositeSpriteObject) )
  284. {
  285. // No, so create it.
  286. %composite = new CompositeSprite();
  287. // Set the composite object.
  288. SpriteStressToy.CompositeSpriteObject = %composite;
  289. // The sprite is static i.e. it won't move.
  290. %composite.BodyType = static;
  291. // Set the batch layout mode. We must do this before we add any sprites.
  292. %composite.SetBatchLayout( "off" );
  293. // Set the batch render isolation.
  294. %composite.SetBatchIsolated( SpriteStressToy.RenderSortMode );
  295. // Set to just in-front of the background layer.
  296. %composite.SceneLayer = SpriteStressToy.SpriteSceneLayer;
  297. // Set the batch to not cull because the sprites are always on the screen
  298. // and it's faster and takes less memory.
  299. %composite.setBatchCulling( false );
  300. // Add to the scene.
  301. SandboxScene.add( %composite );
  302. }
  303. // Fetch the composite object.
  304. %composite = SpriteStressToy.CompositeSpriteObject;
  305. // Create the sprites at the specified rate.
  306. for( %n = 0; %n < SpriteStressToy.SpriteCreateRate; %n++ )
  307. {
  308. // Add a sprite with no logical position.
  309. %composite.addSprite();
  310. // Set the sprites location position to a random location.
  311. %composite.setSpriteLocalPosition( getRandom(SpriteStressToy.MinX, SpriteStressToy.MaxX), getRandom(SpriteStressToy.MinY, SpriteStressToy.MaxY) );
  312. // Set a random size.
  313. %composite.setSpriteSize( SpriteStressToy.SpriteSize );
  314. // Set a random angle.
  315. if ( SpriteStressToy.RandomAngle )
  316. %composite.setSpriteAngle( getRandom(0,360) );
  317. // Set the sprite to use an image.
  318. %composite.setSpriteImage( "ToyAssets:TD_Knight_CompSprite", getRandom(0,63) );
  319. // Set a random depth.
  320. %composite.SetSpriteDepth( getRandom( -100, 100 ) );
  321. // Increase sprite count.
  322. SpriteStressToy.SpriteCount++;
  323. // Finish if max sprites reached.
  324. if ( SpriteStressToy.SpriteCount == SpriteStressToy.MaxSprite )
  325. {
  326. // Update the overlay.
  327. %this.updateSpriteCountOverlay();
  328. // Stop the timer.
  329. SpriteStressToy.stopTimer();
  330. return;
  331. }
  332. }
  333. // Update the overlay.
  334. %this.updateSpriteCountOverlay();
  335. }
  336. //-----------------------------------------------------------------------------
  337. function SpriteStressToy::createAnimatedCompositeSprites( %this )
  338. {
  339. // Finish if max sprites reached.
  340. if ( SpriteStressToy.SpriteCount >= SpriteStressToy.MaxSprite )
  341. {
  342. // Stop the timer.
  343. SpriteStressToy.stopTimer();
  344. return;
  345. }
  346. // Do we have a composite sprite yet?
  347. if ( !isObject(SpriteStressToy.CompositeSpriteObject) )
  348. {
  349. // No, so create it.
  350. %composite = new CompositeSprite();
  351. // Set the composite object.
  352. SpriteStressToy.CompositeSpriteObject = %composite;
  353. // The sprite is static i.e. it won't move.
  354. %composite.BodyType = static;
  355. // Set the batch layout mode. We must do this before we add any sprites.
  356. %composite.SetBatchLayout( "off" );
  357. // Set the batch render isolation.
  358. %composite.SetBatchIsolated( SpriteStressToy.RenderSortMode );
  359. // Set to just in-front of the background layer.
  360. %composite.SceneLayer = SpriteStressToy.SpriteSceneLayer;
  361. // Set the batch to not cull because the sprites are always on the screen
  362. // and it's faster and takes less memory.
  363. %composite.setBatchCulling( false );
  364. // Add to the scene.
  365. SandboxScene.add( %composite );
  366. }
  367. // Fetch the composite object.
  368. %composite = SpriteStressToy.CompositeSpriteObject;
  369. // Create the sprites at the specified rate.
  370. for( %n = 0; %n < SpriteStressToy.SpriteCreateRate; %n++ )
  371. {
  372. // Add a sprite with no logical position.
  373. %composite.addSprite();
  374. // Set the sprites location position to a random location.
  375. %composite.setSpriteLocalPosition( getRandom(SpriteStressToy.MinX, SpriteStressToy.MaxX), getRandom(SpriteStressToy.MinY, SpriteStressToy.MaxY) );
  376. // Set a random size.
  377. %composite.setSpriteSize( SpriteStressToy.SpriteSize );
  378. // Set a random angle.
  379. if ( SpriteStressToy.RandomAngle )
  380. %composite.setSpriteAngle( getRandom(0,360) );
  381. // Set the sprite to use an animation.
  382. %composite.setSpriteAnimation( "ToyAssets:TD_Knight_MoveSouth" );
  383. // Set a random depth.
  384. %composite.SetSpriteDepth( getRandom( -100, 100 ) );
  385. // Increase sprite count.
  386. SpriteStressToy.SpriteCount++;
  387. // Finish if max sprites reached.
  388. if ( SpriteStressToy.SpriteCount == SpriteStressToy.MaxSprite )
  389. {
  390. // Update the overlay.
  391. %this.updateSpriteCountOverlay();
  392. // Stop the timer.
  393. SpriteStressToy.stopTimer();
  394. return;
  395. }
  396. }
  397. // Update the overlay.
  398. %this.updateSpriteCountOverlay();
  399. }
  400. //-----------------------------------------------------------------------------
  401. function SpriteStressToy::setSpriteMaximum( %this, %value )
  402. {
  403. SpriteStressToy.MaxSprite = %value;
  404. }
  405. //-----------------------------------------------------------------------------
  406. function SpriteStressToy::setSpriteSize( %this, %value )
  407. {
  408. SpriteStressToy.SpriteSize = %value;
  409. }
  410. //-----------------------------------------------------------------------------
  411. function SpriteStressToy::setSpriteRandomAngle( %this, %value )
  412. {
  413. SpriteStressToy.RandomAngle = %value;
  414. }
  415. //-----------------------------------------------------------------------------
  416. function SpriteStressToy::setSpriteCreatePeriod( %this, %value )
  417. {
  418. SpriteStressToy.SpriteCreatePeriod = %value;
  419. }
  420. //-----------------------------------------------------------------------------
  421. function SpriteStressToy::setRenderMode( %this, %value )
  422. {
  423. SpriteStressToy.RenderMode = %value;
  424. }
  425. //-----------------------------------------------------------------------------
  426. function SpriteStressToy::setRenderSortMode( %this, %value )
  427. {
  428. SpriteStressToy.RenderSortMode = %value;
  429. // Update the render sort mode.
  430. %this.updateRenderSortMode();
  431. }