Disappearing canvas/image after first frame

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
notyetawizard
Prole
Posts: 3
Joined: Wed Jul 01, 2015 8:24 pm
Contact:

Disappearing canvas/image after first frame

Post by notyetawizard »

Hey lovers,

I'm working on making a löve client for the AI game vindinium, and the source code for my client can be found on github.

I'm currently having issues with drawing the map. As the map itself never changes, I figured I'd save on resources by drawing it to a canvas on the first turn instead of redrawing it—your bot only gets one second to get it's move to the server, so it's best not to waste time drawing! However, the canvas seems to be going blank (or something), after it is drawn the first time. At first I thought this had something to do with the window re-sizing stuff that clears canvases, so I turned the canvas into an image to protect it ... but it's still disappearing!

Other images seem to be okay, and I have one blinking on and off each turn. Does anyone have any ideas why this might be?

The most relevant portions of the code are below, and there are no other calls made to the draw_map() function or to gfx.map where the image is stored, so I doubt it's an issue with writing over it.

Code: Select all

-- Draw the map to a canvas, as it never changes.
local function draw_map()
    local map_canvas = love.graphics.newCanvas(map.size*gfx.tile_size,
        map.size*gfx.tile_size),
    love.graphics.setCanvas(map_canvas)
    for y = 1, map.size do
        if map[y] then
            for x = 1, map.size do
                if map[y][x] ~= 11 then
                    love.graphics.draw(gfx[0], x*gfx.tile_size,
                        y*(gfx.tile_size-gfx.tile_offset))
                end
            end
        end
    end
    love.graphics.setCanvas()
    gfx.map = love.graphics.newImage(map_canvas:getImageData())
end

---

function love.load()
    assert(type(nbot) == "table", "Settings table is invalid.")
    game.key   = assert(nbot.key, "A key must be provided")
    game.mode  = nbot.mode or "training"
    game.loop  = nbot.loop or random_move
    game.pre_loop = nbot.pre_loop

    assert(valid_modes[game.mode], "Invalid game mode: " .. game.mode)

    -- Initial connection request
    local response = request{key = game.key}
    parse_first_response(response)

    -- Run pre_loop function if availiable
    if game.pre_loop then game.pre_loop() end
end

function love.update()
    if not game.finished then
        local move = game.loop()
        response = request{key = game.key, dir = move}
        parse_response(response)
    else
        love.event.quit()
    end
end

function love.draw()
    -- init the map if it doesn't exist
    if not gfx.map then
        draw_map()
    end
    -- draw the map
    love.graphics.draw(gfx.map, 1, 1)

    -- draw a test sprite every other turn, because I
    -- can't figure out why the map disappears and this doesn't.
    if game.turn % 8 == 0 then
        love.graphics.draw(gfx[10], 24, 25)
    end
end
Preemptive thanks for your help! :awesome:
User avatar
vladgalay
Citizen
Posts: 60
Joined: Sun Jan 29, 2012 4:54 pm
Location: The Motherland

Re: Disappearing canvas/image after first frame

Post by vladgalay »

Try to call draw_map function in love.load, so that texture would be drawn for the first time
User avatar
Ranguna259
Party member
Posts: 911
Joined: Tue Jun 18, 2013 10:58 pm
Location: I'm right next to you

Re: Disappearing canvas/image after first frame

Post by Ranguna259 »

It's hard to know what's happening if you don't provide a .love file but try to do what vladgalav said, run draw_map() on love.load() and see what happens.
LoveDebug- A library that will help you debug your game with an on-screen fully interactive lua console, you can even do code hotswapping :D

Check out my twitter.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Vixii and 206 guests