1/*******************************************************************
2 *
3 * Copyright 2006-2007 Dmitry Suzdalev <dimsuz@gmail.com>
4 *
5 * This file is part of the KDE project "KLines"
6 *
7 * KLines is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * KLines is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with KLines; see the file COPYING. If not, write to
19 * the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 ********************************************************************/
23#ifndef KL_RENDERER_H
24#define KL_RENDERER_H
25
26#include <QPixmap>
27
28#include "commondefs.h"
29
30class KGameRenderer;
31
32/**
33 * This class is responsible for rendering all the game graphics.
34 * Graphics is rendered from svg file specified by current theme.
35 * Only one instance of this class exists during a program run.
36 * It can be accessed with static function KLinesRenderer::self().
37 */
38class KLinesRenderer
39{
40public:
41 enum AnimationType { BornAnim, SelectedAnim, DieAnim, MoveAnim };
42
43 static void Init();
44 static void UnInit();
45
46 static inline KGameRenderer * renderer()
47 {
48 return m_renderer;
49 }
50
51 /**
52 * Loads theme specified in preferences or a default one if none specified.
53 */
54 static bool loadTheme();
55
56 /**
57 * @return pixmap of the ball of color c in steady state
58 */
59 static QString ballPixmapId(BallColor c);
60 static QPixmap ballPixmap(BallColor c);
61 /**
62 * @param type type of animation sequence
63 * @param c color of the ball
64 * @param frame frame number (must be between 0..numFrames(type)-1)
65 * @return string containing elementId
66 */
67 static QString animationFrameId(AnimationType type, BallColor c, int frame);
68 /**
69 * @return pixmap for background painting.
70 */
71 static QPixmap backgroundPixmap(const QSize& size);
72 /**
73 * @return pixmap for border surrounding the play field.
74 * Will return an invalid QPixmap if no such element exists
75 * in theme's svg file.
76 * @see hasBorderElement
77 */
78 static QPixmap backgroundBorderPixmap(const QSize& size);
79 /**
80 * @return pixmap of background tile (cell)
81 */
82 static QPixmap backgroundTilePixmap();
83 /**
84 * @return pixmap for PreviewItem
85 */
86 static QPixmap previewPixmap();
87 /**
88 * Sets render sizes for cells
89 */
90 static void setCellSize(int cellSize);
91 /**
92 * @return current cell size
93 */
94 static inline int cellSize()
95 {
96 return m_cellSize;
97 }
98
99 static inline QSize cellExtent()
100 {
101 return QSize(m_cellSize, m_cellSize);
102 }
103
104 static bool hasBorderElement();
105
106 /**
107 * @return number of frames in animation sequence of type t
108 */
109 static inline int frameCount(AnimationType t)
110 {
111 switch(t)
112 {
113 case BornAnim:
114 return m_numBornFrames;
115 case SelectedAnim:
116 return m_numSelFrames;
117 case DieAnim:
118 return m_numDieFrames;
119 default: // e.g. Move - not handled here
120 return 0;
121 }
122 }
123 /**
124 * @return duration of animation sequence of type t
125 */
126 static inline int animDuration(AnimationType t)
127 {
128 switch(t)
129 {
130 case BornAnim:
131 return m_bornDuration;
132 case SelectedAnim:
133 return m_selDuration;
134 case DieAnim:
135 return m_dieDuration;
136 case MoveAnim:
137 return m_moveDuration;
138 default:
139 return 0;
140 }
141 }
142private:
143 // disable copy - it's singleton
144 KLinesRenderer();
145 KLinesRenderer(const KLinesRenderer&);
146 KLinesRenderer& operator=(const KLinesRenderer&);
147 ~KLinesRenderer();
148
149 /**
150 * Pixmap is rendered according to current cellSize
151 * If customSize is not passed, pixmap will be of (m_cellSize,m_cellSize) size
152 *
153 * @return rendered pixmap
154 */
155 static QPixmap getPixmap(const QString& svgName, const QSize& customSize = QSize());
156
157 /**
158 * This is the size of the scene's cell.
159 * All rendered pixmaps (except background) will have this size
160 */
161 static int m_cellSize;
162
163 static KGameRenderer *m_renderer;
164
165 static int m_numBornFrames;
166 static int m_numSelFrames;
167 static int m_numDieFrames;
168
169 static int m_bornDuration;
170 static int m_selDuration;
171 static int m_dieDuration;
172 static int m_moveDuration; // one cell
173};
174
175#endif
176