Skip to content

Commit 9e3143a

Browse files
committed
Reorganized view functionality to align with MVC
1 parent 243716f commit 9e3143a

File tree

9 files changed

+204
-118
lines changed

9 files changed

+204
-118
lines changed

include/GameController.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef GAME_CONTROLLER_H
2+
#define GAME_CONTROLLER_H
3+
4+
class GameBoard;
5+
6+
class GameController {
7+
private:
8+
GameBoard& board;
9+
10+
GameController(const GameController& o) : board(o.board) {} //deleted
11+
GameController& operator=(const GameController& o) { return *this; } //deleted
12+
public:
13+
GameController(GameBoard&);
14+
~GameController();
15+
};
16+
17+
#endif

include/GameView.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef GAME_VIEW_H
2+
#define GAME_VIEW_H
3+
4+
#include "SDL2/SDL.h"
5+
6+
#include "GameVisitor.h"
7+
8+
class GameBoard;
9+
class GameController;
10+
11+
class GameView {
12+
private:
13+
GameBoard& model;
14+
GameController& controller;
15+
16+
GameView(const GameView& o) : model(o.model), controller(o.controller) {} //deleted
17+
GameView& operator=(const GameView& o) { return *this; } //deleted
18+
public:
19+
GameView(GameBoard&, GameController&);
20+
~GameView();
21+
22+
void render();
23+
bool acceptInput(SDL_Event& event);
24+
};
25+
26+
class DrawingGameVisitor : public GameVisitor {
27+
private:
28+
GameView& view;
29+
30+
DrawingGameVisitor(const DrawingGameVisitor& o) : view(o.view) {} //deleted
31+
DrawingGameVisitor& operator=(const DrawingGameVisitor& o) { return *this; } //deleted
32+
public:
33+
DrawingGameVisitor(GameView& view);
34+
~DrawingGameVisitor();
35+
36+
virtual void visit(GameBoard&);
37+
virtual void visit(Road&);
38+
virtual void visit(Settlement&);
39+
virtual void visit(City&);
40+
virtual void visit(Player&);
41+
virtual void visit(ResourceTile&);
42+
virtual void visit(DevelopmentCard&);
43+
};
44+
45+
#endif

include/Renderer.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
#ifndef RENDERER_H
22
#define RENDERER_H
33

4+
#include <SDL2/SDL.h>
5+
#include <SDL2/SDL_opengl.h>
6+
#include <GL/gl.h>
7+
48
#include "GameBoard.h"
59
#include "Player.h"
610

711
void renderBoard(const GameBoard& board, const Player& perspective);
812

13+
GLuint loadImageAsTexture(const std::string& name);
14+
15+
std::pair<float, float> coordToScreen(const Coordinate& coord);
16+
17+
void vertexPair(const Coordinate& coord);
18+
19+
void texCoordPair(const std::pair<float, float>& p);
20+
21+
std::pair<float, float> averagePoint(const std::vector<std::pair<float, float>>& points);
22+
923
#endif

include/UserInput.h

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/GameController.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "GameController.h"
2+
3+
#include "GameBoard.h"
4+
5+
GameController::GameController(GameBoard& board) : board(board) {
6+
7+
}
8+
9+
GameController::~GameController() {
10+
11+
}
12+

