Page 3 of 10

Re: I have a question.

Posted: Wed Oct 27, 2010 9:52 am
by nevon
Doesn't Font:getWidth() require a string as an argument?

Re: I have a question.

Posted: Wed Oct 27, 2010 10:19 am
by kikito
You are completely right - I've corrected my post.

Re: I have a question.

Posted: Wed Oct 27, 2010 11:03 am
by com_1

Code: Select all

function love.load()
  scrx = 640; scry = 480; zx = (scrx/2); zy = (scry/2); zi=200; 
  love.graphics.setMode(scrx, scry, false, true, 0);
  font = love.graphics.newFont("courbi.ttf", 15); --changed 'setFont' to 'newFont'
  height = font:getHeight();
end

function love.draw()
  love.graphics.line(zx - zi, zy, zx + zi, zy); love.graphics.line(zx, zy - zi, zx, zy + zi);
  love.graphics.setFont(font); -- invoked setFont here
  i = "Thanks people";
  width = font:getWidth(i); -- moved getWidth here and added the string
  love.graphics.print(i, zx - (width/2), zy + (height/5));
end

Re: I have a question.

Posted: Wed Oct 27, 2010 12:33 pm
by nevon
com_1, do take a look at love.graphics.printf(). It's very handy when you're just trying to center text. Saves you the trouble of having to do a bunch of getWidth()/getHeight().

Re: I have a question.

Posted: Wed Oct 27, 2010 1:08 pm
by zac352
nevon wrote:com_1, do take a look at love.graphics.printf(). It's very handy when you're just trying to center text. Saves you the trouble of having to do a bunch of getWidth()/getHeight().

Code: Select all

local myStr="Hi there"
love.graphics.print(myStr,myX-myFont:getWidth(myStr)/2,myY)

Re: I have a question.

Posted: Wed Oct 27, 2010 1:44 pm
by kikito
nevon wrote:com_1, do take a look at love.graphics.printf(). It's very handy when you're just trying to center text. Saves you the trouble of having to do a bunch of getWidth()/getHeight().
I'm afraid that love.graphics.draw (for images) has the 'cx,cy' params, but print/printf don't - unless the wiki is incomplete.

Re: I have a question.

Posted: Wed Oct 27, 2010 2:47 pm
by nevon
kikito wrote:
nevon wrote:com_1, do take a look at love.graphics.printf(). It's very handy when you're just trying to center text. Saves you the trouble of having to do a bunch of getWidth()/getHeight().
I'm afraid that love.graphics.draw (for images) has the 'cx,cy' params, but print/printf don't - unless the wiki is incomplete.
You don't need them. love.graphics.printf( text, x, y, limit, align )

So to center text between x=100 and x=300, at y=400, do:

Code: Select all

love.graphics.printf("Our text", 100, 400, 200, "center")

Re: I have a question.

Posted: Wed Oct 27, 2010 4:24 pm
by com_1

Code: Select all

//With
love.graphics.printf("Thanks people", 320, 240, 0, "center")
//or
love.graphics.printf("Thanks"..(" ")..("people"), 320, 240, 0, "center")
//Not possible
This image with PRINTF.

Image

Re: I have a question.

Posted: Wed Oct 27, 2010 11:32 pm
by Robin
com_1 wrote:

Code: Select all

//With
love.graphics.printf("Thanks people", 320, 240, 0, "center")
//or
love.graphics.printf("Thanks"..(" ")..("people"), 320, 240, 0, "center")
//Not possible
Why the second one, may I ask?

Re: I have a question.

Posted: Thu Oct 28, 2010 11:41 am
by zac352
com_1 wrote:

Code: Select all

love.graphics.printf("Thanks"..(" ")..("people"), 320, 240, 0, "center")
I don't understand why that wouldn't work.
"Thanks" "people" of course wouldn't, unless you set the metatable of strings to automatically concatenate when __call is used.