Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Chess game sprites rendering correctly as an individual object but not in "Team"  (Read 1209 times)

0 Members and 1 Guest are viewing this topic.

GCGsauce

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
#pragma once
#include "Windows.h"
#include "Grid.h"
#include "Team.h"
#include "GamePieces.h"

class Field
{
public:
        Field(const std::string&, int);
        void reset();
        void update();
        Windows* getWindow();
        void render();
        void drawTeamStart();
        ~Field();
private:
        GamePieces m_knight;
        Windows m_windows;
        float m_squareDim;
        Grid m_chessGrid;
        void constructChessBoard();
        Team m_wTeam;
        Team m_bTeam;
};



#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)
{
        reset();
}

void Field::reset()
{      
        drawTeamStart();
}

void Field::update()
{
        m_windows.Update();
}

Windows* Field::getWindow()
{
        return &m_windows;
}

void Field::render()
{
        m_windows.BeginDraw();
        constructChessBoard();
        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());
        }
}

Field::~Field()
{
}

void Field::constructChessBoard()
{
        //construct chess grid

        sf::RectangleShape square(sf::Vector2f(m_squareDim,m_squareDim));
        sf::Vector2f position(0, 0);
        int counter = 0; //counter for whether the column is even or odd
        int counter1 = 0; //counter for whether we are on an even or odd row
        for (int row = 0; row < 8; row++) {
                for (int column = 0; column < 8; column++) {

                        if (counter1 % 2 == 0 && counter % 2 == 0 || counter1 % 2 != 0 && counter % 2 != 0) {
                                square.setFillColor(sf::Color::Red);
                        }
                        else {
                                square.setFillColor(sf::Color::White);
                        }

                        square.setPosition(position);
                        m_windows.Draw(square);

                        position.x += m_squareDim;
                        counter++;
                }
                position.y += m_squareDim;
                position.x = 0;
                counter = 0;
                counter1++;
        }
}



struct Grid {

        sf::Vector2f m_gridMap[8][8];
       
        Grid(){}

        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 { //place gamePieces into Grid not field, cus grid is already in field
        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");
                                if (!m_gamePiece.loadFromFile("w_bishop.png")) {
                                        std::cout << "proof" << std::endl;
                                }
                                break;
                        case W_PAWN:
                                m_gamePiece.loadFromFile("w_pawn.png");
                                break;
                        case W_KING:
                                m_gamePiece.loadFromFile("w_king.png");
                                break;
                        case W_QUEEN:
                                m_gamePiece.loadFromFile("w_queen.png");
                                break;
                        case W_ROOK:
                                m_gamePiece.loadFromFile("w_rook.png");
                                break;
                        case W_KNIGHT:
                                m_gamePiece.loadFromFile("w_knight.png");
                                break;
                        case B_BISHOP:
                                m_gamePiece.loadFromFile("b_bishop.png");
                                break;
                        case B_PAWN:
                                m_gamePiece.loadFromFile("b_pawn.png");
                                break;
                        case B_KING:
                                m_gamePiece.loadFromFile("b_king.png");
                                break;
                        case B_QUEEN:
                                m_gamePiece.loadFromFile("b_queen.png");
                                break;
                        case B_ROOK:
                                m_gamePiece.loadFromFile("b_rook.png");
                                break;
                        default:
                                m_gamePiece.loadFromFile("b_knight.png");
                }
                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);
        }

        ~GamePieces(){}

        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() {}

        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]);
                }
                }
        }
};


So  the game sprite loads correctly when i just place it into the header but when i try to draw it to the screen it doesnt work. I ommitted the window class i made cus its not as relevant, but let me know if you want me t post it.
« Last Edit: July 19, 2016, 09:12:56 am by Laurent »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Please provide a good problem description, point to where the problem actually is in your code, what you've already tried and make sure to reduce the code as much as possible.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
The instances of your GamePieces class (which should be named GamePiece btw) are copied when you do "m_chessPieces[...] = GamePieces(...)". And since copy is not handled properly in that class, your sprite -> texture links get broken and nothing is rendered.

There are many many many similar subjects on this forum, you can try to find them if this is not clear.
Laurent Gomila - SFML developer

 

anything