| 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 | #include "track.hpp" |
| 17 | |
| 18 | #include "map.hpp" |
| 19 | #include "renderer.hpp" |
| 20 | #include "scene.hpp" |
| 21 | #include "trackdata.hpp" |
| 22 | #include "tracktile.hpp" |
| 23 | |
| 24 | #include <MCAssetManager> |
| 25 | #include <MCCamera> |
| 26 | #include <MCGLShaderProgram> |
| 27 | #include <MCSurface> |
| 28 | |
| 29 | #include <cassert> |
| 30 | #include <memory> |
| 31 | |
| 32 | using std::static_pointer_cast; |
| 33 | |
| 34 | Track::Track(std::unique_ptr<TrackData> trackData) |
| 35 | : m_trackData(std::move(trackData)) |
| 36 | , m_rows(m_trackData->map().rows()) |
| 37 | , m_cols(m_trackData->map().cols()) |
| 38 | , m_width(m_cols * TrackTile::width()) |
| 39 | , m_height(m_rows * TrackTile::height()) |
| 40 | , m_asphalt(MCAssetManager::surfaceManager().surface("asphalt")) |
| 41 | { |
| 42 | assert(m_trackData); |
| 43 | } |
| 44 | |
| 45 | size_t Track::width() const |
| 46 | { |
| 47 | return m_width; |
| 48 | } |
| 49 | |
| 50 | size_t Track::height() const |
| 51 | { |
| 52 | return m_height; |
| 53 | } |
| 54 | |
| 55 | TrackData & Track::trackData() const |
| 56 | { |
| 57 | return *m_trackData; |
| 58 | } |
| 59 | |
| 60 | TrackTileS Track::trackTileAtLocation(float x, float y) const |
| 61 | { |
| 62 | // X index |
| 63 | x = x < 0 ? 0 : x; |
| 64 | size_t i = static_cast<size_t>(x * m_cols / m_width); |
| 65 | i = i >= m_cols ? m_cols - 1 : i; |
| 66 | |
| 67 | // Y index |
| 68 | y = y < 0 ? 0 : y; |
| 69 | size_t j = static_cast<size_t>(y * m_rows / m_height); |
| 70 | j = j >= m_rows ? m_rows - 1 : j; |
| 71 | |
| 72 | return static_pointer_cast<TrackTile>(m_trackData->map().getTile(i, j)); |
| 73 | } |
| 74 | |
| 75 | TrackTileS Track::finishLine() const |
| 76 | { |
| 77 | auto && map = m_trackData->map(); |
| 78 | for (size_t j = 0; j < map.rows(); j++) |
| 79 | { |
| 80 | for (size_t i = 0; i < map.cols(); i++) |
| 81 | { |
| 82 | const auto tile = static_pointer_cast<TrackTile>(map.getTile(i, j)); |
| 83 | if (tile->tileTypeEnum() == TrackTile::TileType::Finish) |
| 84 | { |
| 85 | return tile; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return nullptr; |
| 91 | } |
| 92 | |
| 93 | void Track::calculateVisibleIndices(const MCBBox<int> & r, size_t & i0, size_t & i2, size_t & j0, size_t & j2) |
| 94 | |
| 95 | { |
| 96 | // Calculate which tiles are visible in the Camera window: |
| 97 | // columns from i0 to i2 and rows from j0 to j2. |
| 98 | |
| 99 | // X low index |
| 100 | i0 = r.x1() * m_cols / m_width; |
| 101 | i0 = i0 >= m_cols ? 0 : i0; |
| 102 | |
| 103 | // X high index |
| 104 | i2 = r.x2() * m_cols / m_width; |
| 105 | i2 = i2 >= m_cols ? m_cols - 1 : i2; |
| 106 | |
| 107 | // Y low index |
| 108 | j0 = r.y1() * m_rows / m_height; |
| 109 | j0 = j0 >= m_rows ? 0 : j0; |
| 110 | |
| 111 | // Y high index |
| 112 | j2 = r.y2() * m_rows / m_height; |
| 113 | j2 = j2 >= m_rows ? m_rows - 1 : j2; |
| 114 | } |
| 115 | |
| 116 | void Track::render(MCCamera & camera) |
| 117 | { |
| 118 | // Calculate which tiles are visible |
| 119 | size_t i2, j2, i0, j0; |
| 120 | calculateVisibleIndices(camera.bbox(), i0, i2, j0, j2); |
| 121 | |
| 122 | MCGLShaderProgramPtr prog2d = Renderer::instance().program("tile2d"); |
| 123 | prog2d->bind(); |
| 124 | renderAsphalt(camera, prog2d, i0, i2, j0, j2); |
| 125 | prog2d->release(); |
| 126 | |
| 127 | MCGLShaderProgramPtr prog3d = Renderer::instance().program("tile3d"); |
| 128 | prog3d->bind(); |
| 129 | renderTiles(camera, prog3d, i0, i2, j0, j2); |
| 130 | prog3d->release(); |
| 131 | } |
| 132 | |
| 133 | void Track::renderAsphalt(MCCamera & camera, MCGLShaderProgramPtr prog, size_t i0, size_t i2, size_t j0, size_t j2) |
| 134 | { |
| 135 | float x1, y1; // Coordinates mapped to camera |
| 136 | |
| 137 | // Bind common geometry and textures for all asphalt tiles. |
| 138 | m_asphalt->setShaderProgram(prog); |
| 139 | m_asphalt->bind(); |
| 140 | |
| 141 | const MapBase & map = m_trackData->map(); |
| 142 | |
| 143 | // Loop through the visible tile matrix and draw the tiles |
| 144 | size_t x; |
| 145 | size_t y = j0 * TrackTile::height(); |
| 146 | const size_t initX = i0 * TrackTile::width(); |
| 147 | for (size_t j = j0; j <= j2; j++) |
| 148 | { |
| 149 | x = initX; |
| 150 | for (size_t i = i0; i <= i2; i++) |
| 151 | { |
| 152 | const auto tile = static_pointer_cast<TrackTile>(map.getTile(i, j)); |
| 153 | if (tile->hasAsphalt()) |
| 154 | { |
| 155 | x1 = x; |
| 156 | y1 = y; |
| 157 | camera.mapToCamera(x1, y1); |
| 158 | prog->setTransform(0, MCVector3dF(x1 + TrackTile::width() / 2, y1 + TrackTile::height() / 2, 0)); |
| 159 | m_asphalt->render(); |
| 160 | } |
| 161 | |
| 162 | x += TrackTile::width(); |
| 163 | } |
| 164 | |
| 165 | y += TrackTile::height(); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void Track::renderTiles(MCCamera & camera, MCGLShaderProgramPtr prog, size_t i0, size_t i2, size_t j0, size_t j2) |
| 170 | { |
| 171 | float x1, y1; // Coordinates mapped to camera |
| 172 | |
| 173 | struct SortedTile |
| 174 | { |
| 175 | TrackTile * tile; |
| 176 | float x1, y1; |
| 177 | }; |
| 178 | |
| 179 | // The tiles are sorted with respect to their surface in order |
| 180 | // to minimize GPU context switches. |
| 181 | std::map<MCSurfacePtr, std::vector<SortedTile>> sortedTiles; |
| 182 | |
| 183 | auto && map = m_trackData->map(); |
| 184 | |
| 185 | // Loop through the visible tile matrix and sort the tiles. |
| 186 | size_t initX = i0 * TrackTile::width(); |
| 187 | size_t x; |
| 188 | size_t y = j0 * TrackTile::height(); |
| 189 | for (size_t j = j0; j <= j2; j++) |
| 190 | { |
| 191 | x = initX; |
| 192 | for (size_t i = i0; i <= i2; i++) |
| 193 | { |
| 194 | const auto tile = static_pointer_cast<TrackTile>(map.getTile(i, j)); |
| 195 | if (auto surface = tile->surface()) |
| 196 | { |
| 197 | x1 = x; |
| 198 | y1 = y; |
| 199 | camera.mapToCamera(x1, y1); |
| 200 | |
| 201 | sortedTiles[surface].push_back({ tile.get(), x1, y1 }); |
| 202 | } |
| 203 | |
| 204 | x += TrackTile::width(); |
| 205 | } |
| 206 | |
| 207 | y += TrackTile::height(); |
| 208 | } |
| 209 | |
| 210 | // Render the tiles. |
| 211 | for (auto && iter : sortedTiles) |
| 212 | { |
| 213 | const auto surface = iter.first; |
| 214 | surface->setShaderProgram(prog); |
| 215 | surface->bind(); |
| 216 | |
| 217 | for (size_t i = 0; i < iter.second.size(); i++) |
| 218 | { |
| 219 | x1 = iter.second[i].x1; |
| 220 | y1 = iter.second[i].y1; |
| 221 | |
| 222 | const auto tile = iter.second[i].tile; |
| 223 | prog->setTransform(tile->rotation(), MCVector3dF(x1 + TrackTile::width() / 2, y1 + TrackTile::height() / 2, 0)); |
| 224 | prog->setScale(TrackTile::width() / surface->width(), TrackTile::height() / surface->height(), 1.0f); |
| 225 | surface->render(); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void Track::setNext(std::weak_ptr<Track> next) |
| 231 | { |
| 232 | m_next = next; |
| 233 | } |
| 234 | |
| 235 | std::weak_ptr<Track> Track::next() const |
| 236 | { |
| 237 | return m_next; |
| 238 | } |
| 239 | |
| 240 | void Track::setPrev(std::weak_ptr<Track> prev) |
| 241 | { |
| 242 | m_prev = prev; |
| 243 | } |
| 244 | |
| 245 | std::weak_ptr<Track> Track::prev() const |
| 246 | { |
| 247 | return m_prev; |
| 248 | } |