Skip to content

Commit 015f0a8

Browse files
committed
Merged together old board renderer with master
2 parents 7a8efb8 + b1d40ab commit 015f0a8

File tree

12 files changed

+350
-31
lines changed

12 files changed

+350
-31
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ EXECUTABLE := warsofcatan
77
ALLFILES := $(wildcard $(SRC_HOME)/*) $(wildcard $(INCL_HOME)/*)
88
export CXX := g++
99
export LD := g++
10-
export CXXFLAGS := -g -I$(INCL_HOME) -std=gnu++11
10+
export CXXFLAGS := -g -I$(INCL_HOME) -std=gnu++0x -Wall
1111
export LDFLAGS := -lSDL2 -lGL -lGLU
1212

1313
.PHONY: all

include/GameBoard.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ class GameBoard {
2525
GameBoard(GameBoard&) = delete;
2626
~GameBoard();
2727
GameBoard& operator=(GameBoard&) = delete;
28-
28+
2929
int saveBoardToFile(std::string filename);
3030
int loadBoardFromFile(std::string filename);
31+
const std::map<Coordinate, std::unique_ptr<GamePiece>>& getPieces() const;
3132
};
3233

3334
#endif

include/GamePiece.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,26 @@ class GamePiece {
1414
GamePiece(GamePiece&) = delete;
1515
virtual ~GamePiece();
1616
//virtual GamePiece& operator=(GamePiece&) = delete;
17+
18+
Coordinate getCoordinates() const;
19+
GameBoard& getBoard();
20+
const GameBoard& getBoard() const;
1721
};
1822

1923
class ResourceTile : public GamePiece {
24+
public:
25+
enum Type { WOOD, SHEEP, ORE, BRICK, GRAIN, DESERT };
2026
private:
21-
27+
Type type;
28+
unsigned short diceValue;
2229
public:
23-
ResourceTile(GameBoard& board);
30+
ResourceTile(GameBoard& board, Type type, unsigned short diceValue);
2431
ResourceTile(ResourceTile&) = delete;
2532
virtual ~ResourceTile();
2633
//virtual ResourceTile& operator=(ResourceTile&) = delete;
34+
35+
Type getType() const;
36+
unsigned short getDiceValue() const;
2737
};
2838

2939
class Settlement : public GamePiece {

include/Player.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
#ifndef PLAYER_H
22
#define PLAYER_H
33

4+
#include <string>
5+
6+
#include "Resources.h"
7+
48
class Player {
59
private:
6-
10+
std::string name;
11+
Resources resources;
712
public:
8-
Player();
13+
Player(const std::string& name);
914
Player(Player&) = delete;
1015
~Player();
1116
Player& operator=(Player&) = delete;
17+
18+
const std::string& getName() const;
19+
void setName(const std::string& newName);
20+
21+
const Resources& getResources() const;
22+
void setResources(const Resources& newResources);
1223
};
1324

1425
#endif

include/Renderer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
#include "GameBoard.h"
55
#include "Player.h"
66

7-
void renderBoard(GameBoard& board, Player& perspective);
7+
void renderBoard(const GameBoard& board, const Player& perspective);
88

99
#endif

include/Resources.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef RESOURCES_H
2+
#define RESOURCES_H
3+
4+
class Resources {
5+
public:
6+
typedef unsigned short rtype;
7+
private:
8+
rtype wood;
9+
rtype sheep;
10+
rtype grain;
11+
rtype ore;
12+
rtype brick;
13+
public:
14+
Resources();
15+
Resources(rtype wood, rtype sheep, rtype grain, rtype ore, rtype brick);
16+
Resources(const Resources&);
17+
~Resources();
18+
Resources& operator=(const Resources&);
19+
20+
rtype getWood() const;
21+
void setWood(rtype newWood);
22+
23+
rtype getSheep() const;
24+
void setSheep(rtype newSheep);
25+
26+
rtype getGrain() const;
27+
void setGrain(rtype newGrain);
28+
29+
rtype getOre() const;
30+
void setOre(rtype newOre);
31+
32+
rtype getBrick() const;
33+
void setBrick(rtype newBrick);
34+
35+
Resources operator+(const Resources&) const;
36+
Resources& operator+=(const Resources&);
37+
38+
Resources operator-(const Resources&) const;
39+
Resources& operator-=(const Resources&);
40+
41+
bool operator<(const Resources&) const;
42+
bool operator>(const Resources&) const;
43+
};
44+
45+
#endif

src/GameBoard.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
#include "GameBoard.h"
22

3+
#include <map>
4+
#include <memory>
5+
6+
using std::map;
7+
using std::unique_ptr;
8+
39
GameBoard::GameBoard() {
4-
10+
pieces.emplace(Coordinate(0, 0), unique_ptr<GamePiece>(new ResourceTile(*this, ResourceTile::WOOD, 2)));
11+
pieces.emplace(Coordinate(1, 1), unique_ptr<GamePiece>(new ResourceTile(*this, ResourceTile::SHEEP, 2)));
12+
pieces.emplace(Coordinate(-1, -1), unique_ptr<GamePiece>(new ResourceTile(*this, ResourceTile::BRICK, 2)));
13+
pieces.emplace(Coordinate(2, -1), unique_ptr<GamePiece>(new ResourceTile(*this, ResourceTile::ORE, 2)));
14+
pieces.emplace(Coordinate(-2, 1), unique_ptr<GamePiece>(new ResourceTile(*this, ResourceTile::DESERT, 2)));
15+
pieces.emplace(Coordinate(1, -2), unique_ptr<GamePiece>(new ResourceTile(*this, ResourceTile::GRAIN, 2)));
16+
pieces.emplace(Coordinate(-1, 2), unique_ptr<GamePiece>(new ResourceTile(*this, ResourceTile::BRICK, 2)));
517
}
618

719
GameBoard::~GameBoard() {
@@ -19,9 +31,6 @@ int GameBoard::saveBoardToFile(std::string filename){
1931
} catch (std::ofstream::failure e){
2032
std::cerr << "Exception opening/reading/closing file \n";
2133
}
22-
23-
24-
2534
return 0;
2635
}
2736

@@ -57,14 +66,6 @@ int GameBoard::constructFileFromBoard(std::ofstream &file){
5766
return 0;
5867
}
5968

60-
61-
62-
63-
64-
65-
66-
67-
68-
69-
70-
69+
const map<Coordinate, unique_ptr<GamePiece>>& GameBoard::getPieces() const {
70+
return pieces;
71+
}

src/GamePiece.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
#include "GameBoard.h"
44

5+
#include <stdexcept>
6+
7+
using std::runtime_error;
8+
59
GamePiece::GamePiece(GameBoard& board) : board(board) {
610

711
}
@@ -10,14 +14,44 @@ GamePiece::~GamePiece() {
1014

1115
}
1216

13-
ResourceTile::ResourceTile(GameBoard& board) : GamePiece(board) {
14-
17+
Coordinate GamePiece::getCoordinates() const {
18+
for(auto& it : board.getPieces()) {
19+
if(it.second.get() == this) {
20+
return it.first;
21+
}
22+
}
23+
throw runtime_error("This GamePiece does not exist on the board it holds a reference to.");
24+
}
25+
26+
GameBoard& GamePiece::getBoard() {
27+
return board;
28+
}
29+
30+
const GameBoard& GamePiece::getBoard() const {
31+
return board;
32+
}
33+
34+
ResourceTile::ResourceTile(GameBoard& board, Type type, unsigned short diceValue) : GamePiece(board), type(type), diceValue(diceValue) {
35+
if(type != WOOD && type != SHEEP && type != ORE && type != BRICK && type != GRAIN && type != DESERT) {
36+
throw runtime_error("Invalid resource tile type");
37+
}
38+
if(diceValue < 2 || diceValue > 12) {
39+
throw runtime_error("Invalid dice value");
40+
}
1541
}
1642

1743
ResourceTile::~ResourceTile() {
1844

1945
}
2046

47+
ResourceTile::Type ResourceTile::getType() const {
48+
return type;
49+
}
50+
51+
unsigned short ResourceTile::getDiceValue() const {
52+
return diceValue;
53+
}
54+
2155
Settlement::Settlement(GameBoard& board, Player& owner) : GamePiece(board), owner(owner) {
2256

2357
}

src/Player.cpp

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,27 @@
11
#include "Player.h"
22

3-
Player::Player() {
3+
using std::string;
4+
5+
Player::Player(const string& name) : name(name), resources() {
46

57
}
68

79
Player::~Player() {
810

9-
}
11+
}
12+
13+
const string& Player::getName() const {
14+
return name;
15+
}
16+
17+
void Player::setName(const string& newName) {
18+
name = newName;
19+
}
20+
21+
const Resources& Player::getResources() const {
22+
return resources;
23+
}
24+
25+
void Player::setResources(const Resources& newResources) {
26+
resources = newResources;
27+
}

src/Renderer.cpp

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,76 @@
11
#include "Renderer.h"
22

3+
#define _USE_MATH_DEFINES
4+
#include <cmath>
5+
36
#include <GL/gl.h>
47

8+
#include <iostream>
9+
#include <utility>
10+
511
#include "GameBoard.h"
612
#include "Player.h"
713

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) {
964
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
1065

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+
1175
glFlush();
1276
}

0 commit comments

Comments
 (0)