Skip to content

Commit 235bb71

Browse files
committed
Merge pull request #9 from Databean/renderCornerPieces
Added rendering of roads and settlements.
2 parents 2b697aa + c626833 commit 235bb71

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

src/GameBoard.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,10 +422,12 @@ void GameBoard::PlaceCity(Coordinate location, Player& Owner){
422422

423423
void GameBoard::accept(GameVisitor& visitor) {
424424
visitor.visit(*this);
425-
for(auto& it : corners) {
425+
// Drawing needs this to happen in this order. Visitors technically should be order-independent, but this was an easy fix at the moment.
426+
// Keep that in mind when modifying this.
427+
for(auto& it : resources) {
426428
it.second->accept(visitor);
427429
}
428-
for(auto& it : resources) {
430+
for(auto& it : corners) {
429431
it.second->accept(visitor);
430432
}
431433
for(auto& roadCoordVec : roads) {

src/GameView.cpp

Lines changed: 43 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,11 +51,50 @@ 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.);
71+
72+
auto cosPerp = std::cos(roadPerpAngle);
73+
auto sinPerp = std::sin(roadPerpAngle);
5174

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) {
86+
static const auto settlementRadius = 0.03;
87+
88+
auto centerScreenPos = coordToScreen(settlement.getLocation());
5589

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();
5698
}
5799

58100
void DrawingGameVisitor::visit(City& city) {
@@ -87,6 +129,7 @@ void DrawingGameVisitor::visit(ResourceTile& tile) {
87129
throw runtime_error("Cannot draw this tile; it is invalid.");
88130
}
89131
const texCoordList& texCoords = resourceTexCoords.find(tile.resource)->second;
132+
glColor3d(1.0, 1.0, 1.0);
90133
glBegin(GL_TRIANGLE_FAN);
91134
texCoordPair(averagePoint(texCoords));
92135
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)