16
Graphics / 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
Platform.cpp
relevant parts of main.cpp
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
#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;
}
#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]));
}
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]));
}