src/GameView.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include "GameView.h"
2+
3+
#include "GameBoard.h"
4+
#include "GameController.h"
5+
#include "Renderer.h"
6+
7+
using std::make_pair;
8+
using std::pair;
9+
using std::runtime_error;
10+
using std::string;
11+
12+
GameView::GameView(GameBoard& model, GameController& controller) : model(model), controller(controller) {
13+
14+
}
15+
16+
GameView::~GameView() {
17+
18+
}
19+
20+
void GameView::render() {
21+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
22+
23+
DrawingGameVisitor visitor(*this);
24+
model.accept(visitor);
25+
26+
glFlush();
27+
}
28+
29+
bool GameView::acceptInput(SDL_Event& event) {
30+
if(event.type == SDL_QUIT) {
31+
return false;
32+
}
33+
return true;
34+
}
35+
36+
DrawingGameVisitor::DrawingGameVisitor(GameView& view) : view(view) {
37+
38+
}
39+
40+
DrawingGameVisitor::~DrawingGameVisitor() {
41+
42+
}
43+
44+
void DrawingGameVisitor::visit(GameBoard& model) {
45+
46+
}
47+
48+
void DrawingGameVisitor::visit(Road& road) {
49+
50+
}
51+
52+
void DrawingGameVisitor::visit(Settlement& settlement) {
53+
54+
}
55+
56+
void DrawingGameVisitor::visit(City& city) {
57+
58+
}
59+
60+
void DrawingGameVisitor::visit(Player& player) {
61+
62+
}
63+
64+
void DrawingGameVisitor::visit(ResourceTile& tile) {
65+
Coordinate coord = tile.getLocation();
66+
static GLuint tileTextures = loadImageAsTexture("resources/tiles.bmp");
67+
glBindTexture(GL_TEXTURE_2D, tileTextures);
68+
typedef std::vector<pair<float, float> > texCoordList;
69+
static const std::map<resourceType, texCoordList> resourceTexCoords = {
70+
make_pair(WHEAT, texCoordList { make_pair(377, 73), make_pair(500, 285),
71+
make_pair(380, 502), make_pair(136, 503), make_pair(10, 288), make_pair(134, 74)}),
72+
make_pair(SHEEP, texCoordList { make_pair(959, 75), make_pair(1076, 288),
73+
make_pair(955, 503), make_pair(712, 501), make_pair(586, 289), make_pair(708, 73)}),
74+
make_pair(WOOD, texCoordList { make_pair(1491, 60), make_pair(1618, 269),
75+
make_pair(1479, 490), make_pair(1260, 493), make_pair(1126, 283), make_pair(1246, 65)}),
76+
make_pair(STONE, texCoordList { make_pair(382, 689), make_pair(506, 898),
77+
make_pair(386, 1118), make_pair(142, 1120), make_pair(17, 905), make_pair(138, 691)}),
78+
make_pair(BRICK, texCoordList { make_pair(1496, 690), make_pair(1617, 908),
79+
make_pair(1490, 1120), make_pair(1248, 1118), make_pair(1124, 898), make_pair(1250, 688)}),
80+
make_pair(DESERT, texCoordList { make_pair(1496, 690), make_pair(1617, 908),
81+
make_pair(1490, 1120), make_pair(1248, 1118), make_pair(1124, 898), make_pair(1250, 688)}),
82+
};
83+
static Coordinate adjacentCoordDiffs[] = {Coordinate(0, 1), Coordinate(1, 0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0), Coordinate(-1, 1)};
84+
if(resourceTexCoords.find(tile.resource) == resourceTexCoords.end()) {
85+
throw runtime_error("Cannot draw this tile; it is invalid.");
86+
}
87+
const texCoordList& texCoords = resourceTexCoords.find(tile.resource)->second;
88+
glBegin(GL_TRIANGLE_FAN);
89+
texCoordPair(averagePoint(texCoords));
90+
vertexPair(coord);
91+
for(unsigned int i = 0; i < texCoords.size(); i++) {
92+
texCoordPair(texCoords[i]);
93+
Coordinate diff = adjacentCoordDiffs[i];
94+
vertexPair(Coordinate(coord.first + diff.first, coord.second + diff.second));
95+
}
96+
texCoordPair(texCoords[0]);
97+
vertexPair(Coordinate(coord.first + adjacentCoordDiffs[0].first, coord.second + adjacentCoordDiffs[0].second));
98+
glEnd();
99+
glBindTexture(GL_TEXTURE_2D, 0);
100+
}
101+
102+
void DrawingGameVisitor::visit(DevelopmentCard& card) {
103+
104+
}

src/Renderer.cpp

Lines changed: 5 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ GLuint loadImageAsTexture(const string& name) {
4343
return texture;
4444
}
4545

