-
HAving the white square problem, but can figure out where the issue exactly is.
TextureManager.h
#pragma once
#include <SFML\Graphics.hpp>
class TextureManager
{
public:
TextureManager(void);
~TextureManager(void);
sf::Texture getTexture(int id);
void InitTextures();
private:
sf::Texture textureArray[20];
};
TextureManager.cpp
#include "TextureManager.h"
using namespace std;
TextureManager::TextureManager(void)
{
}
TextureManager::~TextureManager(void)
{
}
void TextureManager::InitTextures()
{
printf("Starting texture loading process \n");
textureArray[0].loadFromFile("data/graphics/map/grassTile.jpg"); // 0
textureArray[1].loadFromFile("data/graphics/map/mountainTile.png");
printf("Texture Loading ended! \n");
}
sf::Texture TextureManager::getTexture(int id)
{
return textureArray[id];
}
SpriteManager.h
#pragma once
#include <SFML\Graphics.hpp>
#include "TextureManager.h"
class SpriteManager
{
public:
TextureManager texManager;
sf::Sprite getSprite(int id);
SpriteManager(TextureManager tm);
~SpriteManager(void);
private:
sf::Sprite spriteArray[20];
void initSprites();
};
SpriteManager.cpp
#include "SpriteManager.h"
using namespace std;
SpriteManager::SpriteManager(TextureManager tm):texManager(tm)
{
texManager.InitTextures();
initSprites();
}
SpriteManager::~SpriteManager(void)
{
}
void SpriteManager::initSprites()
{
printf("Started loading sprites! \n");
spriteArray[0].setTexture(texManager.getTexture(0)); //0
spriteArray[1].setTexture(texManager.getTexture(1));
printf("Sprite loading complete! \n");
}
sf::Sprite SpriteManager::getSprite(int id)
{
return spriteArray[id];
}
main.cpp
#include <SFML\Graphics.hpp>
#include <conio.h>
#include "SpriteManager.h"
int main()
{
printf("Starting game \n");
TextureManager texManager;
SpriteManager spriteManager(texManager);
sf::RenderWindow window(sf::VideoMode(800,600),"NeverLand");
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
if(event.type==sf::Event::Closed)
{
window.close();
}
}
window.clear();
window.draw(spriteManager.getSprite(0));
window.display();
}
return 0;
}
Can anyone figure out where exactly do I lose the texture and what to do to avoid it?
-
sf::Texture TextureManager::getTexture(int id)
You are returning a copy.
spriteArray[0].setTexture(texManager.getTexture(0));
The copy is a temporary and is destroyed once the call is complete.
-
But I cant seTexture when I have the getTexture return a pointer for an example.
-
Then you could use a shared_ptr or take a look at Thor's resource system, which has two ways of handling resource life times. ;)
-
Then you could use a shared_ptr
how?
-
how?
Google!
-
did so, found nothing I could easiliy implement...
EDIT: can I easliy convert a shared_ptr back tot he object it reffers to for sprite.setTexture() ???
-
Code update! Tried this for the shared_ptr, still getting a white square
TextureManager.h
#pragma once
#include <SFML\Graphics.hpp>
#include <memory>
class TextureManager
{
public:
TextureManager(void);
~TextureManager(void);
std::shared_ptr<sf::Texture> getTexture(int id);
void InitTextures();
private:
sf::Texture textureArray[20];
};
TextureManager.cpp
#include "TextureManager.h"
using namespace std;
TextureManager::TextureManager(void)
{
}
TextureManager::~TextureManager(void)
{
}
void TextureManager::InitTextures()
{
printf("Starting texture loading process \n");
textureArray[0].loadFromFile("data/graphics/map/grassTile.jpg"); // 0
textureArray[1].loadFromFile("data/graphics/map/mountainTile.png");
printf("Texture Loading ended! \n");
}
std::shared_ptr<sf::Texture> TextureManager::getTexture(int id)
{
return std::make_shared<sf::Texture>(textureArray[id]);
//return textureArray[id];
}
SpriteManager.h
#pragma once
#include <SFML\Graphics.hpp>
#include "TextureManager.h"
class SpriteManager
{
public:
TextureManager texManager;
std::shared_ptr<sf::Sprite> getSprite(int id);
SpriteManager(TextureManager tm);
~SpriteManager(void);
private:
sf::Sprite spriteArray[20];
void initSprites();
};
SpriteManager.cpp
#include "SpriteManager.h"
using namespace std;
SpriteManager::SpriteManager(TextureManager tm):texManager(tm)
{
texManager.InitTextures();
initSprites();
}
SpriteManager::~SpriteManager(void)
{
}
void SpriteManager::initSprites()
{
printf("Started loading sprites! \n");
spriteArray[0].setTexture(*texManager.getTexture(0)); //0
spriteArray[1].setTexture(*texManager.getTexture(1));
printf("Sprite loading complete! \n");
}
std::shared_ptr <sf::Sprite> SpriteManager::getSprite(int id)
{
return std::make_shared<sf::Sprite>(spriteArray[id]);
}
main.cpp
#include <SFML\Graphics.hpp>
#include <conio.h>
#include "SpriteManager.h"
int main()
{
printf("Starting game \n");
TextureManager texManager;
SpriteManager spriteManager(texManager);
sf::RenderWindow window(sf::VideoMode(800,600),"NeverLand");
while(window.isOpen())
{
sf::Event event;
while(window.pollEvent(event))
{
if(event.type==sf::Event::Closed)
{
window.close();
}
}
window.clear();
window.draw(*spriteManager.getSprite(0));
window.display();
}
return 0;
}
-
It's a whole concept behind it, you should really learn some more C++. Here's a reference for the shared_ptr (http://en.cppreference.com/w/cpp/memory/shared_ptr) - it's a smart pointer, so similar to a raw pointer but with RAII and shared owner ship. So if you return a shared_ptr but removed the shared_ptr from the manager, the other shared_ptr can still be used and the object gets only destroyed once the shared_ptr runs out of scope.
-
Got it working aftrall, simplert thanthat, no need for shared_ptrs