Help with setContext() [BUG]

Can someone post a small example of setContext() ?

been at it for an hour and can’t get the sprite to show anything

Spritely sprite is working fine

EDIT: This is a bug with setContext - it won’t draw on the first frame, we’ll fix this pretty fast. For now I’d recommend wrapping any setContext usage in a separate function and calling that some time while your code is running (i.e. on touch, some time after it’s started, and so on).

function setup()
    i = image(400,400)
end

function draw()
    background(0)

    setContext(i)
    background(0)
    fill(255,0,0)
    ellipse(0,0,200)
    setContext()

    sprite(i, WIDTH/2, HEIGHT/2)
end

Yes, but this doesn’t


function setup()
    i = image(400,400)
    
    done = false
end

function draw()
    background(0)

    if not done then
    setContext(i)
    background(0)
    fill(255,0,0)
    ellipse(0,0,200)
    setContext()
    done = true
    print(done)
    end

    sprite(i, WIDTH/2, HEIGHT/2)
end

Not sure where I went off course here. Maybe I should do that sleep thing I keep hearing about.

I think this is our bug. I must have introduced it sometime between sending out the beta with setContext and removing the file sharing.

This works, but it is highly stupid:

function setup()
    i = image(400,400)
    
    done = 0
end

function draw()
    background(0)

    if done < 2 then
        setContext(i)
        background(0)
        fill(255,0,0)
        ellipse(0,0,200)
        setContext()
        done = done + 1
        print(done)
    end

    sprite(i, WIDTH/2, HEIGHT/2)
end

For some reason the context is broken in the first draw call. We’ll probably do a quick fix for this.

@Ipda41001 figured out what’s causing this bug, and there’s a slightly easier solution if you’d like to draw your textures in setup()

The problem is an OpenGL texture for the image object is not generated until you attempt to draw it (using sprite()) so attempting to setContext(image) with an image you have not yet tried to draw will fail.

For now you can do the following:

function setup()
    i = image(400,400)
    sprite(i,0,0) -- draw the image to make sure it has a valid texture

    setContext(i)
    -- Draw into the image
    setContext()
end

function draw()
    background(0)
    sprite(i, 0, 0) 
end

A bit of a stupid bug. It will be fixed in the next version.