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

Author Topic: White box glitch when creating multiple objects in a loop  (Read 2750 times)

0 Members and 1 Guest are viewing this topic.

Merle

  • Newbie
  • *
  • Posts: 17
    • View Profile
White box glitch when creating multiple objects in a loop
« on: October 10, 2016, 11:12:29 pm »
I know about the white box drawing error when you load things wrong, and I fixed that in previous programs. But I can't figure out how to prevent this from happening when I create multiple objects in a loop and add them to a list. Here is my code:

Platform.h

#ifndef PLATFORM_H
#define PLATFORM_H
#include <SFML/Graphics.hpp>
#include <iostream>

using namespace sf;
using namespace std;

class Platform {
    private:
        string textStr;
        Sprite sprite;
        Texture image;
        Texture t;
    public:
        Platform(string, int, int, int, int);
        Sprite getSprite();
        Texture getTexture();
        string getTextureStr();
};

#endif // PLATFORM_H
 

Platform.cpp

#include "Platform.h"
#include <SFML/Graphics.hpp>

using namespace sf;

//  Default constructor
Platform::Platform(string ima, int x, int y, int w, int h) {
    //  Setting a texture to the path input
    image.loadFromFile(ima);
    //  Setting it so it keeps drawing the texture to the sprite
    image.setRepeated(true);
    //  Setting the private path to the path input
    textStr = ima;
    //  Setting the sprite to the texture, creating the rect and setting the position
    sprite.setTexture(image);
    sprite.setTextureRect(IntRect(x, y, w * 40, h * 40));
    sprite.setPosition(x, y);
}

//  Sprite returning function
Sprite Platform::getSprite() {
    return sprite;
}

//  Texture returning function
Texture Platform::getTexture() {
    return image;
}

string Platform::getTextureStr() {
    return textStr;
}
 

relevant parts of main.cpp

//  Creating a vector that contains all the platforms
vector<Platform> platforms;

//  An array of all the platforms in level 1 - x, y, width and height (in pixels)
int level1[] = {100, 600, 8, 1};

//  Looping through all the ints in the level list, and creating platforms
for (unsigned i = 0; i < (sizeof(level1)/4) - 1; i += 4) {
    platforms.push_back(Platform("brick.png", level1[i], level1[i + 1], level1[i + 2], level1[i + 3]));
}

 

DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: White box glitch when creating multiple objects in a loop
« Reply #1 on: October 11, 2016, 12:24:05 am »
I know about the white box drawing error when you load things wrong, and I fixed that in previous programs. But I can't figure out how to prevent this from happening when I create multiple objects in a loop and add them to a list. Here is my code:

relevant parts of main.cpp

//  Creating a vector that contains all the platforms
vector<Platform> platforms;

//  An array of all the platforms in level 1 - x, y, width and height (in pixels)
int level1[] = {100, 600, 8, 1};

//  Looping through all the ints in the level list, and creating platforms
for (unsigned i = 0; i < (sizeof(level1)/4) - 1; i += 4) {
    platforms.push_back(Platform("brick.png", level1[i], level1[i + 1], level1[i + 2], level1[i + 3]));
}


 

Quoted from sf::Sprite::setTexture

Quote
The texture argument refers to a texture that must exist as long as the sprite uses it. Indeed, the sprite doesn't store its own copy of the texture, but rather keeps a pointer to the one that you passed to this function. If the source texture is destroyed and the sprite tries to use it, the behavior is undefined.

When you store elements on an std::vector<T> and the .capacity() is exceeded is likely the vector will reallocate the memory (invalidating pointers)

So you have severals options:

For textures you can pack then in a std::list (check the image), or use a vector of pointers (std::vector<T*>)

If want to know if a STL containers invalidate references/pointers are invalidate you can check it on http://en.cppreference.com

int level1[] = {100, 600, 8, 1};
 

If you can use c++11 better use std::array and its .size() method.  ;D
I would like a spanish/latin community...
Problems building for Android? Look here

BlueCobold

  • Full Member
  • ***
  • Posts: 105
    • View Profile
Re: White box glitch when creating multiple objects in a loop
« Reply #2 on: October 11, 2016, 06:43:51 am »
Don't store or return sf::Texture by value. This will cause them to be copied whenever you copy the parent object or return them from your function. A copy will cause the original sprite to reference an invalid texture. You can be glad this only leads to white boxes instead of crashing your application entirely.