Skip to content

Commit 35f28e9

Browse files
committed
Controller now has a reference to View instead of vice versa
1 parent aad772a commit 35f28e9

File tree

5 files changed

+52
-23
lines changed

5 files changed

+52
-23
lines changed

include/GameController.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,17 @@
33

44
class GameBoard;
55
class ClickCoordinateEvent;
6+
class GameView;
67

78
class GameController {
89
private:
910
GameBoard& model;
11+
GameView& view;
1012

11-
GameController(const GameController& o) : model(o.model) {} //deleted
13+
GameController(const GameController& o) : model(o.model), view(o.view) {} //deleted
1214
GameController& operator=(const GameController& o) { return *this; } //deleted
1315
public:
14-
GameController(GameBoard&);
16+
GameController(GameBoard&, GameView& view);
1517
~GameController();
1618

1719
void handleEvent(const ClickCoordinateEvent&);

include/GameView.h

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ class GameView;
1818

1919
class ViewElement {
2020
private:
21-
GameView& view;
2221
std::pair<ScreenCoordinate, ScreenCoordinate> rect;
2322

24-
ViewElement(const ViewElement& vw) : view(vw.view) {} //deleted
23+
ViewElement(const ViewElement& vw) {} //deleted
2524
ViewElement& operator=(const ViewElement&) { return *this; } // deleted
2625
protected:
2726
virtual bool clicked(ScreenCoordinate coord) = 0;
2827
public:
29-
ViewElement(GameView& view, decltype(rect) rect);
28+
ViewElement(decltype(rect) rect);
3029
virtual ~ViewElement();
3130

3231
virtual decltype(rect) getRect() const;
@@ -38,18 +37,19 @@ class ViewElement {
3837
class GameView {
3938
private:
4039
GameBoard& model;
41-
GameController& controller;
4240

4341
std::vector<std::unique_ptr<ViewElement>> viewElements;
4442

45-
GameView(const GameView& o) : model(o.model), controller(o.controller) {} //deleted
43+
GameView(const GameView& o) : model(o.model) {} //deleted
4644
GameView& operator=(const GameView& o) { return *this; } //deleted
4745
public:
48-
GameView(GameBoard&, GameController&);
46+
GameView(GameBoard&);
4947
~GameView();
5048

5149
void render();
5250
bool acceptInput(SDL_Event& event);
51+
52+
void addElement(std::unique_ptr<ViewElement>);
5353
};
5454

5555
class DrawingGameVisitor : public GameVisitor {
@@ -87,23 +87,41 @@ template<class Fn>
8787
class ViewButton : public ViewElement {
8888
private:
8989
Fn action;
90-
std::tuple<float, float, float> color;
9190

9291
ViewButton(const ViewButton& vb) : ViewElement(vb) {} //deleted
93-
ViewButton& operator=(const ViewButton& vb) { return *this; }
92+
ViewButton& operator=(const ViewButton&) { return *this; } //deleted
9493
protected:
9594
virtual bool clicked(ScreenCoordinate coord) {
9695
return action(coord);
9796
}
9897
public:
99-
ViewButton(Fn& action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float, float, float> color) : ViewElement(rect), action(action), color(color) {}
100-
~ViewButton() {}
98+
ViewButton(Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect) : ViewElement(rect), action(action) {}
99+
virtual ~ViewButton() {}
101100

102-
void render() {
101+
virtual void render() {}
102+
};
103+
104+
template<class Fn>
105+
std::unique_ptr<ViewElement> makeViewButton(Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect) {
106+
return std::unique_ptr<ViewElement>(new ViewButton<Fn>(fn, rect));
107+
}
108+
109+
template<class Fn>
110+
class ViewButtonColor : public ViewButton<Fn> {
111+
private:
112+
std::tuple<float, float, float> color;
113+
114+
ViewButtonColor(const ViewButtonColor& vb) : ViewElement(vb) {} //deleted
115+
ViewButtonColor& operator=(const ViewButtonColor& vb) { return *this; }
116+
public:
117+
ViewButtonColor(Fn action, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float, float, float> color) : ViewElement(rect, action), color(color) {}
118+
virtual ~ViewButtonColor() {}
119+
120+
virtual void render() {
103121
glBindTexture(GL_TEXTURE_2D, 0);
104122
glColor3f(std::get<0>(color), std::get<1>(color), std::get<2>(color));
105-
auto topLeft = getRect().first;
106-
auto bottomRight = getRect().second;
123+
auto topLeft = ViewElement::getRect().first;
124+
auto bottomRight = ViewElement::getRect().second;
107125
glBegin(GL_QUADS);
108126
glVertex3f(topLeft.first, topLeft.second);
109127
glVertex3f(bottomRight.first, topLeft.second);
@@ -113,4 +131,9 @@ class ViewButton : public ViewElement {
113131
}
114132
};
115133

134+
template<class Fn>
135+
std::unique_ptr<ViewElement> makeViewButtonColor(Fn fn, std::pair<ScreenCoordinate, ScreenCoordinate> rect, std::tuple<float, float, float> color) {
136+
return std::unique_ptr<ViewElement>(new ViewButtonColor<Fn>(fn, rect, color));
137+
}
138+
116139
#endif

src/GameController.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
#include "GameBoard.h"
66
#include "GameView.h"
7+
#include "Renderer.h"
78

8-
GameController::GameController(GameBoard& model) : model(model) {
9-
9+
GameController::GameController(GameBoard& model, GameView& view) : model(model), view(view) {
10+
view.addElement(makeViewButton([this](ScreenCoordinate coord) { this->handleEvent(ClickCoordinateEvent(screenToCoord(coord))); return true; }, {{0, 0}, {1, 1}}));
1011
}
1112

1213
GameController::~GameController() {

src/GameView.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ using std::pair;
1515
using std::runtime_error;
1616
using std::string;
1717

18-
ViewElement::ViewElement(GameView& view, decltype(rect) rect) : view(view) {
18+
ViewElement::ViewElement(decltype(rect) rect) {
1919
using std::min;
2020
using std::max;
2121

@@ -47,7 +47,7 @@ bool ViewElement::handleClick(ScreenCoordinate coord) {
4747
return false;
4848
}
4949

50-
GameView::GameView(GameBoard& model, GameController& controller) : model(model), controller(controller) {
50+
GameView::GameView(GameBoard& model) : model(model) {
5151

5252
}
5353

@@ -75,14 +75,17 @@ bool GameView::acceptInput(SDL_Event& event) {
7575
ScreenCoordinate screen = {(float) event.button.x / 900.f, 1.f - (float) event.button.y / 800.f};
7676
for(auto& it : viewElements) {
7777
if(it->handleClick(screen)) {
78-
return true;
78+
break;
7979
}
8080
}
81-
controller.handleEvent(ClickCoordinateEvent(screenToCoord(screen)));
8281
}
8382
return true;
8483
}
8584

85+
void GameView::addElement(std::unique_ptr<ViewElement> element) {
86+
viewElements.emplace_back(std::move(element));
87+
}
88+
8689
DrawingGameVisitor::DrawingGameVisitor(GameView& view) : view(view) {
8790

8891
}

src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ int main(int argc, char *argv[]) {
6868
Player& firstPlayer = *players[0];
6969

7070
GameBoard model(std::move(players));
71-
GameController controller(model);
72-
GameView view(model, controller);
71+
GameView view(model);
72+
GameController controller(model, view);
7373

7474
model.PlaceSettlement(Coordinate{0, 0}, firstPlayer);
7575
model.PlaceRoad(Coordinate{0, 0}, Coordinate{1, 0}, firstPlayer);

0 commit comments

Comments
 (0)