Skip to content

Commit 6ba3b53

Browse files
committed
Merge pull request #28 from Databean/render_roll
Render roll
2 parents b1a3e1a + be3a3c2 commit 6ba3b53

File tree

10 files changed

+172
-3
lines changed

10 files changed

+172
-3
lines changed

include/GameBoard.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "Settlement.h"
1717
#include "tinyxml2.h"
1818
#include "Road.h"
19-
19+
#include "GameDice.h"
2020

2121
class GameVisitor;
2222

@@ -29,6 +29,9 @@ class GameBoard {
2929

3030
std::map<Coordinate, std::unique_ptr<ResourceTile>> resources;
3131

32+
GameDice dice;
33+
34+
3235

3336
std::map<Coordinate, std::vector<std::shared_ptr<Road>>> roads;
3437

include/GameDice.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef GAMEDICE_H
2+
#define GAMEDICE_H
3+
4+
5+
6+
7+
8+
class GameVisitor;
9+
10+
class GameDice {
11+
private:
12+
int first;
13+
int second;
14+
15+
16+
public:
17+
int getFirst();
18+
int getSecond();
19+
bool shown;
20+
void setFirst(int newFirst);
21+
void setSecond(int newSecond);
22+
virtual void accept(GameVisitor& visitor);
23+
24+
25+
26+
};
27+
28+
#endif

include/GameView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class DrawingGameVisitor : public GameVisitor {
7979
virtual void visit(Player&);
8080
virtual void visit(ResourceTile&);
8181
virtual void visit(DevelopmentCard&);
82+
virtual void visit(GameDice&);
8283
virtual void visit(Wonder&);
8384
};
8485

include/GameVisitor.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class Road;
88
class City;
99
class Player;
1010
class DevelopmentCard;
11+
class GameDice;
1112
class Wonder;
1213

1314
/**
@@ -29,6 +30,7 @@ class GameVisitor {
2930
virtual void visit(Player&) = 0;
3031
virtual void visit(ResourceTile&) = 0;
3132
virtual void visit(DevelopmentCard&) = 0;
33+
virtual void visit(GameDice&) = 0;
3234
virtual void visit(Wonder&) = 0;
3335
};
3436

include/Serialization.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class XMLVisitor : public GameVisitor {
3333
virtual void visit(Player&);
3434
virtual void visit(ResourceTile&);
3535
virtual void visit(DevelopmentCard&);
36+
virtual void visit(GameDice&);
3637
virtual void visit(Wonder&);
3738

3839
const tinyxml2::XMLDocument& getXMLDoc() const;

resources/catan_dice_new.bmp

6 MB
Binary file not shown.

src/GameBoard.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "tinyxml2.h"
1414

1515
#include "CornerPiece.h"
16+
#include "GameDice.h"
1617

1718
#include "Settlement.h"
1819
#include "City.h"
@@ -45,6 +46,10 @@ GameBoard::GameBoard(const vector<std::string>& playerNames) {
4546
const static vector<int> boardRolls = {0, 2, 3, 3, 4, 4, 5, 5, 6, 6, 8, 8, 9, 9, 10, 10, 11, 11, 12};
4647

4748
bool valid = false;
49+
50+
51+
52+
4853

4954
const static Coordinate center {0, 4};
5055

@@ -665,6 +670,7 @@ void GameBoard::accept(GameVisitor& visitor) {
665670
it->accept(visitor);
666671
}
667672
}
673+
dice.accept(visitor);
668674
}
669675

670676
/**
@@ -805,8 +811,9 @@ std::pair<int, int> GameBoard::startTurn()
805811
int die1 = std::rand() % 6 + 1;
806812
int die2 = std::rand() % 6 + 1;
807813
int roll = die1+die2;
808-
std::cout << "\nDie 1: " << die1 << "\nDie 2: " << die2 << "\nRoll: " << roll <<"\n";
809-
814+
815+
dice.setFirst(die1);
816+
dice.setSecond(die2);
810817
if (roll==7)
811818
enableRobber();
812819

src/GameDice.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#include "GameDice.h"
2+
#include "GameVisitor.h"
3+
4+
int GameDice::getFirst(){
5+
return first;
6+
}
7+
8+
int GameDice::getSecond(){
9+
return second;
10+
}
11+
12+
void GameDice::setFirst(int newFirst){
13+
first = newFirst;
14+
}
15+
16+
void GameDice::setSecond(int newSecond){
17+
second = newSecond;
18+
}
19+
20+
void GameDice::accept(GameVisitor& visitor) {
21+
visitor.visit(*this);
22+
}

src/GameView.cpp

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ void DrawingGameVisitor::visit(GameBoard& model) {
147147

148148
}
149149

150+
151+
150152
/**
151153
* Draw a road.
152154
* @param road The road to draw.
@@ -265,17 +267,112 @@ void drawTexturedCircle(std::pair<float, float> texCenter, float texRadius, std:
265267
double tangle = ((double) -i) * (2. * M_PI) / (double)articulation;
266268
texCoordPair({texCenter.first + texRadius * std::cos(tangle), texCenter.second + texRadius * std::sin(tangle)});
267269
glVertex2d(screenCenter.first + (screenRadius * std::cos(angle)), screenCenter.second + (screenRadius * std::sin(angle)));
270+
268271
}
269272
glEnd();
270273
}
271274

275+
void drawTexturedRectangle(std::pair<float, float> texTopLeft, float sideLength, std::pair<float, float> screenTopLeft, float screenSideLength) {
276+
277+
278+
279+
glBegin(GL_QUADS);
280+
281+
282+
283+
284+
texCoordPair({texTopLeft.first + 0.0f, texTopLeft.second + 0.0f});
285+
glVertex2d(screenTopLeft.first + 0.0f, screenTopLeft.second + 0.0f);
286+
287+
texCoordPair({texTopLeft.first + sideLength, texTopLeft.second + 0.0f});
288+
glVertex2d(screenTopLeft.first + screenSideLength, screenTopLeft.second + 0.0f);
289+
290+
291+
texCoordPair({texTopLeft.first + sideLength, texTopLeft.second + sideLength});
292+
glVertex2d(screenTopLeft.first + screenSideLength, screenTopLeft.second + screenSideLength);
293+
294+
texCoordPair({texTopLeft.first + 0.0f, texTopLeft.second + sideLength});
295+
glVertex2d(screenTopLeft.first + 0.0f, screenTopLeft.second + screenSideLength);
296+
297+
/*
298+
//redraw the image for reasons
299+
texCoordPair({texTopLeft.first + sideLength, texTopLeft.second + sideLength});
300+
glVertex2d(screenTopLeft.first + screenSideLength, screenTopLeft.second + screenSideLength);
301+
302+
texCoordPair({texTopLeft.first + 0.0f, texTopLeft.second + sideLength});
303+
glVertex2d(screenTopLeft.first + 0.0f, screenTopLeft.second + screenSideLength);
304+
305+
texCoordPair({texTopLeft.first + sideLength, texTopLeft.second + 0.0f});
306+
glVertex2d(screenTopLeft.first + screenSideLength, screenTopLeft.second + 0.0f);
307+
308+
texCoordPair({texTopLeft.first + 0.0f, texTopLeft.second + 0.0f});
309+
glVertex2d(screenTopLeft.first + 0.0f, screenTopLeft.second + 0.0f);
310+
311+
*/
312+
313+
314+
315+
glEnd();
316+
317+
318+
319+
320+
321+
322+
}
323+
324+
void DrawingGameVisitor::visit(GameDice& dice) {
325+
326+
static const GLuint diceTextures = loadImageAsTexture("resources/catan_dice_new.bmp");
327+
glBindTexture(GL_TEXTURE_2D, diceTextures);
328+
329+
glColor3d(1.0, 1.0, 1.0);
330+
static const std::map<int, std::pair<float, float>> topLeftOffset = {
331+
make_pair(1, make_pair(9.f, 3.f)),
332+
make_pair(2, make_pair(134.f, 3.f)),
333+
make_pair(3, make_pair(259.f, 3.f)),
334+
make_pair(4, make_pair(9.f, 142.f)),
335+
make_pair(5, make_pair(134.f, 142.f)),
336+
make_pair(6, make_pair(259.f, 142.f))
337+
};
338+
339+
drawTexturedRectangle(topLeftOffset.find(dice.getFirst())->second, 95.f,
340+
make_pair(.7f, .9f), 0.06);
341+
342+
343+
drawTexturedRectangle(topLeftOffset.find(dice.getSecond())->second, 95.f,
344+
make_pair(.78f, .9f), 0.06);
345+
346+
347+
348+
//render all dice
349+
//drawTexturedRectangle(make_pair(9.f, 3.f), 95.f, make_pair(.6f, .9f), 0.06);
350+
//drawTexturedRectangle(make_pair(8.f, 4.f), 96.f, make_pair(.67f, .95f), 0.06);
351+
//drawTexturedRectangle(make_pair(16.f, 8.f), 96.f, make_pair(.74f, .95f), 0.06);
352+
//drawTexturedRectangle(make_pair(2.f, 8.f), 96.f, make_pair(.6f, .95f), 0.06);
353+
//drawTexturedRectangle(make_pair(2.f, 8.f), 96.f, make_pair(.6f, .95f), 0.06);
354+
//drawTexturedRectangle(make_pair(2.f, 8.f), 96.f, make_pair(.6f, .95f), 0.06);
355+
356+
glBindTexture(GL_TEXTURE_2D, 0);
357+
//hardcoded 2 die for testing
358+
//drawTexturedRectangle(make_pair(4.f, 8.f), 96.f, make_pair(.7f, .9f), 0.03);
359+
360+
361+
362+
363+
364+
//std::cout << dice.getFirst() << "\n";
365+
366+
}
367+
272368
/**
273369
* Draw a resource tile.
274370
* @param tile The tile to draw.
275371
*/
276372
void DrawingGameVisitor::visit(ResourceTile& tile) {
277373
Coordinate coord = tile.getLocation();
278374
static const GLuint tileTextures = loadImageAsTexture("resources/catan_sprite_sheet.bmp");
375+
279376
glBindTexture(GL_TEXTURE_2D, tileTextures);
280377
static const std::map<resourceType, pair<float, float>> topRightPoints = {
281378
make_pair(WOOD, make_pair(260.f, 17.f)),
@@ -330,6 +427,11 @@ void DrawingGameVisitor::visit(ResourceTile& tile) {
330427
drawTexturedCircle(numberTexPoints.find(tile.getDiceValue())->second, radius, coordToScreen(coord), 0.04);
331428
}
332429
glBindTexture(GL_TEXTURE_2D, 0);
430+
431+
432+
433+
434+
333435
}
334436

335437
/**

src/Serialization.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,9 @@ void XMLVisitor::visit(ResourceTile& tile) {
211211
tilesElement->InsertEndChild(newTileElement);
212212
}
213213

214+
void XMLVisitor::visit(GameDice& dice){
215+
216+
}
214217
/**
215218
* Serialize a development card.
216219
* @param card The card to serialize.

0 commit comments

Comments
 (0)