Error detection, turn switching, pawn move suggestions and taking implemented
This commit is contained in:
parent
86eff11ec6
commit
4f377d107b
23
.vscode/c_cpp_properties.json
vendored
Normal file
23
.vscode/c_cpp_properties.json
vendored
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
{
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Win32",
|
||||||
|
"includePath": [
|
||||||
|
"${workspaceFolder}/**",
|
||||||
|
"C:\\Users\\Daniel\\Documents\\Projects\\i686-w64-mingw32\\include\\SDL2"
|
||||||
|
|
||||||
|
],
|
||||||
|
"defines": [
|
||||||
|
"_DEBUG",
|
||||||
|
"UNICODE",
|
||||||
|
"_UNICODE"
|
||||||
|
],
|
||||||
|
"windowsSdkVersion": "10.0.14393.0",
|
||||||
|
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe",
|
||||||
|
"cStandard": "c11",
|
||||||
|
"cppStandard": "c++17",
|
||||||
|
"intelliSenseMode": "msvc-x64"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"version": 4
|
||||||
|
}
|
28
.vscode/launch.json
vendored
Normal file
28
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "(gdb) Launch",
|
||||||
|
"type": "cppdbg",
|
||||||
|
"request": "launch",
|
||||||
|
"program": "${workspaceFolder}/Chess.exe",
|
||||||
|
"args": [],
|
||||||
|
"stopAtEntry": false,
|
||||||
|
"cwd": "${workspaceFolder}",
|
||||||
|
"environment": [],
|
||||||
|
"externalConsole": false,
|
||||||
|
"MIMode": "gdb",
|
||||||
|
"miDebuggerPath": "C:\\Program Files (x86)\\mingw-w64\\i686-7.2.0-posix-dwarf-rt_v5-rev1\\mingw32\\bin\\gdb.exe",
|
||||||
|
"setupCommands": [
|
||||||
|
{
|
||||||
|
"description": "Enable pretty-printing for gdb",
|
||||||
|
"text": "-enable-pretty-printing",
|
||||||
|
"ignoreFailures": true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
5
.vscode/settings.json
vendored
Normal file
5
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"files.associations": {
|
||||||
|
"stack": "cpp"
|
||||||
|
}
|
||||||
|
}
|
32
.vscode/tasks.json
vendored
Normal file
32
.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
{
|
||||||
|
// See https://go.microsoft.com/fwlink/?LinkId=733558
|
||||||
|
// for the documentation about the tasks.json format
|
||||||
|
"version": "2.0.0",
|
||||||
|
"tasks": [
|
||||||
|
{
|
||||||
|
"type": "shell",
|
||||||
|
"label": "Build Ouptup",
|
||||||
|
"command": "g++",
|
||||||
|
"args": [
|
||||||
|
"*.cpp",
|
||||||
|
"-IC:\\Users\\Daniel\\Documents\\Projects\\i686-w64-mingw32\\include\\SDL2",
|
||||||
|
"-LC:\\Users\\Daniel\\Documents\\Projects\\i686-w64-mingw32\\lib",
|
||||||
|
"-w",
|
||||||
|
"-Wl,-subsystem,windows",
|
||||||
|
"-lmingw32",
|
||||||
|
"-lSDL2main",
|
||||||
|
"-lSDL2",
|
||||||
|
"-static-libstdc++",
|
||||||
|
"-o",
|
||||||
|
"Chess"
|
||||||
|
],
|
||||||
|
"problemMatcher": [
|
||||||
|
"$gcc"
|
||||||
|
],
|
||||||
|
"group": {
|
||||||
|
"kind": "build",
|
||||||
|
"isDefault": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
637
chess_board.cpp
637
chess_board.cpp
@ -4,65 +4,116 @@
|
|||||||
#include "stdio.h"
|
#include "stdio.h"
|
||||||
#include <stack>
|
#include <stack>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "pieces.hpp"
|
||||||
|
|
||||||
#define MARGIN 200
|
#define MARGIN 300
|
||||||
|
|
||||||
#define PAWN_WHITE 0u
|
#define PAWN_WHITE 0u
|
||||||
#define PAWN_BLACK 1u
|
#define PAWN_BLACK 1u
|
||||||
#define KING_WHITE 2u
|
#define KING_WHITE 2u
|
||||||
#define KING_BLACK 3u
|
#define KING_BLACK 3u
|
||||||
#define QUEEN_WHITE 4u
|
#define ROOK_WHITE 4u
|
||||||
#define QUEEN_BLACK 5u
|
#define ROOK_BLACK 5u
|
||||||
#define ROOK_WHITE 6u
|
#define KNIGHT_WHITE 6u
|
||||||
#define ROOK_BLACK 7u
|
#define KNIGHT_BLACK 7u
|
||||||
#define KNIGHT_WHITE 8u
|
#define BISHOP_WHITE 8u
|
||||||
#define KNIGHT_BLACK 9u
|
#define BISHOP_BLACK 9u
|
||||||
#define BISHOP_WHITE 10u
|
#define QUEEN_WHITE 10u
|
||||||
#define BISHOP_BLACK 11u
|
#define QUEEN_BLACK 11u
|
||||||
#define SQUARE_EMPTY 12u
|
#define SQUARE_EMPTY 12u
|
||||||
#define SQUARE_SHIFT 4u
|
|
||||||
|
|
||||||
#define POTENTIAL_MOVE (1u << SQUARE_SHIFT)
|
#define LIGHT_OFF 0u
|
||||||
#define POTENTIAL_TAKE (2u << SQUARE_SHIFT)
|
#define POTENTIAL_MOVE 1u
|
||||||
#define SUGGESTED_MOVE (3u << SQUARE_SHIFT)
|
#define POTENTIAL_TAKE 2u
|
||||||
#define CHEATING_MOVE (4u << SQUARE_SHIFT)
|
#define SUGGESTED_MOVE 3u
|
||||||
|
#define ERROR_MOVE 4u
|
||||||
|
#define PEICE_ORIGIN 5u
|
||||||
|
#define PEICE_NEEDS_TO_BE_HERE 6u
|
||||||
|
|
||||||
const char file_names[12][16] = {"pawn_white.bmp", "pawn_black.bmp",
|
#define GAME_STATE_IDLE 0u
|
||||||
|
#define GAME_STATE_P1_TURN_BEGINING 1u
|
||||||
|
#define GAME_STATE_P1_TURN_IN_PROGRESS 2u
|
||||||
|
#define GAME_STATE_P1_TURN_TAKING 3u
|
||||||
|
#define GAME_STATE_P2_TURN_BEGINING 4u
|
||||||
|
#define GAME_STATE_P2_TURN_IN_PROGRESS 5u
|
||||||
|
#define GAME_STATE_P2_TURN_TAKING 6u
|
||||||
|
#define GAME_STATE_ERROR_DETECTED 7u
|
||||||
|
//#define GAME_STATE_ 4u
|
||||||
|
|
||||||
|
const char File_Names[12][17] = {"pawn_white.bmp", "pawn_black.bmp",
|
||||||
"king_white.bmp", "king_black.bmp",
|
"king_white.bmp", "king_black.bmp",
|
||||||
"queen_white.bmp", "queen_black.bmp",
|
"tower_white.bmp", "tower_black.bmp",
|
||||||
"rook_white.bmp", "rook_black.bmp",
|
|
||||||
"horse_white.bmp", "horse_black.bmp",
|
"horse_white.bmp", "horse_black.bmp",
|
||||||
"tower_white.bmp", "tower_black.bmp"};
|
"bishop_white.bmp", "bishop_black.bmp",
|
||||||
|
"queen_white.bmp", "queen_black.bmp",
|
||||||
|
};
|
||||||
|
|
||||||
static int height;
|
static int Height;
|
||||||
static int width;
|
static int Width;
|
||||||
static SDL_Texture * board_texture;
|
static SDL_Texture * Board_Texture;
|
||||||
static SDL_Rect rectangle;
|
static SDL_Rect Rectangle;
|
||||||
static int board_width;
|
static int Board_Width;
|
||||||
uint8_t board_state[8][8];
|
static uint8_t Board_State[12][8] = {{0}};
|
||||||
|
static uint8_t Current_Binary_Board[12] = {0};
|
||||||
|
static uint8_t Saved_Binary_Board[12] = {0};
|
||||||
|
static uint8_t Board_Lights[12][8] = {{0}};
|
||||||
SDL_Surface *bitmapSurface = NULL;
|
SDL_Surface *bitmapSurface = NULL;
|
||||||
SDL_Texture * bitmapTextures[12] = {NULL};
|
SDL_Texture * bitmapTextures[12] = {NULL};
|
||||||
std::stack<uint8_t> selected_peices;
|
static uint8_t Game_State = GAME_STATE_P1_TURN_BEGINING;
|
||||||
|
static bool White_Turn = true;
|
||||||
|
static uint8_t Selected_Peice = SQUARE_EMPTY;
|
||||||
|
static uint8_t Error_Count = 0u;
|
||||||
|
static uint8_t Taken_Peice = SQUARE_EMPTY;
|
||||||
|
|
||||||
void pawn_move(uint8_t row, uint8_t column)
|
static inline bool square_in_jail(uint8_t j)
|
||||||
{
|
{
|
||||||
if((0x0Fu & board_state[row][column]) == SQUARE_EMPTY)
|
return (j > 8u);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void clear_lights(void)
|
||||||
{
|
{
|
||||||
board_state[row][column] = (uint8_t)((POTENTIAL_MOVE) | (board_state[row][column] & 0x0Fu));
|
for (size_t i = 0; i < 8; i++)
|
||||||
|
{
|
||||||
|
for (size_t j = 0; j < 8; j++)
|
||||||
|
{
|
||||||
|
if(Board_Lights[i][j] != ERROR_MOVE)
|
||||||
|
{
|
||||||
|
Board_Lights[i][j] = LIGHT_OFF;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void pawn_take(uint8_t row, uint8_t column)
|
}
|
||||||
|
|
||||||
|
static bool white_team(uint8_t piece)
|
||||||
{
|
{
|
||||||
if ((0x0Fu & board_state[row][column]) < SQUARE_EMPTY)
|
return ((piece % 2u) == 0u);
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool opposite_teams(uint8_t piece_one, uint8_t piece_two)
|
||||||
{
|
{
|
||||||
board_state[row][column] = (uint8_t)((POTENTIAL_TAKE) | (board_state[row][column] & 0x0Fu));
|
return (((piece_one % 2u) == 0u) ^ ((piece_two % 2u) == 0u));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void pawn_move(uint8_t row, uint8_t column)
|
||||||
|
{
|
||||||
|
if(Board_State[row][column] == SQUARE_EMPTY)
|
||||||
|
{
|
||||||
|
Board_Lights[row][column] = POTENTIAL_MOVE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mark_Potential_Moves(uint8_t peice, uint8_t column, uint8_t row)
|
static void pawn_take(uint8_t row, uint8_t column)
|
||||||
{
|
{
|
||||||
switch (peice)
|
if (Board_State[row][column] < SQUARE_EMPTY)
|
||||||
|
{
|
||||||
|
Board_Lights[row][column] = POTENTIAL_TAKE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Mark_Potential_Moves(uint8_t piece, uint8_t column, uint8_t row)
|
||||||
|
{
|
||||||
|
switch (piece)
|
||||||
{
|
{
|
||||||
case PAWN_WHITE :
|
case PAWN_WHITE :
|
||||||
if (row == 6)
|
if (row == 6)
|
||||||
@ -70,11 +121,11 @@ void Mark_Potential_Moves(uint8_t peice, uint8_t column, uint8_t row)
|
|||||||
pawn_move(row - 2, column);
|
pawn_move(row - 2, column);
|
||||||
}
|
}
|
||||||
pawn_move(row - 1, column);
|
pawn_move(row - 1, column);
|
||||||
if(column >= 1)
|
if((column >= 1) && opposite_teams(Board_State[row][column], Board_State[row - 1][column - 1]))
|
||||||
{
|
{
|
||||||
pawn_take(row - 1, column - 1);
|
pawn_take(row - 1, column - 1);
|
||||||
}
|
}
|
||||||
if (column <= 6)
|
if ((column <= 6) && opposite_teams(Board_State[row][column], Board_State[row - 1][column + 1]))
|
||||||
{
|
{
|
||||||
pawn_take(row - 1, column + 1);
|
pawn_take(row - 1, column + 1);
|
||||||
}
|
}
|
||||||
@ -83,9 +134,17 @@ void Mark_Potential_Moves(uint8_t peice, uint8_t column, uint8_t row)
|
|||||||
case PAWN_BLACK:
|
case PAWN_BLACK:
|
||||||
if (row == 1)
|
if (row == 1)
|
||||||
{
|
{
|
||||||
board_state[row + 2][column] = (uint8_t)((POTENTIAL_MOVE) | (board_state[row + 2][column] & 0x0Fu));
|
pawn_move(row + 2, column);
|
||||||
|
}
|
||||||
|
pawn_move(row + 1, column);
|
||||||
|
if ((column >= 1) && opposite_teams(Board_State[row][column], Board_State[row + 1][column - 1]))
|
||||||
|
{
|
||||||
|
pawn_take(row + 1, column - 1);
|
||||||
|
}
|
||||||
|
if ((column <= 6) && opposite_teams(Board_State[row][column], Board_State[row + 1][column + 1]))
|
||||||
|
{
|
||||||
|
pawn_take(row + 1, column + 1);
|
||||||
}
|
}
|
||||||
board_state[row + 1][column] = (uint8_t)((uint8_t)(POTENTIAL_MOVE) | (uint8_t)(board_state[row + 1][column] & 0x0Fu));
|
|
||||||
break;
|
break;
|
||||||
case ROOK_WHITE:
|
case ROOK_WHITE:
|
||||||
case ROOK_BLACK:
|
case ROOK_BLACK:
|
||||||
@ -96,170 +155,504 @@ void Mark_Potential_Moves(uint8_t peice, uint8_t column, uint8_t row)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool click(SDL_Renderer *p_renderer, int x, int y, bool pick_up)
|
void Mark_Taken_Peices_Destination(void)
|
||||||
{
|
{
|
||||||
SDL_Point const point = {x, y};
|
uint8_t add = (white_team(Taken_Peice) ? 8u : 10u);
|
||||||
bool ret_val = false;
|
switch (Taken_Peice)
|
||||||
rectangle.w = board_width;
|
|
||||||
rectangle.h = board_width;
|
|
||||||
rectangle.x = (width - board_width) / 2;
|
|
||||||
rectangle.y = (height - board_width) / 2;
|
|
||||||
if (SDL_PointInRect(&point, &rectangle))
|
|
||||||
{
|
{
|
||||||
int square_size = board_width / 8;
|
case PAWN_WHITE:
|
||||||
int starting_x = rectangle.x;
|
case PAWN_BLACK:
|
||||||
rectangle.w = square_size;
|
|
||||||
rectangle.h = square_size;
|
|
||||||
for (size_t j = 0; j < 8; j++)
|
|
||||||
{
|
{
|
||||||
rectangle.x = starting_x;
|
for (uint8_t j = 0; j < 2u; j++)
|
||||||
for (size_t i = 0; i < 8; i++)
|
|
||||||
{
|
{
|
||||||
if(SDL_PointInRect(&point, &rectangle))
|
for (uint8_t i = 0; i < 4u; i++)
|
||||||
{
|
{
|
||||||
if (pick_up && (board_state[j][i] < SQUARE_EMPTY))
|
if(Board_State[add + j][i] != Taken_Peice)
|
||||||
{
|
{
|
||||||
selected_peices.push(board_state[j][i]);
|
Board_State[add + j][i] = Taken_Peice;
|
||||||
Mark_Potential_Moves(board_state[j][i], i, j);
|
Taken_Peice = SQUARE_EMPTY;
|
||||||
board_state[j][i] = SQUARE_EMPTY;
|
return;
|
||||||
}
|
}
|
||||||
else if (!selected_peices.empty() && (board_state[j][i] >= SQUARE_EMPTY))
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case ROOK_WHITE:
|
||||||
|
case ROOK_BLACK:
|
||||||
|
case KNIGHT_WHITE:
|
||||||
|
case KNIGHT_BLACK:
|
||||||
|
case BISHOP_WHITE:
|
||||||
|
case BISHOP_BLACK:
|
||||||
|
case QUEEN_WHITE:
|
||||||
|
case QUEEN_BLACK:
|
||||||
{
|
{
|
||||||
board_state[j][i] = selected_peices.top();
|
Board_Lights[add][(Taken_Peice / 2u) + 2u] = PEICE_NEEDS_TO_BE_HERE;
|
||||||
selected_peices.pop();
|
Board_Lights[add + 1u][(Taken_Peice / 2u) + 2u] = PEICE_NEEDS_TO_BE_HERE;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Switch_Turns(void)
|
||||||
|
{
|
||||||
|
Game_State = (White_Turn ? GAME_STATE_P2_TURN_BEGINING : GAME_STATE_P1_TURN_BEGINING);
|
||||||
|
White_Turn = !White_Turn;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Board_Square_Was_Toggled(uint8_t j, uint8_t i, bool square_active)
|
||||||
|
{
|
||||||
|
switch (Game_State)
|
||||||
|
{
|
||||||
|
case GAME_STATE_ERROR_DETECTED:
|
||||||
|
{
|
||||||
|
if (Board_Lights[j][i] == PEICE_ORIGIN)
|
||||||
|
{
|
||||||
|
Board_State[j][i] = Selected_Peice;
|
||||||
|
Selected_Peice = SQUARE_EMPTY;
|
||||||
|
clear_lights();
|
||||||
|
}
|
||||||
|
else if (Board_Lights[j][i] == PEICE_NEEDS_TO_BE_HERE)
|
||||||
|
{
|
||||||
|
if (j < 8u)
|
||||||
|
{
|
||||||
|
Board_State[j][i] = Selected_Peice;
|
||||||
|
Selected_Peice = SQUARE_EMPTY;
|
||||||
|
Board_Lights[j][i] = LIGHT_OFF;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* code */
|
Board_State[j][i] = Taken_Peice;
|
||||||
|
Board_Lights[(j / 2u) * 2u][i] = LIGHT_OFF;
|
||||||
|
Board_Lights[(j / 2u) * 2u + 1u][i] = LIGHT_OFF;
|
||||||
|
Taken_Peice = SQUARE_EMPTY;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (Board_Lights[j][i] == ERROR_MOVE)
|
||||||
|
{
|
||||||
|
Error_Count--;
|
||||||
|
Board_Lights[j][i] = LIGHT_OFF;
|
||||||
|
if(Error_Count == 0u)
|
||||||
|
{
|
||||||
|
if (Taken_Peice != SQUARE_EMPTY)
|
||||||
|
{
|
||||||
|
Game_State = White_Turn ? GAME_STATE_P1_TURN_TAKING : GAME_STATE_P2_TURN_TAKING;
|
||||||
|
}
|
||||||
|
if (Selected_Peice == SQUARE_EMPTY)
|
||||||
|
{
|
||||||
|
Game_State = White_Turn ? GAME_STATE_P1_TURN_BEGINING : GAME_STATE_P2_TURN_BEGINING;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Game_State = White_Turn ? GAME_STATE_P1_TURN_IN_PROGRESS : GAME_STATE_P2_TURN_IN_PROGRESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Error_Count++;
|
||||||
|
Board_Lights[j][i] = ERROR_MOVE;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GAME_STATE_IDLE:
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GAME_STATE_P2_TURN_BEGINING:
|
||||||
|
case GAME_STATE_P1_TURN_BEGINING:
|
||||||
|
{
|
||||||
|
/* We are waiting till the player who's turn it is picks up a peice that is on their team */
|
||||||
|
if ((Board_State[j][i] != SQUARE_EMPTY) && (opposite_teams(Board_State[j][i], (White_Turn ? PAWN_BLACK : PAWN_WHITE))))
|
||||||
|
{
|
||||||
|
Mark_Potential_Moves(Board_State[j][i], i, j);
|
||||||
|
Selected_Peice = Board_State[j][i];
|
||||||
|
Board_State[j][i] = SQUARE_EMPTY;
|
||||||
|
Game_State++;
|
||||||
|
Board_Lights[j][i] = PEICE_ORIGIN;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Game_State = GAME_STATE_ERROR_DETECTED;
|
||||||
|
Board_Lights[j][i] = ERROR_MOVE;
|
||||||
|
Error_Count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GAME_STATE_P2_TURN_IN_PROGRESS:
|
||||||
|
case GAME_STATE_P1_TURN_IN_PROGRESS:
|
||||||
|
{
|
||||||
|
/* We are waiting till the player who's turn it is picks up a peice that is on their team */
|
||||||
|
if (Board_Lights[j][i] == POTENTIAL_MOVE)
|
||||||
|
{
|
||||||
|
Board_State[j][i] = Selected_Peice;
|
||||||
|
Selected_Peice = SQUARE_EMPTY;
|
||||||
|
clear_lights();
|
||||||
|
Switch_Turns();
|
||||||
|
}
|
||||||
|
else if (Board_Lights[j][i] == POTENTIAL_TAKE)
|
||||||
|
{
|
||||||
|
Taken_Peice = Board_State[j][i];
|
||||||
|
Board_State[j][i] = SQUARE_EMPTY;
|
||||||
|
Game_State = (White_Turn ? GAME_STATE_P1_TURN_TAKING : GAME_STATE_P2_TURN_TAKING);
|
||||||
|
Mark_Taken_Peices_Destination();
|
||||||
|
clear_lights();
|
||||||
|
Board_Lights[j][i] = PEICE_NEEDS_TO_BE_HERE;
|
||||||
|
}
|
||||||
|
else if (Board_Lights[j][i] == PEICE_ORIGIN)
|
||||||
|
{
|
||||||
|
Board_State[j][i] = Selected_Peice;
|
||||||
|
Selected_Peice = SQUARE_EMPTY;
|
||||||
|
clear_lights();
|
||||||
|
Game_State--;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Game_State = GAME_STATE_ERROR_DETECTED;
|
||||||
|
Board_Lights[j][i] = ERROR_MOVE;
|
||||||
|
Error_Count++;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case GAME_STATE_P2_TURN_TAKING:
|
||||||
|
case GAME_STATE_P1_TURN_TAKING:
|
||||||
|
{
|
||||||
|
if (Board_Lights[j][i] == PEICE_NEEDS_TO_BE_HERE)
|
||||||
|
{
|
||||||
|
if(j < 8u)
|
||||||
|
{
|
||||||
|
Board_State[j][i] = Selected_Peice;
|
||||||
|
Selected_Peice = SQUARE_EMPTY;
|
||||||
|
Board_Lights[j][i] = LIGHT_OFF;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Board_State[j][i] = Taken_Peice;
|
||||||
|
Board_Lights[(j / 2u) * 2u][i] = LIGHT_OFF;
|
||||||
|
Board_Lights[(j / 2u) * 2u + 1u][i] = LIGHT_OFF;
|
||||||
|
Taken_Peice = SQUARE_EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Game_State = GAME_STATE_ERROR_DETECTED;
|
||||||
|
Board_Lights[j][i] = ERROR_MOVE;
|
||||||
|
Error_Count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((Selected_Peice == SQUARE_EMPTY) && (Taken_Peice == SQUARE_EMPTY))
|
||||||
|
{
|
||||||
|
Switch_Turns();
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief The Board changed so now we have to see what is different and act accordingly.
|
||||||
|
* @note Yes i know the design of this seems really bad but it's important to remember this is supposed to simulate the chess board I'm creating.
|
||||||
|
* so I'm designing it this way because of the hardware that I'm using.
|
||||||
|
* @retval None
|
||||||
|
*/
|
||||||
|
void Board_Changed(void)
|
||||||
|
{
|
||||||
|
for (uint8_t j = 0u; j < 12u; j++)
|
||||||
|
{
|
||||||
|
uint8_t difference = (Current_Binary_Board[j] ^ Saved_Binary_Board[j]);
|
||||||
|
if (difference != 0u)
|
||||||
|
{
|
||||||
|
for (uint8_t i = 0u; i < 8u; i++)
|
||||||
|
{
|
||||||
|
if((difference & (1u << i)) != 0u)
|
||||||
|
{
|
||||||
|
Board_Square_Was_Toggled(j, i, ((Current_Binary_Board[j] & (1u << i)) != 0u));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Saved_Binary_Board[j] = Current_Binary_Board[j];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Function for registering an incoming click
|
||||||
|
* @param p_renderer: Pointer to the renderer
|
||||||
|
* @param x: x location of the click
|
||||||
|
* @param y: y location of the click
|
||||||
|
* @retval
|
||||||
|
*/
|
||||||
|
void click(SDL_Renderer *p_renderer, int x, int y)
|
||||||
|
{
|
||||||
|
SDL_Point const point = {x, y};
|
||||||
|
const int square_size = Board_Width / 8;
|
||||||
|
Rectangle.w = Board_Width + (4 * square_size);
|
||||||
|
Rectangle.h = Board_Width;
|
||||||
|
Rectangle.x = ((Width - Board_Width) / 2) - (2 * square_size);
|
||||||
|
Rectangle.y = (Height - Board_Width) / 2;
|
||||||
|
if (SDL_PointInRect(&point, &Rectangle))
|
||||||
|
{
|
||||||
|
Rectangle.x = (Width - Board_Width) / 2;
|
||||||
|
Rectangle.w = Board_Width;
|
||||||
|
int starting_y = Rectangle.y;
|
||||||
|
const int starting_x = Rectangle.x;
|
||||||
|
Rectangle.w = square_size;
|
||||||
|
Rectangle.h = square_size;
|
||||||
|
for (size_t j = 0; j < 8; j++)
|
||||||
|
{
|
||||||
|
Rectangle.x = starting_x;
|
||||||
|
for (size_t i = 0; i < 8; i++)
|
||||||
|
{
|
||||||
|
if(SDL_PointInRect(&point, &Rectangle))
|
||||||
|
{
|
||||||
|
Current_Binary_Board[j] ^= (1u << i);
|
||||||
|
Board_Changed();
|
||||||
goto draw_square;
|
goto draw_square;
|
||||||
}
|
}
|
||||||
rectangle.x += square_size;
|
Rectangle.x += square_size;
|
||||||
|
}
|
||||||
|
Rectangle.y += square_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
Rectangle.x = ((Width - Board_Width) / 2) - (2 * square_size);
|
||||||
|
/* Now we draw the jail */
|
||||||
|
for (size_t j = 8; j < 12; j++)
|
||||||
|
{
|
||||||
|
Rectangle.y = starting_y;
|
||||||
|
for (size_t i = 0; i < 8; i++)
|
||||||
|
{
|
||||||
|
if (SDL_PointInRect(&point, &Rectangle))
|
||||||
|
{
|
||||||
|
Current_Binary_Board[j] ^= (1u << i);
|
||||||
|
Board_Changed();
|
||||||
|
goto draw_square;
|
||||||
|
}
|
||||||
|
Rectangle.y += square_size;
|
||||||
|
}
|
||||||
|
/*If we are at the end of second jail row, jump to the other side */
|
||||||
|
if (j == 9)
|
||||||
|
{
|
||||||
|
Rectangle.x += (Board_Width + square_size);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Rectangle.x += square_size;
|
||||||
}
|
}
|
||||||
rectangle.y += square_size;
|
|
||||||
}
|
}
|
||||||
draw_square:
|
draw_square:
|
||||||
SDL_SetRenderTarget(p_renderer, board_texture);
|
SDL_SetRenderTarget(p_renderer, Board_Texture);
|
||||||
//SDL_SetRenderDrawColor(p_renderer, 0x00, 0xFF, 0x00, 0x00);
|
|
||||||
//SDL_RenderDrawRect(p_renderer, &rectangle);
|
|
||||||
//SDL_RenderFillRect(p_renderer, &rectangle);
|
|
||||||
//SDL_SetRenderTarget(p_renderer, NULL);
|
|
||||||
draw_board(p_renderer);
|
draw_board(p_renderer);
|
||||||
SDL_RenderCopy(p_renderer, board_texture, NULL, NULL);
|
SDL_RenderCopy(p_renderer, Board_Texture, NULL, NULL);
|
||||||
SDL_RenderPresent(p_renderer);
|
SDL_RenderPresent(p_renderer);
|
||||||
}
|
}
|
||||||
return ret_val;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Function for resizing the display of the board
|
||||||
|
* @param p_renderer: pointer to the renderer
|
||||||
|
* @param w: width of the new window
|
||||||
|
* @param h: hight of the new window
|
||||||
|
* @retval None
|
||||||
|
*/
|
||||||
void chess_board_resize(SDL_Renderer *p_renderer, int w, int h)
|
void chess_board_resize(SDL_Renderer *p_renderer, int w, int h)
|
||||||
{
|
{
|
||||||
width = w;
|
Width = w;
|
||||||
height = h;
|
Height = h;
|
||||||
SDL_DestroyTexture(board_texture);
|
SDL_DestroyTexture(Board_Texture);
|
||||||
board_texture = SDL_CreateTexture(p_renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
|
Board_Texture = SDL_CreateTexture(p_renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
|
||||||
board_width = ((w > h) ? h : w) - MARGIN;
|
Board_Width = ((w > h) ? h : w) - MARGIN;
|
||||||
// get rid of rounding errors
|
// get rid of rounding errors
|
||||||
board_width -= board_width % 8;
|
Board_Width -= Board_Width % 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Function for initializing the board
|
||||||
|
* @note
|
||||||
|
* @param *p_renderer pointer to the renderer:
|
||||||
|
* @retval None
|
||||||
|
*/
|
||||||
void chess_board_init(SDL_Renderer *p_renderer)
|
void chess_board_init(SDL_Renderer *p_renderer)
|
||||||
{
|
{
|
||||||
for (uint8_t i = 0; i < 8; i++)
|
for (uint8_t i = 0u; i < 12u; i++)
|
||||||
{
|
{
|
||||||
for (uint8_t j = 0; j < 8; j++)
|
for (uint8_t j = 0u; j < 8u; j++)
|
||||||
{
|
{
|
||||||
board_state[i][j] = SQUARE_EMPTY;
|
Board_State[i][j] = SQUARE_EMPTY;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
Current_Binary_Board[0] = 0xFF;
|
||||||
|
Current_Binary_Board[1] = 0xFF;
|
||||||
|
Current_Binary_Board[6] = 0xFF;
|
||||||
|
Current_Binary_Board[7] = 0xFF;
|
||||||
|
Saved_Binary_Board[0] = 0xFF;
|
||||||
|
Saved_Binary_Board[1] = 0xFF;
|
||||||
|
Saved_Binary_Board[6] = 0xFF;
|
||||||
|
Saved_Binary_Board[7] = 0xFF;
|
||||||
|
|
||||||
|
//Place black pieces
|
||||||
|
Board_State[0][0] = ROOK_BLACK;
|
||||||
|
Board_State[0][7] = ROOK_BLACK;
|
||||||
|
Board_State[0][1] = KNIGHT_BLACK;
|
||||||
|
Board_State[0][6] = KNIGHT_BLACK;
|
||||||
|
Board_State[0][2] = BISHOP_BLACK;
|
||||||
|
Board_State[0][5] = BISHOP_BLACK;
|
||||||
|
Board_State[0][3] = QUEEN_BLACK;
|
||||||
|
Board_State[0][4] = KING_BLACK;
|
||||||
|
Board_State[7][0] = ROOK_WHITE;
|
||||||
|
Board_State[7][7] = ROOK_WHITE;
|
||||||
|
Board_State[7][1] = KNIGHT_WHITE;
|
||||||
|
Board_State[7][6] = KNIGHT_WHITE;
|
||||||
|
Board_State[7][2] = BISHOP_WHITE;
|
||||||
|
Board_State[7][5] = BISHOP_WHITE;
|
||||||
|
Board_State[7][3] = QUEEN_WHITE;
|
||||||
|
Board_State[7][4] = KING_WHITE;
|
||||||
|
|
||||||
//Place black peices
|
|
||||||
board_state[0][0] = BISHOP_BLACK;
|
|
||||||
board_state[0][7] = BISHOP_BLACK;
|
|
||||||
board_state[0][1] = KNIGHT_BLACK;
|
|
||||||
board_state[0][6] = KNIGHT_BLACK;
|
|
||||||
board_state[0][2] = ROOK_BLACK;
|
|
||||||
board_state[0][5] = ROOK_BLACK;
|
|
||||||
board_state[0][3] = QUEEN_BLACK;
|
|
||||||
board_state[0][4] = KING_BLACK;
|
|
||||||
board_state[7][0] = BISHOP_WHITE;
|
|
||||||
board_state[7][7] = BISHOP_WHITE;
|
|
||||||
board_state[7][1] = KNIGHT_WHITE;
|
|
||||||
board_state[7][6] = KNIGHT_WHITE;
|
|
||||||
board_state[7][2] = ROOK_WHITE;
|
|
||||||
board_state[7][5] = ROOK_WHITE;
|
|
||||||
board_state[7][3] = QUEEN_WHITE;
|
|
||||||
board_state[7][4] = KING_WHITE;
|
|
||||||
for (uint8_t i = 0; i < 8; i++)
|
for (uint8_t i = 0; i < 8; i++)
|
||||||
{
|
{
|
||||||
board_state[1][i] = PAWN_BLACK;
|
Board_State[1][i] = PAWN_BLACK;
|
||||||
board_state[6][i] = PAWN_WHITE;
|
Board_State[6][i] = PAWN_WHITE;
|
||||||
}
|
}
|
||||||
for (uint8_t i = 0; i < 12; i++)
|
for (uint8_t i = 0; i < 12; i++)
|
||||||
{
|
{
|
||||||
//location of all the sprites plus the size of file names
|
//location of all the sprites plus the size of file names
|
||||||
char file[25] = "sprites\\";
|
char file[25] = "sprites\\";
|
||||||
memcpy(&file[8], file_names[i], 16);
|
memcpy(&file[8], File_Names[i], 16);
|
||||||
bitmapSurface = SDL_LoadBMP(file);
|
bitmapSurface = SDL_LoadBMP(file);
|
||||||
bitmapTextures[i] = SDL_CreateTextureFromSurface(p_renderer, bitmapSurface);
|
bitmapTextures[i] = SDL_CreateTextureFromSurface(p_renderer, bitmapSurface);
|
||||||
}
|
}
|
||||||
SDL_FreeSurface(bitmapSurface);
|
SDL_FreeSurface(bitmapSurface);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Funtion for that will draw the current state of the board including pecies and colors for suggested and possible moves.
|
||||||
|
* @param *p_renderer pointer to the renderer object:
|
||||||
|
* @retval None
|
||||||
|
*/
|
||||||
void draw_board(SDL_Renderer *p_renderer)
|
void draw_board(SDL_Renderer *p_renderer)
|
||||||
{
|
{
|
||||||
SDL_SetRenderTarget(p_renderer, board_texture);
|
SDL_SetRenderTarget(p_renderer, Board_Texture);
|
||||||
SDL_SetRenderDrawColor(p_renderer, 0x7f, 0x7f, 0x7f, 0);
|
SDL_SetRenderDrawColor(p_renderer, 0x7f, 0x7f, 0x7f, 0);
|
||||||
SDL_RenderClear(p_renderer);
|
SDL_RenderClear(p_renderer);
|
||||||
SDL_RenderDrawRect(p_renderer, &rectangle);
|
SDL_RenderDrawRect(p_renderer, &Rectangle);
|
||||||
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0xFF, 0xFF, 0x00);
|
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0xFF, 0xFF, 0x00);
|
||||||
rectangle.w = board_width;
|
Rectangle.w = Board_Width;
|
||||||
rectangle.h = board_width;
|
Rectangle.h = Board_Width;
|
||||||
rectangle.x = (width - board_width) / 2;
|
Rectangle.x = (Width - Board_Width) / 2;
|
||||||
rectangle.y = (height - board_width) / 2;
|
Rectangle.y = (Height - Board_Width) / 2;
|
||||||
SDL_RenderFillRect(p_renderer, &rectangle);
|
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||||
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
||||||
int square_size = board_width / 8;
|
const int square_size = Board_Width / 8;
|
||||||
int starting_x = rectangle.x;
|
int starting_x = Rectangle.x;
|
||||||
rectangle.w = square_size;
|
Rectangle.w = square_size;
|
||||||
rectangle.h = square_size;
|
Rectangle.h = square_size;
|
||||||
for (size_t j = 0; j < 8; j++)
|
for (size_t j = 0; j < 8; j++)
|
||||||
{
|
{
|
||||||
rectangle.x = starting_x;
|
Rectangle.x = starting_x;
|
||||||
for (size_t i = 0; i < 8; i++)
|
for (size_t i = 0; i < 8; i++)
|
||||||
{
|
{
|
||||||
if((board_state[j][i] & 0xF0u) == POTENTIAL_MOVE)
|
if(Board_Lights[j][i] == POTENTIAL_MOVE)
|
||||||
{
|
{
|
||||||
SDL_SetRenderDrawColor(p_renderer, 0x00, 0xFF, 0x00, 0x00);
|
SDL_SetRenderDrawColor(p_renderer, 0x00, 0xFF, 0x00, 0x00);
|
||||||
SDL_RenderFillRect(p_renderer, &rectangle);
|
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||||
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
||||||
}
|
}
|
||||||
else if ((board_state[j][i] & 0xF0u) == POTENTIAL_TAKE)
|
else if ((Board_Lights[j][i] == POTENTIAL_TAKE) || (Board_Lights[j][i] == PEICE_NEEDS_TO_BE_HERE))
|
||||||
{
|
{
|
||||||
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0x00, 0x00, 0x00);
|
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0x00, 0x00, 0x00);
|
||||||
SDL_RenderFillRect(p_renderer, &rectangle);
|
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||||
|
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
||||||
|
}
|
||||||
|
else if (Board_Lights[j][i] == PEICE_ORIGIN)
|
||||||
|
{
|
||||||
|
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0x00, 0xFF, 0x00);
|
||||||
|
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||||
|
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
||||||
|
}
|
||||||
|
else if (Board_Lights[j][i] == ERROR_MOVE)
|
||||||
|
{
|
||||||
|
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0xFF, 0x00, 0x00);
|
||||||
|
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||||
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
|
||||||
}
|
}
|
||||||
else if (((i % 2) + (j % 2)) == 1)
|
else if (((i % 2) + (j % 2)) == 1)
|
||||||
{
|
{
|
||||||
SDL_RenderFillRect(p_renderer, &rectangle);
|
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
/* code */
|
/* code */
|
||||||
}
|
}
|
||||||
|
|
||||||
if((board_state[j][i] & 0x0Fu) != SQUARE_EMPTY)
|
if((Board_State[j][i] & 0x0Fu) != SQUARE_EMPTY)
|
||||||
{
|
{
|
||||||
SDL_RenderCopy(p_renderer, bitmapTextures[(board_state[j][i] & 0x0Fu)], NULL, &rectangle);
|
SDL_RenderCopy(p_renderer, bitmapTextures[(Board_State[j][i] & 0x0Fu)], NULL, &Rectangle);
|
||||||
}
|
}
|
||||||
|
|
||||||
rectangle.x += square_size;
|
Rectangle.x += square_size;
|
||||||
|
}
|
||||||
|
Rectangle.y += square_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
Rectangle.x = ((Width - Board_Width) / 2) - (2 * square_size);
|
||||||
|
Rectangle.y = (Height - Board_Width) / 2;
|
||||||
|
int starting_y = Rectangle.y;
|
||||||
|
SDL_SetRenderDrawColor(p_renderer, 0x6F, 0x6f, 0x6f, 0x00);
|
||||||
|
|
||||||
|
/* Now we draw the jail */
|
||||||
|
for (size_t j = 8; j < 12; j++)
|
||||||
|
{
|
||||||
|
Rectangle.y = starting_y;
|
||||||
|
for (size_t i = 0; i < 8; i++)
|
||||||
|
{
|
||||||
|
if (Board_Lights[j][i] == PEICE_NEEDS_TO_BE_HERE)
|
||||||
|
{
|
||||||
|
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0x00, 0x00, 0x00);
|
||||||
|
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||||
|
SDL_SetRenderDrawColor(p_renderer, 0x6F, 0x6F, 0x6F, 0x00);
|
||||||
|
}
|
||||||
|
else if (Board_Lights[j][i] == ERROR_MOVE)
|
||||||
|
{
|
||||||
|
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0xFF, 0x00, 0x00);
|
||||||
|
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||||
|
SDL_SetRenderDrawColor(p_renderer, 0x6F, 0x6F, 0x6F, 0x00);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
SDL_RenderFillRect(p_renderer, &Rectangle);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if ((Board_State[j][i] & 0x0Fu) != SQUARE_EMPTY)
|
||||||
|
{
|
||||||
|
SDL_RenderCopy(p_renderer, bitmapTextures[(Board_State[j][i] & 0x0Fu)], NULL, &Rectangle);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Rectangle.y += square_size;
|
||||||
|
}
|
||||||
|
/*If we are at the end of second jail row, jump to the other side */
|
||||||
|
if(j == 9)
|
||||||
|
{
|
||||||
|
Rectangle.x += (Board_Width + square_size);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Rectangle.x += square_size;
|
||||||
}
|
}
|
||||||
rectangle.y += square_size;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_SetRenderTarget(p_renderer, NULL);
|
SDL_SetRenderTarget(p_renderer, NULL);
|
||||||
SDL_RenderCopy(p_renderer, board_texture, NULL, NULL);
|
SDL_RenderCopy(p_renderer, Board_Texture, NULL, NULL);
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <SDL_video.h>
|
#include <SDL_video.h>
|
||||||
|
|
||||||
bool click(SDL_Renderer *p_renderer, int x, int y, bool pick_up);
|
void click(SDL_Renderer *p_renderer, int x, int y);
|
||||||
void chess_board_resize(SDL_Renderer *p_renderer, int w, int h);
|
void chess_board_resize(SDL_Renderer *p_renderer, int w, int h);
|
||||||
void chess_board_init(SDL_Renderer *p_renderer);
|
void chess_board_init(SDL_Renderer *p_renderer);
|
||||||
void draw_board(SDL_Renderer * p_renderer);
|
void draw_board(SDL_Renderer * p_renderer);
|
108
game.cpp
Normal file
108
game.cpp
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <SDL.h>
|
||||||
|
#include <SDL_video.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include "chess_board.h"
|
||||||
|
|
||||||
|
static clock_t start_time, end_time;
|
||||||
|
|
||||||
|
int begin_game(SDL_Renderer *renderer, SDL_Window *win)
|
||||||
|
{
|
||||||
|
SDL_Rect srcR, destR;
|
||||||
|
|
||||||
|
srcR.h = 800;
|
||||||
|
srcR.w = 800;
|
||||||
|
destR.h = 800;
|
||||||
|
destR.w = 800;
|
||||||
|
chess_board_resize(renderer, 800, 800);
|
||||||
|
chess_board_init(renderer);
|
||||||
|
srcR.x = 0;
|
||||||
|
srcR.y = 0;
|
||||||
|
destR.x = 0;
|
||||||
|
destR.y = 0;
|
||||||
|
bool run = true;
|
||||||
|
while (run)
|
||||||
|
{
|
||||||
|
clock_t start_t, end_t;
|
||||||
|
SDL_Event event;
|
||||||
|
bool redraw = false;
|
||||||
|
while (SDL_PollEvent(&event))
|
||||||
|
{
|
||||||
|
/* handle your event here */
|
||||||
|
if (event.type == SDL_MOUSEBUTTONDOWN)
|
||||||
|
{
|
||||||
|
if (event.button.button == SDL_BUTTON_MIDDLE) // scroll up
|
||||||
|
{
|
||||||
|
run = false;
|
||||||
|
}
|
||||||
|
else if ((event.button.button == SDL_BUTTON_LEFT)
|
||||||
|
|| (event.button.button == SDL_BUTTON_RIGHT)) // scroll up
|
||||||
|
{
|
||||||
|
start_time = clock();
|
||||||
|
|
||||||
|
click(renderer, event.button.x, event.button.y);
|
||||||
|
|
||||||
|
end_time = clock();
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
clock_t t = end_time - start_time;
|
||||||
|
SDL_Log("No. of clicks %ld clicks (%f seconds) to process the incoming click.\n", t, ((float)t) / CLOCKS_PER_SEC);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Do nothing. */
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (event.type == SDL_QUIT)
|
||||||
|
{
|
||||||
|
run = false;
|
||||||
|
}
|
||||||
|
else if (event.type == SDL_WINDOWEVENT)
|
||||||
|
{
|
||||||
|
switch (event.window.event)
|
||||||
|
{
|
||||||
|
case SDL_WINDOWEVENT_ENTER:
|
||||||
|
case SDL_WINDOWEVENT_SHOWN:
|
||||||
|
case SDL_WINDOWEVENT_MOVED:
|
||||||
|
case SDL_WINDOWEVENT_EXPOSED:
|
||||||
|
case SDL_WINDOWEVENT_RESTORED:
|
||||||
|
redraw = true;
|
||||||
|
break;
|
||||||
|
case SDL_WINDOWEVENT_RESIZED:
|
||||||
|
case SDL_WINDOWEVENT_SIZE_CHANGED:
|
||||||
|
case SDL_WINDOWEVENT_MAXIMIZED:
|
||||||
|
redraw = true;
|
||||||
|
SDL_GetWindowSize(win, &destR.w, &destR.h);
|
||||||
|
chess_board_resize(renderer, destR.w, destR.h);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
/* Do nothing. */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (redraw)
|
||||||
|
{
|
||||||
|
start_time = clock();
|
||||||
|
|
||||||
|
draw_board(renderer);
|
||||||
|
|
||||||
|
end_time = clock();
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
clock_t t = end_time - start_time;
|
||||||
|
SDL_Log("No. of clicks %ld clicks (%f seconds).\n",
|
||||||
|
t, ((float)t) / CLOCKS_PER_SEC);
|
||||||
|
}
|
||||||
|
/* do some other stuff here -- draw your app, etc. */
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
//g++ main.cpp -o blah `sdl2-config --cflags --libs`
|
||||||
|
/*
|
||||||
|
g++ main.cpp -IC:\Users\Daniel\Documents\Projects\i686-w64-mingw32\include\SDL2 -LC:\Users\Daniel\Documents\Projects\i686-w64-mingw32\lib -w -Wl,-subsystem,windows -lmingw32 -lSDL2main -lSDL2 -o SDLMain
|
||||||
|
*/
|
5
game.h
Normal file
5
game.h
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#define SDL_MAIN_HANDLED
|
||||||
|
#include <SDL.h>
|
||||||
|
#include <SDL_video.h>
|
||||||
|
|
||||||
|
int begin_game(SDL_Renderer *renderer, SDL_Window *win);
|
90
main.cpp
90
main.cpp
@ -6,20 +6,14 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <SDL_video.h>
|
#include <SDL_video.h>
|
||||||
#include <time.h>
|
#include "game.h"
|
||||||
#include "chess_board.h"
|
|
||||||
|
|
||||||
clock_t start_time, end_time;
|
|
||||||
|
|
||||||
int main( int argc, const char* argv[] )
|
int main( int argc, const char* argv[] )
|
||||||
{
|
{
|
||||||
SDL_Renderer *renderer = NULL;
|
SDL_Renderer *renderer = NULL;
|
||||||
SDL_Texture *texture = NULL;
|
SDL_Texture *texture = NULL;
|
||||||
SDL_Surface *bitmapSurface = NULL;
|
|
||||||
SDL_Window *win;
|
SDL_Window *win;
|
||||||
SDL_Surface *image;
|
|
||||||
SDL_Rect srcR, destR;
|
SDL_Rect srcR, destR;
|
||||||
Uint32 rmask, gmask, bmask, amask;
|
|
||||||
const char name[] = "Game";
|
const char name[] = "Game";
|
||||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
|
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
|
||||||
{
|
{
|
||||||
@ -44,89 +38,9 @@ int main( int argc, const char* argv[] )
|
|||||||
rectangle.h = 50;
|
rectangle.h = 50;
|
||||||
SDL_RenderFillRect(renderer, &rectangle);
|
SDL_RenderFillRect(renderer, &rectangle);
|
||||||
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 800, 800);
|
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 800, 800);
|
||||||
srcR.h = 800;
|
|
||||||
srcR.w = 800;
|
|
||||||
destR.h = 800;
|
|
||||||
destR.w = 800;
|
|
||||||
chess_board_resize(renderer, 800, 800);
|
|
||||||
chess_board_init(renderer);
|
|
||||||
srcR.x = 0;
|
|
||||||
srcR.y = 0;
|
|
||||||
destR.x = 0;
|
|
||||||
destR.y = 0;
|
|
||||||
bool run = true;
|
|
||||||
while (run)
|
|
||||||
{
|
|
||||||
clock_t start_t, end_t;
|
|
||||||
SDL_Event event;
|
|
||||||
bool redraw = false;
|
|
||||||
while (SDL_PollEvent(&event))
|
|
||||||
{
|
|
||||||
/* handle your event here */
|
|
||||||
if (event.type == SDL_MOUSEBUTTONDOWN)
|
|
||||||
{
|
|
||||||
if (event.button.button == SDL_BUTTON_MIDDLE) // scroll up
|
|
||||||
{
|
|
||||||
run = false;
|
|
||||||
}
|
|
||||||
else if (event.button.button == SDL_BUTTON_LEFT) // scroll up
|
|
||||||
{
|
|
||||||
redraw = click(renderer, event.button.x, event.button.y, true);
|
|
||||||
}
|
|
||||||
else if (event.button.button == SDL_BUTTON_RIGHT) // scroll up
|
|
||||||
{
|
|
||||||
redraw = click(renderer, event.button.x, event.button.y, false);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Do nothing. */
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
(void)begin_game(renderer, win);
|
||||||
else if (event.type == SDL_QUIT)
|
|
||||||
{
|
|
||||||
run = false;
|
|
||||||
}
|
|
||||||
else if (event.type == SDL_WINDOWEVENT)
|
|
||||||
{
|
|
||||||
switch (event.window.event)
|
|
||||||
{
|
|
||||||
case SDL_WINDOWEVENT_ENTER:
|
|
||||||
case SDL_WINDOWEVENT_SHOWN:
|
|
||||||
case SDL_WINDOWEVENT_MOVED:
|
|
||||||
case SDL_WINDOWEVENT_EXPOSED:
|
|
||||||
case SDL_WINDOWEVENT_RESTORED:
|
|
||||||
redraw = true;
|
|
||||||
break;
|
|
||||||
case SDL_WINDOWEVENT_RESIZED:
|
|
||||||
case SDL_WINDOWEVENT_SIZE_CHANGED:
|
|
||||||
case SDL_WINDOWEVENT_MAXIMIZED:
|
|
||||||
redraw = true;
|
|
||||||
SDL_GetWindowSize(win, &destR.w, &destR.h);
|
|
||||||
chess_board_resize(renderer, destR.w, destR.h);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
/* Do nothing. */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (redraw)
|
|
||||||
{
|
|
||||||
start_time = clock();
|
|
||||||
|
|
||||||
draw_board(renderer);
|
|
||||||
|
|
||||||
end_time = clock();
|
|
||||||
SDL_RenderPresent(renderer);
|
|
||||||
clock_t t = end_time - start_time;
|
|
||||||
SDL_Log("No. of clicks %ld clicks (%f seconds).\n",
|
|
||||||
t, ((float)t) / CLOCKS_PER_SEC);
|
|
||||||
}
|
|
||||||
/* do some other stuff here -- draw your app, etc. */
|
|
||||||
}
|
|
||||||
SDL_FreeSurface(image);
|
|
||||||
SDL_DestroyTexture(texture);
|
SDL_DestroyTexture(texture);
|
||||||
SDL_DestroyRenderer(renderer);
|
SDL_DestroyRenderer(renderer);
|
||||||
SDL_DestroyWindow(win);
|
SDL_DestroyWindow(win);
|
||||||
|
12
pieces.cpp
Normal file
12
pieces.cpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#include "pieces.hpp"
|
||||||
|
|
||||||
|
pieces::pieces(uint8_t row, uint8_t column)
|
||||||
|
{
|
||||||
|
my_position.row = row;
|
||||||
|
my_position.column = column;
|
||||||
|
}
|
||||||
|
|
||||||
|
pieces::~pieces()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
19
pieces.hpp
Normal file
19
pieces.hpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
#define UNINIT 0xFFu
|
||||||
|
|
||||||
|
struct position
|
||||||
|
{
|
||||||
|
uint8_t row;
|
||||||
|
uint8_t column;
|
||||||
|
};
|
||||||
|
|
||||||
|
class pieces
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
position my_position;
|
||||||
|
|
||||||
|
public:
|
||||||
|
pieces(uint8_t row, uint8_t column);
|
||||||
|
~pieces();
|
||||||
|
};
|
Before Width: | Height: | Size: 156 KiB After Width: | Height: | Size: 156 KiB |
Before Width: | Height: | Size: 156 KiB After Width: | Height: | Size: 156 KiB |
Loading…
Reference in New Issue
Block a user