Problems with setContext and off screen drawing

My graphics is getting quite complex and therefore slows the draw loop down to much.
Up till now I have calculated and drawn every component in every draw loop.
So the smart move was to move most of the grahics to setup since it mostly could belong to a backdrop image.

setContext(myImage) is easy to use, but appears to have limits !
Seting the image(1024,768) only produces an image 768x768 ??
Making it a square instead of a rectangular shape does not help either

Trying to use setContext several times for making a tile of images goes well for the first tile (image) but the next one is stretched.
I would imagine that calling setContext() ends the drawing to a specific image and should be ready to accept the next one, with a different name of course. ??

SetContext works very well and as expected for’‘small’’ images. When you start to work with images of size WIDTHxHEIGHT, then you get strange behaviors. I your case it could be that the max image size is limited by displaymode. Try ‘FULLSCREEN’, it may solve it. Myself i still have a problem of x,y shift when i sprite images made that way, and have not found the cause.

Using FULLSCREEN allready.
But the x,y shift explains some of what I see also

I have a plane B though.
Drawing the backdrop in a graphics program (skinman) and sprite it into place.

I have no issue using it with full width*height and have not encountered Jmv38’s bugs.
Can you post some code? The function can very easily get offended and mess up your program :stuck_out_tongue:

Cheers
Xavier

Ha, finally i caught it! The code below show the shift bug with setContext. @Simeon could you please have a look at it? Took me quite a while to isolate, because it goes away when i change almost anything. Unless i have to change my glasses, that’s a bug. I have beta11 on ipad1 and ios5.1. This bug is really annoying because you never know when it will show up…
Just in case everything is ok on your side, this is what i get:
http://www.youtube.com/watch?v=5slGplXY4gw
[edit] i just noticed: all the sprite() of the draw() are not aligned with the text() along the vertical axis… The shift affects all the sprite() drawing no matter setContext or not. But do not affect text()…



--# Main
-- this file always show the image shift bug

function setup()
    resetStyle()resetMatrix()
    img1 = image(120,100)    -- this is a simple red rectangle in blue background
    setContext(img1)
        background(0, 26, 255, 255)
        fill(255, 0, 0, 255)
        rect(5,5,30,30)
    setContext()
    img2 = image(120,100)    -- this is a simple green background
    setContext(img2)
        background(0, 255, 20, 255)
    setContext()
    img3 = image(120,100)    -- image3 is same as img2
    setContext(img3)
        background(0, 255, 20, 255)
    setContext()
    -- now we run the nasty function:
    iCreateAshift()    -- comment this line to see the expected behavior
--    iCreateAshift()    -- surprisingly, calling twice solves the problem!!??
    -- now when we use this simple function to copy the image to another it is shifted
    setImage(img1,img3)
end

function draw()
    background(40, 40, 50)
    text("img1:",WIDTH/2, HEIGHT/8*7)
    sprite(img1,WIDTH/2, HEIGHT/4*3)
    text("img2:",WIDTH/2, HEIGHT/8*5)
    sprite(img2,WIDTH/2, HEIGHT/2)
    text("img3:",WIDTH/2, HEIGHT/8*3)
    sprite(img3,WIDTH/2, HEIGHT/4)
    fill(255, 255, 255, 255)
    text("img3 should be like img1",WIDTH/2, HEIGHT/6)
end

function setImage(out,img)
    resetStyle()resetMatrix()
    setContext(img)
        pushStyle()
        spriteMode(CORNER)
        sprite(out)
        popStyle()
    setContext()
end

function iCreateAshift()
    resetStyle()resetMatrix()
    local w,h = WIDTH*0.19,30
    local img0 = image(w,h)
    setContext(img0)
        background(255, 255, 255, 255)
    setContext()
    
    local img = image(w,h)
    local sMode = spriteMode()
    setContext(img)
    pushStyle() pushMatrix()
        background(0, 0, 0, 255)
        spriteMode(CENTER)
        translate(w/2,h/2)    -- this is the line source of the shift
        sprite(img0,0,0)      -- this line must be there too, the pb does not show up
    popStyle() popMatrix()
    spriteMode(sMode)
    setContext()
    resetStyle() resetMatrix() -- this does not reset the translation
end

Here is a little test code that produces unexpected results on my iPad (3 gen)
Don’t look at the picture itself…it is ugly i Know


function setup()
    displayMode(FULLSCREEN)
    myimage = image(1024,768)
    setContext(myimage)
    fill(155, 76, 76, 255) ellipseMode(CENTER) rectMode(CORNER) 
    strokeWidth(10) stroke(22, 18, 18, 255)
    rect(0,0,1024,768)
    ellipse(512,384,768,768)
    setContext()
end

function draw()
    background(58, 90, 90, 255)
    spriteMode(CENTER)
    sprite(myimage,512,384)
end

macflyerdc - I’m having the issue you mentioned about the second setContext drawing being stretched. I simplified this out and it’s particularly the width that gets stretched for me. I tinkered a bunch and reproduce the same issue with the rect function and ellipse function. It’s like the width is artificially being nearly doubled. The height is slightly bigger too.

I think I figured it out - you can’t do any drawing (even setting up an image for later) in the setup method. When I moved that code to my draw loop (with a flag to ensure it happens only once) everything started working.

Ok, that is interesting. I will try it out.
I have put this on hold until 1.5 (very soon now) because I beleive there is a total rewrite of the setContext function.
However it makes sence that the “drawing” should occour in the draw loop and not elsewhere and a flag is not hard to handle.
Re Peter