A generic love.run() loop?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
pygy
Citizen
Posts: 98
Joined: Mon Jan 25, 2010 4:06 pm

A generic love.run() loop?

Post by pygy »

Several libraries require (or work optimally with) a custom love.run loop.

Pölygamy is quite invasive in this regard since it inserts and swaps callbacks on the fly on state change...

To that end, I've written a custom love.run loop that allows to do it without pain, and it may be useful to extract it and use it as a common base for the various libs.

The main loop is simplified, at the expense of a long preamble :

Code: Select all

	while running do
	
		love.timer.step()
		delta = love.timer.getDelta()
		manageCallbacks()

		-- love.update, love.draw, and more if you want
		for _,cb in ipairs( callbacks ) do
			cb( delta )
		end
 
		-- Process events.
		for e,a,b,c in love.event.poll() do
			love.handlers[e](a,b,c)
		end
 
		love.timer.sleep( 1 )
		love.graphics.present()
    end
The callbacks are ordered according to a table
e.g.: callbackOrder={"polykeyheld", "polytimer", "update", "clear", "draw"}
Callbacks not referenced in the list are inserted before everything else.

The API is:

Polygamy.setCallback(name,callback) -- inserts or swaps a given callback
Polygamy.removeCallback(name)
Polygamy.setTransition(function,parameters enclosed in a tuple) -- see below.

These function don't act immediately, they queue the instructions, that are executed at the beginning of the main loop by manageCallbacks(). the setTransition allows to execute functions at that point too, I use them to run the before and after callbacks in Polygamy.state at a "clean" point in the loop.

The full loop is in the init file of Polygamy 0.4.0, in the Polygamy post in the Project forum

What do you think?

Edit, I just realised that there was a bug in the current implementation, I'll have to use double buffering in the queue.
Hermaphroditism is not a crime. -- LSB Superstar

All code published with this account is licensed under the Romantic WTF public license unless otherwise stated.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 25 guests