Problem overriding love.graphics.print

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.
User avatar
BlackBulletIV
Inner party member
Posts: 1261
Joined: Wed Dec 29, 2010 8:19 pm
Location: Queensland, Australia
Contact:

Re: Problem overriding love.graphics.print

Post by BlackBulletIV »

They made it like this:

Code: Select all

love.graphics.print = function (...)
    if not love.graphics.getFont() then
        love.graphics.setFont(12)
    end
    love.graphics.print1(...)
    love.graphics.print = love.graphics.print1
end
So that the first time print is called, it will create a default font to be used to actually output the text (if none has been set already of course).
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Problem overriding love.graphics.print

Post by miko »

Simtex wrote:This scales properly:

Code: Select all

love.graphics.oldPrint = love.graphics.print

function love.draw()
	love.graphics.scale(2, 2)
	love.graphics.print("Hardcore LOVE", 1, 1)
end

function love.graphics.print(text, x, y)
	love.graphics.oldPrint(text, x, y)
end
This doesn't (only the position of love.graphics.scale has changed):

Code: Select all

love.graphics.oldPrint = love.graphics.print

function love.draw()
	love.graphics.print("Hardcore LOVE", 1, 1)
end

function love.graphics.print(text, x, y)
	love.graphics.scale(2, 2)
	love.graphics.oldPrint(text, x, y)
end
Why?
The love.graphics.print gets reset for every loop iteration for some reason (unknown to me).
You can verify this by:

Code: Select all

function love.draw()
	print(love.graphics.print)
	love.graphics.print("Hardcore LOVE", 1, 1)
end
And that is the reason to use your own function, rather than redefining the original one.

Btw, in your own function, if you do scale(2,2) before oldPrint, then you should reverse it by scale(0.5, 0.5) after it. If not, every time you call print(), it would doble the scale.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests