|
1 | 1 | #include "GameView.h" |
2 | 2 |
|
| 3 | +#define _USE_MATH_DEFINES |
| 4 | + |
3 | 5 | #include <stdexcept> |
| 6 | +#include <cmath> |
4 | 7 |
|
5 | 8 | #include "GameBoard.h" |
6 | 9 | #include "GameController.h" |
@@ -48,11 +51,50 @@ void DrawingGameVisitor::visit(GameBoard& model) { |
48 | 51 | } |
49 | 52 |
|
50 | 53 | void DrawingGameVisitor::visit(Road& road) { |
| 54 | + static const auto roadWidth = 0.01; |
| 55 | + |
| 56 | + auto startScreenPos = coordToScreen(road.getStart()); |
| 57 | + auto endScreenPos = coordToScreen(road.getEnd()); |
| 58 | + |
| 59 | + auto roadAngle = 0.; |
| 60 | + /* |
| 61 | + * Fixes a bug in road rendering of horizontal road. My best guess is that |
| 62 | + * this is necessary because small floating point errors in the conversion from game coordinates to screen coordinates |
| 63 | + * mess up the angle calculation. |
| 64 | + */ |
| 65 | + if(road.getEnd().second == road.getStart().second) { |
| 66 | + roadAngle = 0; |
| 67 | + } else { |
| 68 | + roadAngle = std::atan2(endScreenPos.first - startScreenPos.first, endScreenPos.second - startScreenPos.second);\ |
| 69 | + } |
| 70 | + auto roadPerpAngle = roadAngle + (M_PI / 2.); |
| 71 | + |
| 72 | + auto cosPerp = std::cos(roadPerpAngle); |
| 73 | + auto sinPerp = std::sin(roadPerpAngle); |
51 | 74 |
|
| 75 | + glColor3d(0., 0., 0.); |
| 76 | + glBindTexture(GL_TEXTURE_2D, 0); |
| 77 | + glBegin(GL_QUADS); |
| 78 | + glVertex2d(startScreenPos.first - cosPerp * roadWidth, startScreenPos.second - sinPerp * roadWidth); |
| 79 | + glVertex2d(startScreenPos.first + cosPerp * roadWidth, startScreenPos.second + sinPerp * roadWidth); |
| 80 | + glVertex2d(endScreenPos.first + cosPerp * roadWidth, endScreenPos.second + sinPerp * roadWidth); |
| 81 | + glVertex2d(endScreenPos.first - cosPerp * roadWidth, endScreenPos.second - sinPerp * roadWidth); |
| 82 | + glEnd(); |
52 | 83 | } |
53 | 84 |
|
54 | 85 | void DrawingGameVisitor::visit(Settlement& settlement) { |
| 86 | + static const auto settlementRadius = 0.03; |
| 87 | + |
| 88 | + auto centerScreenPos = coordToScreen(settlement.getLocation()); |
55 | 89 |
|
| 90 | + glBindTexture(GL_TEXTURE_2D, 0); |
| 91 | + glColor3d(0., 0., 0.); |
| 92 | + glBegin(GL_QUADS); |
| 93 | + glVertex2d(centerScreenPos.first, centerScreenPos.second + settlementRadius); |
| 94 | + glVertex2d(centerScreenPos.first + settlementRadius, centerScreenPos.second); |
| 95 | + glVertex2d(centerScreenPos.first, centerScreenPos.second - settlementRadius); |
| 96 | + glVertex2d(centerScreenPos.first - settlementRadius, centerScreenPos.second); |
| 97 | + glEnd(); |
56 | 98 | } |
57 | 99 |
|
58 | 100 | void DrawingGameVisitor::visit(City& city) { |
@@ -87,6 +129,7 @@ void DrawingGameVisitor::visit(ResourceTile& tile) { |
87 | 129 | throw runtime_error("Cannot draw this tile; it is invalid."); |
88 | 130 | } |
89 | 131 | const texCoordList& texCoords = resourceTexCoords.find(tile.resource)->second; |
| 132 | + glColor3d(1.0, 1.0, 1.0); |
90 | 133 | glBegin(GL_TRIANGLE_FAN); |
91 | 134 | texCoordPair(averagePoint(texCoords)); |
92 | 135 | vertexPair(coord); |
|
0 commit comments