kgrrenderer.h 6.18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/****************************************************************************
 *    Copyright 2012  Ian Wadham <iandw.au@gmail.com>                       *
 *                                                                          *
 *    This program is free software; you can redistribute it and/or         *
 *    modify it under the terms of the GNU General Public License as        *
 *    published by the Free Software Foundation; either version 2 of        *
 *    the License, or (at your option) any later version.                   *
 *                                                                          *
 *    This program is distributed in the hope that it will be useful,       *
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *    GNU General Public License for more details.                          *
 *                                                                          *
 *    You should have received a copy of the GNU General Public License     *
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ****************************************************************************/

#ifndef KGRRENDERER_H
#define KGRRENDERER_H

21 22 23 24
#include <QObject>
#include <QString>
#include <KGameRenderer>

25
class QGraphicsScene;
26 27 28
class KgThemeProvider;
class KgThemeSelector;

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/* @short A class to assist theme-handling and rendering in KGoldrunner.
 *
 * KGoldrunner has two SVG files for each theme: one to hold the Actors (hero
 * and enemies) and one to hold the Set (bricks, ladders, background, etc.).
 *
 * The files are marked with the keywords "Actors" and "Set" in each theme's
 * .desktop file, rather than the usual "Filename" keyword. There are two
 * KgThemeProvider objects and two KGameRenderer objects, each with its own
 * set of SVG files and KgTheme objects.
 *
 * There is one KgThemeSelector object, which selects the "Set" theme and uses
 * the "Set" KgThemeProvider. Its currentThemeChanged signal is connected to a
 * currentThemeChanged slot in KGrRenderer, which finds the KgTheme for the
 * corresponding "Actors" theme and SVG file.
 *
 * KGoldrunner also has several different usages of the KGameRenderer concepts
 * of "frameSuffix" and "frameBaseIndex". For animation frames (hero, enemies
 * and dug bricks), it always has frameSuffix = "_%1" and frameBaseIndex = 1, so
 * animation will be handled normally by KGameRenderer. 
 *
 * Depending on the theme and the artist's choices, backgrounds and tiles can
 * have one or more variants, to add variety to the look of brick walls, etc.
 * The suffixes used can be "-%1" or just "%1" and the frameBaseIndex = 0, or
 * there can be just one variant, with no suffix. The keyTable structure and the
 * getPixmapKey() and getBackgroundKey() methods of KGrRenderer provide ways to
 * go from KGoldrunner's internal tile-types to SVG element names that can be
 * used as pixmap keys in KGameRenderer.
 */
57 58 59 60
class KGrRenderer : public QObject
{
    Q_OBJECT
public:
61
    KGrRenderer (QGraphicsScene * scene);
62 63
    virtual ~KGrRenderer();

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    /*
     * Get a pointer to the KGameRenderer for "Set" graphics (bricks, etc.).
     */
    KGameRenderer * getSetRenderer()    { return m_setRenderer; }

    /*
     * Get a pointer to the KGameRenderer for "Actors" graphics (hero, etc.).
     */
    KGameRenderer * getActorsRenderer() { return m_actorsRenderer; }

    /*
     * Get the SVG element name for a KGoldrunner tile type. If the theme has
     * more than one tile of that type (e.g. BRICK), make a random selection.
     *
     * @param picType The internal KGoldrunner type of a tile or background.
     */
80 81
    QString getPixmapKey (const char picType);

82 83 84 85 86 87 88 89 90
    /*
     * Get the SVG element name for a KGoldrunner background. If the theme has
     * more than one background, cycle though the choices as the KGoldrunner
     * game's level changes.
     *
     * @param level   The current level in a KGoldrunner game.
     */
    QString getBackgroundKey (const int level);

91
public slots:
92 93 94 95 96
    /*
     * Show the theme-selector dialog. When the theme changes, KGrRenderer uses
     * a signal and slot to keep the "Set" and "Actors" parts of the theme and
     * SVG files in synch.
     */
97 98
    void selectTheme();

99 100 101 102 103
private slots:
     // Keep the "Set" and "Actors" parts of a KGoldrunner theme in synch as
     // the theme-selection changes.
    void currentThemeChanged(const KgTheme * currentSetTheme);

104
private:
105 106 107
    enum   PicSrc     {Actors, Set};

    // Structure of table-row to specify a tile or pixmap type in a theme.
108
    struct PixmapSpec {
109 110 111 112 113 114 115 116 117
        const char    picType;		// KGoldrunner's internal type.
	const PicSrc  picSource;	// Actors or Set?
	const char *  picKey;		// Prefix of SVG element name.
	const char *  frameSuffix;	// Format of suffix or "" if none.
	const int     frameBaseIndex;	// Lowest value of suffix or -1 if none.
	      int     frameCount;	// Number of variants available.
					// -2 = not yet counted, -1 = element
					// not found, 0 = only one variant with
					// no suffix, >0 = number of variants.
118 119
    };

120 121
    QGraphicsScene  * m_scene;		// The scene to be rendered.

122 123
    KgThemeProvider * m_setProvider;	// Provider for Set themes.
    KgThemeProvider * m_actorsProvider;	// Provider for Actors themes.
124

125 126
    KgThemeSelector * m_themeSelector;	// Selector (dialog) for themes.

127 128 129 130 131
    bool              m_hasBorder;	// Whether the theme has a border or the
					// background pixmap fills the view.
    QColor            m_borderColor;	// The color of the theme's border.
    QColor            m_textColor;	// The color of the theme's text.

132 133 134 135 136 137
    KGameRenderer   * m_setRenderer;	// Renderer for Set SVG files.
    KGameRenderer   * m_actorsRenderer;	// Renderer for Actors SVG files.

    static PixmapSpec keyTable [];	// Table of tile/background specs.

    // Set the frame counts to -2 at startup and when the theme changes.
138
    void              initPixmapKeys();
139 140 141 142 143 144

    // Find a tile type or background in the table of tiles and backgrounds.
    int               findKeyTableIndex (const char picType);

    // Count the number of variants of a tile or background.
    int               countFrames (const int index);
145 146 147
};

#endif // KGRRENDERER_H