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.
//#hack
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;
}
//#hack
void close(bool closeFontToo = true) {
SDL_DestroyTexture(mTex);
SDL_FreeSurface(mSur);
if (closeFontToo)
TTF_CloseFont(mFont);
}
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");