Log In  


Cart #circularthinking-0 | 2021-05-16 | Code ▽ | Embed ▽ | License: CC4-BY-NC-SA
7

In my last game, I included an iris effect to transition between two scenes. I used the midpoint circle algorithm to calculate the edges of the iris. This algo avoids trig and square root functions and so is super fast. Even at 60 frames a second, there's no lag. I decided to make a more generalized version, implemented as a coroutine. I hope you find it useful for your projects.

To use:

  • Copy the iris function from the cartridge above into your own cartridge.

  • When it's time for a transition, assign the coroutine to a variable in your update function: effect = cocreate(iris)

  • In the draw function, call the coroutine: coresume(effect, 1, 128, 1, 15)

The parameters for the coroutine are starting radius, ending radius, step, and color. A positive step indicates by how much to open the iris on each frame. A negative step indicates a shrinking iris. See the code for examples.

7


good basic effect - note that your circle draw routine can fit into much less tokens:

function warp_draw(r)
	local r2=r*r
	for j=0,127 do
		local y=64-j
		local x=sqrt(r2-y*y)
		rectfill(0,j,64-x,j,0)
		rectfill(64+x,j,127,j,0)
	end
end

using few tokens is usually a good quality for code snippets!


@fred72, your approach definitely wins on tokens, but by avoiding the square root function, the mid-point algorithm is significantly less CPU intensive. Also, your function isn't written as a coroutine, which would necessarily add a few more tokens. While the your function is very small, somewhere you need to have code for incrementing r, which should be considered in the token count, since its part of the overhead.

Frankly, I didn't worry much about optimizing for tokens. Perhaps its possible to get the token count down for my coroutine while still sticking to the midpoint algo for speed. If anyone has advice on that, it would be greatly appreciated.



[Please log in to post a comment]