how to draw text in a limited area of the screen

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
borr
Prole
Posts: 42
Joined: Wed Oct 16, 2019 7:39 pm

how to draw text in a limited area of the screen

Post 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?
Attachments
stencil_text.jpg
stencil_text.jpg (1.98 KiB) Viewed 3759 times
User avatar
slime
Solid Snayke
Posts: 3134
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

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

Post by slime »

love.graphics.printf and Text:setf might do what you want.
borr
Prole
Posts: 42
Joined: Wed Oct 16, 2019 7:39 pm

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

Post 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?
Attachments
printf_text.jpg
printf_text.jpg (3.88 KiB) Viewed 3741 times
User avatar
slime
Solid Snayke
Posts: 3134
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

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

Post 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.
borr
Prole
Posts: 42
Joined: Wed Oct 16, 2019 7:39 pm

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

Post by borr »

Thanks
borr
Prole
Posts: 42
Joined: Wed Oct 16, 2019 7:39 pm

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

Post by borr »

can i add parameters for StencilFunction?
User avatar
keharriso
Citizen
Posts: 98
Joined: Fri Nov 16, 2012 9:34 pm

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

Post 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
LÖVE-Nuklear - a lightweight immediate mode GUI for LÖVE games
borr
Prole
Posts: 42
Joined: Wed Oct 16, 2019 7:39 pm

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

Post by borr »

it works. Thanks
borr
Prole
Posts: 42
Joined: Wed Oct 16, 2019 7:39 pm

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

Post 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?
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 62 guests