Drawing only once

Hello all again,

Today I came here with a small issue, it’s about drawing into Codea.

I need to draw some elements once, not every frame.

For a better understand I’ll show you a sample code:

-- Lines

function setup()
    displayMode(FULLSCREEN)
    strokeWidth(2)
    stroke(0, 0, 0, 255)
    for i = 0 , 60 do
        line(i*20, HEIGHT, i*20, 0)
        line(0, i*20, WIDTH, i*20)
    end
end

function draw()
    background(255, 255, 255, 255)
end

```

I need to draw these lines one time. If these lines are drawn in draw(), it will affect the performance. 
I tried a trick. But, the screen blinks continuously. Here's the code:
-- Lines

function setup()
    displayMode(FULLSCREEN)
    background(255, 255, 255, 255)
    strokeWidth(2)
    stroke(0, 0, 0, 255)
    for i = 0 , 60 do
        line(i*20, HEIGHT, i*20, 0)
        line(0, i*20, WIDTH, i*20)
    end
end

function draw()
    --background(255, 255, 255, 255)
end

```

The solution? Possibility to drawing in setup() or Adding a new function called drawOnce(). 
Maybe I can already do this in Codea, but maybe I don't know how.
So, Please help me to solve this problem.

Just put a ‘backingMode(RETAINED)‘ and work some magic

Does not work. The screen is black.

And in any case, if I use backingMode(RETAINED), this would affect my project.

Other ideas? Other solutions?

I think you can not draw in setup. Try something along the lines of


local setting_up = true

...

function draw()
  if setup then
    -- draw here
    setting_up = false
  end
  ...
end

This should work @Georgian:

-- Lines
displayMode(FULLSCREEN) 

function setup()
    backingMode(RETAINED)
    background(255, 255, 255, 255)
    strokeWidth(2)
    stroke(0, 0, 0, 255)
    for i = 0 , 60 do
        line(i*20, HEIGHT, i*20, 0)
        line(0, i*20, WIDTH, i*20)
    end
end
 
function draw()

end

```


Using RETAINED backing *and* moving the displayMode call outside of setup() allows Codea to initialise the display immediately in fullscreen mode, and prevents it from clearing any drawing that you do in setup().

@Simeon, it works! But, backingMode(RETAINED) affect my project.

I need to use two kinds of drawings, one time drawing and second: real time drawing.

To understand, see this code:

-- Lines
displayMode(FULLSCREEN)
function setup()
    backingMode(RETAINED)
    background(255, 255, 255, 255)
    strokeWidth(2)
    stroke(0, 0, 0, 255)
    for i = 0 , 60 do
        line(i*20, HEIGHT, i*20, 0)
        line(0, i*20, WIDTH, i*20)
    end
end

function draw()
    noStroke()

    if CurrentTouch.state == BEGAN then
        fill(16, 178, 197, 255)
    elseif CurrentTouch.state == MOVING then
        fill(255, 0, 0, 255)
    elseif CurrentTouch.state == ENDED then
        fill(210, 218, 16, 255)
    end

    ellipse(CurrentTouch.x, CurrentTouch.y, 100,100)
end

```

backingMode(RETAINED) affect my project, see the result: 
The result

In that case, I would suggest rendering the lines to an image, then rendering the image once per frame.

So something like this:

-- Lines
displayMode(FULLSCREEN) 
 
function setup()
    backgroundImage = image( WIDTH, HEIGHT )

    setContext(backgroundImage)
    background(255, 255, 255, 255)
    strokeWidth(2)
    stroke(0, 0, 0, 255)
    for i = 0 , 60 do
        line(i*20, HEIGHT, i*20, 0)
        line(0, i*20, WIDTH, i*20)
    end
    setContext()
end
  
function draw()
    sprite( backgroundImage, WIDTH/2, HEIGHT/2 )

    -- Draw the rest of your scene
end

```

Yeah, Simeon’s logic is sound. Drawing an image is fast, and at the top of draw() it’ll work as your background.

@Simeon, it works fine! () Thanks! ()