-- $LastChangedDate: 2008-12-26 21:33:34 -0500 (Fri, 26 Dec 2008) $
-- This chunk is called every timer-tick.
-- See module.txt for documentation and pitfalls.

-- NOP if controlled by trackball.
if ( sim.argvMap["-t"] ) then
	return
end

-- Call registered timer-tick callbacks.
Timer:Tick()

--------------------------------------------------------------------------------
-- The main timer-tick handler.

-- Keyboard handler.
local keyEvent = sim.Keyboard:DequeueKey()
if ( keyEvent ) then
	Keyboard:Handler( keyEvent )
end

-- Joystick handler.
Joystick:Handler()

-- Update sounds.
Sound:Tick()

-- Process commands from the simulator's "command queue".
CommandQueue:Process()  -- cmd.lua

-- CRITICAL: Collect garbage to induce freeing memory of 3D models
-- (which consume large amounts of memory).
if ( (not garbageCollectionTime) or (sim.Program:GetElapsedTime() - garbageCollectionTime > 500) ) then
	garbageCollectionTime = sim.Program:GetElapsedTime()  -- milliseconds
	collectgarbage()
end
