PDA

View Full Version : Drawing something at first and avoid redrawing on update() calls



jacopo
1st September 2010, 16:14
Hi all,
I'm working at a monopoly-like game project.
The game has to draw a path made of spaces, in which some tokens move. So what really changes during the iterations is:
- tokens position
- the active space (think about purchase or mortgage actions)
So I wonder if there is a way to draw all the spaces (their borders, their names..) during the game setup and, during the game itself, tell the paintEvent to paint only what really has changed.

Thanks in advance. ;)

wysota
1st September 2010, 19:30
Yes and no. Yes - it is possible to request redraws of particular areas only. No - you can't guarantee that paint events will be called only for those regions (as the windowing system may request redraw of your whole window when it's obscured or resized). What you probably want is to draw the permanent content on a pixmap and rendering the pixmap to the widget in paint events before drawing the volatile content.

jacopo
1st September 2010, 22:07
Hi wysota, thanks for your answer.
I spent some time thinking about a new strategy. Essentially, I have different types of spaces, in which I have to draw something additional. For example, a BuildingSpace where I draw the color corresponding to the owner; a CompanySpace, where I draw the colors correspondig to multiple owners..
I could have a:
- mainClass, its paintEvent deals with drawing the path, that is spaces with their borders and their names.
- classes for each type of spaces, each inheriting QWidget. So each class has its own paintEvent, who only cares of drawing additional info and, obviously, the token area inside the space -> so their paintEvents draw inside a region
In such way, mainClass::paintEvent is called automatically as usual and I will not call it manually.
Instead, Space classes paintEvents can be called:
- by mainClass paintEvent when there is a complete redrawing
- manually by some methods during the game iteration, like when updating a token's position.
So, in the end, during normal game iterations and excluding events that cause entire redrawing, I'm drawing only spaces that are really changed.

What do you think about ?

SixDegrees
1st September 2010, 23:35
Why don't you use a QGraphicsScene and override the paintBackground() member to paint your board? The pieces and whatnot are placed in a layer on top of the background, and can be moved independently, or even animated.

Basically, you're talking about reimplementing that class anyway, and it provides a great deal more than what you've described in addition to addressing your current problem.

wysota
2nd September 2010, 02:58
So, in the end, during normal game iterations and excluding events that cause entire redrawing, I'm drawing only spaces that are really changed.
Forget about redrawing only the things that changed. Essentially you'll be doing it this way anyway, regardless of what approach you take. And regardless of what approach you take you won't be able to guarantee that the windowing system will order you to redraw everything. So in the end you have to be prepared to redraw your whole board but you can optimize by checking what are needs to be redrawn in a particular paint event. If you choose Graphics View approach the framework will help you in this task by forcing redraw of only those objects that occupy the exposed area.

jacopo
3rd September 2010, 18:59
Ok, QGraphicsView seems to be the best solution. :)

However, is it possible to build a QGraphicsScene on a polygon that would not be a rectangle ?
See for example this: http://qt-apps.org/CONTENT/content-pre1/113173-1.png
I would like to have a QGraphicsScene on the path only, while at center I would have a QWidget.

Probably it's not possible, but I want to be sure about it.

wysota
3rd September 2010, 19:21
You can place any widget over any other but actually you can place a QWidget inside a scene to get the effect you want. Although personally I don't like the screenshot you posted. I think placing such a boring widget inside such a nice board is a waste of creativity. I'm hoping your version will make extensive use of Qt's Animation Framework.