Log In  

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

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.

P#92059 2021-05-16 23:58

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)

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

P#92070 2021-05-17 06:04

@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.

P#92077 2021-05-17 11:41 ( Edited 2021-05-17 11:41)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2023-12-03 23:18:39 | 0.041s | Q:17