1
General / textures not rendering once called through visitor (white square problem?)
« on: October 03, 2019, 05:21:15 pm »
Hello all, I have been working with sfml for about a month now and this is the first major issue I haven't been able to solve through the documentation or other forum posts.
In my project I currently use variants and visitors to tell objects to update and to draw their shapes, but now textures are not rendering for anything using this method. I am not sure where the problem originates so I will include blocks of code.
This is the class that creates and updates other objects using visitors.
This is a (cut down) class of a object that would be updated through the event class by a visitor.
So the texture is set in the setsprite() function as is other variables, and the object still retains the changes to the variables through the visitor functions, and it updates normally because the bullet objects still spawn in the normal place, if I add the settexture to the update function the texture actually appears, although without the correct origin. If anyone can figure out what my issue is I would be very grateful. I will include the entire (messy) code if anyone finds that useful.
Edit: Ok it seems this issue is also called the white square problem, however I still have no idea how to proceed with this.
In my project I currently use variants and visitors to tell objects to update and to draw their shapes, but now textures are not rendering for anything using this method. I am not sure where the problem originates so I will include blocks of code.
class Eventmanager{
public:
void loadup(){
Boss boss;
EnemyShip eship;
boss.setsprite();
eship.setsprite();
Event ll;
Event kk;
ll.myVari = boss;
kk.myVari = eship;
ll.create(500.f,150.f);
kk.create(200.f,300.f);
//ll.myAny = boss;
ll.doevent();
kk.doevent();
Eventlist.push_back(ll);
Eventlist.push_back(kk);
}
void update(){
for (std::list<Event>::iterator rt = Eventlist.begin() ; rt != Eventlist.end(); ++rt){
rt->update();
}
}
};
public:
void loadup(){
Boss boss;
EnemyShip eship;
boss.setsprite();
eship.setsprite();
Event ll;
Event kk;
ll.myVari = boss;
kk.myVari = eship;
ll.create(500.f,150.f);
kk.create(200.f,300.f);
//ll.myAny = boss;
ll.doevent();
kk.doevent();
Eventlist.push_back(ll);
Eventlist.push_back(kk);
}
void update(){
for (std::list<Event>::iterator rt = Eventlist.begin() ; rt != Eventlist.end(); ++rt){
rt->update();
}
}
};
This is the class that creates and updates other objects using visitors.
class Event{
public:
//std::variant<Boss,EnemyShip> myVariant;
//Boss rat;
std::variant<Boss, EnemyShip> myVari;
void create(float c, float d){
setvars sv;
sv.a = c;
sv.b = d;
std::visit(sv, myVari);
}
void doevent(){
std::visit(Viss{}, myVari);
}
void update(){
std::visit(MyVisitor{}, myVari);
}
};
This is the class that functions as a kind of generic shell for other classes so that they can be manipulatedpublic:
//std::variant<Boss,EnemyShip> myVariant;
//Boss rat;
std::variant<Boss, EnemyShip> myVari;
void create(float c, float d){
setvars sv;
sv.a = c;
sv.b = d;
std::visit(sv, myVari);
}
void doevent(){
std::visit(Viss{}, myVari);
}
void update(){
std::visit(MyVisitor{}, myVari);
}
};
struct MyVisitor
{
void operator()(Boss& _in){_in.update();}
void operator()(EnemyShip& _in){_in.update();}
};
struct Viss
{
void operator()(Boss& _in){_in.x = 500;}
void operator()(EnemyShip& _in){_in.update();}
};
struct setvars
{
float a;
float b;
void operator()(Boss& _in){_in.setsprite(); _in.x = a; _in.y = b;}
void operator()(EnemyShip& _in){_in.setsprite(); _in.x = a; _in.y = b;}
};
This are the visitors that are used on the variants.{
void operator()(Boss& _in){_in.update();}
void operator()(EnemyShip& _in){_in.update();}
};
struct Viss
{
void operator()(Boss& _in){_in.x = 500;}
void operator()(EnemyShip& _in){_in.update();}
};
struct setvars
{
float a;
float b;
void operator()(Boss& _in){_in.setsprite(); _in.x = a; _in.y = b;}
void operator()(EnemyShip& _in){_in.setsprite(); _in.x = a; _in.y = b;}
};
class Boss{
public:
sf::Texture texture;
sf::Sprite sprite;
float x;
float y;
void setsprite(){
if (!texture.loadFromFile("/home/cryptocrayon/Downloads/C++ Projects/BossHead.png"))
{
std::cerr << "Error while loading texture" << std::endl;
}
texture.setSmooth(true);
sprite.setTexture(texture);
sf::FloatRect spriteSize=sprite.getGlobalBounds();
sprite.setOrigin(spriteSize.width/2.,spriteSize.height/2.);
x=500;
y=150;
sprite.setPosition(x,y);
}
void shoot(int which){
Bullet B1;
B1.setsprite();
currentangle+=((6.29/bnum)+0.05)*mode;
B1.setvars(x,y,currentangle,3.f);
Bulletlist.push_back(B1);
}
void update(){
sprite.setPosition(x,y);
if (timer>=3){
for (int i=0; i<bnum; ++i) {
shoot(1);
}
timer=0;
}else{
timer++;
}
window.draw(sprite);
}
};
public:
sf::Texture texture;
sf::Sprite sprite;
float x;
float y;
void setsprite(){
if (!texture.loadFromFile("/home/cryptocrayon/Downloads/C++ Projects/BossHead.png"))
{
std::cerr << "Error while loading texture" << std::endl;
}
texture.setSmooth(true);
sprite.setTexture(texture);
sf::FloatRect spriteSize=sprite.getGlobalBounds();
sprite.setOrigin(spriteSize.width/2.,spriteSize.height/2.);
x=500;
y=150;
sprite.setPosition(x,y);
}
void shoot(int which){
Bullet B1;
B1.setsprite();
currentangle+=((6.29/bnum)+0.05)*mode;
B1.setvars(x,y,currentangle,3.f);
Bulletlist.push_back(B1);
}
void update(){
sprite.setPosition(x,y);
if (timer>=3){
for (int i=0; i<bnum; ++i) {
shoot(1);
}
timer=0;
}else{
timer++;
}
window.draw(sprite);
}
};
This is a (cut down) class of a object that would be updated through the event class by a visitor.
So the texture is set in the setsprite() function as is other variables, and the object still retains the changes to the variables through the visitor functions, and it updates normally because the bullet objects still spawn in the normal place, if I add the settexture to the update function the texture actually appears, although without the correct origin. If anyone can figure out what my issue is I would be very grateful. I will include the entire (messy) code if anyone finds that useful.
Edit: Ok it seems this issue is also called the white square problem, however I still have no idea how to proceed with this.