Page 1 of 1

how to draw text in a limited area of the screen

Posted: Sun Oct 09, 2022 1:48 pm
by borr
i want to limit the text to a given area
the only method i found along with stencil

Code: Select all

local function myStencilFunction()
    love.graphics.rectangle("fill", 100, s_height/2, 95, 30)
end

function love.load()
     text_obj = love.graphics.newText( font, "This text is aligned center")
end

function love.draw()
    love.graphics.stencil(myStencilFunction, "replace", 1)
    love.graphics.setStencilTest("greater", 0)
    love.graphics.draw( text_obj, 100, s_height/2 )
    love.graphics.setStencilTest()
end
are there any more productive methods for this?

Re: how to draw text in a limited area of the screen

Posted: Sun Oct 09, 2022 3:19 pm
by slime
love.graphics.printf and Text:setf might do what you want.

Re: how to draw text in a limited area of the screen

Posted: Sun Oct 09, 2022 3:32 pm
by borr

Code: Select all

love.graphics.printf("This text is aligned center", s_width/2, s_height/2, 200,"center")
thanks for your reply.
but that's not what I want. I want the text outside of its area to be cut off
am I doing something wrong with the printf?

Re: how to draw text in a limited area of the screen

Posted: Sun Oct 09, 2022 3:35 pm
by slime
If you want text to be clipped instead of wrapping, love.graphics.setScissor or stencils (as you've already discovered) are two good tools for it.

Re: how to draw text in a limited area of the screen

Posted: Sun Oct 09, 2022 3:43 pm
by borr
Thanks

Re: how to draw text in a limited area of the screen

Posted: Sun Oct 09, 2022 4:01 pm
by borr
can i add parameters for StencilFunction?

Re: how to draw text in a limited area of the screen

Posted: Sun Oct 09, 2022 5:54 pm
by keharriso
borr wrote: Sun Oct 09, 2022 4:01 pm can i add parameters for StencilFunction?
Maybe try returning a stencil function from a generator?

Code: Select all

local function generateStencil(x, y, width, height)
    return function ()
        love.graphics.rectangle("fill", x, y, width, height)
    end
end

function love.draw()
    love.graphics.stencil(generateStencil(100, 100, 95, 30), "replace", 1)
    ...
end

Re: how to draw text in a limited area of the screen

Posted: Sun Oct 09, 2022 6:37 pm
by borr
it works. Thanks

Re: how to draw text in a limited area of the screen

Posted: Sun Oct 09, 2022 7:43 pm
by borr
love.graphics.setScissor( s_width/2 - text_obj:getWidth()/2, s_height / 2 - text_obj:getHeight() / 2, 95, 30 )
love.graphics.clear(0, 0, 0, 0)

clear with alpha 0, does not work without canvas?