File indexing completed on 2024-04-14 04:02:10

0001 /*
0002     SPDX-FileCopyrightText: 2008 Ian Wadham <iandw.au@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef GAMEGLVIEW_H
0008 #define GAMEGLVIEW_H
0009 
0010 // Qt includes
0011 #include <QOpenGLWidget>
0012 #include <QMouseEvent>
0013 
0014 // Local includes
0015 #include "game.h"
0016 #include "kbkglobal.h"
0017 
0018 #include <memory>
0019 
0020 class Game;         // Forward declaration of Game class.
0021 
0022 class QOpenGLTexture;
0023 
0024 /**
0025  * Demonstrate the features of the Qt OpenGL
0026  * view and OpenGL itself.
0027  */
0028 class GameGLView : public QOpenGLWidget
0029 {
0030 public:
0031     /**
0032     * Create a new GL view for the game
0033     **/
0034     explicit GameGLView (Game * g, QWidget * parent = nullptr);
0035 
0036     /**
0037     * Dump all OpenGL and GLU extensions to stdout
0038     **/
0039     void dumpExtensions     ();
0040 
0041     // IDW - Key K for switching the background (temporary) - FIX IT FOR KDE 4.2
0042     void changeBackground   ();
0043 
0044     void pushGLMatrix       ();
0045     void moveGLView         (float xChange, float yChange, float zChange);
0046     void rotateGLView       (float degrees, float x, float y, float z);
0047     void popGLMatrix        ();
0048 
0049     void drawACubie         (float size, float centre[], int axis, int angle);
0050     void finishCubie        ();
0051     void drawASticker       (float size, int color, bool blinking,
0052                  int faceNormal[], float faceCentre []);
0053     void setBlinkIntensity  (float intensity);
0054 
0055     QPoint getMousePosition ();
0056 
0057     void setBevelAmount     (int bevelPerCent);
0058 
0059 protected:
0060     /**
0061     * Reimplemented to initialize the OpenGL context.
0062     *
0063     * This method is called automatically by Qt once.
0064     */
0065     void initializeGL() override;
0066 
0067     /**
0068     * Called by Qt when the size of the GL view changes.
0069     **/
0070     void resizeGL(int w, int h) override;
0071 
0072     /**
0073     * This method actually renders the scene. It is called automatically by Qt
0074     * when paint events occur. A manual repaint (for example for animations) can
0075     * be made by calling updateGL which also calls this method.
0076     *
0077     * Do not call this method directly!
0078     **/
0079     void paintGL() override;
0080 
0081     /**
0082     * Handle mouse events. In these implementations, game->handleMouseEvent
0083     * is called with event type, button, X co-ordinate and Y co-ordinate in
0084     * OpenGL convention (zero at bottom of window).
0085     **/
0086     void mousePressEvent   (QMouseEvent* e) override;
0087     void mouseReleaseEvent (QMouseEvent* e) override;
0088 
0089     /**
0090     * Check for an OpenGL error. Dump any error to stdout
0091     * @return true, if an error occurred, otherwise false
0092     **/
0093     bool checkGLError();
0094 
0095 private:
0096     BackgroundType backgroundType;
0097     void     loadBackground (const QString & filepath);
0098     void     drawPictureBackground();
0099     void     draw4WayGradientBackground();
0100 
0101     std::unique_ptr<QOpenGLTexture> bgTexture;
0102 
0103     GLfloat  txWidth;
0104     GLfloat  txHeight;
0105 
0106     GLdouble glAspect;
0107     GLdouble bgAspect;
0108 
0109     GLfloat  bgRectX;
0110     GLfloat  bgRectY;
0111     GLfloat  bgRectZ;
0112 
0113     void turnOnLighting     ();
0114 
0115     Game * game;
0116     float bevelAmount;      // Fraction of bevelling on a cubie (eg. 0.1).
0117     QColor bgColor;     // Background color.
0118 
0119     static float colors [7] [3];
0120     float  blinkIntensity;
0121 };
0122 
0123 #endif