I fixed the text rendering issue.
Turns out you don't want to create any objects before the window is created. Pretty easy fix. Strange though.
As for the lots of ram usage. I made a new example to test that.
I made two tests. Both tests drawing 4000 stars each frame.
Differences:- Test without render textures stabilizes at 35.1mb.
- Test with 30 render textures doesn't stabilize, goes 137mb and continues to slowly rise.
- Test without render textures uses anti-aliasing.
- Test with 30 render textures does not use anti-aliasing despite having smooth set to true.
It seems render textures are flawed in that they use lots of ram and don't anti-alias.
Though they are essential to lower the CPU required to draw a very large, moving, generated parallax background.
Test program:
module main;
import dsfml.graphics;
import std.stdio;
import core.memory;
import std.file;
import std.conv;
import std.string;
import std.random;
void main(string[] args)
{
//GC.disable();
GC.minimize();
ContextSettings settings;
settings.antialiasingLevel = 8
;
settings.majorVersion = 3;
settings.minorVersion = 2;
auto window = new RenderWindow(VideoMode(1152, 720),"SpaceCraft", RenderWindow.Style.DefaultStyle, settings);
window.setFramerateLimit(60);
Font GameFont = new Font();
string path = thisExePath();
int index = to!int(path.lastIndexOf("/"));
if (!GameFont.loadFromFile(path[0 .. index]~"/vertiup2.ttf")) {
writeln("Font failed to load");
}
Text text = new Text("Hello DSFML", GameFont, 50);
text.setStyle(Text.Style.Bold);
text.setColor(Color(255, 0, 0));
text.origin = Vector2f(0, 0);
text.position = Vector2f(100, 100);
CircleShape shape = new CircleShape(30);
RenderTexture[] textures = new RenderTexture[](0);
Sprite[] textureSprites = new Sprite[](0);
for (int i = 0; i < 30; i++) {
RenderTexture canvas = new RenderTexture();
canvas.create(512, 512);
canvas.clear(Color(0, 0, 0, 0));
canvas.smooth = true;
textures ~= canvas;
Sprite tileSprite = new Sprite(canvas.getTexture());
tileSprite.origin = Vector2f(512/2,512/2);
textureSprites ~= tileSprite;
}
while (window.isOpen())
{
Event event;
while(window.pollEvent(event))
{
if(event.type == event.EventType.Closed)
{
window.close();
}
}
window.clear();
drawStarsWithRenderTextures(textureSprites, textures, shape, window);
//drawStars(shape, window, 4000);
window.draw(text);
window.display();
//GC.collect();
//GC.minimize();
}
}
/*************** Hits 137.5mb+, not stabilizing after 5 minutes **************/
void drawStarsWithRenderTextures(Sprite[] tileSprites, RenderTexture[] textures, CircleShape shape, RenderTarget window) {
int starCount = to!int(4000/textures.length);
for (int i = 0; i < textures.length; i++) {
RenderTexture canvas = textures[i];
canvas.clear(Color(0, 0, 0, 0));
drawStars(shape, canvas, starCount);
canvas.display();
Sprite tileSprite = tileSprites[i];
Random gen;
gen.seed(unpredictableSeed);
int x = uniform(0, window.getSize().x, gen);
int y = uniform(0, window.getSize().y, gen);
tileSprite.position = Vector2f(x, y);
window.draw(tileSprite);
}
}
/*************** Stabilizes at 35.1mb **************/
void drawStars(CircleShape shape, RenderTarget window, int starCount) {
//Draw stars
for (int i = 0; i < starCount; i++) {
Random gen;
gen.seed(unpredictableSeed);
int r = uniform(0, 255, gen);
int g = uniform(0, 255, gen);
int b = uniform(0, 255, gen);
int x = uniform(0, window.getSize().x, gen);
int y = uniform(0, window.getSize().y, gen);
shape.fillColor = Color(to!ubyte(r), to!ubyte(b), to!ubyte(g));
shape.position = Vector2f(x, y);
shape.origin = Vector2f(0.5, 0.5);
shape.scale = Vector2f(15 / 30.0 , 15 / 30.0);
window.draw(shape);
}
}