diff --git a/chess_board.cpp b/chess_board.cpp new file mode 100644 index 0000000..5463acf --- /dev/null +++ b/chess_board.cpp @@ -0,0 +1,113 @@ +#include "chess_board.h" +#include +#include +#include "stdio.h" + +#define MARGIN 200 + +typedef enum +{ + SQUARE_EMPTY = 0, + KING, + QUEEN, + ROOK, + KNIGHT, + BISHOP, + PAWN +}; + +static int height; +static int width; +static SDL_Texture *board_texture; +static SDL_Rect rectangle; +static int board_width; +uint8_t board_state[8][8] = 0u; + + +bool click(SDL_Renderer *p_renderer, int x, int y) +{ + SDL_Point const point = {x, y}; + bool ret_val = false; + 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; + 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)) + { + + goto draw_square; + } + rectangle.x += square_size; + } + rectangle.y += square_size; + } + draw_square: + 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); + SDL_RenderCopy(p_renderer, board_texture, NULL, NULL); + SDL_RenderPresent(p_renderer); + } + return ret_val; +} + +void chess_board_init(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; +} + +void draw_board(SDL_Renderer *p_renderer) +{ + 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, 0, 0, 0, 0x00); + 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; + if ((j % 2) == 0) + { + rectangle.x += square_size; + } + for (size_t i = 0; i < 4; i++) + { + SDL_RenderFillRect(p_renderer, &rectangle); + rectangle.x += 2 * square_size; + } + rectangle.y += square_size; + } + + SDL_SetRenderTarget(p_renderer, NULL); + SDL_RenderCopy(p_renderer, board_texture, NULL, NULL); +} diff --git a/chess_board.h b/chess_board.h new file mode 100644 index 0000000..e70eae9 --- /dev/null +++ b/chess_board.h @@ -0,0 +1,6 @@ +#include +#include + +bool click(SDL_Renderer *p_renderer, int x, int y); +void chess_board_init(SDL_Renderer *p_renderer, int w, int h); +void draw_board(SDL_Renderer * p_renderer); \ No newline at end of file diff --git a/main.cpp b/main.cpp index b233be5..9727e5e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,21 +1,15 @@ #define SDL_MAIN_HANDLED #include #include +#include #include #include #include #include #include +#include "chess_board.h" -void draw_line(SDL_Point source, SDL_Point destination, SDL_Renderer * rend) -{ - -} - -void draw_maze(void) -{ - -} +clock_t start_time, end_time; int main( int argc, const char* argv[] ) { @@ -54,6 +48,7 @@ int main( int argc, const char* argv[] ) srcR.w = 800; destR.h = 800; destR.w = 800; + chess_board_init(renderer, 800, 800); srcR.x = 0; srcR.y = 0; destR.x = 0; @@ -75,34 +70,13 @@ int main( int argc, const char* argv[] ) } else if (event.button.button == SDL_BUTTON_LEFT) // scroll up { - rectangle.x += 50; - rectangle.y += 0; - redraw = true; - } - } - else if (event.type == SDL_KEYDOWN) - { - if (event.key.keysym.sym == SDLK_UP) - { - rectangle.y -= 50; - } - else if (event.key.keysym.sym == SDLK_DOWN) - { - rectangle.y += 50; - } - else if (event.key.keysym.sym == SDLK_RIGHT) - { - rectangle.x += 50; - } - else if (event.key.keysym.sym == SDLK_LEFT) - { - rectangle.x -= 50; + redraw = click(renderer, event.button.x, event.button.y); } else { /* Do nothing. */ } - redraw = true; + } else if (event.type == SDL_QUIT) { @@ -124,12 +98,7 @@ int main( int argc, const char* argv[] ) case SDL_WINDOWEVENT_MAXIMIZED: redraw = true; SDL_GetWindowSize(win, &destR.w, &destR.h); - SDL_Texture *texture_temp = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, destR.w, destR.h); - srcR.h = destR.h; - srcR.w = destR.w; - SDL_RenderCopy(renderer, texture_temp, &srcR, &destR); - SDL_DestroyTexture(texture); - texture = texture_temp; + chess_board_init(renderer, destR.w, destR.h); break; } } @@ -140,15 +109,15 @@ int main( int argc, const char* argv[] ) } if (redraw) { - SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00); - SDL_RenderClear(renderer); - SDL_SetRenderTarget(renderer, texture); - SDL_RenderDrawRect(renderer, &rectangle); - SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0x00); - SDL_RenderFillRect(renderer, &rectangle); - SDL_SetRenderTarget(renderer, NULL); - SDL_RenderCopy(renderer, texture, &srcR, &destR); + 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. */ }