Skip to content

Commit 3bebd46

Browse files
committed
Added pieces
1 parent 76ec92b commit 3bebd46

File tree

1 file changed

+67
-8
lines changed

1 file changed

+67
-8
lines changed

src/main.cpp

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
1-
#include <iostream>
1+
#define _USE_MATH_DEFINES
2+
#include <math.h>
23

34
#include <SDL2/SDL.h>
45
#include <SDL2/SDL_opengl.h>
56

67
#include <GL/gl.h>
78
#include <GL/glu.h>
89

10+
#include <iostream>
11+
#include <utility>
12+
913
enum PieceType {
1014
NO_PIECE, WHITE_PIECE, BLACK_PIECE
15+
};
16+
17+
PieceType board[8][8];
18+
19+
void initGame() {
20+
for(int row = 0; row < 3; row++) {
21+
for(int col = 0; col < 8; col++) {
22+
board[row][col] = (row + col) % 2 == 0 ? NO_PIECE : WHITE_PIECE;
23+
}
24+
}
25+
for(int row = 3; row < 5; row++) {
26+
for(int col = 0; col < 8; col++) {
27+
board[row][col] = NO_PIECE;
28+
}
29+
}
30+
for(int row = 5; row < 8; row++) {
31+
for(int col = 0; col < 8; col++) {
32+
board[row][col] = (row + col) % 2 == 0 ? NO_PIECE : BLACK_PIECE;
33+
}
34+
}
1135
}
1236

1337
void initOpenGL() {
@@ -16,6 +40,7 @@ void initOpenGL() {
1640
glEnable (GL_BLEND);
1741
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1842
glClearColor(1.f, 1.f, 1.f, 1.f);
43+
//glDepthFunc(GL_NEVER);
1944
}
2045

2146
/* function to reset our viewport after a window resize */
@@ -28,8 +53,13 @@ int updateViewport(int width, int height) {
2853
glMatrixMode(GL_MODELVIEW);
2954
}
3055

56+
std::pair<int, int> windowToGameCoords(int x, int y) {
57+
return std::pair<int, int>(8. * x / 800., 8. * y / 600.);
58+
}
59+
3160
bool handleMouseButtonEvent(const SDL_MouseButtonEvent& event) {
32-
std::cout << "mouse click on " << event.x << " " << event.y << std::endl;
61+
std::pair<int, int> gameCoords = windowToGameCoords(event.x, event.y);
62+
std::cout << "mouse click on " << gameCoords.first << " " << gameCoords.second << std::endl;
3363

3464
return true;
3565
}
@@ -61,15 +91,44 @@ bool handleEvent(const SDL_Event& event) {
6191
return true;
6292
}
6393

94+
void drawCircle(float x, float y, float radius) {
95+
const int vertices = 20;
96+
glBegin(GL_TRIANGLE_FAN);
97+
glVertex2f(x, y);
98+
for(int i = 0; i < vertices + 1; i++) {
99+
glVertex2f(x + radius * cos(2.0 * M_PI * i / vertices), y + radius * sin(2.0 * M_PI * i / vertices));
100+
}
101+
glEnd();
102+
}
103+
64104
void render(SDL_Renderer* displayRenderer) {
65105
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
66106

107+
for(int row = 0; row < 8; row++) {
108+
for(int col = 0; col < 8; col++) {
109+
if(board[row][col] == WHITE_PIECE) {
110+
glColor3f(1, 1, 1);
111+
} else if(board[row][col] == BLACK_PIECE) {
112+
glColor3f(0, 0, 0);
113+
}
114+
115+
if(board[row][col] != NO_PIECE) {
116+
drawCircle((col + .5) / 8., (row + .5) / 8., 1/16.);
117+
}
118+
}
119+
}
120+
67121
glBegin(GL_QUADS);
68-
glColor3f(1.0, 0., 0.);
69-
glVertex2d(0.0, 0.0);
70-
glVertex2d(0.0, 0.5);
71-
glVertex2d(0.5, 0.5);
72-
glVertex2d(0.5, 0.0);
122+
for(int row = 0; row < 8; row++) {
123+
for(int col = 0; col < 8; col++) {
124+
float shade = (row + col) % 2 == 0 ? .75f : .25f;
125+
glColor3f(shade, shade, shade);
126+
glVertex2f((row + 0) / 8.f, (col + 0) / 8.f);
127+
glVertex2f((row + 1) / 8.f, (col + 0) / 8.f);
128+
glVertex2f((row + 1) / 8.f, (col + 1) / 8.f);
129+
glVertex2f((row + 0) / 8.f, (col + 1) / 8.f);
130+
}
131+
}
73132
glEnd();
74133

75134
SDL_RenderPresent(displayRenderer);
@@ -90,7 +149,7 @@ int main(int argc, char *argv[]) {
90149
return 0;
91150
}
92151

93-
152+
initGame();
94153
initOpenGL();
95154

96155
updateViewport(800, 600);

0 commit comments

Comments
 (0)