SVN_SILENT made messages (.desktop file)
[kdegames.git] / kbreakout / src / renderer.cpp
blob4f4271cc175caa11ece8e03e8dafca0eecca4ab6
1 /*
2 Copyright 2007 Dmitry Suzdalev <dimsuz@gmail.com>
3 Copyright 2007-2008 Fela Winkelmolen <fela.kde@gmail.com>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "renderer.h"
20 #include "settings.h"
22 #include <QPainter>
24 #include <KSvgRenderer>
25 #include <KGameTheme>
26 #include <kpixmapcache.h>
28 Renderer* Renderer::self()
30 static Renderer instance;
31 return &instance;
34 Renderer::Renderer()
36 m_renderer = new KSvgRenderer();
37 m_cache = new KPixmapCache("kbreakout");
38 m_cache->setCacheLimit(3*1024); // TODO: put somewhere else!!!!!!!! (also in KNetWalk!!)
40 if(!loadTheme(Settings::theme()))
41 kDebug() << "Failed to load any game theme!";
44 Renderer::~Renderer()
46 delete m_renderer;
47 delete m_cache;
50 bool Renderer::loadTheme(const QString& themeName)
52 // variable saying whether to discard old cache upon
53 // successful new theme loading
54 // we won't discard it if m_currentTheme is empty meaning that
55 // this is the first time loadTheme() is called
56 // (i.e. during startup) as we want to pick the cache from disc
57 bool discardCache = !m_currentTheme.isEmpty();
59 if(!m_currentTheme.isEmpty() && m_currentTheme == themeName) {
60 kDebug() << "Notice: not loading the same theme";
61 return true; // this is not an error
63 KGameTheme theme;
64 if (!theme.load(themeName)) {
65 kDebug() << "Failed to load theme" << Settings::theme();
66 kDebug() << "Trying to load default";
67 if(!theme.loadDefault())
68 return false;
71 m_currentTheme = themeName;
73 bool res = m_renderer->load( theme.graphics() );
74 kDebug() << "loading" << theme.graphics();
75 if (!res) return false;
77 if (discardCache) {
78 kDebug() << "discarding cache";
79 m_cache->discard();
81 return true;
84 QPixmap Renderer::renderedSvgElement(const QString &id, const QSize &size) const
86 int const w = size.width();
87 int const h = size.height();
89 QPixmap pixmap;
90 QString cacheName =
91 QString("%1-%2x%3").arg(id).arg(w).arg(h);
92 if(m_cache->find(cacheName, pixmap)) return pixmap;
94 // else
95 kDebug() << "re-rendering pixmap" << cacheName;
96 pixmap = QPixmap(size);
97 pixmap.fill(Qt::transparent);
98 QPainter p(&pixmap);
100 if (!m_renderer->elementExists(id)) {
101 kDebug() << "Invalid elementId: " << id;
102 return pixmap;
105 m_renderer->render(&p, id/*, size*/);
106 p.end();
107 m_cache->insert(cacheName, pixmap);
108 kDebug() << "cache size:" << m_cache->size() << "kb";
110 return pixmap;