Compare commits

..

10 Commits

18 changed files with 1690 additions and 1456 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
build/
.rscons*
.vscode/

View File

@ -1,23 +0,0 @@
{
"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
View File

@ -1,28 +0,0 @@
{
// 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
}
]
}
]
}

View File

@ -1,5 +0,0 @@
{
"files.associations": {
"stack": "cpp"
}
}

32
.vscode/tasks.json vendored
View File

@ -1,32 +0,0 @@
{
// 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
}
},
]
}

21
CMakeLists.txt Normal file
View File

@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.8)
set(This Chess)
project(${This} C CXX)
enable_language(CXX)
# set(SDL2_INCLUDE_DIR /usr/include/SDL2)
# set(SDL2_LIBRARY /usr/lib/x86_64-linux-gnu/libSDL2.so)
# list(APPEND CMAKE_MODULE_PATH SDL2_INCLUDE_DIR)
# list(APPEND CMAKE_MODULE_PATH SDL2_LIBRARY)
#list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2)
find_package(SDL2 REQUIRED)
file(GLOB FILES
src/*.cpp)
add_executable(${This} ${FILES})
include_directories(${SDL2_INCLUDE_DIRS})
target_link_libraries(${This} ${SDL2_LIBRARIES})

BIN
Chess.exe

Binary file not shown.

BIN
blah

Binary file not shown.

View File

@ -1,7 +0,0 @@
#include <SDL.h>
#include <SDL_video.h>
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_init(SDL_Renderer *p_renderer);
void draw_board(SDL_Renderer * p_renderer);

View File

@ -1,12 +0,0 @@
#include "pieces.hpp"
pieces::pieces(uint8_t row, uint8_t column)
{
my_position.row = row;
my_position.column = column;
}
pieces::~pieces()
{
}

View File

@ -1,19 +0,0 @@
#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();
};

File diff suppressed because it is too large Load Diff

47
src/chess_board.h Normal file
View File

@ -0,0 +1,47 @@
#include "stdint.h"
#define LIGHT_OFF 0u
#define POTENTIAL_MOVE 1u
#define POTENTIAL_TAKE 2u
#define SUGGESTED_MOVE 3u
#define ERROR_MOVE 4u
#define PIECE_ORIGIN 5u
#define PIECE_NEEDS_TO_BE_HERE 6u
#define POTENTIAL_CASTLE 7u
#define PIECE_NEEDS_TO_BE_REMOVED 8u
#define CONVERTING_PAWN 9u
#define PAWN_WHITE 0u
#define PAWN_BLACK 1u
#define KING_WHITE 2u
#define KING_BLACK 3u
#define ROOK_WHITE 4u
#define ROOK_BLACK 5u
#define KNIGHT_WHITE 6u
#define KNIGHT_BLACK 7u
#define BISHOP_WHITE 8u
#define BISHOP_BLACK 9u
#define QUEEN_WHITE 10u
#define QUEEN_BLACK 11u
#define SQUARE_EMPTY 12u
#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_OVER 8u
#define GAME_STATE_OVER_WHITE_WIN 8u
#define GAME_STATE_OVER_BLACK_WIN 9u
#define GAME_STATE_OVER_STALE_MATE 10u
void chess_board_init(void);
void Board_Changed(uint8_t current_binary_board[12]);
void Board_get_lights_and_state(uint8_t board_lights[12][8], uint8_t board_state[12][8]);
void Board_get_game_state(uint8_t * game_state);

View File

@ -1,108 +1,109 @@
#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
*/
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <stdint.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#include <time.h>
#include "chess_board.h"
#include "user_interface_abstraction.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_init();
ui_resize(renderer, 800, 800);
ui_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();
ui_click(renderer, event.button.x, event.button.y);
end_time = clock();
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);
ui_resize(renderer, destR.w, destR.h);
break;
}
}
else
{
/* Do nothing. */
}
}
if (redraw)
{
start_time = clock();
ui_redraw_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
*/

View File

@ -1,5 +1,6 @@
#define SDL_MAIN_HANDLED
#include <SDL.h>
#include <SDL_video.h>
int begin_game(SDL_Renderer *renderer, SDL_Window *win);
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
int begin_game(SDL_Renderer *renderer, SDL_Window *win);

View File

@ -1,52 +1,52 @@
#define SDL_MAIN_HANDLED
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <stdint.h>
#include <SDL.h>
#include <SDL_video.h>
#include "game.h"
int main( int argc, const char* argv[] )
{
SDL_Renderer *renderer = NULL;
SDL_Texture *texture = NULL;
SDL_Window *win;
SDL_Rect srcR, destR;
const char name[] = "Game";
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return 1;
}
win = SDL_CreateWindow(name, 0, 0, 800, 800, SDL_WINDOW_RESIZABLE);
if (win == NULL)
{
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Rect rectangle;
rectangle.x = 0;
rectangle.y = 0;
rectangle.w = 50;
rectangle.h = 50;
SDL_RenderFillRect(renderer, &rectangle);
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 800, 800);
(void)begin_game(renderer, win);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
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
#define SDL_MAIN_HANDLED
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <stdint.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#include "game.h"
int main( int argc, const char* argv[] )
{
SDL_Renderer *renderer = NULL;
SDL_Texture *texture = NULL;
SDL_Window *win;
SDL_Rect srcR, destR;
const char name[] = "Game";
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return 1;
}
win = SDL_CreateWindow(name, 0, 0, 800, 800, SDL_WINDOW_RESIZABLE);
if (win == NULL)
{
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Rect rectangle;
rectangle.x = 0;
rectangle.y = 0;
rectangle.w = 50;
rectangle.h = 50;
SDL_RenderFillRect(renderer, &rectangle);
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 800, 800);
(void)begin_game(renderer, win);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
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
*/

View File

@ -0,0 +1,351 @@
#include "user_interface_abstraction.h"
#include "chess_board.h"
#include <string.h>
#define MARGIN 300
const char File_Names[12][17] = {"pawn_white.bmp", "pawn_black.bmp",
"king_white.bmp", "king_black.bmp",
"tower_white.bmp", "tower_black.bmp",
"horse_white.bmp", "horse_black.bmp",
"bishop_white.bmp", "bishop_black.bmp",
"queen_white.bmp", "queen_black.bmp",
};
static int Height;
static int Width;
static SDL_Texture * Board_Texture;
static SDL_Rect Rectangle;
static int Board_Width;
SDL_Surface *bitmapSurface = NULL;
SDL_Texture * bitmapTextures[12] = {NULL};
static uint8_t Current_Binary_Board[12] = {0};
static void ui_draw_end_game(SDL_Renderer *p_renderer, uint8_t board_state[12][8], uint8_t game_state)
{
SDL_SetRenderTarget(p_renderer, Board_Texture);
SDL_SetRenderDrawColor(p_renderer, 0x7f, 0x7f, 0x7f, 0);
SDL_RenderClear(p_renderer);
SDL_RenderDrawRect(p_renderer, &Rectangle);
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0xFF, 0xFF, 0x00);
Rectangle.w = Board_Width;
Rectangle.h = Board_Width;
Rectangle.x = (Width - Board_Width) / 2;
Rectangle.y = (Height - Board_Width) / 2;
SDL_RenderFillRect(p_renderer, &Rectangle);
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
const int square_size = Board_Width / 8;
int starting_x = Rectangle.x;
Rectangle.w = square_size;
Rectangle.h = square_size;
uint8_t white_color[4] = {0xFF, 0xFF, 0x00, 0x00};
uint8_t black_color[4] = {0xFF, 0xFF, 0x00, 0x00};
if(game_state == GAME_STATE_OVER_WHITE_WIN)
{
white_color[0] = 0x00; white_color[1] = 0xFF; white_color[2] = 0x00; white_color[3] = 0x00;
black_color[0] = 0xFF; black_color[1] = 0x00; black_color[2] = 0x00; black_color[3] = 0x00;
}
else if (game_state == GAME_STATE_OVER_BLACK_WIN)
{
black_color[0] = 0x00; black_color[1] = 0xFF; black_color[2] = 0x00; black_color[3] = 0x00;
white_color[0] = 0xFF; white_color[1] = 0x00; white_color[2] = 0x00; white_color[3] = 0x00;
}
for (size_t j = 0; j < 8; j++)
{
Rectangle.x = starting_x;
for (size_t i = 0; i < 8; i++)
{
if((board_state[j][i] & 0x0Fu) != SQUARE_EMPTY)
{
uint8_t * render_color;
if((board_state[j][i] % 2u) == 0u )
{
render_color = white_color;
}
else
{
render_color = black_color;
}
SDL_SetRenderDrawColor(p_renderer, render_color[0], render_color[1], render_color[2], render_color[3]);
SDL_RenderFillRect(p_renderer, &Rectangle);
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
SDL_RenderCopy(p_renderer, bitmapTextures[(board_state[j][i] & 0x0Fu)], NULL, &Rectangle);
}
else if (((i % 2) + (j % 2)) == 1)
{
SDL_RenderFillRect(p_renderer, &Rectangle);
}
else
{
/* code */
}
Rectangle.x += square_size;
}
Rectangle.y += square_size;
}
SDL_SetRenderTarget(p_renderer, NULL);
SDL_RenderCopy(p_renderer, Board_Texture, NULL, NULL);
}
/**
* @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
*/
static void ui_draw_board(SDL_Renderer *p_renderer, uint8_t board_lights[12][8], uint8_t board_state[12][8])
{
SDL_SetRenderTarget(p_renderer, Board_Texture);
SDL_SetRenderDrawColor(p_renderer, 0x7f, 0x7f, 0x7f, 0);
SDL_RenderClear(p_renderer);
SDL_RenderDrawRect(p_renderer, &Rectangle);
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0xFF, 0xFF, 0x00);
Rectangle.w = Board_Width;
Rectangle.h = Board_Width;
Rectangle.x = (Width - Board_Width) / 2;
Rectangle.y = (Height - Board_Width) / 2;
SDL_RenderFillRect(p_renderer, &Rectangle);
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
const int square_size = Board_Width / 8;
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 ((board_lights[j][i] == POTENTIAL_MOVE) || (board_lights[j][i] == POTENTIAL_CASTLE))
{
SDL_SetRenderDrawColor(p_renderer, 0x00, 0xFF, 0x00, 0x00);
SDL_RenderFillRect(p_renderer, &Rectangle);
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
}
else if ((board_lights[j][i] == POTENTIAL_TAKE) || (board_lights[j][i] == PIECE_NEEDS_TO_BE_HERE) || (board_lights[j][i] == PIECE_NEEDS_TO_BE_REMOVED))
{
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0x00, 0x00, 0x00);
SDL_RenderFillRect(p_renderer, &Rectangle);
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
}
else if (board_lights[j][i] == PIECE_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);
}
else if (board_lights[j][i] == CONVERTING_PAWN)
{
SDL_SetRenderDrawColor(p_renderer, 0xFF, 0x3B, 0x7A, 0x57);
SDL_RenderFillRect(p_renderer, &Rectangle);
SDL_SetRenderDrawColor(p_renderer, 0x85, 0x5E, 0x42, 0x00);
}
else if (((i % 2) + (j % 2)) == 1)
{
SDL_RenderFillRect(p_renderer, &Rectangle);
}
else
{
/* code */
}
if((board_state[j][i] & 0x0Fu) != SQUARE_EMPTY)
{
SDL_RenderCopy(p_renderer, bitmapTextures[(board_state[j][i] & 0x0Fu)], NULL, &Rectangle);
}
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] == PIECE_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;
}
}
SDL_SetRenderTarget(p_renderer, NULL);
SDL_RenderCopy(p_renderer, Board_Texture, NULL, NULL);
}
void ui_redraw_board(SDL_Renderer *p_renderer)
{
uint8_t board_lights[12][8];
uint8_t board_state[12][8];
uint8_t game_state;
Board_get_game_state(&game_state);
Board_get_lights_and_state(board_lights, board_state);
if(game_state < GAME_STATE_OVER)
{
ui_draw_board(p_renderer, board_lights, board_state);
}
else
{
ui_draw_end_game(p_renderer, board_state, game_state);
}
}
/**
* @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 ui_resize(SDL_Renderer *p_renderer, int w, int h)
{
Width = w;
Height = h;
SDL_DestroyTexture(Board_Texture);
Board_Texture = SDL_CreateTexture(p_renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
Board_Width = ((w > h) ? h : w) - MARGIN;
// get rid of rounding errors
Board_Width -= Board_Width % 8;
}
/**
* @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 ui_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(Current_Binary_Board);
goto draw_square;
}
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(Current_Binary_Board);
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;
}
}
draw_square:
SDL_SetRenderTarget(p_renderer, Board_Texture);
ui_redraw_board(p_renderer);//, Board_Lights, Board_State);
SDL_RenderCopy(p_renderer, Board_Texture, NULL, NULL);
SDL_RenderPresent(p_renderer);
}
}
void ui_init(SDL_Renderer *p_renderer)
{
Current_Binary_Board[0] = 0xFF;
Current_Binary_Board[1] = 0xFF;
Current_Binary_Board[6] = 0xFF;
Current_Binary_Board[7] = 0xFF;
for (uint8_t i = 0; i < 12; i++)
{
//location of all the sprites plus the size of file names
char file[25] = "sprites/";
memcpy(&file[8], File_Names[i], 16);
bitmapSurface = SDL_LoadBMP(file);
bitmapTextures[i] = SDL_CreateTextureFromSurface(p_renderer, bitmapSurface);
}
SDL_FreeSurface(bitmapSurface);
}

View File

@ -0,0 +1,14 @@
/**
* The file is used to to abstract away anything realated to the user interface.
* The intent is that the only thing that would need to be re-written between
* the PC test software and the actual chess board is this module.
*/
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
void ui_resize(SDL_Renderer *p_renderer, int w, int h);
void ui_redraw_board(SDL_Renderer *p_renderer);
void ui_click(SDL_Renderer *p_renderer, int x, int y);
void ui_init(SDL_Renderer *p_renderer);