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

Author Topic: Having the infamous white square problem  (Read 5138 times)

0 Members and 3 Guests are viewing this topic.

makerimages

  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email
Having the infamous white square problem
« on: November 15, 2013, 07:37:27 am »
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?
« Last Edit: November 15, 2013, 07:39:51 am by makerimages »
Makerimages-It`s in the pixel

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Having the infamous white square problem
« Reply #1 on: November 15, 2013, 07:46:15 am »
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.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

makerimages

  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email
Re: Having the infamous white square problem
« Reply #2 on: November 15, 2013, 12:53:06 pm »
But I cant seTexture when I have the getTexture return a pointer for an example.
Makerimages-It`s in the pixel

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Having the infamous white square problem
« Reply #3 on: November 15, 2013, 01:02:08 pm »
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. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

makerimages

  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email
Re: Having the infamous white square problem
« Reply #4 on: November 15, 2013, 01:12:24 pm »
Makerimages-It`s in the pixel

BaneTrapper

  • Full Member
  • ***
  • Posts: 213
  • Do you even see this, i dont need it.
    • View Profile
    • Email
BaneTrapperDev@hotmail.com Programing, Coding
Projects: Not in development(unfinished/playable):
http://en.sfml-dev.org/forums/index.php?topic=11073.msg76266#msg76266
UP and in Development: The Wanderer - Lost in time
http://en.sfml-dev.org/forums/index.php?topic=14563.0

makerimages

  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email
Re: Having the infamous white square problem
« Reply #6 on: November 15, 2013, 01:35:00 pm »
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() ???
« Last Edit: November 15, 2013, 01:42:02 pm by makerimages »
Makerimages-It`s in the pixel

makerimages

  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email
Re: Having the infamous white square problem
« Reply #7 on: November 15, 2013, 01:58:43 pm »
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;
}
 
Makerimages-It`s in the pixel

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Having the infamous white square problem
« Reply #8 on: November 15, 2013, 02:00:40 pm »
It's a whole concept behind it, you should really learn some more C++. Here's a reference for the 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

makerimages

  • Newbie
  • *
  • Posts: 24
  • Yay!
    • View Profile
    • Email
Re: Having the infamous white square problem
« Reply #9 on: November 15, 2013, 02:39:24 pm »
Got it working aftrall, simplert thanthat, no need for shared_ptrs
Makerimages-It`s in the pixel

 

anything