#include "Field.h"
Field::Field(const std::string& l_title, int size) : m_windows(l_title, sf::Vector2u(size, size)),
m_squareDim(size / 8), m_chessGrid(m_squareDim), m_wTeam(TeamColor::WHITE, m_chessGrid),
m_bTeam(TeamColor::BLACK, m_chessGrid), m_knight(PieceType::W_KNIGHT, sf::Vector2f(105,105))
{
reset();
}
void Field::reset()
{
drawTeamStart();
}
void Field::render()
{
m_windows.BeginDraw();
constructChessBoard();
m_windows.Draw(m_knight.getPieceSprite());
drawTeamStart();
m_windows.EndDraw();
}
void Field::drawTeamStart() //draws both the black and white team in their starting positions
{
for (int i = 0; i < 16; i++) {
m_windows.Draw(m_wTeam.m_chessPieces[i].getPieceSprite());
m_windows.Draw(m_bTeam.m_chessPieces[i].getPieceSprite());
}
}
struct Grid {
sf::Vector2f m_gridMap[8][8];
Grid(float squareDim) {
sf::Vector2f coord;
coord.x += squareDim/2;
coord.y += squareDim/2;
for (int i = 0; i < 8; i++) {
coord.y += squareDim;
for (int j = 0; j < 8; j++) {
coord.x += squareDim;
m_gridMap[i][j] = coord;
}
}
}
};
#pragma once
#include "SFML\Graphics.hpp"
#include "Grid.h"
enum PieceType {
W_BISHOP,
W_PAWN,
W_KING,
W_QUEEN,
W_ROOK,
W_KNIGHT,
B_BISHOP,
B_PAWN,
B_KING,
B_QUEEN,
B_ROOK,
B_KNIGHT
};
class GamePieces
{
public:
GamePieces(){}
GamePieces(const PieceType& type, const sf::Vector2f& position) : m_type(type),m_position(position)
{
switch (type) {
case W_BISHOP:
m_gamePiece.loadFromFile("w_bishop.png");
break;
//other cases are practically the same
}
m_size = m_gamePiece.getSize();
m_gamePieceSprite.setTexture(m_gamePiece);
m_gamePieceSprite.setOrigin(sf::Vector2f(m_size.x/2, m_size.y/2));
m_gamePieceSprite.setPosition(position);
}
sf::Sprite getPieceSprite() const {
return m_gamePieceSprite;
}
private:
sf::Texture m_gamePiece;
sf::Sprite m_gamePieceSprite;
sf::Vector2f m_position;
PieceType m_type;
sf::Vector2u m_size;
};
#pragma once
#include "SFML\Graphics.hpp"
#include "GamePieces.h"
enum TeamColor {
BLACK,
WHITE
};
struct Team {
//16 pieces in regular chess
//map that assigns specific game pieces coordinates
GamePieces m_chessPieces[16];
TeamColor color;
Team(const TeamColor& color, const Grid& grid) { //reference to constant grid variable
sf::Vector2f coord;
switch (color) {
case WHITE:
{
m_chessPieces[0] = GamePieces(PieceType::W_ROOK, grid.m_gridMap[0][0]);
m_chessPieces[1] = GamePieces(PieceType::W_KNIGHT, grid.m_gridMap[0][1]);
m_chessPieces[2] = GamePieces(PieceType::W_BISHOP, grid.m_gridMap[0][2]);
m_chessPieces[3] = GamePieces(PieceType::W_KING, grid.m_gridMap[0][3]);
m_chessPieces[4] = GamePieces(PieceType::W_QUEEN, grid.m_gridMap[0][4]);
m_chessPieces[5] = GamePieces(PieceType::W_BISHOP, grid.m_gridMap[0][5]);
m_chessPieces[6] = GamePieces(PieceType::W_KNIGHT, grid.m_gridMap[0][6]);
m_chessPieces[7] = GamePieces(PieceType::W_ROOK, grid.m_gridMap[0][7]);
int counter = 8;
for (int j = 0; j < 8; j++) {
//arranging pawns left to right on
m_chessPieces[counter] = GamePieces(PieceType::W_PAWN, grid.m_gridMap[1][j]);
counter++;
}
}
break;
default:
{
for (int j = 0; j < 8; j++) {
//arranging pawns left to right on
m_chessPieces[j] = GamePieces(PieceType::B_PAWN, grid.m_gridMap[7][j]);
}
m_chessPieces[8] = GamePieces(PieceType::B_ROOK, grid.m_gridMap[8][0]);
m_chessPieces[9] = GamePieces(PieceType::B_KNIGHT, grid.m_gridMap[8][1]);
m_chessPieces[10] = GamePieces(PieceType::B_BISHOP, grid.m_gridMap[8][2]);
m_chessPieces[11] = GamePieces(PieceType::B_KING, grid.m_gridMap[8][3]);
m_chessPieces[12] = GamePieces(PieceType::B_QUEEN, grid.m_gridMap[8][4]);
m_chessPieces[13] = GamePieces(PieceType::B_BISHOP, grid.m_gridMap[8][5]);
m_chessPieces[14] = GamePieces(PieceType::B_KNIGHT, grid.m_gridMap[8][6]);
m_chessPieces[15] = GamePieces(PieceType::B_ROOK, grid.m_gridMap[8][7]);
}
}
}
};
所以问题是它没有正确渲染,有时会出现一些白色方块代替精灵。我已经将一个GamePiece对象直接作为Field类的成员m_knight作为测试,并且渲染得很好,因此我的对象没有任何问题。