SpriteTest.lua 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. local numSprites = 100
  2. local sprites = {}
  3. local speeds = {}
  4. local context = GetContext()
  5. local cache = GetCache()
  6. local engine = GetEngine()
  7. local graphics = GetGraphics()
  8. local ui = GetUI()
  9. function Start()
  10. if engine.headless then
  11. ErrorDialog("SpriteTest", "Headless mode is not supported. The program will now exit.")
  12. engine:Exit()
  13. return
  14. end
  15. InitUI()
  16. InitSprites()
  17. SubscribeToEvent("Update", "HandleUpdate")
  18. SubscribeToEvent("KeyDown", "HandleKeyDown")
  19. end
  20. function Stop()
  21. end
  22. function InitUI()
  23. local uiStyle = cache:GetXMLFile("UI/DefaultStyle.xml")
  24. local debugHud = engine:CreateDebugHud()
  25. debugHud.defaultStyle = uiStyle
  26. debugHud.mode = DEBUGHUD_SHOW_ALL
  27. local console = engine:CreateConsole()
  28. console.defaultStyle = uiStyle
  29. local cursor = Cursor:new(context)
  30. cursor.styleAuto = uiStyle
  31. cursor.position = IntVector2(graphics:GetWidth() / 2, graphics:GetHeight() / 2)
  32. ui.cursor = cursor
  33. if GetPlatform() == "Android" or GetPlatform() == "iOS" then
  34. ui.cursor.visible = false
  35. end
  36. end
  37. function InitSprites()
  38. local decalTex = cache:GetTexture2D("Textures/UrhoDecal.dds")
  39. local width = graphics.width
  40. local height = graphics.height
  41. for i = 1, numSprites do
  42. local sprite = Sprite:new(context)
  43. sprite.texture = decalTex
  44. sprite:SetFullImageRect()
  45. sprite.position = Vector2(Random(width), Random(height))
  46. sprite:SetSize(128, 128)
  47. sprite.hotSpot = IntVector2(64, 64)
  48. sprite.rotation = Random(360)
  49. sprite.scale = Vector2(1, 1) * (Random(1) + 0.5)
  50. sprite:SetColor(Color(Random(0.5) + 0.5, Random(0.5) + 0.5, Random(0.5) + 0.5, 1.0))
  51. sprite.blendMode = BLEND_ADD
  52. ui.root:AddChild(sprite)
  53. table.insert(sprites, sprite)
  54. table.insert(speeds, Vector2(Random(200) - 100, Random(200) - 100))
  55. end
  56. end
  57. function HandleUpdate(eventType, eventData)
  58. local timeStep = eventData:GetFloat("TimeStep")
  59. local width = graphics.width
  60. local height = graphics.height
  61. for i = 1, numSprites do
  62. local sprite = sprites[i]
  63. sprite.rotation = sprite.rotation + timeStep * 30
  64. local newPos = sprite.position
  65. newPos = newPos + speeds[i] * timeStep
  66. if newPos.x >= width then
  67. newPos.x = newPos.x - width
  68. elseif newPos.x < 0 then
  69. newPos.x = newPos.x + width
  70. end
  71. if newPos.y >= height then
  72. newPos.y = newPos.y - height
  73. elseif newPos.y < 0 then
  74. newPos.y = newPos.y + height
  75. end
  76. sprite.position = newPos
  77. end
  78. end
  79. function HandleKeyDown(eventType, eventData)
  80. local key = eventData:GetInt("Key")
  81. if key == KEY_ESC then
  82. if ui.focusElement == nil then
  83. engine:Exit();
  84. else
  85. local console = GetConsole()
  86. console.visible = false
  87. end
  88. end
  89. if key == KEY_F1 then
  90. GetConsole():Toggle()
  91. end
  92. if key == KEY_T then
  93. GetDebugHud():Toggle(DEBUGHUD_SHOW_PROFILER)
  94. end
  95. end