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

Author Topic: passing texture through function  (Read 1877 times)

0 Members and 1 Guest are viewing this topic.

IntoTheBush

  • Newbie
  • *
  • Posts: 2
    • View Profile
passing texture through function
« on: December 23, 2011, 01:10:31 am »
Alright, I switched from SDL to SFML a few days ago, compiled 2.0 libraries and am currently using that.

In my code I create a struct for a sprite and fill the information in with a given function.

Code: [Select]

uint cSpriteManager::CreateSprite(string File, float X, float Y, uint TotalFrames, uint TotalAnimations, uint FrameWidth, uint FrameHeight, Uint32 Delay, int Repeations)
{
     uint SpriteID = m_Sprites.size() + 1;
     sSprite NewSprite;

     NewSprite.nTexture = LoadTextureFile(File);
     NewSprite.Animation = CreateAnimation(TotalFrames, TotalAnimations, FrameWidth, FrameHeight, Delay, Repeations);
     NewSprite.X = X;
     NewSprite.Y = Y;
     m_Sprites[SpriteID] = NewSprite;

     return SpriteID;
}

Texture cSpriteManager::LoadTextureFile(string filename)
{
     Texture Temp;
     Temp.LoadFromFile(filename.c_str());

     if (!Temp.LoadFromFile(filename.c_str()))
     {
          cout << "cSpriteManager: Could Not Load File " << filename.c_str() << endl;
          exit(1);
     }

     Temp.SetSmooth(true);
     return Temp;
}


There is no problem with this, it returns the Temp texture and fills it into NewSprite.nTexture no problems, the problem is trying to access it. I define the container for all the sprites as:

Code: [Select]

private:
     map<uint, sSprite> m_Sprites;


 and I have a function that I call to return the texture to display, since it is private, which is:

Code: [Select]

Texture cSpriteManager::GetTexture(uint SpriteId)
{
     return m_Sprites[SpriteId].nTexture;
}


Once I have all my textures and information stored I run the following code:

Code: [Select]

Sprite.SetTexture(SpriteMan->GetTexture(SpriteId));


This just gives me a white shape where the sprite would display, but if I make the container public and call it with:
Code: [Select]

Sprite.SetTexture(SpriteMan->m_Sprites[SpriteId].nTexture);


It works just fine, I probably missed something very simple here, I was passing the Texture around when I first loaded it up, but it doesn't seem to be working now.  I just don't understand whats going on after tinkering for an hour or so. I would greatly appreciate any opinions on the matter.

Turbine

  • Full Member
  • ***
  • Posts: 102
    • View Profile
passing texture through function
« Reply #1 on: December 23, 2011, 04:03:02 am »
Use sf::Texture&

IntoTheBush

  • Newbie
  • *
  • Posts: 2
    • View Profile
passing texture through function
« Reply #2 on: December 23, 2011, 05:16:32 am »
Doh, thanks alot! You're my hero Turbine.  :D

Turbine

  • Full Member
  • ***
  • Posts: 102
    • View Profile
passing texture through function
« Reply #3 on: December 24, 2011, 09:35:14 am »
Quote from: "IntoTheBush"
Doh, thanks alot! You're my hero Turbine.  :D

You're mine too. <3 :o

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
passing texture through function
« Reply #4 on: December 24, 2011, 11:11:05 am »
Erhm this will result in problems in the end. The underlying problem is that you are working with temporary variables of the texture. So they are only reserved in memory till the end of the local scope. After that, no one knows when the memory will be overwritten. The reference "fix" is only temporary and will stop working eventually.

Though how does the sSprite look? Does it take it's own copy? Because then as it is now you have two copy of a heavy resource.
(If sSprite has it's own copy then there's no worries and the reference fix actually does work. But you still should try and remove those copy of texture) :wink:
Developer and Maker of rbSFML and Programmer at Paradox Development Studio