~keith-penguin/kdegames/trunk

« back to all changes in this revision

Viewing changes to kgoldrunner/src/kgrtheme.cpp

  • Committer: Keith Worrell
  • Date: 2009-03-18 05:35:28 UTC
  • Revision ID: keith.worrell@gmail.com-20090318053528-mx6x9c0ngmg0kg6p
imported project

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *                       kgrtheme.cpp  -  description                      *
 
3
 *                           -------------------                           *
 
4
 *  begin                : Wed Jul 7 2007                                  *
 
5
 *  Copyright 2002 Marco Krüger <grisuji@gmx.de>                           *
 
6
 *  Copyright 2002 Ian Wadham <ianw2@optusnet.com.au>                      *
 
7
 *  Copyright 2007 Luciano Montanaro <mikelima@cirulla.net>                *
 
8
 ***************************************************************************/
 
9
 
 
10
/***************************************************************************
 
11
 *                                                                         *
 
12
 *   This program is free software; you can redistribute it and/or modify  *
 
13
 *   it under the terms of the GNU General Public License as published by  *
 
14
 *   the Free Software Foundation; either version 2 of the License, or     *
 
15
 *   (at your option) any later version.                                   *
 
16
 *                                                                         *
 
17
 ***************************************************************************/
 
18
 
 
19
#include "kgrtheme.h"
 
20
 
 
21
#include <KConfig>
 
22
#include <KConfigGroup>
 
23
#include <KGlobal>
 
24
#include <KDebug>
 
25
#include <QPainter>
 
26
#include <QFileInfo>
 
27
 
 
28
KGrTheme::KGrTheme (const QString &systemDataDir) : 
 
29
        themeDataDir (systemDataDir + "../theme/"),
 
30
        m_themeFilepath (""), 
 
31
        numBackgrounds (0),
 
32
        pixCache (NULL)
 
33
{
 
34
    KConfigGroup group (KGlobal::config(), "Debugging");
 
35
    
 
36
    // Initialize theme lookup table
 
37
    for (int i = 0; i < TileTypeCount; ++i) {
 
38
        offsets[i] = i;
 
39
        counts[i] = 1;
 
40
    }
 
41
}
 
42
 
 
43
KGrTheme::~ KGrTheme()
 
44
{
 
45
    delete pixCache;
 
46
    pixCache = NULL;
 
47
}
 
48
 
 
49
 
 
50
bool KGrTheme::load (const QString& themeFilepath)
 
51
{
 
52
    kDebug() << "New Theme -" << themeFilepath;
 
53
    if (!m_themeFilepath.isEmpty() && (themeFilepath == m_themeFilepath)) {
 
54
        kDebug() << "NO CHANGE OF THEME ...";
 
55
        return true;                                    // No change of theme.
 
56
    }
 
57
    
 
58
    KConfig theme (themeFilepath, KConfig::SimpleConfig);       // Read graphics config.
 
59
    KConfigGroup group = theme.group ("KDEGameTheme");
 
60
 
 
61
    QString f = group.readEntry ("Set", "");
 
62
    if (f.endsWith (".svg") || f.endsWith (".svgz")) {
 
63
        // Load a SVG theme (KGoldrunner 3+ and KDE 4+).
 
64
        QString path = themeFilepath.left (themeFilepath.lastIndexOf ("/") + 1) + f;
 
65
        if (! path.isEmpty()) {
 
66
            svgSet.load (path);
 
67
            
 
68
            // The theme may have multiple backgrounds, called
 
69
            // background0...backgroundN or just one background, called
 
70
            // background0 or simply background.
 
71
            QString backgroundPattern ("background%1");
 
72
            numBackgrounds = 0;
 
73
            while (svgSet.elementExists (backgroundPattern.arg (numBackgrounds))) {
 
74
                ++numBackgrounds;
 
75
            }
 
76
            if (numBackgrounds == 0) {
 
77
                if (svgSet.elementExists ("background")) {
 
78
                    numBackgrounds = 1;
 
79
                }
 
80
            }
 
81
        }
 
82
        
 
83
        f = group.readEntry ("Actors", "default/actors.svg");
 
84
        if (f.endsWith (".svg") || f.endsWith (".svgz")) 
 
85
        {
 
86
            QString path = themeFilepath.left (themeFilepath.lastIndexOf ("/") + 1) + f;
 
87
            if (!path.isEmpty()) 
 
88
            {
 
89
                svgActors.load (path);
 
90
            }
 
91
        }
 
92
    }
 
93
    else {
 
94
        return false;           // Not SVG: old XPM themes no longer supported.
 
95
    }
 
96
 
 
97
    // Check if the theme asks us to draw a border and set the specified color.
 
98
    themeDrawBorder = group.readEntry ("DrawCanvasBorder", 0);
 
99
 
 
100
    // The border color (default black) is also used as the view's background
 
101
    // color, to soften the ugly look of empty rectangles during repainting.
 
102
    QString themeBorderColor = group.readEntry ("BorderColor", "#000000");
 
103
    if (! themeBorderColor.isEmpty()) {
 
104
        m_borderColor.setNamedColor (themeBorderColor);
 
105
    }
 
106
 
 
107
    // If specified, also set the title color.
 
108
    QString themeTextColor = group.readEntry ("TextColor", "");
 
109
    if (! themeTextColor.isEmpty()) {
 
110
        m_textColor.setNamedColor (themeTextColor);
 
111
    }
 
112
 
 
113
    // Save the user's selected theme in KDE's config-group data for the game.
 
114
    KConfigGroup gameGroup (KGlobal::config(), "KDEGame");
 
115
    gameGroup.writeEntry ("ThemeFilepath", themeFilepath);
 
116
    gameGroup.sync();                   // Ensure that the entry goes to disk.
 
117
    m_themeFilepath = themeFilepath;
 
118
    
 
119
    createPixCache();
 
120
    
 
121
    return true;
 
122
}
 
