Skip to content

Commit 113b4a2

Browse files
committed
refactored some dice code
1 parent acfdddc commit 113b4a2

File tree

2 files changed

+32
-55
lines changed

2 files changed

+32
-55
lines changed

include/GameView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class GameController;
1616
class ViewElement;
1717
class GameView;
1818

19+
1920
/**
2021
* An element that is drawn on screen and can receive inputs from the user. These all occupy a rectangular area on screen
2122
* and can choose to handle clicks from the user that are inside their area.

src/GameView.cpp

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ using std::pair;
1616
using std::runtime_error;
1717
using std::string;
1818

19+
float DiceXCoords[3] = {9.f, 134.f, 259.f};
20+
float DiceYCoords[2] = {3.f, 142.f};
21+
22+
#define EMPLACE_SQUARE_VERTEX(imXOff, imYOff, scXOff, scYOff) \
23+
texCoordPair({texTopLeft.first + imXOff, texTopLeft.second + imYOff}); \
24+
glVertex2d(screenTopLeft.first + scXOff, screenTopLeft.second + scYOff);
25+
26+
1927
/**
2028
* Construct a ViewElement covering a particular rectangle on screen.
2129
* @param rect The rectangle on screen that the view element occupies.
@@ -272,69 +280,54 @@ void drawTexturedCircle(std::pair<float, float> texCenter, float texRadius, std:
272280
glEnd();
273281
}
274282

283+
/**
284+
* Draw a textured square oriented parallel to the ground
285+
* @param texTopLeft image coordinates of the top left side of our square texture
286+
* @param sideLength image domain side length (in pixels)
287+
* @param screenTopLeft GL coordinates for the top left of our square to render
288+
* @param screenSideLength GL image domain square side length
289+
*/
275290
void drawTexturedRectangle(std::pair<float, float> texTopLeft, float sideLength, std::pair<float, float> screenTopLeft, float screenSideLength) {
276291

277-
278-
279292
glBegin(GL_QUADS);
280293

294+
EMPLACE_SQUARE_VERTEX(0.0f, 0.0f, 0.0f, 0.0f)
295+
EMPLACE_SQUARE_VERTEX(sideLength, 0.0f, screenSideLength, 0.0f)
296+
EMPLACE_SQUARE_VERTEX(sideLength, sideLength, screenSideLength, screenSideLength)
297+
EMPLACE_SQUARE_VERTEX(0.0f, sideLength, 0.0f, screenSideLength)
281298

282-
283299

284-
texCoordPair({texTopLeft.first + 0.0f, texTopLeft.second + 0.0f});
285-
glVertex2d(screenTopLeft.first + 0.0f, screenTopLeft.second + 0.0f);
286300

287-
texCoordPair({texTopLeft.first + sideLength, texTopLeft.second + 0.0f});
301+
/*texCoordPair({texTopLeft.first + sideLength, texTopLeft.second + 0.0f});
288302
glVertex2d(screenTopLeft.first + screenSideLength, screenTopLeft.second + 0.0f);
289303
290304
291305
texCoordPair({texTopLeft.first + sideLength, texTopLeft.second + sideLength});
292306
glVertex2d(screenTopLeft.first + screenSideLength, screenTopLeft.second + screenSideLength);
293307
294308
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-
309+
glVertex2d(screenTopLeft.first + 0.0f, screenTopLeft.second + screenSideLength);*/
314310

315311
glEnd();
316312

317-
318-
319-
320-
321-
322313
}
323314

315+
/**
316+
* Draw both dice.
317+
* @param dice the dice data structure for the board
318+
*/
324319
void DrawingGameVisitor::visit(GameDice& dice) {
325320

326321
static const GLuint diceTextures = loadImageAsTexture("resources/catan_dice_new.bmp");
327322
glBindTexture(GL_TEXTURE_2D, diceTextures);
328323

329324
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-
};
325+
static std::map<int, std::pair<float, float>> topLeftOffset;
326+
//construct offset map
327+
for (int i = 1; i < 7; i++) {
328+
topLeftOffset.emplace(i, make_pair(DiceXCoords[(i-1)%3], DiceYCoords[i/4]));
329+
}
330+
338331

339332
drawTexturedRectangle(topLeftOffset.find(dice.getFirst())->second, 95.f,
340333
make_pair(.7f, .8f), 0.06);
@@ -343,26 +336,9 @@ void DrawingGameVisitor::visit(GameDice& dice) {
343336
drawTexturedRectangle(topLeftOffset.find(dice.getSecond())->second, 95.f,
344337
make_pair(.78f, .8f), 0.06);
345338

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);
355339

356340
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-
361341

362-
363-
364-
//std::cout << dice.getFirst() << "\n";
365-
366342
}
367343

368344
/**

0 commit comments

Comments
 (0)