Chess
graphics.h
Go to the documentation of this file.
1 
7 #pragma once
8 #include <SDL2/SDL.h>
9 
10 #include <memory>
11 #include <optional>
12 #include <string>
13 #include <vector>
14 class Piece;
15 class Chessboard;
16 
18 class Graphics {
19  public:
20  Graphics(const std::string &title);
21  ~Graphics();
22 
23  void clear();
24  void update();
25 
26  void draw_background();
27  void draw_board();
28  void draw_pieces(Chessboard &chessboard);
29  void highlight_tiles(const Chessboard &chessboard);
30 
31  bool check_pixel_bounds(int x, int y) const;
33  int pixel_to_board(const int &x, const int &y) const;
35  std::pair<int, int> board_to_pixel(const int &i) const;
36 
37  int selected_tile = -1;
38  int king_in_check = -1;
39  std::vector<int> previous_move;
40  std::vector<int> possible_moves;
41 
42  bool show_possible_moves = false;
43  const int grid_size = 8; // 8x8
44  // variables below are in pixels
45  const int screen_width = 1600;
46  const int screen_height = 900;
47  const int board_width = 728;
48  const int upper_bound = 86;
49  const int left_bound = 436;
50  const int bottom_bound = 814;
51  const int right_bound = 1164;
52  const int tile_size = board_width / grid_size;
53 
54  private:
55  void load_sprites();
56  void initialize_graphics(const std::string title);
57  void destroy_textures();
58 
59  SDL_Window *window;
60  SDL_Renderer *renderer;
61 
62  SDL_Texture *loadTexture(SDL_Renderer *renderer, const std::string &path);
63  SDL_Texture *find_texture(std::optional<Piece> piece);
64 
66  std::vector<SDL_Texture *> piece_textures;
67  SDL_Texture *dark_square_texture;
68  SDL_Texture *light_square_texture;
69 
71  void draw_sprite(SDL_Texture *spriteTexture, SDL_Rect rectPos);
73  void highlight_previous_move(const Chessboard &chessboard);
75  void highlight_selected_tile(const Chessboard &chessboard);
77  void highlight_possible_moves(const Chessboard &chessboard);
79  void highlight_king_in_check(const Chessboard &chessboard);
80 
81  Graphics(const Graphics &other) = delete; // copy ctor
82  Graphics &operator=(const Graphics &rhs) = delete; // copy assignment operator
83  Graphics(Graphics &&other) = delete; // move ctor
84  Graphics &operator=(Graphics &&rhs) = delete; // move assignment
85 };
Class structure for the pieces that fill a game state&#39;s board.
Definition: piece.h:31
Used to create, store, and make changes to a game state.
Definition: chessboard.h:24
Handles all SDL2 functionalities require to visualize the game.
Definition: graphics.h:18
std::pair< int, int > board_to_pixel(const int &i) const
Conversion used to go from board indices to screen pixel coordinates.
Definition: graphics.cpp:215
int pixel_to_board(const int &x, const int &y) const
Conversion used to go from screen pixel coordinates to board indices.
Definition: graphics.cpp:206