Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Help with setContext() [BUG]

edited January 2012 in Questions Posts: 622

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

Tagged:

Comments

  • SimeonSimeon Admin Mod
    edited January 2012 Posts: 5,386

    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
    
  • Posts: 622

    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.

  • SimeonSimeon Admin Mod
    Posts: 5,386

    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.

  • SimeonSimeon Admin Mod
    Posts: 5,386

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

Sign In or Register to comment.