1
Graphics / Re: C++ white boxes displaying
« on: April 03, 2014, 08:00:04 pm »#include <iostream>
#include <vector>
#include <SFML/Graphics.hpp>
class SpriteDatabase
{
public:
SpriteDatabase();
~SpriteDatabase();
sf::Sprite & getSpriteId(unsigned int id);
private:
void loadSprite(sf::Sprite *sprite);
bool preloadDatabase();
std::vector<sf::Sprite*> spriteIndex;
sf::Texture texture;
};
sf::Sprite & SpriteDatabase::getSpriteId(unsigned int id)
{
std::cout << "Returning Sprite\n" << std::endl;
return (*spriteIndex[id]);
}
void SpriteDatabase::loadSprite(sf::Sprite *sprite) /* Push into vector container */
{
std::cout << "Loading Sprite\n" << std::endl;
spriteIndex.push_back(sprite);
}
bool SpriteDatabase::preloadDatabase()
{
sf::Sprite *sprite;
if(!spriteIndex.empty())
return false;
if(!texture.loadFromFile("gfxPack.png"))
return false;
sprite = new sf::Sprite(texture, sf::IntRect(0,0,89,90));
loadSprite(sprite);
sprite = new sf::Sprite(texture, sf::IntRect(90,0,89,90));
loadSprite(sprite);
sprite = new sf::Sprite(texture, sf::IntRect(180,0,89,90));
loadSprite(sprite);
sprite = new sf::Sprite(texture, sf::IntRect(270,0,89,90));
loadSprite(sprite);
sprite = new sf::Sprite(texture, sf::IntRect(360,0,89,90));
loadSprite(sprite);
return true;
}
SpriteDatabase::~SpriteDatabase()
{
for (int = 0; i < spriteIndex.size(); i++)
{
delete spriteIndex[i];
}
spriteIndex.clear();
}
it work?
ADD:
Oh, Fuck...
You use sprite.setTextureRect(sf::IntRect(360,0,449,90));
1 parametr start texture rect x
2 parametr start texture rect y
3 parametr width texture rect
4 parametr height texture rectsprite.setTextureRect(sf::IntRect(0,0,89,90)); /* Lime Grass */
loadSprite(sprite);
sprite.setTextureRect(sf::IntRect(90,0,89,90)); /* Rough Sand */
loadSprite(sprite);
sprite.setTextureRect(sf::IntRect(180,0,89,90)); /* Pure Sand */
loadSprite(sprite);
sprite.setTextureRect(sf::IntRect(270,0,89,90)); /* Shallow Water */
loadSprite(sprite);
sprite.setTextureRect(sf::IntRect(360,0,89,90)); /* Dirt */
loadSprite(sprite);
Thanks!
Works like a charm!
Regarding the setTextureRect, I am just used to SDL, and in SDL you explicitly give the x1,y1, x2,y2 coordinates of a rect rather than using a vector to later displace the sprite. Thats why I got it like that :p
I thought it actually returns a copy of a sprite rather than a reference. I figure i just didnt have the sprites properly allocated. Anyhow, a man learns his entire life, thanks
Quote from: Nexus
In general, you should always come up with a minimal and complete example that reproduces the problem. Read the link carefully, doing so will help you get better and faster answers.
By the way, your code is wrong (you don't adhere to the Rule Of Three) and unnecessarily error-prone (you don't use RAII).
No problem, I usually do that last, same with exception handling, I like to have everything working OK first before implementing memory management and operator overloading, same with exception handling (i usually just place a std::cout where an exception is most likely to appear at the end). I found this works perfect for me since I can just afterwards ran it over with a valgrind to patch everything up and optimize the code.
No worry I am not the type of a person which commits broken code to Git
Thanks for your feedback though, much appreciated