Hi guys, I've encountered a problem when trying to draw objects, and I think that it might have something to do with sf::Sprite or sf::Texture, I'm not sure. Basically, my object consist of a rectangle shape and a sprite, when I try to draw it for the first object everything is exactly how I pictured it. However, the problem arise when I try to different objects of the same class. Specifically, for the subsequent objects drawn, only the rectangle shape was showing and not the sprite. How can I fix this problem? Here's my code:
Box.h
#ifndef LINKEDLIST_TEMPLATE_BOX_H
#define LINKEDLIST_TEMPLATE_BOX_H
#include <SFML/Graphics.hpp>
class Box:public sf::Drawable, sf::Transformable {
protected:
int col;
int row;
sf::Color color;
sf::RectangleShape boxBackGround;
sf::Sprite queen;
sf::Texture texture;
public:
//sf::RectangleShape newBox(float x,float y);
void draw(sf::RenderTarget &window, sf::RenderStates state) const;
int getRow();
int getCol();
void setRow(int row);
void setCol(int col);
Box(int row, int col);
void setBox(int x, int y);
Box();
void setTexture();
void setQueen();
};
#endif //LINKEDLIST_TEMPLATE_BOX_H
Box.cpp
#include "Box.h"
//
// Created by baoto on 10/24/2020.
//
#include "Box.h"
int Box::getCol() {
return col;
}
int Box::getRow() {
return row;
}
void Box::setCol(int col) {
this->col = col;
}
void Box::setRow(int row) {
this->row = row;
}
Box::Box(){
//this->setBox();
row = 0;
col = 0;
}
Box::Box(int row, int col) {
this->row = row;
this->col = col;
this->setBox(this->row,this->col);
this->setTexture();
this->setQueen();
}
void Box::setBox(int x, int y) {
boxBackGround.setSize({200.f,200.f});
boxBackGround.setFillColor(sf::Color::White);
boxBackGround.setPosition(x*210,y*210);
}
/*sf::RectangleShape Box::newBox(float x, float y)
{
sf::RectangleShape s;
s.setSize({200.f,200.f});
s.setFillColor(sf::Color::White);
s.setPosition(x,y);
return s;
}*/
void Box::draw(sf::RenderTarget &window, sf::RenderStates state) const {
window.draw(boxBackGround);
window.draw(queen);
}
void Box::setTexture() {
this->texture.loadFromFile("queen.png");
}
void Box::setQueen() {
this->queen.setTexture(this->texture);
this->queen.setScale(0.08f,0.08f);
this->queen.setPosition(this->boxBackGround.getGlobalBounds().left+50,this->boxBackGround.getGlobalBounds().top+30);
}
main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "CardGrid.h"
#include "NQueens.h"
int main() {
Box test(1,1);
Box test1(2,2);
sf::RenderWindow window(sf::VideoMode(1980, 1080, 32), "Test");
while(window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear(sf::Color::Black);
window.draw(test);
window.draw(test1);
window.display();
}
return 0;
i didn't checked all possibilities, but first you should check if the texture is being loaded at all. something like:
if (this->texture.loadFromFile("queen.png") == false) std::cout << "texture not loaded";
another thing (probably unrelated to the problem) is that each instance of your class has its own texture, but all textures are the same. one of the points in using textures is that you can reuse it in more sprites. maybe you could make this texture static?