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

Mathias Kraus k.hias at gmx.de
Sun Jul 1 09:47:16 UTC 2012


Am Sonntag, 1. Juli 2012, 11:06:05 schrieb Ian Wadham:
> Hi guys,
> 
> When looking at how to port KGoldrunner to QGraphicsView, I have struck some
> design problems and am looking for some help.
> 
> In the QGraphicsView module, a scene (QGraphicsScene) and a view (QGraphicsView)
> each have their own co-ordinate system and there is an automatic transformation
> between them.  This works fine for simple items like lines, rectangles, circles and
> ellipses, but how does it work for pixmaps rendered from SVG by KGameRenderer?
> 
> Suppose I have a 10x10 grid full of pixmaps in a scene.  It would be nice if I could
> use scene co-ordinates X = 0 to 9 and Y = 0 to 9 and the view would work out
> however many pixels that comes to, e.g. the view is 400x400 pixels, so each
> grid square is painted as 40x40 pixels.  That works fine if there is a
> QGraphicsRectItem in each square.
> 
> So I find out what size the QGraphicsView is in pixels (e.g. 400x400) and I
> divide by 10 to get the grid-square size and ask KGameRenderer to provide
> me a pixmap item (actually KGameRenderedItem) of the right size (i.e. 40x40),
> but now the pixmap gets scaled up when it is painted on the view and it is
> horribly pixellated.
> 
> 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.

Regards,
Mathias

> Cheers, Ian W.
> 
> _______________________________________________
> kde-games-devel mailing list
> kde-games-devel at kde.org
> https://mail.kde.org/mailman/listinfo/kde-games-devel


More information about the kde-games-devel mailing list