| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- local numSprites = 100
- local sprites = {}
- local speeds = {}
- local context = GetContext()
- local cache = GetCache()
- local engine = GetEngine()
- local graphics = GetGraphics()
- local ui = GetUI()
- function Start()
- if engine.headless then
- ErrorDialog("SpriteTest", "Headless mode is not supported. The program will now exit.")
- engine:Exit()
- return
- end
-
- InitUI()
- InitSprites()
-
- SubscribeToEvent("Update", "HandleUpdate")
- SubscribeToEvent("KeyDown", "HandleKeyDown")
- end
- function Stop()
- end
- function InitUI()
- local uiStyle = cache:GetXMLFile("UI/DefaultStyle.xml")
-
- local debugHud = engine:CreateDebugHud()
- debugHud.defaultStyle = uiStyle
- debugHud.mode = DEBUGHUD_SHOW_ALL
-
- local console = engine:CreateConsole()
- console.defaultStyle = uiStyle
-
- local cursor = Cursor:new(context)
- cursor.styleAuto = uiStyle
- cursor.position = IntVector2(graphics:GetWidth() / 2, graphics:GetHeight() / 2)
- ui.cursor = cursor
-
- if GetPlatform() == "Android" or GetPlatform() == "iOS" then
- ui.cursor.visible = false
- end
- end
- function InitSprites()
- local decalTex = cache:GetTexture2D("Textures/UrhoDecal.dds")
-
- local width = graphics.width
- local height = graphics.height
-
- for i = 1, numSprites do
- local sprite = Sprite:new(context)
- sprite.texture = decalTex
- sprite:SetFullImageRect()
- sprite.position = Vector2(Random(width), Random(height))
- sprite:SetSize(128, 128)
- sprite.hotSpot = IntVector2(64, 64)
- sprite.rotation = Random(360)
- sprite.scale = Vector2(1, 1) * (Random(1) + 0.5)
-
- sprite:SetColor(Color(Random(0.5) + 0.5, Random(0.5) + 0.5, Random(0.5) + 0.5, 1.0))
- sprite.blendMode = BLEND_ADD
-
- ui.root:AddChild(sprite)
-
- table.insert(sprites, sprite)
- table.insert(speeds, Vector2(Random(200) - 100, Random(200) - 100))
- end
- end
- function HandleUpdate(eventType, eventData)
- local timeStep = eventData:GetFloat("TimeStep")
-
- local width = graphics.width
- local height = graphics.height
-
- for i = 1, numSprites do
- local sprite = sprites[i]
- sprite.rotation = sprite.rotation + timeStep * 30
-
- local newPos = sprite.position
- newPos = newPos + speeds[i] * timeStep
-
- if newPos.x >= width then
- newPos.x = newPos.x - width
- elseif newPos.x < 0 then
- newPos.x = newPos.x + width
- end
- if newPos.y >= height then
- newPos.y = newPos.y - height
- elseif newPos.y < 0 then
- newPos.y = newPos.y + height
- end
- sprite.position = newPos
- end
- end
- function HandleKeyDown(eventType, eventData)
- local key = eventData:GetInt("Key")
-
- if key == KEY_ESC then
- if ui.focusElement == nil then
- engine:Exit();
- else
- local console = GetConsole()
- console.visible = false
- end
- end
-
- if key == KEY_F1 then
- GetConsole():Toggle()
- end
-
- if key == KEY_T then
- GetDebugHud():Toggle(DEBUGHUD_SHOW_PROFILER)
- end
- end
|