123
 
 
124
 
 
125
QPixmap KGrTheme::background (unsigned int width, unsigned int height, 
 
126
                              unsigned int variant)
 
127
{
 
128
    variant %= numBackgrounds;
 
129
    QTime t;
 
130
    t.restart();
 
131
    QPixmap pixmap;
 
132
    //for (int i = 0; i < 5; i++) {
 
133
    if ((width != 0) && (height != 0) && (numBackgrounds > 0))
 
134
    {
 
135
        if (svgSet.elementExists(QString("background%1").arg(variant)))
 
136
        {
 
137
            pixmap = loadGraphic(QSize(width, height), QString("background%1").arg(variant),svgSet);
 
138
        }
 
139
        else if (svgSet.elementExists("background"))
 
140
        {
 
141
            pixmap = loadGraphic(QSize(width, height), "background",svgSet);
 
142
        }
 
143
 
 
144
    }
 
145
    //}
 
146
    qDebug() << "background took" << t.elapsed() << "ms to render";
 
147
    return pixmap;
 
148
}
 
149
 
 
150
QList<QPixmap> KGrTheme::hero (unsigned int size)
 
151
{
 
152
    QList<QPixmap> frames;
 
153
 
 
154
    for (int i = 1; i <= 36; i++)
 
155
    {
 
156
        frames << loadGraphic(QSize(size, size), QString("hero_%1").arg(i), svgActors);
 
157
    }
 
158
 
 
159
    return frames;
 
160
}
 
161
 
 
162
QList<QPixmap> KGrTheme::enemy (unsigned int size)
 
163
{
 
164
    QList<QPixmap> frames;
 
165
    for (int i = 1; i <= 36; i++)
 
166
    {
 
167
        frames << loadGraphic(QSize(size, size), QString("enemy_%1").arg(i), svgActors);
 
168
    }
 
169
    for (int i = 1; i <= 36; i++)
 
170
    {
 
171
        frames << loadGraphic(QSize(size, size), QString("gold_enemy_%1").arg(i), svgActors);
 
172
    }
 
173
 
 
174
    return frames;
 
175
}
 
176
 
 
177
QList<QPixmap> KGrTheme::tiles (unsigned int size)
 
178
{
 
179
    QList<QPixmap> list;
 
180
 
 
181
    // Create a list of rendered tiles. The tiles must be appended in the
 
182
    // same order they appear in the TileType enum.
 
183
    // While creating the tiles, count the variants, and fill the offset and
 
184
    // count tables.
 
185
    
 
186
    QVector< QString > tileNames;
 
187
    int i = 0;
 
188
 
 
189
    // These tiles can never have variants
 
190
    tileNames << "empty" << "hidden_ladder" << "false_brick";
 
191
    foreach (const QString &name, tileNames) {
 
192
        list.append (loadGraphic(QSize(size, size), name, svgSet));
 
193
        offsets[i] = i;
 
194
        counts[i] = 1;
 
195
        i++;
 
196
    }
 
197
 
 
198
    // These tiles, used in the game-editor, come from the Actors SVG file
 
199
    tileNames.clear();
 
200
    tileNames << "hero_1" << "enemy_1";
 
201
    foreach (const QString &name, tileNames) {
 
202
        list.append (loadGraphic(QSize(size, size), name, svgActors));
 
203
        offsets[i] = i;
 
204
        counts[i] = 1;
 
205
        i++;
 
206
    }
 
207
 
 
208
    // These tiles can have variants
 
209
    tileNames.clear();
 
210
    tileNames << "gold" << "bar" << "ladder" << "concrete" << "brick";
 
211
    foreach (const QString &name, tileNames) {
 
212
        int tileCount = 0;
 
213
        QString tileNamePattern = name + "-%1";
 
214
        while (svgSet.elementExists (tileNamePattern.arg (tileCount))) {
 
215
            kDebug() << tileNamePattern.arg(tileCount);
 
216
            list.append (loadGraphic( QSize(size, size), tileNamePattern.arg(tileCount), svgSet));
 
217
            tileCount++;
 
218
        }
 
219
        if (tileCount > 0) {
 
220
            counts[i] = tileCount;
 
221
        } else {
 
222
            list.append (loadGraphic(QSize(size, size), name, svgSet));
 
223
            counts[i] = 1;
 
224
        } 
 
225
        offsets[i] = offsets[i - 1] + counts[i - 1];
 
226
        i++;
 
227
    }
 
228
 
 
229
    // Add SVG versions of blasted bricks.
 
230
    QString brickPattern("brick_%1");
 
231
    for (int j = 1; j <= 9; ++j) {
 
232
        list.append (loadGraphic(QSize(size, size), brickPattern.arg(j), svgSet));
 
233
    }
 
234
    offsets[i] = offsets[i - 1] + counts[i - 1];
 
235
    counts[i] = 9;
 
236
    
 
237
    return list;
 
238
}
 
