3

In love2d, the contents of the screen are reset between the draw calls. So that I cannot add something to the screen created during the last draw operation, for example, print another line below the line printed during the previous iteration.

How can I do that with the love game engine. Specifically I have a debug area alongside the main game area, and I want to print the logged messages in that window.

Other use cases are drawing some effects over the game area when the player wins(or looses), blurring the background when a dialog is displayed.

saga
  • 1,933
  • 2
  • 17
  • 44
  • The game-engine tag wiki says to only use it when developing your own engine or when the engine lacks its own tag. Love2d has its own tag. – code11 Jul 06 '18 at 16:30
  • @code11 right. Question edited accordingly – saga Jul 06 '18 at 16:38

2 Answers2

2

Love2ds default loop, which calls love.draw, automatically clears the screen. see https://love2d.org/wiki/love.run

To avoid this you can use your own run function and remove love.graphics.clear() or you can use a canvas https://love2d.org/wiki/Canvas.

Luke100000
  • 1,395
  • 2
  • 6
  • 18
0

I don't know about a visual debug console. However, manipulating pixels during draw calls can be done using some sort of Shader approach. Here is a sample of a Gaussian Blur pixel effect:

local graphics = love.graphics    
function love.load()    
    local program = ([[
        const float kernel[5] = float[](0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162);
        vec4 effect(vec4 color, sampler2D tex, vec2 tex_coords, vec2 pos) {
            color = texture2D(tex, tex_coords) * kernel[0];
            for(int i = 1; i < 5; i++) {
                color += texture2D(tex, vec2(tex_coords.x + i * %f, tex_coords.y)) * kernel[i];
                color += texture2D(tex, vec2(tex_coords.x - i * %f, tex_coords.y)) * kernel[i];
            }
            return color;
        }
    ]]):format(1 / graphics.getWidth(), 1 / graphics.getWidth())
    fx = graphics.newPixelEffect(program)

    local program = ([[
        const float kernel[5] = float[](0.2270270270, 0.1945945946, 0.1216216216, 0.0540540541, 0.0162162162);
        vec4 effect(vec4 color, sampler2D tex, vec2 tex_coords, vec2 pos) {
            color = texture2D(tex, tex_coords) * kernel[0];
            for(int i = 1; i < 5; i++) {
                color += texture2D(tex, vec2(tex_coords.x, tex_coords.y + i * %f)) * kernel[i];
                color += texture2D(tex, vec2(tex_coords.x, tex_coords.y - i * %f)) * kernel[i];
            }
            return color;
        }
    ]]):format(1 / graphics.getHeight(), 1 / graphics.getHeight())
    fy = graphics.newPixelEffect(program)


    print(fx:getWarnings())
    print(fy:getWarnings())


    canvas_x = graphics.newCanvas(graphics.width, graphics.height)
    canvas_y = graphics.newCanvas(graphics.width, graphics.height)
end

t = 0

function love.draw()
    t = t + 0.02
    local x = 400 + math.sin(t) * 400
    local y = 300 + math.sin(t * 0.8) * 300    

    graphics.setCanvas(canvas_x)
    graphics.push()
    graphics.translate(x, y)
    graphics.rotate(t * 1.3)
    graphics.rectangle("fill", -10, -50, 20, 100)
    graphics.pop()    

    graphics.setPixelEffect(fx)
    graphics.setCanvas(canvas_y)
    graphics.draw(canvas_x, 0, 0)

    graphics.setPixelEffect(fy)
    graphics.setCanvas(canvas_x)
    graphics.draw(canvas_y, 0, 0)

    graphics.setPixelEffect()
    graphics.setCanvas()
    graphics.draw(canvas_x, 0, 0)
end
wp78de
  • 18,207
  • 7
  • 43
  • 71