KDE Games gained a new cache for card games

March 10, 2008 at 11:22 pm | Posted in KDE | 4 Comments

Finally I’ve managed to finish the new cache for KDE card games :)

Until now the two card games that shipped with KDE Games in KDE4 both managed their own cache of rendered svg card decks. They’ve simply put the rendered images into png files in a special directory in $HOME/.kde4/share/apps. This has a few drawbacks:

  • each card game needs to render the SVG at least once in the size needed
  • wasted disk space if the same theme is used in both games
  • a lot of similar code (almost duplicated)
  • possibly some slowdowns because each frontside of the card deck was stored in a separate png file, so the game loaded 32-52 png files during startup

The new cache class is part of libkdegames, its called KCardCache and is rather easy to use. Simply store it as a member variable in your theme manager (or whereever your game currently renders the svg files) and then make sure to set the theme and proper size before asking it for cards. Some basic code may look like this:

mycache = new KCardCache();
KConfigGroup themeconfiggroup( KGlobal::config(), "Theme" );
mycache->setTheme( themeconfiggroup.readEntry( "Cardname", "Oxygen (SVG)" ) );
mycache->setSize( themeconfiggroup.readEntry( "CardSize", QSize( 60,80 ) );
QPixmap heart_ace = mycache->frontside( KCardInfo( KCardInfo::Heart, KCardInfo::Ace ) );

This would render the fronside for the ace of hearts into a pixmap which you can then use to draw on the board. Internally the cache uses the KPixmapCache class which was written specifically to cache pixmaps that need a lot of time for rendering. KCardCache uses different pixmap caches for each theme, but it allows to share the same cache among all kde card games that use it. So when you run KPatience and choose the Oxygen theme, the first time you run lskat and choose the same theme it already uses the cached images. Also KCardCache doesn’t need to read 52 PNG files because KPixmapCache stores all pixmaps in one data file instead of many.

As far as numbers go, I don’t have any. My feeling tells me that rendering during startup got a bit faster, but even if there’s no real noticeable speed improvement the other benefits weight quite heavily IMHO. Having the same caching code for all games, using a well tested cache implementation which is used all over KDE for icons and various other things and sharing the same theme-cache among all games is quite valuable in itself.

About these ads

4 Comments

  1. Thanks, Andreas! Hurray! :)

  2. Nice work, congratulations!

  3. apaku you code KDevelop, you code some other KDE pieces, do you still have some time to sleep or have a life? :) That’s surprising.

  4. If you look more closely at the commit logs, you’ll see that I’m not really doing a lot anymore. I usually only have about an hour of time during the week (a bit more during the weekend) compared to last year – when I was still a student…

    But yes, any time I have an itch to scratch I somehow get involved a bit more than I planned to. Thats how I came to KDevelop in the first place :)


Sorry, the comment form is closed at this time.

Create a free website or blog at WordPress.com. | The Pool Theme.
Entries and comments feeds.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: