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

Author Topic: Sprite fail to load when creating multiple objects  (Read 1733 times)

0 Members and 1 Guest are viewing this topic.

ToanBao122

  • Newbie
  • *
  • Posts: 8
    • View Profile
Sprite fail to load when creating multiple objects
« on: November 06, 2020, 02:09:44 am »
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;
« Last Edit: November 07, 2020, 08:52:25 pm by ToanBao122 »

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Sprite fail to load when creating multiple objects
« Reply #1 on: November 06, 2020, 03:38:12 pm »
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?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

ToanBao122

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Sprite fail to load when creating multiple objects
« Reply #2 on: November 06, 2020, 11:22:48 pm »
Well, as I said, the first intance of the class, the image is loaded properly, however for the subsequent instances, only the rectangle shape is drawn and not the sprite. Specifically, in the main.cpp file, "Box test(1,1)" is drawn properly with both the rectangleshape and the sprite, but for the "Box test1(2,2)" only the rectangle shape is drawn and not the sprite.

ToanBao122

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Sprite fail to load when creating multiple objects
« Reply #3 on: November 07, 2020, 01:39:06 am »
To be more precise, Box test(1,1) (regardless whether which instances are declared first) is the only instance where the sprite is drawn, all the other instances of box just draw the rectangle shape and not the sprite.

ToanBao122

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Sprite fail to load when creating multiple objects
« Reply #4 on: November 07, 2020, 08:51:39 pm »
Anyway, I've fixed it by changing the setPosition in Box::setPosition to left and top instead of width and height.

 

anything