File indexing completed on 2024-04-14 03:59:49

0001 /*
0002     This file is part of the KDE project "KLines"
0003 
0004     SPDX-FileCopyrightText: 2006-2007 Dmitry Suzdalev <dimsuz@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef KL_RENDERER_H
0010 #define KL_RENDERER_H
0011 
0012 #include <QPixmap>
0013 
0014 #include "commondefs.h"
0015 
0016 class KGameRenderer;
0017 
0018 /**
0019  * This class is responsible for rendering all the game graphics.
0020  * Graphics is rendered from svg file specified by current theme.
0021  * Only one instance of this class exists during a program run.
0022  * It can be accessed with static function KLinesRenderer::self().
0023  */
0024 class KLinesRenderer
0025 {
0026 public:
0027     enum AnimationType { BornAnim, SelectedAnim, DieAnim, MoveAnim };
0028 
0029     static void Init();
0030     static void UnInit();
0031 
0032     static inline KGameRenderer * renderer()
0033     {
0034         return m_renderer;
0035     }
0036     
0037     /**
0038      * Loads theme specified in preferences or a default one if none specified.
0039      */
0040     static bool loadTheme();
0041 
0042     /**
0043      * @return pixmap of the ball of color c in steady state
0044      */
0045     static QString ballPixmapId(BallColor c);
0046     static QPixmap ballPixmap(BallColor c);
0047     /**
0048      * @param type type of animation sequence
0049      * @param c color of the ball
0050      * @param frame frame number (must be between 0..numFrames(type)-1)
0051      * @return string containing elementId
0052      */
0053     static QString animationFrameId(AnimationType type, BallColor c, int frame);
0054     /**
0055      * @return pixmap for background painting.
0056      */
0057     static QPixmap backgroundPixmap(QSize size);
0058     /**
0059      * @return pixmap for border surrounding the play field.
0060      * Will return an invalid QPixmap if no such element exists
0061      * in theme's svg file.
0062      * @see hasBorderElement
0063      */
0064     static QPixmap backgroundBorderPixmap(QSize size);
0065     /**
0066      * @return pixmap of background tile (cell)
0067      */
0068     static QPixmap backgroundTilePixmap();
0069     /**
0070      * @return pixmap for PreviewItem
0071      */
0072     static QPixmap previewPixmap();
0073     /**
0074      * Sets render sizes for cells
0075      */
0076     static void setCellSize(int cellSize);
0077     /**
0078      * @return current cell size
0079      */
0080     static inline int cellSize()
0081     {
0082         return m_cellSize;
0083     }
0084 
0085     static inline QSize cellExtent()
0086     {
0087         return QSize(m_cellSize, m_cellSize);
0088     }
0089 
0090     static bool hasBorderElement();
0091 
0092     /**
0093      * @return number of frames in animation sequence of type t
0094      */
0095     static inline int frameCount(AnimationType t)
0096     {
0097         switch(t)
0098         {
0099         case BornAnim:
0100             return m_numBornFrames;
0101         case SelectedAnim:
0102             return m_numSelFrames;
0103         case DieAnim:
0104             return m_numDieFrames;
0105         default: // e.g. Move - not handled here
0106             return 0;
0107         }
0108     }
0109     /**
0110      * @return duration of animation sequence of type t
0111      */
0112     static inline int animDuration(AnimationType t)
0113     {
0114         switch(t)
0115         {
0116         case BornAnim:
0117             return m_bornDuration;
0118         case SelectedAnim:
0119             return m_selDuration;
0120         case DieAnim:
0121             return m_dieDuration;
0122         case MoveAnim:
0123             return m_moveDuration;
0124         default:
0125             return 0;
0126         }
0127     }
0128 private:
0129     // disable copy - it's singleton
0130     KLinesRenderer();
0131     KLinesRenderer(const KLinesRenderer&);
0132     KLinesRenderer& operator=(const KLinesRenderer&);
0133     ~KLinesRenderer();
0134 
0135     /**
0136      * Pixmap is rendered according to current cellSize
0137      * If customSize is not passed, pixmap will be of (m_cellSize,m_cellSize) size
0138      *
0139      * @return rendered pixmap
0140      */
0141     static QPixmap getPixmap(const QString& svgName, QSize customSize = QSize());
0142 
0143     /**
0144      *  This is the size of the scene's cell.
0145      *  All rendered pixmaps (except background) will have this size
0146      */
0147     static int m_cellSize;
0148 
0149     static KGameRenderer *m_renderer;
0150 
0151     static int m_numBornFrames;
0152     static int m_numSelFrames;
0153     static int m_numDieFrames;
0154 
0155     static int m_bornDuration;
0156     static int m_selDuration;
0157     static int m_dieDuration;
0158     static int m_moveDuration; // one cell
0159 };
0160 
0161 #endif