File indexing completed on 2024-05-12 08:00:35

0001 /*
0002  *  Copyright 2007-2010  Parker Coates <coates@kde.org>
0003  *
0004  *  This program is free software; you can redistribute it and/or
0005  *  modify it under the terms of the GNU General Public License as
0006  *  published by the Free Software Foundation; either version 2 of
0007  *  the License, or (at your option) any later version.
0008  *
0009  *  This program is distributed in the hope that it will be useful,
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *  GNU General Public License for more details.
0013  *
0014  *  You should have received a copy of the GNU General Public License
0015  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
0016  *
0017  */
0018 
0019 #include "renderer.h"
0020 
0021 // own
0022 #include "settings.h"
0023 // KDEGames
0024 #include <KGameThemeProvider>
0025 
0026 Renderer *Renderer::s_instance = nullptr;
0027 
0028 Renderer *Renderer::self()
0029 {
0030     if (s_instance == nullptr)
0031         s_instance = new Renderer();
0032     return s_instance;
0033 }
0034 
0035 void Renderer::deleteSelf()
0036 {
0037     delete s_instance;
0038     s_instance = nullptr;
0039 }
0040 
0041 qreal Renderer::aspectRatioOfElement(const QString &elementId)
0042 {
0043     const QSizeF size = boundsOnSprite(elementId).size();
0044     return size.width() / size.height();
0045 }
0046 
0047 QColor Renderer::colorOfElement(const QString &elementId)
0048 {
0049     if (m_cachedTheme != theme()->identifier()) {
0050         m_colors.clear();
0051         m_cachedTheme = theme()->identifier();
0052     }
0053 
0054     QColor color;
0055     QHash<QString, QColor>::const_iterator it = m_colors.constFind(elementId);
0056     if (it != m_colors.constEnd()) {
0057         color = it.value();
0058     } else {
0059         QPixmap pix = spritePixmap(elementId, QSize(3, 3));
0060         color = pix.toImage().pixel(1, 1);
0061         m_colors.insert(elementId, color);
0062     }
0063     return color;
0064 }
0065 
0066 static KGameThemeProvider *provider()
0067 {
0068     KGameThemeProvider *prov = new KGameThemeProvider;
0069     prov->discoverThemes(QStringLiteral("themes"), // theme file location
0070                          QStringLiteral("greenblaze") // default theme file name
0071     );
0072     return prov;
0073 }
0074 
0075 Renderer::Renderer()
0076     : KGameRenderer(provider())
0077 {
0078     setStrategyEnabled(KGameRenderer::UseDiskCache, false);
0079 }