SDL_ttf example

I would like help with my font text stuff. At the moment if a have a large number of objects, it seems to have an error with too many open files, or so.

    module jecsdl.text;

    import jecsdl.base;

    struct JText {
        string mText;
        SDL_Texture* mTex;
        private SDL_Surface* mSur;
        SDL_Rect mRect;
        Point mPos;
        Point mSize;
        SDL_Color mCol;
        TTF_Font* mFont;

        void pos(Point pos0) { mPos = pos0; mRect.x = pos0.Xi; mRect.y = pos0.Yi; }
        auto pos() { return mPos; }

        this(string message, SDL_Rect r, SDL_Color col, int fontSize, string fileName) {
            import std.string : toStringz;
            mFont = TTF_OpenFont(fileName.toStringz, fontSize);
            assert(mFont, "Font fail...");
            setString(message, col);
            mRect = SDL_Rect(r.x, r.y, mSur.w, mSur.h);
            mPos = Point(r.x, r.y);

        void setString(string message) {
            setString(message, mCol);

        void setString(string message, SDL_Color col) { //} = SDL_Color(255,180,0,0xFF)) {
            mCol = col;
            if (mTex || mSur)
                close(/* font too: */ false);
            mText = message;
            //assert(gmFont, "Text font no made (makeFont(name, size))");
            if (! message.length)
                mText = message = " ";            
            mSur = TTF_RenderText_Blended(mFont, message.toStringz, col);
            mRect = SDL_Rect(mRect.x, mRect.y, mSur.w, mSur.h);
            mTex = SDL_CreateTextureFromSurface( gRenderer, mSur );
            int w, h;
            assert(TTF_SizeText(mFont, message.toStringz, &w, &h) == 0);
            mSize = Point(w, h);

        void position(Point pos0) {
            pos = pos0;
    //        mRect.x = pos.Xi;
    //        mRect.y = pos.Yi;
        void colour(SDL_Color col) {
            setString(mText, col);

        auto colour() {
            return mCol;

        void close(bool closeFontToo = true) {
            if (closeFontToo)

        void draw(SDL_Renderer* renderer) {
            SDL_RenderCopy(renderer, mTex, null, &mRect);
        string toString() const {
            return text(`text: "`, mText, `" pos: (`, mRect.x, ",", mRect.y, ")");

JText[] txts;
txts.length = 10_000;
foreach(i; 0 .. txts.length)
    txts[i] = JText(text(i), SDL_Rect(1,1,1,1), SDL_Color(255,180,0), 12, "dummy.ttf");

The first, and most important, issue I see in your code is the font loading. In each JText instance, you’re loading a font file and allocating memory for it. This is a big issue since it means that you’re hypothetically loading the same font file multiple times (if multiple JText objects are using the same font file, which in your case is true). In your code, you’re creating 10.000 JText instances, which means that you’re loading your “dummy.ttf” file, and allocating memory for it, 10.000 times(!). You can probably see why this is a problem. You should load the font files separately, outside of the JText creation, and share the SDL_TTF pointer(s) among all the JText objects.