aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/java/coffee/liz/abstractionengine/app/AbstractionEngineGame.java
blob: 8c418af4f113ce84b2e80e75df22e9c3460d105a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package coffee.liz.abstractionengine.app;

import coffee.liz.abstractionengine.app.screen.ScrollLogo;
import coffee.liz.abstractionengine.app.config.Settings;
import coffee.liz.ecs.math.Vec2;
import coffee.liz.ecs.math.Vec2f;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.utils.viewport.FitViewport;

public class AbstractionEngineGame extends Game {
	public static final Vec2<Float> WORLD_SIZE = Vec2f.builder().x(200f).y(200f).build();
	private static final String RENDERABLE_CHARS = "λ" + FreeTypeFontGenerator.DEFAULT_CHARS;
	private static final String FONT = "fonts/JetBrainsMonoNerdFont-Regular.ttf";

	public SpriteBatch batch;
	public BitmapFont font;
	public FitViewport viewport;
	public ShapeRenderer shapeRenderer;
	public Settings settings = Settings.builder().build();

	/**
	 * Game initialization hook.
	 */
	public void create() {
		viewport = new FitViewport(WORLD_SIZE.getX(), WORLD_SIZE.getY());
		batch = new SpriteBatch();
		shapeRenderer = new ShapeRenderer();
		font = initFont(24, FONT);

		this.setScreen(new ScrollLogo(this));

		viewport.update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
	}

	/**
	 * Game render hook.
	 */
	public void render() {
		super.render();
	}

	/**
	 * Game cleanup hook.
	 */
	public void dispose() {
		batch.dispose();
		font.dispose();
		shapeRenderer.dispose();
	}

	private BitmapFont initFont(final int size, final String path) {
		final int scaleForHidpi = 2;
		final FreeTypeFontGenerator gen = new FreeTypeFontGenerator(Gdx.files.internal(path));
		final FreeTypeFontGenerator.FreeTypeFontParameter params = new FreeTypeFontGenerator.FreeTypeFontParameter();
		params.characters = RENDERABLE_CHARS;
		params.size = scaleForHidpi * size;

		final BitmapFont font = gen.generateFont(params);
		font.setFixedWidthGlyphs(RENDERABLE_CHARS);
		font.setUseIntegerPositions(false);
		font.getData().setScale((1f / scaleForHidpi) * WORLD_SIZE.getY() / Gdx.graphics.getHeight());
		font.getRegion().getTexture().setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);

		gen.dispose();
		return font;
	}
}