|
1 | 1 | #include "Renderer.h" |
2 | 2 |
|
| 3 | +#define _USE_MATH_DEFINES |
| 4 | +#include <cmath> |
| 5 | + |
3 | 6 | #include <GL/gl.h> |
4 | 7 |
|
| 8 | +#include <iostream> |
| 9 | +#include <utility> |
| 10 | + |
5 | 11 | #include "GameBoard.h" |
6 | 12 | #include "Player.h" |
7 | 13 |
|
8 | | -void renderBoard(GameBoard& board, Player& perspective) { |
| 14 | +using std::pair; |
| 15 | +using std::sin; |
| 16 | +using std::cos; |
| 17 | + |
| 18 | +inline pair<float, float> coordToScreen(const Coordinate& coord) { |
| 19 | + // TODO not magic numbers |
| 20 | + float scale = 0.1f; |
| 21 | + float angle = M_PI / 3.f; |
| 22 | + float x = .5f + (scale * coord.first) + (scale * coord.second) * cos(angle); |
| 23 | + float y = .5f + (scale * coord.second) * sin(angle); |
| 24 | + return std::make_pair(x, y); |
| 25 | +} |
| 26 | + |
| 27 | +inline void glVertexCoord(const Coordinate& coord) { |
| 28 | + pair<float, float> screenCoord = coordToScreen(coord); |
| 29 | + glVertex2f(screenCoord.first, screenCoord.second); |
| 30 | +} |
| 31 | + |
| 32 | +void drawHex(const Coordinate& coord, const ResourceTile& tile) { |
| 33 | + switch(tile.getType()) { |
| 34 | + case ResourceTile::WOOD: |
| 35 | + glColor3d(133. / 255., 66. / 255., 11. / 255.); |
| 36 | + break; |
| 37 | + case ResourceTile::SHEEP: |
| 38 | + glColor3d(191. / 255., 255. / 255., 189. / 255.); |
| 39 | + break; |
| 40 | + case ResourceTile::ORE: |
| 41 | + glColor3d(59. / 255., 59. / 255., 69. / 255.); |
| 42 | + break; |
| 43 | + case ResourceTile::BRICK: |
| 44 | + glColor3d(196. / 255., 0, 0); |
| 45 | + break; |
| 46 | + case ResourceTile::GRAIN: |
| 47 | + glColor3d(255 / 255., 249 / 255., 87 / 255.); |
| 48 | + break; |
| 49 | + case ResourceTile::DESERT: |
| 50 | + glColor3d(255. / 255., 220. / 255., 138. / 138.); |
| 51 | + break; |
| 52 | + } |
| 53 | + Coordinate adjacentCoordDiffs[] = {Coordinate(0, 1), Coordinate(1, 0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0), Coordinate(-1, 1)}; |
| 54 | + glBegin(GL_TRIANGLE_FAN); |
| 55 | + glVertexCoord(coord); |
| 56 | + for(const Coordinate& diff : adjacentCoordDiffs) { |
| 57 | + glVertexCoord(Coordinate(coord.first + diff.first, coord.second + diff.second)); |
| 58 | + } |
| 59 | + glVertexCoord(Coordinate(coord.first + adjacentCoordDiffs[0].first, coord.second + adjacentCoordDiffs[0].second)); |
| 60 | + glEnd(); |
| 61 | +} |
| 62 | + |
| 63 | +void renderBoard(const GameBoard& board, const Player& perspective) { |
9 | 64 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
10 | 65 |
|
| 66 | + for(auto& it : board.getPieces()) { |
| 67 | + const Coordinate& coord = it.first; |
| 68 | + const GamePiece& piece = *(it.second); |
| 69 | + if(dynamic_cast<const ResourceTile*>(&piece)) { |
| 70 | + const ResourceTile& tile = static_cast<const ResourceTile&>(piece); |
| 71 | + drawHex(coord, tile); |
| 72 | + } |
| 73 | + } |
| 74 | + |
11 | 75 | glFlush(); |
12 | 76 | } |
0 commit comments