Re: Drawing a lot of SVG images is slow

"'Eric Jensen' via wx-users" <[email protected]> Tue, 1 Apr 2025 23:16:35 +0200
Newsgroups gmane.comp.lib.wxwindows.general
Message-ID <[email protected]>
Hello Quentin,

Tuesday, April 1, 2025, 7:36:31 PM, you wrote:

QC> Hello,

QC> I'm developing a game. I'm trying to use SVG images to draw the board and all game elements (tokens, dice, etc.).
QC> I wanted to use SVG because it allows the board to be seen all at once and have its size adapted to the size of the window, i.e. fill the window as much as possible / make the board as big as possible, without pixelization effects. It also allows the user to customize the color of some game elements (for example tokens) quite easily, as well as select their own colors to make their own dark or high-contrast mode. All this is good for visually impaired players.

QC> However, displaying everything (~1000 elements) seems to be quite slow, and it makes the interface lagging globally. It takes hundreds of milliseconds to update the screen after having pressed a key and/or moved the mouse.
QC> Am I doing something wrong ? Do you have ideas on how I could improve performances ?

QC> Drawing code looks as follows, I have tried to simplify it a little:

QC> void GameWindow::OnPaint (wxPaintEvent& e) {
QC>    wxAutoBufferedPaintDC dc(this);
QC>    std::unique_ptr<wxGraphicsContext> g(wxGraphicsContext::Create(dc));
QC>      ...
QC>    for (auto& square: squares) {
QC>      wxRect pos = ... ; // boundaries of the square
QC>      g->ResetClip();
QC>      g->Clip(pos);
QC>      for (std::string& image: square.getElements()) {
QC>        auto bundle = app.getImage(image);
QC>        wxSize defaultSize = bundle.GetDefaultSize();
QC>        wxSize finalImageSize = calcGreatestPossibleSize(wxSize(pos.width, pos.height), defaultSize);
QC>        auto bitmap = bundle.GetBitmap(finalImageSize);
QC>        if (bitmap.IsOk()) {
QC>          finalImageSize = bitmap.GetSize();
QC>          g->DrawBitmap(bitmap, pos.x, pos.y, finalImageSize.x, finalImageSize.y);
QC>        }
QC>      }
QC>      g->ResetClip();
QC>    }
QC> }

In addition to what Vadim wrote:

At least when using GDI+, conversion from wxBitmap to wxGraphicsBitmap is slow and should be avoided at all costs. It's possible that this is not the case with the D2D renderer, but if in doubt, you should cache the bitmaps as wxGraphicsBitmap, not wxBitmap.

Also you should check if wxAutoBufferedPaintDC actually buffers, or not. If yes, explicitly using wxBufferedPaintDC with a static bitmap will save some time, too, as it avoids creating a new bitmap each time.

Eric



-- 

-- 
Please read https://www.wxwidgets.org/support/mlhowto.htm before posting.
--- 
You received this message because you are subscribed to the Google Groups "wx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/wx-users/2310285343.20250401231635%40j-dev.de.