Adding chess board
This commit is contained in:
parent
979dff9c6c
commit
94f26a11bf
113
chess_board.cpp
Normal file
113
chess_board.cpp
Normal file
@ -0,0 +1,113 @@
|
||||
#include "chess_board.h"
|
||||
#include <SDL.h>
|
||||
#include <SDL_video.h>
|
||||
#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);
|
||||
}
|
6
chess_board.h
Normal file
6
chess_board.h
Normal file
@ -0,0 +1,6 @@
|
||||
#include <SDL.h>
|
||||
#include <SDL_video.h>
|
||||
|
||||
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);
|
61
main.cpp
61
main.cpp
@ -1,21 +1,15 @@
|
||||
#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 <time.h>
|
||||
#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. */
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user