| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2012 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 | #include "startlightsoverlay.hpp" |
| 17 | #include "startlights.hpp" |
| 18 | |
| 19 | #include <MCAssetManager> |
| 20 | #include <MCGLColor> |
| 21 | #include <MCSurface> |
| 22 | |
| 23 | StartlightsOverlay::StartlightsOverlay(Startlights & model) |
| 24 | : m_startLightOn(MCAssetManager::surfaceManager().surface("startLightOn")) |
| 25 | , m_startLightOnCorner(MCAssetManager::surfaceManager().surface("startLightOnCorner")) |
| 26 | , m_startLightOff(MCAssetManager::surfaceManager().surface("startLightOff")) |
| 27 | , m_startLightOffCorner(MCAssetManager::surfaceManager().surface("startLightOffCorner")) |
| 28 | , m_startLightGlow(MCAssetManager::surfaceManager().surface("startLightGlow")) |
| 29 | , m_model(model) |
| 30 | , m_alpha(1.0) |
| 31 | { |
| 32 | m_startLightOff->material()->setAlphaBlend(true); |
| 33 | m_startLightOffCorner->material()->setAlphaBlend(true); |
| 34 | m_startLightGlow->setColor(MCGLColor(1.5f, 0.25f, 0.25f, 0.4f)); |
| 35 | } |
| 36 | |
| 37 | void StartlightsOverlay::renderLights(int rows, int litRows, float glowScale, bool glowAlways) const |
| 38 | { |
| 39 | const int cols = 8; |
| 40 | |
| 41 | const float x = m_model.pos().i() - (cols - 1) * m_startLightOn->width() / 2; |
| 42 | const float y = m_model.pos().j() - (rows - 1) * m_startLightOn->height() / 2; |
| 43 | const float h = rows * m_startLightOn->height(); |
| 44 | |
| 45 | // Body |
| 46 | for (int row = 0; row < rows; row++) |
| 47 | { |
| 48 | for (int col = 0; col < cols; col++) |
| 49 | { |
| 50 | const MCVector3dF pos( |
| 51 | x + col * m_startLightOff->width(), |
| 52 | y + h - row * m_startLightOff->height()); |
| 53 | |
| 54 | if (row < litRows) |
| 55 | { |
| 56 | if (row == 0 && col == 0) |
| 57 | { |
| 58 | m_startLightOnCorner->render(nullptr, pos, 0); |
| 59 | } |
| 60 | else if (row == rows - 1 && col == 0) |
| 61 | { |
| 62 | m_startLightOnCorner->render(nullptr, pos, 90); |
| 63 | } |
| 64 | else if (row == rows - 1 && col == cols - 1) |
| 65 | { |
| 66 | m_startLightOnCorner->render(nullptr, pos, 180); |
| 67 | } |
| 68 | else if (row == 0 && col == cols - 1) |
| 69 | { |
| 70 | m_startLightOnCorner->render(nullptr, pos, 270); |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | m_startLightOn->render(nullptr, pos, 0); |
| 75 | } |
| 76 | } |
| 77 | else |
| 78 | { |
| 79 | if (row == 0 && col == 0) |
| 80 | { |
| 81 | m_startLightOffCorner->render(nullptr, pos, 0); |
| 82 | } |
| 83 | else if (row == rows - 1 && col == 0) |
| 84 | { |
| 85 | m_startLightOffCorner->render(nullptr, pos, 90); |
| 86 | } |
| 87 | else if (row == rows - 1 && col == cols - 1) |
| 88 | { |
| 89 | m_startLightOffCorner->render(nullptr, pos, 180); |
| 90 | } |
| 91 | else if (row == 0 && col == cols - 1) |
| 92 | { |
| 93 | m_startLightOffCorner->render(nullptr, pos, 270); |
| 94 | } |
| 95 | else |
| 96 | { |
| 97 | m_startLightOff->render(nullptr, pos, 0); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Glow |
| 104 | for (int row = 0; row < rows; row++) |
| 105 | { |
| 106 | for (int col = 0; col < cols; col++) |
| 107 | { |
| 108 | if (row < litRows || glowAlways) |
| 109 | { |
| 110 | m_startLightGlow->setScale(MCVector3dF(glowScale, glowScale, 1.0f)); |
| 111 | m_startLightGlow->render( |
| 112 | nullptr, |
| 113 | MCVector3dF( |
| 114 | x + col * m_startLightOn->width(), |
| 115 | y + h - row * m_startLightOn->height()), |
| 116 | 0); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void StartlightsOverlay::setDimensions(int width, int height) |
| 123 | { |
| 124 | OverlayBase::setDimensions(width, height); |
| 125 | m_model.setDimensions(width, height); |
| 126 | } |
| 127 | |
| 128 | void StartlightsOverlay::render() |
| 129 | { |
| 130 | glDisable(GL_DEPTH_TEST); |
| 131 | |
| 132 | switch (m_model.state()) |
| 133 | { |
| 134 | case Startlights::State::FirstRow: |
| 135 | m_alpha = 1.0; |
| 136 | renderLights(3, 1, m_model.glowScale()); |
| 137 | break; |
| 138 | |
| 139 | case Startlights::State::SecondRow: |
| 140 | renderLights(3, 2, m_model.glowScale()); |
| 141 | break; |
| 142 | |
| 143 | case Startlights::State::ThirdRow: |
| 144 | renderLights(3, 3, m_model.glowScale()); |
| 145 | break; |
| 146 | |
| 147 | case Startlights::State::Go: |
| 148 | m_alpha *= 0.98f; |
| 149 | m_startLightOff->setColor(MCGLColor(1.0, 1.0, 1.0, m_alpha)); |
| 150 | m_startLightOffCorner->setColor(MCGLColor(1.0, 1.0, 1.0, m_alpha)); |
| 151 | renderLights(3, 0, m_model.glowScale(), true); |
| 152 | break; |
| 153 | |
| 154 | case Startlights::State::Disappear: |
| 155 | case Startlights::State::Appear: |
| 156 | m_startLightOff->setColor(MCGLColor(1.0, 1.0, 1.0, 1.0)); |
| 157 | m_startLightOffCorner->setColor(MCGLColor(1.0, 1.0, 1.0, 1.0)); |
| 158 | renderLights(3, 0, m_model.glowScale()); |
| 159 | break; |
| 160 | |
| 161 | case Startlights::State::End: |
| 162 | break; |
| 163 | |
| 164 | default: |
| 165 | break; |
| 166 | } |
| 167 | } |