| 1 | // This file is part of Dust Racing 2D. |
| 2 | // Copyright (C) 2013 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 "graphicsfactory.hpp" |
| 17 | |
| 18 | #include <MCAssetManager> |
| 19 | #include <MCSurface> |
| 20 | #include <MCSurfaceManager> |
| 21 | #include <MCSurfaceMetaData> |
| 22 | |
| 23 | // These are needed to generate the number plate surface on top of the car. |
| 24 | #include <QFont> |
| 25 | #include <QFontMetrics> |
| 26 | #include <QPainter> |
| 27 | #include <QPixmap> |
| 28 | |
| 29 | #include <cassert> |
| 30 | |
| 31 | MCSurfacePtr GraphicsFactory::generateNumberSurface(size_t index) |
| 32 | { |
| 33 | const int pixmapWidth = 32; |
| 34 | const int pixmapHeight = 32; |
| 35 | |
| 36 | QPixmap numberPixmap(pixmapWidth, pixmapHeight); |
| 37 | numberPixmap.fill(Qt::white); |
| 38 | |
| 39 | QPainter painter; |
| 40 | painter.begin(&numberPixmap); |
| 41 | |
| 42 | QFont font; |
| 43 | font.setPixelSize(pixmapHeight); |
| 44 | font.setBold(true); |
| 45 | |
| 46 | painter.setFont(font); |
| 47 | painter.setPen(QColor(0, 0, 0)); |
| 48 | painter.drawText( |
| 49 | 0, |
| 50 | 0, |
| 51 | pixmapWidth, |
| 52 | pixmapHeight, |
| 53 | Qt::AlignCenter, |
| 54 | std::to_string(index + 1).c_str()); |
| 55 | |
| 56 | painter.end(); |
| 57 | |
| 58 | // Note, that the size of the pixmap doesn't affect the size of the actual |
| 59 | // surface / texture rendering that pixmap. |
| 60 | MCSurfaceMetaData surfaceData; |
| 61 | surfaceData.height = { 9, true }; |
| 62 | surfaceData.width = { 9, true }; |
| 63 | surfaceData.minFilter = { GL_LINEAR, true }; |
| 64 | surfaceData.magFilter = { GL_LINEAR, true }; |
| 65 | surfaceData.handle = "Number" + std::to_string(index); |
| 66 | |
| 67 | return MCAssetManager::surfaceManager().createSurfaceFromImage(surfaceData, numberPixmap.toImage()); |
| 68 | } |
| 69 | |
| 70 | MCSurfacePtr GraphicsFactory::generateMinimapMarker() |
| 71 | { |
| 72 | const int pixmapWidth = 64; |
| 73 | const int pixmapHeight = 64; |
| 74 | |
| 75 | QPixmap markerPixmap(pixmapWidth, pixmapHeight); |
| 76 | markerPixmap.fill(Qt::transparent); |
| 77 | |
| 78 | QPainter painter; |
| 79 | painter.begin(&markerPixmap); |
| 80 | painter.setBrush(Qt::white); |
| 81 | auto rect = markerPixmap.rect(); |
| 82 | rect.adjust(2, 2, -2, -2); |
| 83 | painter.drawEllipse(rect); |
| 84 | painter.end(); |
| 85 | |
| 86 | // Note, that the size of the pixmap doesn't affect the size of the actual |
| 87 | // surface / texture rendering that pixmap. |
| 88 | MCSurfaceMetaData surfaceData; |
| 89 | surfaceData.height = { 9, true }; |
| 90 | surfaceData.width = { 9, true }; |
| 91 | surfaceData.minFilter = { GL_LINEAR, true }; |
| 92 | surfaceData.magFilter = { GL_LINEAR, true }; |
| 93 | surfaceData.handle = "Minimap"; |
| 94 | |
| 95 | return MCAssetManager::surfaceManager().createSurfaceFromImage(surfaceData, markerPixmap.toImage()); |
| 96 | } |