Page 1 of 2

Problem overriding love.graphics.print

Posted: Sun Mar 13, 2011 8:42 pm
by Simtex
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?

Re: Problem overriding love.graphics.print

Posted: Sun Mar 13, 2011 8:46 pm
by crow
This might sound silly but try putting the print function before draw this might be wrong but I had a problem just link this in a standard lua function when calling functions like you are maybe it will work maybe not but at lest its a try ?

Re: Problem overriding love.graphics.print

Posted: Sun Mar 13, 2011 8:51 pm
by Simtex
crow wrote:This might sound silly but try putting the print function before draw this might be wrong but I had a problem just link this in a standard lua function when calling functions like you are maybe it will work maybe not but at lest its a try ?
Yeah I messed around with positioning a bit myself, it doesn't seem to help.

Re: Problem overriding love.graphics.print

Posted: Sun Mar 13, 2011 9:03 pm
by BlackBulletIV
crow wrote:This might sound silly but try putting the print function before draw this might be wrong but I had a problem just link this in a standard lua function when calling functions like you are maybe it will work maybe not but at lest its a try ?
This shouldn't be a problem, Lua doesn't statically find functions like C/C++. If it's in the global namespace when called, it's all good.

I'm not really sure why it's not working, although I think should only need to call love.graphics.scale once, in love.load or something. But, I might be wrong.

Re: Problem overriding love.graphics.print

Posted: Sun Mar 13, 2011 9:20 pm
by Simtex
BlackBulletIV wrote: I think should only need to call love.graphics.scale once, in love.load or something. But, I might be wrong.
Scale & translate both only last until love.draw() exits, so you have to call them every time you call love.draw().

Re: Problem overriding love.graphics.print

Posted: Sun Mar 13, 2011 9:21 pm
by Robin
BlackBulletIV wrote:I'm not really sure why it's not working, although I think should only need to call love.graphics.scale once, in love.load or something. But, I might be wrong.
You are, in fact, wrong. You need to do it every frame before drawing the things you want scaled (also: look at love.graphics.push and -pop).

Not sure why the code in the OP doesn't work. I would suggest not to redefine any API functions, which will probably only lead to people crying and being confused. ;)

Re: Problem overriding love.graphics.print

Posted: Sun Mar 13, 2011 9:54 pm
by BlackBulletIV
Oh ok, I was wrong then. I guess that's why my Camera class wouldn't work unless I activated it every frame.

Re: Problem overriding love.graphics.print

Posted: Sun Mar 13, 2011 11:57 pm
by EmmanuelOga
This is way it does not work:

https://bitbucket.org/rude/love/src/13f ... ua#cl-1268

Try:

Code: Select all

love.graphics.oldPrint = love.graphics.print1
function love.graphics.print1(...)
   love.graphics.scale(2, 2)
   love.graphics.oldPrint(...)
end

function love.draw()
   love.graphics.print("Hardcore LOVE", 1, 1)
end
That's an horrible idea though. I would not recommend doing that. If you need that extra functionality, just add a new function. You don't even need to define it inside the love.graphics namespace.
Robin wrote:I would suggest not to redefine any API functions
I agree.

Re: Problem overriding love.graphics.print

Posted: Mon Mar 14, 2011 12:36 am
by BlackBulletIV
Looking at that, I think you might need to call love.graphics.print once, and then redefine it (or, you could use the method shown above of course). That'll probably work. But as said, it's not a good idea to go hacking around with the Love internals like that.

Re: Problem overriding love.graphics.print

Posted: Mon Mar 14, 2011 12:46 am
by Simtex
EmmanuelOga wrote: That's an horrible idea though. I would not recommend doing that. If you need that extra functionality, just add a new function. You don't even need to define it inside the love.graphics namespace.
Robin wrote:I would suggest not to redefine any API functions
I agree.
Well that is certainly interesting. I wonder why they did it that way, thanks for your answer though.

I understand overriding the API is usually a bad idea, but there's a very specific reason I'm doing this and it's important for testing without having to rewrite a bunch of code.

Thanks again.