239
 
 
240
QList< QPixmap > KGrTheme::namedTiles (QList< QString > names, 
 
241
                                       unsigned int size)
 
242
{
 
243
    QList< QPixmap > list;
 
244
    
 
245
    foreach (const QString &name, names) {
 
246
        if (svgSet.elementExists (name)) {
 
247
            list.append (loadGraphic(QSize(size, size), name, svgSet));
 
248
            kDebug() << name << "found";
 
249
        }
 
250
        else {
 
251
            list.clear();
 
252
            kDebug() << name << "NOT found, exiting";
 
253
            break;
 
254
        }
 
255
    }
 
256
 
 
257
    kDebug() << "namedTiles() tiles:" << list.size();
 
258
 
 
259
    return list;
 
260
}
 
261
 
 
262
QList< QPixmap > KGrTheme::displayTiles (unsigned int size)
 
263
{
 
264
    QList< QString > tileNames;
 
265
    
 
266
    tileNames << "display-left" << "display-centre" << "display-right";
 
267
 
 
268
    return namedTiles (tileNames, size);
 
269
}
 
270
 
 
271
QList< QPixmap > KGrTheme::frameTiles (unsigned int size)
 
272
{
 
273
    QList< QString > tileNames;
 
274
    
 
275
    tileNames << "frame-topleft" << "frame-top" << "frame-topright" << 
 
276
                 "frame-left" << "frame-fill" << "frame-right" <<
 
277
                 "frame-bottomleft" << "frame-bottom" << "frame-bottomright";
 
278
 
 
279
    return namedTiles (tileNames, size);
 
280
}
 
281
 
 
282
QPixmap KGrTheme::loadGraphic(const QSize & size, const QString & strName, KSvgRenderer &Svg, double boundsAdjust)
 
283
{
 
284
    if (! pixCache)
 
285
    {
 
286
        kWarning() << "Cannot load graphics until pixmap cache object has been created!";
 
287
        return QPixmap();
 
288
    }
 
289
    QPixmap pix(size);
 
290
    
 
291
    // create tag name:
 
292
    QString strTagName = QString("%1|%2|%3x%4").arg(m_themeFilepath).arg(strName).arg(size.width()).arg(size.height());
 
293
    
 
294
    if (! pixCache->find(strTagName, pix))
 
295
    {
 
296
//        kWarning() << "Element" << strName << "Not in cache, rendering from SVG";
 
297
        if (! Svg.elementExists(strName))
 
298
        {
 
299
            kWarning() << "Element" << strName << "Not found in SVG document - unable to load!";
 
300
            return pix;
 
301
        }
 
302
        else
 
303
        {
 
304
            pix.fill(QColor(0,0,0,0));
 
305
            QPainter p;
 
306
            p.begin(&pix);
 
307
            QRectF bounds(0,0, size.width(), size.height());
 
308
            bounds.adjust(-boundsAdjust, -boundsAdjust, boundsAdjust, boundsAdjust);
 
309
            Svg.render(&p, strName, bounds);
 
310
            p.end();
 
311
            pixCache->insert(strTagName, pix);
 
312
        }
 
313
        
 
314
    }
 
315
    return pix;
 
316
}
 
317
 
 
318
void KGrTheme::createPixCache()
 
319
{
 
320
    delete pixCache;
 
321
    pixCache = NULL;
 
322
    
 
323
    QString strCacheName = m_themeFilepath.mid(m_themeFilepath.lastIndexOf('/') + 1);
 
324
    strCacheName = strCacheName.left(strCacheName.indexOf('.'));
 
325
    kWarning() << strCacheName;
 
326
    
 
327
    pixCache = new KPixmapCache(QString("kgoldrunner-pixmap-cache-") + strCacheName);
 
328
    pixCache->setRemoveEntryStrategy(KPixmapCache::RemoveLeastRecentlyUsed);
 
329
    pixCache->setCacheLimit(1024 * 3);  // set cache size to 3 MB PER THEME
 
330
    
 
331
    // Check the file modification time of the theme. If it is newer than the pixmap cache
 
332
    // timestamp then we invalidate the entire cache for this theme only.
 
333
    QFileInfo fi(m_themeFilepath);
 
334
    if (fi.lastModified().toTime_t() != pixCache->timestamp())
 
335
    {
 
336
        kWarning() << "Pixmap cache for theme '" << strCacheName << "' is outdated; invalidating cache.";
 
337
        pixCache->discard();
 
338
        pixCache->setTimestamp(fi.lastModified().toTime_t());
 
339
    }
 
340
}
 
341