46-
inline pair<float, float> coordToScreen(const Coordinate& coord) {
46+
pair<float, float> coordToScreen(const Coordinate& coord) {
4747
using std::sin;
4848
using std::cos;
4949
// TODO not magic numbers
@@ -54,16 +54,16 @@ inline pair<float, float> coordToScreen(const Coordinate& coord) {
5454
return std::make_pair(x - 0.25f, y - 0.4f);
5555
}
5656

57-
inline void vertexPair(const Coordinate& coord) {
57+
void vertexPair(const Coordinate& coord) {
5858
pair<float, float> screenCoord = coordToScreen(coord);
5959
glVertex2f(screenCoord.first, screenCoord.second);
6060
}
6161

62-
inline void texCoordPair(const pair<float, float>& p) {
62+
void texCoordPair(const pair<float, float>& p) {
6363
glTexCoord2f(p.first / 2048., p.second / 2048.);
6464
}
6565

66-
inline pair<float, float> averagePoint(const std::vector<pair<float, float>>& points) {
66+
pair<float, float> averagePoint(const std::vector<pair<float, float>>& points) {
6767
pair<float, float> average;
6868
for(auto& it : points) {
6969
average.first += it.first;
@@ -72,64 +72,4 @@ inline pair<float, float> averagePoint(const std::vector<pair<float, float>>& po
7272
average.first /= (float) points.size();
7373
average.second /= (float) points.size();
7474
return average;
75-
}
76-
77-
void drawHex(const Coordinate& coord, const ResourceTile& tile) {
78-
static GLuint tileTextures = loadImageAsTexture("resources/tiles.bmp");
79-
glBindTexture(GL_TEXTURE_2D, tileTextures);
80-
typedef std::vector<pair<float, float> > texCoordList;
81-
static const std::map<resourceType, texCoordList> resourceTexCoords = {
82-
make_pair(WHEAT, texCoordList { make_pair(377, 73), make_pair(500, 285),
83-
make_pair(380, 502), make_pair(136, 503), make_pair(10, 288), make_pair(134, 74)}),
84-
make_pair(SHEEP, texCoordList { make_pair(959, 75), make_pair(1076, 288),
85-
make_pair(955, 503), make_pair(712, 501), make_pair(586, 289), make_pair(708, 73)}),
86-
make_pair(WOOD, texCoordList { make_pair(1491, 60), make_pair(1618, 269),
87-
make_pair(1479, 490), make_pair(1260, 493), make_pair(1126, 283), make_pair(1246, 65)}),
88-
make_pair(STONE, texCoordList { make_pair(382, 689), make_pair(506, 898),
89-
make_pair(386, 1118), make_pair(142, 1120), make_pair(17, 905), make_pair(138, 691)}),
90-
make_pair(BRICK, texCoordList { make_pair(1496, 690), make_pair(1617, 908),
91-
make_pair(1490, 1120), make_pair(1248, 1118), make_pair(1124, 898), make_pair(1250, 688)}),
92-
make_pair(DESERT, texCoordList { make_pair(1496, 690), make_pair(1617, 908),
93-
make_pair(1490, 1120), make_pair(1248, 1118), make_pair(1124, 898), make_pair(1250, 688)}),
94-
};
95-
static Coordinate adjacentCoordDiffs[] = {Coordinate(0, 1), Coordinate(1, 0), Coordinate(1, -1), Coordinate(0, -1), Coordinate(-1, 0), Coordinate(-1, 1)};
96-
if(resourceTexCoords.find(tile.resource) == resourceTexCoords.end()) {
97-
throw runtime_error("Cannot draw this tile; it is invalid.");
98-
}
99-
const texCoordList& texCoords = resourceTexCoords.find(tile.resource)->second;
100-
glBegin(GL_TRIANGLE_FAN);
101-
texCoordPair(averagePoint(texCoords));
102-
vertexPair(coord);
103-
for(unsigned int i = 0; i < texCoords.size(); i++) {
104-
texCoordPair(texCoords[i]);
105-
Coordinate diff = adjacentCoordDiffs[i];
106-
vertexPair(Coordinate(coord.first + diff.first, coord.second + diff.second));
107-
}
108-
texCoordPair(texCoords[0]);
109-
vertexPair(Coordinate(coord.first + adjacentCoordDiffs[0].first, coord.second + adjacentCoordDiffs[0].second));
110-
glEnd();
111-
/*
112-
glBegin(GL_QUADS);
113-
glTexCoord2f(0, 0); glVertex2f(0, 0);
114-
glTexCoord2f(1, 0); glVertex2f(1, 0);
115-
glTexCoord2f(1, 1); glVertex2f(1, 1);
116-
glTexCoord2f(0, 1); glVertex2f(0, 1);
117-
glEnd();
118-
*/
119-
glBindTexture(GL_TEXTURE_2D, 0);
120-
}
121-
122-
void renderBoard(const GameBoard& board, const Player& perspective) {
123-
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
124-
125-
for(auto& it : board.getResources()) {
126-
const Coordinate& coord = it.first;
127-
const GamePiece& piece = *(it.second);
128-
if(dynamic_cast<const ResourceTile*>(&piece)) {
129-
const ResourceTile& tile = static_cast<const ResourceTile&>(piece);
130-
drawHex(coord, tile);
131-
}
132-
}
133-
134-
glFlush();
135-
}
75+
}

src/UserInput.cpp

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/main.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
#include <memory>
1414

1515
#include "GameBoard.h"
16-
#include "UserInput.h"
1716
#include "Player.h"
18-
#include "Renderer.h"
17+
#include "GameView.h"
18+
#include "GameController.h"
1919

2020
using std::vector;
2121
using std::unique_ptr;
@@ -66,18 +66,18 @@ int main(int argc, char *argv[]) {
6666
vector<unique_ptr<Player>> players;
6767
players.emplace_back(unique_ptr<Player>(new Player("test")));
6868

69-
Player& testPlayer = *(players[0]);
70-
71-
GameBoard testBoard(std::move(players));
69+
GameBoard model(std::move(players));
70+
GameController controller(model);
71+
GameView view(model, controller);
7272

7373
bool running = true;
7474
while(running) {
7575
SDL_Event event;
7676
while(SDL_PollEvent(&event)) {
77-
running = acceptInput(testBoard, testPlayer, event);
77+
running = view.acceptInput(event);
7878
}
7979

80-
renderBoard(testBoard, testPlayer);
80+
view.render();
8181

8282
SDL_GL_SwapWindow(displayWindow);
8383
SDL_Delay(100);

0 commit comments

Comments
 (0)