Skip to content

Commit 7f0cca0

Browse files
committed
Added road drawing (colorless)
1 parent 2b697aa commit 7f0cca0

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

src/GameView.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#include "GameView.h"
22

3+
#define _USE_MATH_DEFINES
4+
35
#include <stdexcept>
6+
#include <cmath>
47

58
#include "GameBoard.h"
69
#include "GameController.h"
@@ -48,7 +51,35 @@ void DrawingGameVisitor::visit(GameBoard& model) {
4851
}
4952

5053
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.);
5171

72+
auto cosPerp = std::cos(roadPerpAngle);
73+
auto sinPerp = std::sin(roadPerpAngle);
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();
5283
}
5384

5485
void DrawingGameVisitor::visit(Settlement& settlement) {
@@ -87,6 +118,7 @@ void DrawingGameVisitor::visit(ResourceTile& tile) {
87118
throw runtime_error("Cannot draw this tile; it is invalid.");
88119
}
89120
const texCoordList& texCoords = resourceTexCoords.find(tile.resource)->second;
121+
glColor3d(1.0, 1.0, 1.0);
90122
glBegin(GL_TRIANGLE_FAN);
91123
texCoordPair(averagePoint(texCoords));
92124
vertexPair(coord);

src/main.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@ void initGame() {
2525
}
2626

2727
void initOpenGL() {
28-
glEnable(GL_DEPTH_TEST);
2928
glEnable(GL_TEXTURE_2D);
3029
glEnable (GL_BLEND);
3130
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
3231
glClearColor(1.f, 1.f, 1.f, 1.f);
33-
//glDepthFunc(GL_NEVER);
32+
glDepthFunc(GL_NEVER);
3433
}
3534

3635
/* function to reset our viewport after a window resize */
@@ -66,10 +65,17 @@ int main(int argc, char *argv[]) {
6665
vector<unique_ptr<Player>> players;
6766
players.emplace_back(unique_ptr<Player>(new Player("test")));
6867

68+
Player& firstPlayer = *players[0];
69+
6970
GameBoard model(std::move(players));
7071
GameController controller(model);
7172
GameView view(model, controller);
7273

74+
model.PlaceSettlement(Coordinate{0, 0}, firstPlayer);
75+
model.PlaceRoad(Coordinate{0, 0}, Coordinate{1, 0}, firstPlayer);
76+
model.PlaceRoad(Coordinate{1, 0}, Coordinate{1, 1}, firstPlayer);
77+
model.PlaceRoad(Coordinate{1, 1}, Coordinate{0, 2}, firstPlayer);
78+
7379
bool running = true;
7480
while(running) {
7581
SDL_Event event;
@@ -84,6 +90,8 @@ int main(int argc, char *argv[]) {
8490
}
8591

8692
SDL_GL_DeleteContext(glContext);
93+
SDL_DestroyWindow(displayWindow);
94+
SDL_DestroyRenderer(displayRenderer);
8795
SDL_Quit();
8896

8997
return 0;

0 commit comments

Comments
 (0)