[Kde-games-devel] Tip: Resizing a background picture
Ian Wadham
ianw2 at optusnet.com.au
Wed Jul 2 04:54:19 CEST 2008
Hi again guys,
Several of our games have SVG background pictures that get distorted
when the player resizes the main window too much in the horizontal or
vertical direction. The objects in the picture become short and fat or
tall and thin as the window is stretched one way or the other. This is
not a serious problem with abstract-looking backgrounds, but does
not look so good in the more realistic pictures.
Part of the problem is finding out what shape the picture was when
the artist drew it, i.e. its aspect ratio, or ratio of width to height. If
you can preserve the aspect ratio, the distortion will go away, but the
contents of the picture will have to be clipped top-and-bottom in a
very wide window or left-and-right in a very tall one.
In Qt 4.2, the QSvgRenderer class introduced a new method:
QRectF QSvgRenderer::boundsOnElement ( const QString & id ) const.
This gives you the position and size of the rectangle containing element
"id", in abstract (scalable) SVG co-ordinates, as drawn by the artist.
You can use code to get the aspect ratio, e.g.
QRectF r = svg.boundsOnElement ("background");
double aspectRatio = r.width() / r.height();
If the window's aspect ratio is greater than the picture's, you need
to scale the picture to the window's width and clip it top and bottom
(because it will be too tall to fit). Otherwise scale to the window's
height and clip left and right.
I see that some games are already using QSvgRenderer::boundsOnElement,
so apologies for teaching people to suck eggs. I note though, that KBlocks
is using the method but has an early "return;" in
KBlocksGraphics::adjustForSize(). What gives, Mauricio?
Cheers, Ian W.
More information about the kde-games-devel
mailing list