[Kde-games-devel] Co-ordinates in QGraphicsView

Ian Wadham iandw.au at gmail.com
Wed Jul 4 05:57:40 UTC 2012


On 01/07/2012, at 7:47 PM, Mathias Kraus wrote:
> Am Sonntag, 1. Juli 2012, 11:06:05 schrieb Ian Wadham:
>> I guess the scaling of QGraphicsPixmapItem is OK for photos, etc., but how
>> do you use it with pixmaps pre-rendered to a certain size?
>> 
>> It seems to me that I have to use the same pixel-based co-ordinates in both
>> the QGraphicsScene and the QGraphicsView and change all the scene item
>> co-ordinates and sizes after every resize of the QGraphicsView (not to mention
>> the size of the whole scene), so that the scale factor for rendered pixmaps is
>> always 1.0.
>> 
>> Or am I missing something?
>> 
>> I am asking because it would be nice if I could use the same co-ordinate
>> system in both the game engine (model) and the QGraphicsScene.
> 
> If I understand you right, I ran into the same problem while porting Granatier to KGameRenderer.
> 
> Did you try to render the pixmap in the size of the view and scale them afterwards?
> This is what I did in Granatier:
> 
>    //calculate the size of the item on the view
>    QPoint viewTopLeft = scene()->views().first()->mapFromScene(0, 0);
>    QPoint viewBottomRight = scene()->views().first()->mapFromScene(m_itemSizeSet.width(), m_itemSizeSet.height());
> 
>    int viewWidth = viewBottomRight.x() - viewTopLeft.x();
>    int viewHeight = viewBottomRight.y() - viewTopLeft.y();
> 
>    setRenderSize(QSize(viewWidth, viewHeight));
>    setScale(svgScaleFactor);
> 
> The svgScaleFactor is calculated at a resizeEvent.
> 
> Have a look at resizeSprites(...) in gamescene.cpp and updateGraphicsInternal(...) in elementitem.cpp for more details.

Thanks very much, Mathias.  Inspired by your example, I have arrived at a
QGraphicsScene that has grid-spacing 1.0 and, in effect, uses the same
co-ordinate system as the KGoldrunner game-engine (or model), using
setRenderSize(QSize(tileSize, tileSize) and setScale(1.0/tileSize) to get
the tile sizes in the view coming out at the right size.

Thanks again, Ian W.



More information about the kde-games-devel mailing list