Skip to content

Commit 3a7fa8d

Browse files
committed
Added rule-less piece movement
1 parent 3bebd46 commit 3a7fa8d

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/main.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,24 @@ int updateViewport(int width, int height) {
5454
}
5555

5656
std::pair<int, int> windowToGameCoords(int x, int y) {
57-
return std::pair<int, int>(8. * x / 800., 8. * y / 600.);
57+
return std::pair<int, int>((8. * x / 800.), (8. * y / 600.));
5858
}
5959

60+
std::pair<int, int> selectedCoord(-1, -1);
61+
6062
bool handleMouseButtonEvent(const SDL_MouseButtonEvent& event) {
6163
std::pair<int, int> gameCoords = windowToGameCoords(event.x, event.y);
62-
std::cout << "mouse click on " << gameCoords.first << " " << gameCoords.second << std::endl;
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+
}
6375

6476
return true;
6577
}
@@ -123,6 +135,9 @@ void render(SDL_Renderer* displayRenderer) {
123135
for(int col = 0; col < 8; col++) {
124136
float shade = (row + col) % 2 == 0 ? .75f : .25f;
125137
glColor3f(shade, shade, shade);
138+
if(selectedCoord == std::make_pair(row, col)) {
139+
glColor3f(1.f, 0.f, 0.f);
140+
}
126141
glVertex2f((row + 0) / 8.f, (col + 0) / 8.f);
127142
glVertex2f((row + 1) / 8.f, (col + 0) / 8.f);
128143
glVertex2f((row + 1) / 8.f, (col + 1) / 8.f);

0 commit comments

Comments
 (0)