{lwjgl} drawing font cause very low FPS and remove all the other graphics

Hello,
it’s the first time i try to draw font using SlickUtile, i followed the example in the main lwjgl page and then when i tried to apply it in my game the result is quite strange, i can see the text but the frame rate is between 7 and 9 and it also remove all the other graphics after they do a little “eye flash” (i saw the level then it disappear )

i call the font from another method :


// the update method
	private void update(int delta) {
		painting();
		draw.text();
		
		loop.updateFPS();
	}

the draw text method :

public void text() {
		TrueTypeFont font;
		Font awtFont = new Font("Times New Roman", Font.BOLD, 24);
		font = new TrueTypeFont(awtFont, true);
		font.drawString(100, 50, "test ", Color.white);
	}

the openGL initialization :

public void initGL() {

		GL11.glEnable(GL11.GL_TEXTURE_2D);
		GL11.glShadeModel(GL11.GL_SMOOTH);
		GL11.glDisable(GL11.GL_DEPTH_TEST);
		GL11.glDisable(GL11.GL_LIGHTING);

		GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		GL11.glClearDepth(1);

		GL11.glEnable(GL11.GL_BLEND);
		GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);

		GL11.glMatrixMode(GL11.GL_PROJECTION);
		GL11.glLoadIdentity();
		GL11.glOrtho(0, 800, 600, 0, 1, -1);

	}

thank you

First off, you are initializing and loading the font very frame. Load it once and render it every frame.

First of all, shouldn’t delta be a float? Or are you just storing the time in milliseconds as opposed to .003 for example? Second, if you can’t figure out why it causes low FPS (longarmx’s rationale is correct, I think) you need to really think about what happens when you intialize variables and call methods…

Longarmx is correct. You should initialize the font in the draw object’s constructor. However I still don’t understand why draw object doesn’t contain static methods

imo

draw.text();

is pretty terrible code naming…
text() should not be a method, unless you are referring to texting someone. Instead, should be drawText() or something of the sort. The “draw” object would be better renamed “rednerer” or “drawer”. All for code readability ofc.

[quote=“Jimmt,post:5,topic:41906”]
I think it’s just some simplified test code.

[quote=“Jimmt,post:3,topic:41906”]
Yeah, that is actually good advice. Try to read some more books and tutorials on Java programming, and learn a bit more about the garbage collector. Oracle has some pretty good tutorials on its site.

About your code:

public void text() {
		TrueTypeFont font;
		Font awtFont = new Font("Times New Roman", Font.BOLD, 24); // This creates a new object (which costs quite some time)
		font = new TrueTypeFont(awtFont, true);  // This creates another new object (more time wasted)
		font.drawString(100, 50, "test ", Color.white);  // Here the object is used
	} // Here both objects are discarded

Try creating the objects only once and reusing the objects, e.g.:

private TrueTypeFont font;

public void init() {
		Font awtFont = new Font("Times New Roman", Font.BOLD, 24);
		font = new TrueTypeFont(awtFont, true); 
}

public void text() {
		font.drawString(100, 50, "test ", Color.white);
}

That should resolve your performance issue.

^
it still doesn’t work :clue:
even when i don’t render the font, doing the initialization only make the whole game black but the frame rate is in it’s normal state, and when i do the rendering it’s always black and the frame rate go down to 7 FPS
yes am initializing it outside of the game loop

thank you

Have you tried to just render the font without any of your other game rendering and logic?

yes it works when i don’t do any rendering (still work when the logic is active) and also if render the font before the game rendering it also show the font with normal FPS but always a black background
the painting method :


private void painting() {
		// clear the screen and depth buffer
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
		// draw the world
		world.setWorld();

		// draw the player
		draw.coloring("white");
		draw.fillRect(hero.getX(), hero.getY(), hero.getWidth(),
				hero.getHeight());

		
		draw.coloring("black");
		draw.drawRect(hero.getX(), hero.getY(), hero.getWidth(),
				hero.getHeight());

		
	}

ps: am not calling draw.text() now,


private TrueTypeFont font;
	private Font awtFont;

	// initializing
	public void init() {

		awtFont = new Font("Times New Roman", Font.BOLD, 24);
		font = new TrueTypeFont(awtFont, true);

		loop.init();
		initHero();
		hero = new Player(heroX, heroY, heroW, heroH);
		onGround = false;
		canJump = false;
		falling = false;
		inJump = false;
		world = new World(width, height);
		myWorld = world.getWorld();

	}

	// the update method
	private void update(float delta) {

		font.drawString(100, 50, "test ", Color.white);
		//painting();
		heroUpdate();

		
		loop.updateFPS();
	}


thank you