BackingMode Retained

I am currently (just starting) to work on a pixel - type app. My plan is to make a grid of pixels over the screen, and keep all of them the same except thos which I need to change. Right away I ran into a problem; backingMode(RETAINED) doesn’t seem to work as expected. If I draw the grid, it dissapeared In a fraction of a second as you’d expect with standard backing mode. I’m not setting any background colours. Here’s the code (ps, it’s probabbly a stupid error on my part)


--# Main
-- Pixel Game
displayMode(STANDARD)
-- Use this function to perform your initial setup
function setup()
view=View()
end

-- This function gets called once every frame
function draw()
--view:draw()
end


--# View
View = class()

function View:init(x)
backingMode(RETAINED)
parameter.watch("1/DeltaTime")
self.pixels = vec2(math.ceil(WIDTH/80), math.ceil(HEIGHT/60))
print(self.pixels)
self:setScene()
self.scene = false
end

function View:draw()
if self.scene == false then
--self:setScene() 
fill(75, 255, 0, 255)
for x = 1, 80 do
for y = 1, 60 do
rect(x*self.pixels.x, y*self.pixels.y, self.pixels.x, self.pixels.y)      
end
end 
self.scene = true
end
end
function View:setScene()
fill(75, 255, 0, 255)
for x = 1, 80 do
for y = 1, 60 do
rect(x*self.pixels.x, y*self.pixels.y, self.pixels.x, self.pixels.y)      
end
end
end
function View:touched(touch)
    -- Codea does not automatically call this method
end

@Mr_Ninja - I’m not sure, but by testing, I found that your grid stays on the screen if you draw it from the draw function, eg

function draw()
    --draw background only once
    if not alreadyDrawn then 
        view:draw()
        alreadyDrawn=true
    end
end

Alternatively, you could do without backing mode, and simply draw your grid to an image in memory, which you can sprite at each frame

function View:setScene()
    fill(75, 255, 0, 255)
    self.img=image(WIDTH,HEIGHT)
    setContext(self.img) --start drawing on image
    for x = 1, 80 do
        for y = 1, 60 do
            rect(x*self.pixels.x, y*self.pixels.y, self.pixels.x, self.pixels.y)      
        end
    end
    setContext()
end

--then
function View:draw()
    sprite(self.img,WIDTH/2,HEIGHT/2)
    --the rest of your code
end

@Simeon There’s a problem with backingMode(RETAINED). It doesn’t save the first 11 or 12 frames when a program is started. Run the program below that draws 20 lines of text on the screen. When the program is started, you’ll see sometimes 11 or 12 lines disappear and the rest will stay on the screen. If the program is restarted, then all of the text stays on the screen. @Mr_Ninja There’s probably nothing wrong with your code. I thought it wasn’t acting the way it should so I checked into it a little more and found the RETAINED problem.


displayMode(FULLSCREEN)

function setup()
    backingMode(RETAINED)
    count=0
    fill(255)
end

function draw()
    if count<20 then
        count=count+1
        text(count,WIDTH/2,HEIGHT-count*30)
    end
end

@Mr_Ninja I changed your code a little. It doesn’t work when first started because of the RETAINED problem explained above, but if you do a restart, then it works.


displayMode(STANDARD)

function setup()
    backingMode(RETAINED)
    view=View()
end

function draw()
    view:draw()
end

View = class()

function View:init(x)
    parameter.watch("1/DeltaTime")
    self.pixels = vec2(math.ceil(WIDTH/80), math.ceil(HEIGHT/60))
    self:setScene()
    self.scene = false
end

function View:draw()
    if self.scene == false then
        self:setScene() 
        self.scene = true
    end
end

function View:setScene()
    fill(75, 255, 0, 255)
    for x = 1, 80 do
        for y = 1, 60 do
            rect(x*self.pixels.x, y*self.pixels.y, self.pixels.x, self.pixels.y)      
        end
    end
end

function View:touched(touch)
end

@dave1707 thanks for tracking down the bug. Sounds like it’s taking some time for the gl context to update. Will look into it.

@Simeon Thanks

@Simeon - is this bug related to the issues with WIDTH & HEIGHT not being updated correctly until after a restart as well?

@dave1707 in your example I commented out backingMode(RETAINED) just to see what would happen and strange results occurred as well (some numbers didn’t appear at all) until the program was was restarted.

hi :slight_smile:

I managed to fix the problem myself by adding the following code in my scripts. Enjoy!! :smiley:

function draw()
    
    if ElapsedTime > 0.5 then
       <insert all of your drawing code here>
    end

end