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

Author Topic: Tiles not rendering, when it is essentially a rectangle class with extra methods  (Read 1149 times)

0 Members and 1 Guest are viewing this topic.

GCGsauce

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
class Tile {

public:
        Tile();
        ~Tile();
        sf::RectangleShape getRect() const;
        sf::Vector2f getPos() const;

        void highlightTile();
        bool isTileHighlighted() const;
        void turnOffHighlight();

        void setPos(const sf::Vector2f&);
        void setColor(const sf::Color&);

private:
        sf::RectangleShape m_tile;
        bool m_isHighlighted;
};


#include "Tile.h"

Tile::Tile() : m_tile(sf::Vector2f(0, 0)), m_isHighlighted(false) {
}

void Tile::setPos(const sf::Vector2f& pos){
        m_tile.setPosition(pos);
}

sf::RectangleShape Tile::getRect() const {
        return m_tile;
}

sf::Vector2f Tile::getPos() const
{
        return m_tile.getPosition();
}

bool Tile::isTileHighlighted() const {
        return (m_tile.getOutlineColor() == sf::Color::Yellow);
}

void Tile::turnOffHighlight(){
        m_tile.setOutlineThickness(0);
}

void Tile::setColor(const sf::Color& col){
        m_tile.setFillColor(col);
}

void Tile::highlightTile() {
        m_tile.setOutlineThickness(5);
        m_tile.setOutlineColor(sf::Color::Yellow);
}

Tile::~Tile(){
}


Grid::Grid(float squareDim)
{
        Tile tilePiece;
        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) {
                                tilePiece.setColor(sf::Color::Red);
                        }
                        else {
                                tilePiece.setColor(sf::Color::White);
                        }

                        tilePiece.setPos(position);
                        m_tileSet[row][column] = tilePiece; //correct coordinates
                        m_gridMap[row][column] = sf::Vector2f(tilePiece.getPos().x + squareDim / 2, tilePiece.getPos().y + squareDim / 2);
                        position.x += squareDim;
                        counter++;
                }
                position.y += squareDim;
                position.x = 0;
                counter = 0;
                counter1++;
        }
}



void Grid::drawBoard(Windows& wind)
{
        for (int i = 0; i < 8; i++) {
                for (int j = 0; j < 8; j++) {
                        wind.Draw(m_tileSet[i][j].getRect());
                }
        }
}

 
Then I put the grid in a Game class where i have a bunch of draw calls, and place my drawcall into that. I dont understand at all why my tiles won't render since before I basically had a rectangle instead in my Grid's constructor, but wanted the better design so opted for a tile class. really confused since this is essentially the same thing and its not rendering at all.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Where do you set the size of your sf::RectangleShapes?
Laurent Gomila - SFML developer

GCGsauce

  • Newbie
  • *
  • Posts: 6
    • View Profile
    • Email
good point. fixed, thanks. program still crashes after a while, but it did before.