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

Author Topic: Trouble with sf::Text  (Read 4236 times)

0 Members and 1 Guest are viewing this topic.

Chuckleluck

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Trouble with sf::Text
« on: June 01, 2012, 06:39:07 pm »
Hello,
I'm having trouble with the sf::Text class.  Here is part of my code:

// gui.cpp

playerCardGUI::playerCardGUI()
{
    texture.loadFromFile("data\\GUI\\playercard.png");
    texture.setSmooth(false);
    sprite.setTexture(texture);
}

playerCardGUI::~playerCardGUI()
{
}

void playerCardGUI::create(unsigned short int argOwner, NATIONALITY natl, sf::Vector2f pos)
{
    texture.loadFromFile("data\\GUI\\playercard.png");
    texture.setSmooth(false);
    sprite.setTexture(texture);

    playerName.setFont(data.mainFont);
    health.setFont(data.mainFont);
    ammoMag.setFont(data.mainFont);
    ammoTotal.setFont(data.mainFont);

    playerName.setString("Name");

    owner = argOwner;

    if(natl == US)
        txNational.loadFromFile("data\\GUI\\ally_US.png");
    else if(natl == GR)
        txNational.loadFromFile("data\\GUI\\axis_GR.png");

    sprite.setPosition(pos);
    playerName.setPosition(pos);
}

void playerCardGUI::update(int HP, int ammo, int totalAmmo)
{
    std::stringstream stream;

    stream << HP;
    health.setString(stream.str());

    stream.clear();
    stream << ammo;
    ammoMag.setString(stream.str());

    stream.clear();
    stream << totalAmmo;
    ammoTotal.setString(stream.str());
}

void playerCardGUI::draw()
{
}
// gui.h
#ifndef GUI_H
#define GUI_H

class playerCardGUI
{
    private:
        // graphics variables
        sf::Sprite sprite;
        sf::Texture texture;
        sf::Sprite spClass, spNational;
        sf::Texture txClass, txNational;

        // text variables
        sf::Text playerName;
        sf::Text health;
        sf::Text ammoMag, ammoTotal;

        // misc variables
        unsigned short int owner;
    public:
        playerCardGUI();
        ~playerCardGUI();
        void create(unsigned short int argOwner, NATIONALITY natl, sf::Vector2f pos);

        void update(int HP, int ammo, int totalAmmo);
        void draw();

    friend class Player;
};

#endif // GUI_H

Specifically, the issue is with playerName.  When I try to compile this, it works fine-- until I close the main program window.  Then the process continues to run until I manually end it using the Windows Task Manager.

I don't see anything wrong with my code.  Does anyone else see anything wrong?
Thanks in advance.

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Trouble with sf::Text
« Reply #1 on: June 02, 2012, 08:37:09 am »
The famous AMD/ATI bug strikes again!  ;D

You should avoid the default sf::Text constructor.
The solution I found is creating a temporary sf::Text object inside my draw method:

sf::Text playerName (p1Name, data.mainFont, 30);
...
window.draw (playerName);
 
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Zefz

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Trouble with sf::Text
« Reply #2 on: June 02, 2012, 01:18:55 pm »
Isn't that a lot slower? Having to reallocate a Text object and recalculate the text geometry every frame?

Chuckleluck

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Re: Trouble with sf::Text
« Reply #3 on: June 02, 2012, 05:29:09 pm »
Thanks, mateandmetal.  ;D
I'll probably just add something like
playerName = new sf::Text(p1Name, data.mainFont, 30);
in the constructor.

Does anyone know if Laurent will fix this bug before the official SFML 2 release?  It seems like it will get pretty annoying.  :(

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Trouble with sf::Text
« Reply #4 on: June 02, 2012, 07:10:54 pm »
I'll probably just add something like
playerName = new sf::Text(p1Name, data.mainFont, 30);
in the constructor.
Please don't use new unless really necessary. Why can't the sf::Text be an automatic variable?

Does anyone know if Laurent will fix this bug before the official SFML 2 release?  It seems like it will get pretty annoying.  :(
The bug is quite complex to fix and potentially requires an internal redesign, so I doubt it will be fixed for version 2.0. The workaround is to link statically.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Chuckleluck

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Re: Trouble with sf::Text
« Reply #5 on: June 03, 2012, 01:45:13 am »
I'll probably just add something like
playerName = new sf::Text(p1Name, data.mainFont, 30);
in the constructor.
Please don't use new unless really necessary. Why can't the sf::Text be an automatic variable?
Without the new, the process continues to run after I close the program window.