| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2011 Jussi Lind <[email protected]> |
| 3 | // |
| 4 | // Dust Racing 2D is free software: you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation, either version 3 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // Dust Racing 2D is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | // GNU General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Dust Racing 2D. If not, see <http://www.gnu.org/licenses/>. |
| 15 | |
| 16 | #ifndef RENDERER_HPP |
| 17 | #define RENDERER_HPP |
| 18 | |
| 19 | #include <MCGLEW> |
| 20 | #include <MCGLShaderProgram> |
| 21 | |
| 22 | #include "eventhandler.hpp" |
| 23 | |
| 24 | #include <QOpenGLFunctions> |
| 25 | #include <QWindow> |
| 26 | |
| 27 | #include <memory> |
| 28 | #include <string> |
| 29 | #include <unordered_map> |
| 30 | |
| 31 | class InputHandler; |
| 32 | class QKeyEvent; |
| 33 | class QOpenGLFramebufferObject; |
| 34 | class QPaintEvent; |
| 35 | class Scene; |
| 36 | class MCGLScene; |
| 37 | |
| 38 | //! The singleton renderer widget and the main "window". |
| 39 | class Renderer : public QWindow, protected QOpenGLFunctions |
| 40 | { |
| 41 | Q_OBJECT |
| 42 | |
| 43 | public: |
| 44 | Renderer(int hRes, int vRes, int fullHRes, int fullVRes, bool fullScreen, MCGLScene & glScene); |
| 45 | virtual ~Renderer() override; |
| 46 | |
| 47 | //! \return the single instance. |
| 48 | static Renderer & instance(); |
| 49 | |
| 50 | void initialize(); |
| 51 | |
| 52 | //! Set game scene to be rendered. |
| 53 | void setScene(Scene & scene); |
| 54 | |
| 55 | //! Set event handler to be used. |
| 56 | void setEventHandler(EventHandler & eventHandler); |
| 57 | |
| 58 | //! \return shader program object by the given id string. |
| 59 | MCGLShaderProgramPtr program(const std::string & id); |
| 60 | |
| 61 | //! \return scene face factor 0.0..1.0. |
| 62 | float fadeValue() const; |
| 63 | |
| 64 | QSize resolution() const; |
| 65 | bool fullScreen() const; |
| 66 | |
| 67 | signals: |
| 68 | |
| 69 | void closed(); |
| 70 | void initialized(); |
| 71 | |
| 72 | public slots: |
| 73 | |
| 74 | void setEnabled(bool enable); |
| 75 | void setFadeValue(float value); |
| 76 | void setResolution(QSize resolution); |
| 77 | |
| 78 | void renderLater(); |
| 79 | void renderNow(); |
| 80 | |
| 81 | protected: |
| 82 | //! \reimp |
| 83 | bool event(QEvent *) override; |
| 84 | |
| 85 | //! \reimp |
| 86 | void exposeEvent(QExposeEvent *) override; |
| 87 | |
| 88 | //! \reimp |
| 89 | void resizeEvent(QResizeEvent *) override; |
| 90 | |
| 91 | //! \reimp |
| 92 | void keyPressEvent(QKeyEvent * event) override; |
| 93 | |
| 94 | //! \reimp |
| 95 | void keyReleaseEvent(QKeyEvent * event) override; |
| 96 | |
| 97 | //! \reimp |
| 98 | void mousePressEvent(QMouseEvent * event) override; |
| 99 | |
| 100 | //! \reimp |
| 101 | void mouseReleaseEvent(QMouseEvent * event) override; |
| 102 | |
| 103 | //! \reimp |
| 104 | void mouseMoveEvent(QMouseEvent * event) override; |
| 105 | |
| 106 | private: |
| 107 | void initializeFrameBufferObjects(); |
| 108 | void initializeMaterial(); |
| 109 | |
| 110 | //! Load vertex and fragment shaders. |
| 111 | void loadShaders(); |
| 112 | void loadFonts(); |
| 113 | |
| 114 | void createProgramFromSource(std::string handle, std::string vshSource, std::string fshSource); |
| 115 | |
| 116 | void resizeWindow(); |
| 117 | void centerWindow(); |
| 118 | |
| 119 | void render(); |
| 120 | void renderHud(); |
| 121 | void renderObjects(); |
| 122 | void renderScreen(); |
| 123 | void resizeGlScene(int viewWidth, int viewHeight); |
| 124 | |
| 125 | typedef std::unordered_map<std::string, MCGLShaderProgramPtr> ShaderHash; |
| 126 | |
| 127 | QOpenGLContext * m_context = nullptr; |
| 128 | Scene * m_scene = nullptr; |
| 129 | EventHandler * m_eventHandler = nullptr; |
| 130 | |
| 131 | const float m_viewAngle = 22.5f; |
| 132 | const float m_zNear = 10.0f; |
| 133 | const float m_zFar = 10'000.0f; |
| 134 | |
| 135 | float m_fadeValue = 1.0f; |
| 136 | |
| 137 | ShaderHash m_shaderHash; |
| 138 | |
| 139 | bool m_enabled = false; |
| 140 | |
| 141 | int m_hRes; |
| 142 | int m_vRes; |
| 143 | int m_fullHRes; |
| 144 | int m_fullVRes; |
| 145 | |
| 146 | int m_frameCounter = 0; |
| 147 | bool m_fullScreen; |
| 148 | bool m_updatePending = false; |
| 149 | |
| 150 | static Renderer * m_instance; |
| 151 | |
| 152 | std::unique_ptr<QOpenGLFramebufferObject> m_fbo; |
| 153 | std::unique_ptr<QOpenGLFramebufferObject> m_shadowFbo; |
| 154 | |
| 155 | MCGLScene & m_glScene; |
| 156 | std::shared_ptr<MCGLMaterial> m_material; |
| 157 | bool m_rendererInitialized = false; |
| 158 | }; |
| 159 | |
| 160 | #endif // RENDERER_HPP |