1

I have a font class that will load glyphs from a .ttf to openGL textures using sdl. The class consists of individual textures for each glyph and will obviously render them in succession to create displayed text. I figure it's costly to keep loading the .ttf to an sdl surface and then, in turn, using the surface pixel data to generate an openGL texture. So, I have for my program, a KText class that has a member: static std::vector<Font*> OpenedFonts;

When a new item that has inherited KText attempts to open a font, I scan the vector for the opened glyphs and then just return a pointer to the opened glyphs and just use the already made textures for all instances of that text (size/name/color).

The code is

bool KText::LoadFont() {
    _Font = CheckOpenedFonts(); //_Font is KFont* pointer
    if(_Font == NULL) {
        _Font = new KFont;
        _Font->LoadFont();
    }
    if(_Font == NULL) return false;
    return true;
}

Clearly, not every time will I open a new font, so in my destructor I wouldn't want to simply use delete _Font. For namely these two reasons: there is another KText object pointer to that same set of glyphs and it would be good to keep them in memory until the program terminates in case another object is created and attempts to use that font.

Is there a way to wait until all the instances of KFont have left scope?

Thank you!

The solution was:

class KText {
private:
    static int TextCnt;
public:
    KText();
    ~KText();
};

//Implementation
static int KText::TextCnt = 0;
KText::KText() {
   TextCnt++;
}

KText::~KText() {
   TextCnt--;
   if(TextCnt < 1) {
       OpenedFonts* t = FntPnter; //FntPnter is a head pointer to the linked list of fonts
       while(t != NULL ) {
           FntPnter = t->Next;
           delete t;
           t = FntPnter;
        }
    }
}
Chemistpp
  • 2,006
  • 2
  • 28
  • 48
  • 2
    All memory is freed when the program terminates. – Barmar Jul 09 '13 at 05:11
  • isn't it bad coding standard to not free it though? – Chemistpp Jul 09 '13 at 05:11
  • You asked if there's a way to wait until the program terminates, not if it's a good idea to do it. :) – Barmar Jul 09 '13 at 05:12
  • 1
    Technically, it is not required to be freed by the OS on termination. In other news, you're using a [reserved identifier](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier). – chris Jul 09 '13 at 05:13
  • 2
    Maybe a shared_ptr will be helpful? – Barmar Jul 09 '13 at 05:13
  • fixed my question ;) Oh, I am eh? Crap. Let me find which one. I don't normally use underscores either. just starting doing it because I'm a fan of accessors/setters and private/protected variables. – Chemistpp Jul 09 '13 at 05:15
  • I think you're right Barmar. Thank you. – Chemistpp Jul 09 '13 at 05:16
  • Breaking my habit of using _ now. – Chemistpp Jul 09 '13 at 05:20
  • static std::vector> OpenedFonts; So if I used that for a static vector then a shared_ptr would persist so long as there is at least one class object in existence and I wouldn't have to reload fonts time and time again. When the last one goes out of existence, from my understanding, when the vector gets destroyed so do the objects of the pointers I think this is perfect. I would have given you an answer @Barmar – Chemistpp Jul 09 '13 at 15:02

1 Answers1

1

You can create a static integer member in KFont Let's say static int KFontCnt;, which is the number of KFont objects. Then increase KFontCnt in the KFont constructor, decrease it in KFont destructor. When it reaches 0 in the destructor you can free all KFont instances.

Chemistpp
  • 2,006
  • 2
  • 28
  • 48
mika314
  • 163
  • 1
  • 6