SFML white taxture while loading from array(works without the array)

Hello!

So I'm getting a stange error when I'm creating a array from something I call "Obstacle", the problem I'm having is when I'm trying to load the array(sprites) they are all turning white. Now to the strange everything works perfectly when I'm not having the array (only loading one object).

I think that I'm loading the array too many times so that everything get overwriten.

I'm not going to post the Object and the Obstacle code tho I known thats right beacuse it works when I'm creating only one obejct in gameHandler.

gameHandler.h(creating the objects here and calling to load the sprites)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#pragma once
#include "Player.h"
#include "Obstacle.h"
#include "Background.h"

class gameHandler :public sf::Drawable
{
private:
	static const int CAP = 3;
	Player player = ("img/playerSheet.png");
	Obstacle obstacles[CAP];
	//Obstacle obstacles = ("img/obstacle.png");
	Background bg = ("img/cloud.png");
	void draw(sf::RenderTarget &target, sf::RenderStates states)const;


gameHandler.cpp

calling for the constructor to load the image
1
2
3
4
5
6
7
gameHandler::gameHandler()
{
	for (int i = 0; i < this->CAP; i++)
	{
		this->obstacles[i] = Obstacle("img/obstacle.png");
	}
}


drawing the sprites
1
2
3
4
5
6
7
8
void gameHandler::draw(sf::RenderTarget &target, sf::RenderStates states)const
{
	target.draw(this->bg, states);
	target.draw(this->player, states);
	target.draw(this->obstacles[0], states);
	target.draw(this->obstacles[1], states);
	
}


update function
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void gameHandler::update()
{
	this->player.jump();

	for (int i = 0; i < this->CAP; i++)
	{
		this->collision(this->obstacles[i]);
	}
	int yPos = rand() % 690+200;
	int xPos = rand() % 200 + 1;;

	this->obstacles[0].moveObstacle(1000, -yPos);
	this->obstacles[1].moveObstacle(1000, 1050 - yPos);

}



game.cpp (main)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <sfml/Graphics.hpp>
#include "gameHandler.h"
#include <iostream>
using namespace std;

int main()
{

	gameHandler handler;
	sf::RenderWindow window(sf::VideoMode(1000, 900), "Flappy bird <<Fist14>> Edition");
	srand(int(time(0)));
	sf::Clock clock;
	window.setFramerateLimit(900);
	while (window.isOpen())
	{
		sf::Event event;
		while (window.pollEvent(event))
		{
			if (event.type == sf::Event::Closed)
				window.close();

		}
		sf::Time time = clock.getElapsedTime();
		//cout << 1.0f/time.asSeconds() << endl;
		clock.restart().asSeconds();
		
		window.clear();
		handler.update();
		window.draw(handler);
		window.display();

	}

	return 0;
}



Thanks Stolle
This is a common problem usually it means you have destroyed the texture.
Sprites keep a pointer to the texture. If the texture gets destroyed you get a white square.

http://www.sfml-dev.org/tutorials/2.1/graphics-sprite.php
at the bottom there is a section labeled "The White Square Problem"
Well then why does it work when I'm only using one object instead of the array? Because It works with only one object I can't really see the problem.
At a guess, your Obstacle class has inadequate copy constructor and copy assignment implementation. You've decided that code is irrelevant because it works with a single object, though these things never come into play with only one object.
Topic archived. No new replies allowed.