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

Author Topic: Texture lifetime.  (Read 1276 times)

0 Members and 1 Guest are viewing this topic.

Fortas

  • Newbie
  • *
  • Posts: 1
    • View Profile
Texture lifetime.
« on: December 12, 2013, 08:05:41 pm »
Hello.
I use 4 classes in my project atm:
-ScreenMenager: use to menage screens;
-VirtualClass: using virtual methods;
-GameScreen: main screen which I want to use for interactions between other classes;
-PlayerClass;

ScreenMenager has a pointer to VirtualClass. I use it to display GameScreens methods by virtual ones. PlayerClass has private Sprite and Texture which I want to display. Its going like:

GameScreen class has no Texture and no Sprite.

Quote
void GameScreen::Draw(sf::RenderWindow &window)
{
       players.Draw(window); // <-- PlayerClass object;
}

void PlayerClass::Draw(sf::RenderWindow &window)
{
    window.draw(playerSprite);
}
I loaded texture in PlayerClass by using constructor.
My question is how can I transfer the texture to PlayerClass or how can I display it from PlayerClass level? Is it even possible or I have to find other way?
« Last Edit: December 12, 2013, 08:09:45 pm by Fortas »

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Texture lifetime.
« Reply #1 on: December 12, 2013, 08:38:42 pm »
I don't quite understand your question...

Just load the texture once into your program (if your game is something bigger I'd make a class for managing them) and simply set the texture to the sprite, which is a member of your PlayerClass (probally in the constructor, or a setTexture function). No need to transfer anything or store the texture itself in playerClass.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: Texture lifetime.
« Reply #2 on: December 12, 2013, 10:04:49 pm »
You mighr want to look into resource managment. The SFML Game Dev book has a nice approach to this. If you can't buy it, I suggest you simply take a look at the source code, which is located on Laurent's GitHub account.

Optionally you could look at Thor's system, but that one might be too complex for your needs.

Generally you shouldn't store the texture within you entity/player.

For a simple way to handle screens you could look at my SmallGameEngine on my GitHub account.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/