Difference between revisions of "love.run"

(explain code: if love.timer then love.timer.sleep(0.001) end)
(Improve the note about the delay a bit)
Line 160: Line 160:
 
</source>
 
</source>
  
 
+
== Notes ==
=== For what this delay?: ===
+
=== Why is there a delay? ===
 
<source lang="lua">
 
<source lang="lua">
 
if love.timer then love.timer.sleep(0.001) end
 
if love.timer then love.timer.sleep(0.001) end
 
</source>
 
</source>
  
Answer.
+
It does a few useful things:
It does a few things:
 
 
* Limits FPS to 1000 if vsync isn't enabled.
 
* Limits FPS to 1000 if vsync isn't enabled.
 
* Massively reduces CPU usage in many situations (especially with vsync disabled.)
 
* Massively reduces CPU usage in many situations (especially with vsync disabled.)
* gives control back to the OS for a bit, as Plu mentioned.
+
* Gives control back to the OS for a bit.
(https://love2d.org/forums/viewtopic.php?f=4&t=76998)
+
For more information see https://love2d.org/forums/viewtopic.php?f=4&t=76998.
 
== See Also ==
 
== See Also ==
 
* [[parent::love]]
 
* [[parent::love]]

Revision as of 11:41, 26 September 2015

The main function, containing the main loop. A sensible default is used when left out.

Function

Synopsis

love.run( )

Arguments

None.

Returns

Nothing.

Examples

The default function for 0.9.0, 0.9.1, and 0.9.2, used if you don't supply your own.

function love.run()

	if love.math then
		love.math.setRandomSeed(os.time())
		for i=1,3 do love.math.random() end
	end

	if love.event then
		love.event.pump()
	end

	if love.load then love.load(arg) end

	-- We don't want the first frame's dt to include time taken by love.load.
	if love.timer then love.timer.step() end

	local dt = 0

	-- Main loop time.
	while true do
		-- Process events.
		if love.event then
			love.event.pump()
			for e,a,b,c,d in love.event.poll() do
				if e == "quit" then
					if not love.quit or not love.quit() then
						if love.audio then
							love.audio.stop()
						end
						return
					end
				end
				love.handlers[e](a,b,c,d)
			end
		end

		-- Update dt, as we'll be passing it to update
		if love.timer then
			love.timer.step()
			dt = love.timer.getDelta()
		end

		-- Call update and draw
		if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled

		if love.window and love.graphics and love.window.isCreated() then
			love.graphics.clear()
			love.graphics.origin()
			if love.draw then love.draw() end
			love.graphics.present()
		end

		if love.timer then love.timer.sleep(0.001) end
	end

end

The default function for 0.8.0, used if you don't supply your own.

function love.run()

	math.randomseed(os.time())
	math.random() math.random()

	if love.load then love.load(arg) end

	local dt = 0

	-- Main loop time.
	while true do
		-- Process events.
		if love.event then
			love.event.pump()
			for e,a,b,c,d in love.event.poll() do
				if e == "quit" then
					if not love.quit or not love.quit() then
						if love.audio then
							love.audio.stop()
						end
						return
					end
				end
				love.handlers[e](a,b,c,d)
			end
		end

		-- Update dt, as we'll be passing it to update
		if love.timer then
			love.timer.step()
			dt = love.timer.getDelta()
		end

		-- Call update and draw
		if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled
		if love.graphics then
			love.graphics.clear()
			if love.draw then love.draw() end
		end

		if love.timer then love.timer.sleep(0.001) end
		if love.graphics then love.graphics.present() end
	end

end

The default function for 0.7.0, 0.7.1 and 0.7.2, used if you don't supply your own.

function love.run()

	if love.load then love.load(arg) end

	local dt = 0

	-- Main loop time.
	while true do
		if love.timer then
			love.timer.step()
			dt = love.timer.getDelta()
		end
		if love.update then love.update(dt) end -- will pass 0 if love.timer is disabled
		if love.graphics then
			love.graphics.clear()
			if love.draw then love.draw() end
		end

		-- Process events.
		if love.event then
			for e,a,b,c in love.event.poll() do
				if e == "q" then
					if not love.quit or not love.quit() then
						if love.audio then
							love.audio.stop()
						end
						return
					end
				end
				love.handlers[e](a,b,c)
			end
		end

		if love.timer then love.timer.sleep(1) end
		if love.graphics then love.graphics.present() end
	end

end

Notes

Why is there a delay?

if love.timer then love.timer.sleep(0.001) end

It does a few useful things:

  • Limits FPS to 1000 if vsync isn't enabled.
  • Massively reduces CPU usage in many situations (especially with vsync disabled.)
  • Gives control back to the OS for a bit.

For more information see https://love2d.org/forums/viewtopic.php?f=4&t=76998.

See Also


Other Languages