1010#include < iostream>
1111#include < utility>
1212
13- enum PieceType {
14- NO_PIECE, WHITE_PIECE, BLACK_PIECE
15- };
16-
17- PieceType board[8 ][8 ];
18-
1913void 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- }
14+
3515}
3616
3717void initOpenGL () {
@@ -53,34 +33,13 @@ int updateViewport(int width, int height) {
5333 glMatrixMode (GL_MODELVIEW);
5434}
5535
56- std::pair<int , int > windowToGameCoords (int x, int y) {
57- return std::pair<int , int >((8 . * x / 800 .), (8 . * y / 600 .));
58- }
59-
60- std::pair<int , int > selectedCoord (-1 , -1 );
61-
6236bool handleMouseButtonEvent (const SDL_MouseButtonEvent& event) {
63- std::pair<int , int > gameCoords = windowToGameCoords (event.x , event.y );
64-
65- if (event.type == SDL_MOUSEBUTTONDOWN) {
66- std::cout << " mouse click on " << gameCoords.first << " " << gameCoords.second << std::endl;
67- if (selectedCoord == std::make_pair (-1 , -1 )) {
68- selectedCoord = gameCoords;
69- } else {
70- board[gameCoords.second ][gameCoords.first ] = board[selectedCoord.second ][selectedCoord.first ];
71- board[selectedCoord.second ][selectedCoord.first ] = NO_PIECE;
72- selectedCoord = std::make_pair (-1 , -1 );
73- }
74- }
7537
7638 return true ;
7739}
7840
7941bool handleKeyboardEvent (const SDL_KeyboardEvent& event) {
80- if (event.keysym .sym == SDLK_ESCAPE) {
81- return false ;
82- }
83- // TODO: key-specific code
42+
8443 return true ;
8544}
8645
@@ -103,49 +62,9 @@ bool handleEvent(const SDL_Event& event) {
10362 return true ;
10463}
10564
106- void drawCircle (float x, float y, float radius) {
107- const int vertices = 20 ;
108- glBegin (GL_TRIANGLE_FAN);
109- glVertex2f (x, y);
110- for (int i = 0 ; i < vertices + 1 ; i++) {
111- glVertex2f (x + radius * cos (2.0 * M_PI * i / vertices), y + radius * sin (2.0 * M_PI * i / vertices));
112- }
113- glEnd ();
114- }
115-
11665void render (SDL_Window* displayWindow) {
11766 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
11867
119- for (int row = 0 ; row < 8 ; row++) {
120- for (int col = 0 ; col < 8 ; col++) {
121- if (board[row][col] == WHITE_PIECE) {
122- glColor3f (1 , 1 , 1 );
123- } else if (board[row][col] == BLACK_PIECE) {
124- glColor3f (0 , 0 , 0 );
125- }
126-
127- if (board[row][col] != NO_PIECE) {
128- drawCircle ((col + .5 ) / 8 ., (row + .5 ) / 8 ., 1 /16 .);
129- }
130- }
131- }
132-
133- glBegin (GL_QUADS);
134- for (int row = 0 ; row < 8 ; row++) {
135- for (int col = 0 ; col < 8 ; col++) {
136- float shade = (row + col) % 2 == 0 ? .75f : .25f ;
137- glColor3f (shade, shade, shade);
138- if (selectedCoord == std::make_pair (row, col)) {
139- glColor3f (1 .f , 0 .f , 0 .f );
140- }
141- glVertex2f ((row + 0 ) / 8 .f , (col + 0 ) / 8 .f );
142- glVertex2f ((row + 1 ) / 8 .f , (col + 0 ) / 8 .f );
143- glVertex2f ((row + 1 ) / 8 .f , (col + 1 ) / 8 .f );
144- glVertex2f ((row + 0 ) / 8 .f , (col + 1 ) / 8 .f );
145- }
146- }
147- glEnd ();
148-
14968 glFlush ();
15069 SDL_GL_SwapWindow (displayWindow);
15170}
0 commit comments