Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Draw from the center
- - By il mietitore Date 2011-10-07 19:15
Hi again, comrades, i have a new nooby question for you (searched around the forum, no topics found). As some of you may know I'm italian, so excuse me if there is some grammar butchery around the message.

I'm having some troubles drawing images and texts. In particular I'm having troubles with texts.

The point is that it's a bit difficult to have my text exactly where i want, if I have to draw it from the upper left corner. I don't know how much the text is long in pixel terms, so I can't make calculations about the exact point where i have to draw.

For example:

@font_corpo_60.draw("Options", 410, 700, 0, 1, 1, 0xFFFFFF00)

Here I know that this text is 60 pixels tall. But I don't know how much it's "long", so how am I supposed to have this text centered in his frame?

So, here is the question. Is there a way to draw something determining its position from the center of the image/text, and not from the upper left corner?

I admit, I'm sure there must be a way, but I can't understand anything in the gosu documentation, so here I am.
Parent - - By Spooner Date 2011-10-07 22:37
You can actually do the maths yourself using @font_corpo_60.text_width("my message"), but there is already a system to change where images/text is drawn relative to. The center_x and center_y values give hints on where to draw (they default to 0).

@font_corpo_60.draw("Options", 410, 700, 0, 0, 0, 0xFFFFFF00) # Top left - default.
@font_corpo_60.draw("Options", 410, 700, 0, 1, 1, 0xFFFFFF00) # Bottom right.
@font_corpo_60.draw("Options", 410, 700, 0, 1, 0, 0xFFFFFF00) # Top right, as you requested.
Parent - - By il mietitore Date 2011-10-07 23:41
Uhm. There is something not working in here >.<

The gosu documentation says that the default value is 1 for both the factors:

draw(text, x, y, z, factor_x=1, factor_y=1, color=0xffffffff, mode=:default)

and in fact up to now I used those values. And it writes from the top left corner.
I tried to put 0 and 0 on some images as an experiment, but the image doesn't appear.

And by the way, I'm not trying to draw from the top right, but from the middle of the image, excuse me if I wasn't clear in my previous message :(
Parent - - By Spooner Date 2011-10-07 23:44
My fault, I meant to tell you to use #draw_rel  with rel_x and rel_y (what I was doing was changing the factor in #draw, not the relative position in #draw_rel)

draw_rel(text, x, y, z, rel_x, rel_y, factor_x=1, factor_y=1, color=0xffffffff, mode=:default)

rel_x and rel_y are what you want to be messing with (they are effectively 0 and 0 when using #draw). factor_x and factor_y are the relative size, not position.

Sorry for confusing you!
Parent - - By il mietitore Date 2011-10-08 12:11
Ooook, that's fine.

What about images? Is there a way to draw them from the middle, as for the font?
Parent - - By jlnr (dev) Date 2011-10-08 17:35
You can (ab)use Image#draw_rot for that. It is quite common. :)

You can also manually calculate the position using Image#width and Font#text_width, respectively. But it's seldom worth the trouble.
Parent - By il mietitore Date 2011-10-08 19:29
great! that works!

thanks a lot again, people, mission accomplished ;)
Up Topic Gosu / Gosu Exchange / Draw from the center

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill