File indexing completed on 2024-04-28 04:05:04

0001 /*
0002     SPDX-FileCopyrightText: 2010 Stefan Majewsky <majewsky@gmx.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef KGAMERENDERERCLIENT_H
0008 #define KGAMERENDERERCLIENT_H
0009 
0010 // own
0011 #include "kdegames_export.h"
0012 // Qt
0013 #include <QPixmap>
0014 // Std
0015 #include <memory>
0016 
0017 class KGameRendererClientPrivate;
0018 class KGameRenderer;
0019 class KGameRendererPrivate;
0020 
0021 #ifndef KDEGAMES_QCOLOR_QHASH
0022 #define KDEGAMES_QCOLOR_QHASH
0023 inline uint qHash(const QColor &color)
0024 {
0025     return color.rgba();
0026 }
0027 #endif // KDEGAMES_QCOLOR_QHASH
0028 
0029 /**
0030  * @class KGameRendererClient kgamerendererclient.h <KGameRendererClient>
0031  * @short An object that receives pixmaps from a KGameRenderer.
0032  *
0033  * This class abstracts a sprite rendered by KGameRenderer. Given a sprite key,
0034  * render size and possibly a frame index, it returns the QPixmap for this
0035  * sprite (frame) once it becomes available. See the KGameRenderer class
0036  * documentation for details.
0037  *
0038  * Subclasses have to reimplement the receivePixmap() method.
0039  * @since 4.6
0040  */
0041 class KDEGAMES_EXPORT KGameRendererClient
0042 {
0043 public:
0044     /// Creates a new client which receives pixmaps for the sprite with the
0045     /// given @a spriteKey as provided by the given @a renderer.
0046     KGameRendererClient(KGameRenderer *renderer, const QString &spriteKey);
0047     virtual ~KGameRendererClient();
0048 
0049     /// @return the renderer used by this client
0050     KGameRenderer *renderer() const;
0051     /// @return the frame count, or 0 for non-animated sprites, or -1 if the
0052     /// sprite does not exist at all
0053     /// @see KGameRenderer::frameCount()
0054     int frameCount() const;
0055 
0056     /// @return the key of the sprite currently rendered by this client
0057     QString spriteKey() const;
0058     /// Defines the key of the sprite which is rendered by this client.
0059     void setSpriteKey(const QString &spriteKey);
0060     /// @return the current frame number, or -1 for non-animated sprites
0061     int frame() const;
0062     /// For animated sprites, render another frame. The given frame number is
0063     /// normalized by taking the modulo of the frame count, so the following
0064     /// code works fine:
0065     /// @code
0066     ///     class MyClient : public KGameRendererClient { ... }
0067     ///     MyClient client;
0068     ///     client.setFrame(client.frame() + 1); //cycle to next frame
0069     ///     client.setFrame(KRandom::random());  //choose a random frame
0070     /// @endcode
0071     void setFrame(int frame);
0072     /// @return the size of the pixmap requested from KGameRenderer
0073     QSize renderSize() const;
0074     /// Defines the size of the pixmap that will be requested from
0075     /// KGameRenderer. For pixmaps rendered on the screen, you usually want
0076     /// to set this size such that the pixmap does not have to be scaled when
0077     /// it is rendered onto your primary view (for speed reasons).
0078     ///
0079     /// The default render size is very small (width = height = 3 pixels), so
0080     /// that you notice when you forget to set this. ;-)
0081     void setRenderSize(QSize renderSize);
0082     /// @return the custom color replacements for this client
0083     QHash<QColor, QColor> customColors() const;
0084     /// Defines the custom color replacements for this client. That is, for
0085     /// each entry in this has, the key color will be replaced by its value
0086     /// if it is encountered in the sprite.
0087     ///
0088     /// @note Custom colors increase the rendering time considerably, so use
0089     ///       this feature only if you really need its flexibility.
0090     void setCustomColors(const QHash<QColor, QColor> &customColors);
0091 
0092 protected:
0093     /// This method is called when the KGameRenderer has provided a new
0094     /// pixmap for this client (esp. after theme changes and after calls to
0095     /// setFrame(), setRenderSize() and setSpriteKey()).
0096     virtual void receivePixmap(const QPixmap &pixmap) = 0;
0097 
0098 private:
0099     friend class KGameRendererClientPrivate;
0100     friend class KGameRenderer;
0101     friend class KGameRendererPrivate;
0102     std::unique_ptr<KGameRendererClientPrivate> const d_ptr;
0103     Q_DECLARE_PRIVATE(KGameRendererClient)
0104 };
0105 
0106 #endif // KGAMERENDERERCLIENT_H