Skip to content

Commit c3023f1

Browse files
committed
Simple color-based tile renderer
1 parent 6f7d465 commit c3023f1

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
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++11 -Wall
1111
export LDFLAGS := -lSDL2 -lGL -lGLU
1212

1313
.PHONY: all

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

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
}

src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void initOpenGL() {
2929
}
3030

3131
/* function to reset our viewport after a window resize */
32-
int updateViewport(int width, int height) {
32+
void updateViewport(int width, int height) {
3333
glViewport(0, 0, width, height);
3434
glMatrixMode(GL_PROJECTION);
3535
glLoadIdentity ();
@@ -43,7 +43,7 @@ int main(int argc, char *argv[]) {
4343
SDL_Window* displayWindow;
4444
SDL_Renderer* displayRenderer;
4545
SDL_RendererInfo displayRendererInfo;
46-
SDL_CreateWindowAndRenderer(800, 600, SDL_WINDOW_OPENGL, &displayWindow, &displayRenderer);
46+
SDL_CreateWindowAndRenderer(800, 800, SDL_WINDOW_OPENGL, &displayWindow, &displayRenderer);
4747
SDL_GetRendererInfo(displayRenderer, &displayRendererInfo);
4848
/*TODO: Check that we have OpenGL */
4949
if ((displayRendererInfo.flags & SDL_RENDERER_ACCELERATED) == 0 ||

0 commit comments

Comments
 (0)