main.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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 text sprite.
  134. %object = new TextSprite();
  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 and size.
  141. %object.Position = "-45 -35";
  142. %object.Size = "5 2.5";
  143. // Set the size.
  144. %object.FontSize = 2.2;
  145. // Set the text alignment.
  146. %object.TextAlignment = Left;
  147. // Set to the nearest layer.
  148. %object.SceneLayer = 0;
  149. // Set a font image.
  150. %object.Font = "ToyAssets:ArialFont";
  151. //Set the overflow Modes
  152. %object.OverflowModeX = "Visible";
  153. %object.OverflowModeY = "Visible";
  154. // Set the blend color.
  155. %object.BlendColor = White;
  156. // Add the sprite to the scene.
  157. SandboxScene.add( %object );
  158. // Update the overlay.
  159. %this.updateSpriteCountOverlay();
  160. }
  161. //-----------------------------------------------------------------------------
  162. function SpriteStressToy::updateSpriteCountOverlay( %this )
  163. {
  164. // Update sprite count overlay.
  165. SpriteStressToy.OverlayFontObject.Text = SpriteStressToy.SpriteCount;
  166. }
  167. //-----------------------------------------------------------------------------
  168. function SpriteStressToy::updateRenderSortMode( %this )
  169. {
  170. // Set the sprite layer sort mode.
  171. SandboxScene.setLayerSortMode( SpriteStressToy.SpriteSceneLayer, SpriteStressToy.RenderSortMode );
  172. // Finish if no composite-sprite object.
  173. if ( !isObject(SpriteStressToy.CompositeSpriteObject) )
  174. return;
  175. // Set the batch sort mode.
  176. SpriteStressToy.CompositeSpriteObject.SetBatchSortMode( SpriteStressToy.RenderSortMode );
  177. }
  178. //-----------------------------------------------------------------------------
  179. function SpriteStressToy::createStaticSprites( %this )
  180. {
  181. // Finish if max sprites reached.
  182. if ( SpriteStressToy.SpriteCount >= SpriteStressToy.MaxSprite )
  183. {
  184. // Stop the timer.
  185. SpriteStressToy.stopTimer();
  186. return;
  187. }
  188. // Create the sprites at the specified rate.
  189. for( %n = 0; %n < SpriteStressToy.SpriteCreateRate; %n++ )
  190. {
  191. // Create the sprite.
  192. %object = new Sprite();
  193. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  194. // The sprite is static i.e. it won't move.
  195. %object.BodyType = static;
  196. // Set the position.
  197. %object.Position = getRandom(SpriteStressToy.MinX, SpriteStressToy.MaxX) SPC getRandom(SpriteStressToy.MinY, SpriteStressToy.MaxY);
  198. // Set to just in-front of the background layer.
  199. %object.SceneLayer = SpriteStressToy.SpriteSceneLayer;
  200. // Set a random scene-layer depth.
  201. %object.SceneLayerDepth = getRandom(-100,100);
  202. // Set the size of the sprite.
  203. %object.Size = SpriteStressToy.SpriteSize;
  204. // Set a random angle if selected.
  205. if ( SpriteStressToy.RandomAngle )
  206. %object.Angle = getRandom(0,360);
  207. // Set the sprite to use an image. This is known as "static" image mode.
  208. %object.Image = "ToyAssets:TD_Knight_CompSprite";
  209. // We don't really need to do this as the frame is set to zero by default.
  210. %object.Frame = getRandom(0,63);
  211. // Add the sprite to the scene.
  212. SandboxScene.add( %object );
  213. // Increase sprite count.
  214. SpriteStressToy.SpriteCount++;
  215. // Finish if max sprites reached.
  216. if ( SpriteStressToy.SpriteCount == SpriteStressToy.MaxSprite )
  217. {
  218. // Update the overlay.
  219. %this.updateSpriteCountOverlay();
  220. // Stop the timer.
  221. SpriteStressToy.stopTimer();
  222. return;
  223. }
  224. }
  225. // Update the overlay.
  226. %this.updateSpriteCountOverlay();
  227. }
  228. //-----------------------------------------------------------------------------
  229. function SpriteStressToy::createAnimatedSprites( %this )
  230. {
  231. // Finish if max sprites reached.
  232. if ( SpriteStressToy.SpriteCount >= SpriteStressToy.MaxSprite )
  233. {
  234. // Stop the timer.
  235. SpriteStressToy.stopTimer();
  236. return;
  237. }
  238. // Create the sprites at the specified rate.
  239. for( %n = 0; %n < SpriteStressToy.SpriteCreateRate; %n++ )
  240. {
  241. // Create the sprite.
  242. %object = new Sprite();
  243. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  244. // The sprite is static i.e. it won't move.
  245. %object.BodyType = static;
  246. // Set the position.
  247. %object.Position = getRandom(SpriteStressToy.MinX, SpriteStressToy.MaxX) SPC getRandom(SpriteStressToy.MinY, SpriteStressToy.MaxY);
  248. // Set to just in-front of the background layer.
  249. %object.SceneLayer = SpriteStressToy.SpriteSceneLayer;
  250. // Set a random scene-layer depth.
  251. %object.SceneLayerDepth = getRandom(-100,100);
  252. // Set the size of the sprite.
  253. %object.Size = SpriteStressToy.SpriteSize;
  254. // Set a random angle if selected.
  255. if ( SpriteStressToy.RandomAngle )
  256. %object.Angle = getRandom(0,360);
  257. // Set the sprite to use an animation. This is known as "animated" image mode.
  258. %object.Animation = "ToyAssets:TD_Knight_MoveSouth";
  259. // Add the sprite to the scene.
  260. SandboxScene.add( %object );
  261. // Increase sprite count.
  262. SpriteStressToy.SpriteCount++;
  263. // Finish if max sprites reached.
  264. if ( SpriteStressToy.SpriteCount == SpriteStressToy.MaxSprite )
  265. {
  266. // Update the overlay.
  267. %this.updateSpriteCountOverlay();
  268. // Stop the timer.
  269. SpriteStressToy.stopTimer();
  270. return;
  271. }
  272. }
  273. // Update the overlay.
  274. %this.updateSpriteCountOverlay();
  275. }
  276. //-----------------------------------------------------------------------------
  277. function SpriteStressToy::createStaticCompositeSprites( %this )
  278. {
  279. // Finish if max sprites reached.
  280. if ( SpriteStressToy.SpriteCount >= SpriteStressToy.MaxSprite )
  281. {
  282. // Stop the timer.
  283. SpriteStressToy.stopTimer();
  284. return;
  285. }
  286. // Do we have a composite sprite yet?
  287. if ( !isObject(SpriteStressToy.CompositeSpriteObject) )
  288. {
  289. // No, so create it.
  290. %composite = new CompositeSprite();
  291. // Set the composite object.
  292. SpriteStressToy.CompositeSpriteObject = %composite;
  293. // The sprite is static i.e. it won't move.
  294. %composite.BodyType = static;
  295. // Set the batch layout mode. We must do this before we add any sprites.
  296. %composite.SetBatchLayout( "off" );
  297. // Set the batch render isolation.
  298. %composite.SetBatchIsolated( SpriteStressToy.RenderSortMode );
  299. // Set to just in-front of the background layer.
  300. %composite.SceneLayer = SpriteStressToy.SpriteSceneLayer;
  301. // Set the batch to not cull because the sprites are always on the screen
  302. // and it's faster and takes less memory.
  303. %composite.setBatchCulling( false );
  304. // Add to the scene.
  305. SandboxScene.add( %composite );
  306. }
  307. // Fetch the composite object.
  308. %composite = SpriteStressToy.CompositeSpriteObject;
  309. // Create the sprites at the specified rate.
  310. for( %n = 0; %n < SpriteStressToy.SpriteCreateRate; %n++ )
  311. {
  312. // Add a sprite with no logical position.
  313. %composite.addSprite();
  314. // Set the sprites location position to a random location.
  315. %composite.setSpriteLocalPosition( getRandom(SpriteStressToy.MinX, SpriteStressToy.MaxX), getRandom(SpriteStressToy.MinY, SpriteStressToy.MaxY) );
  316. // Set a random size.
  317. %composite.setSpriteSize( SpriteStressToy.SpriteSize );
  318. // Set a random angle.
  319. if ( SpriteStressToy.RandomAngle )
  320. %composite.setSpriteAngle( getRandom(0,360) );
  321. // Set the sprite to use an image.
  322. %composite.setSpriteImage( "ToyAssets:TD_Knight_CompSprite", getRandom(0,63) );
  323. // Set a random depth.
  324. %composite.SetSpriteDepth( getRandom( -100, 100 ) );
  325. // Increase sprite count.
  326. SpriteStressToy.SpriteCount++;
  327. // Finish if max sprites reached.
  328. if ( SpriteStressToy.SpriteCount == SpriteStressToy.MaxSprite )
  329. {
  330. // Update the overlay.
  331. %this.updateSpriteCountOverlay();
  332. // Stop the timer.
  333. SpriteStressToy.stopTimer();
  334. return;
  335. }
  336. }
  337. // Update the overlay.
  338. %this.updateSpriteCountOverlay();
  339. }
  340. //-----------------------------------------------------------------------------
  341. function SpriteStressToy::createAnimatedCompositeSprites( %this )
  342. {
  343. // Finish if max sprites reached.
  344. if ( SpriteStressToy.SpriteCount >= SpriteStressToy.MaxSprite )
  345. {
  346. // Stop the timer.
  347. SpriteStressToy.stopTimer();
  348. return;
  349. }
  350. // Do we have a composite sprite yet?
  351. if ( !isObject(SpriteStressToy.CompositeSpriteObject) )
  352. {
  353. // No, so create it.
  354. %composite = new CompositeSprite();
  355. // Set the composite object.
  356. SpriteStressToy.CompositeSpriteObject = %composite;
  357. // The sprite is static i.e. it won't move.
  358. %composite.BodyType = static;
  359. // Set the batch layout mode. We must do this before we add any sprites.
  360. %composite.SetBatchLayout( "off" );
  361. // Set the batch render isolation.
  362. %composite.SetBatchIsolated( SpriteStressToy.RenderSortMode );
  363. // Set to just in-front of the background layer.
  364. %composite.SceneLayer = SpriteStressToy.SpriteSceneLayer;
  365. // Set the batch to not cull because the sprites are always on the screen
  366. // and it's faster and takes less memory.
  367. %composite.setBatchCulling( false );
  368. // Add to the scene.
  369. SandboxScene.add( %composite );
  370. }
  371. // Fetch the composite object.
  372. %composite = SpriteStressToy.CompositeSpriteObject;
  373. // Create the sprites at the specified rate.
  374. for( %n = 0; %n < SpriteStressToy.SpriteCreateRate; %n++ )
  375. {
  376. // Add a sprite with no logical position.
  377. %composite.addSprite();
  378. // Set the sprites location position to a random location.
  379. %composite.setSpriteLocalPosition( getRandom(SpriteStressToy.MinX, SpriteStressToy.MaxX), getRandom(SpriteStressToy.MinY, SpriteStressToy.MaxY) );
  380. // Set a random size.
  381. %composite.setSpriteSize( SpriteStressToy.SpriteSize );
  382. // Set a random angle.
  383. if ( SpriteStressToy.RandomAngle )
  384. %composite.setSpriteAngle( getRandom(0,360) );
  385. // Set the sprite to use an animation.
  386. %composite.setSpriteAnimation( "ToyAssets:TD_Knight_MoveSouth" );
  387. // Set a random depth.
  388. %composite.SetSpriteDepth( getRandom( -100, 100 ) );
  389. // Increase sprite count.
  390. SpriteStressToy.SpriteCount++;
  391. // Finish if max sprites reached.
  392. if ( SpriteStressToy.SpriteCount == SpriteStressToy.MaxSprite )
  393. {
  394. // Update the overlay.
  395. %this.updateSpriteCountOverlay();
  396. // Stop the timer.
  397. SpriteStressToy.stopTimer();
  398. return;
  399. }
  400. }
  401. // Update the overlay.
  402. %this.updateSpriteCountOverlay();
  403. }
  404. //-----------------------------------------------------------------------------
  405. function SpriteStressToy::setSpriteMaximum( %this, %value )
  406. {
  407. SpriteStressToy.MaxSprite = %value;
  408. }
  409. //-----------------------------------------------------------------------------
  410. function SpriteStressToy::setSpriteSize( %this, %value )
  411. {
  412. SpriteStressToy.SpriteSize = %value;
  413. }
  414. //-----------------------------------------------------------------------------
  415. function SpriteStressToy::setSpriteRandomAngle( %this, %value )
  416. {
  417. SpriteStressToy.RandomAngle = %value;
  418. }
  419. //-----------------------------------------------------------------------------
  420. function SpriteStressToy::setSpriteCreatePeriod( %this, %value )
  421. {
  422. SpriteStressToy.SpriteCreatePeriod = %value;
  423. }
  424. //-----------------------------------------------------------------------------
  425. function SpriteStressToy::setRenderMode( %this, %value )
  426. {
  427. SpriteStressToy.RenderMode = %value;
  428. }
  429. //-----------------------------------------------------------------------------
  430. function SpriteStressToy::setRenderSortMode( %this, %value )
  431. {
  432. SpriteStressToy.RenderSortMode = %value;
  433. // Update the render sort mode.
  434. %this.updateRenderSortMode();
  435. }