Move asset loading to central Assets object. Add joystix font

This commit is contained in:
Matt Low 2019-12-24 15:11:41 +04:00
parent 8336838fc4
commit 9985da33e0
12 changed files with 116 additions and 22 deletions

View File

@ -45,7 +45,7 @@ project(":desktop") {
implementation project(":core") implementation project(":core")
api "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion" api "com.badlogicgames.gdx:gdx-backend-lwjgl:$gdxVersion"
api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop" api "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
api "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
} }
} }
@ -55,6 +55,6 @@ project(":core") {
dependencies { dependencies {
api "com.badlogicgames.gdx:gdx:$gdxVersion" api "com.badlogicgames.gdx:gdx:$gdxVersion"
api "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
} }
} }

Binary file not shown.

View File

Before

Width:  |  Height:  |  Size: 2.9 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

View File

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 788 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 895 B

View File

@ -0,0 +1,81 @@
package com.me.pacman;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGeneratorLoader;
import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader;
public class Assets {
private AssetManager manager;
public TextureRegion[][] level;
public TextureRegion[][] chase;
public TextureRegion[][] death;
public TextureRegion[][] ghosts;
public TextureRegion[][] pacman;
public TextureRegion[][] points;
public TextureRegion[][] volume;
public Assets() {
this.manager = new AssetManager();
}
public void loadAssets() {
manager.load("level_background.png", Texture.class);
manager.load("logo.png", Texture.class);
manager.load("sprites/level.png", Texture.class);
manager.load("sprites/chase.png", Texture.class);
manager.load("sprites/death.png", Texture.class);
manager.load("sprites/ghosts.png", Texture.class);
manager.load("sprites/pacman.png", Texture.class);
manager.load("sprites/points.png", Texture.class);
manager.load("sprites/volume.png", Texture.class);
// Yayyy! all of this to load a font
FileHandleResolver resolver = new InternalFileHandleResolver();
manager.setLoader(FreeTypeFontGenerator.class, new FreeTypeFontGeneratorLoader(resolver));
manager.setLoader(BitmapFont.class, ".ttf", new FreetypeFontLoader(resolver));
FreetypeFontLoader.FreeTypeFontLoaderParameter font = new FreetypeFontLoader.FreeTypeFontLoaderParameter();
font.fontFileName = "fonts/joystix.ttf";
font.fontParameters.size = 10;
font.fontParameters.mono = true;
manager.load("fonts/joystix.ttf", BitmapFont.class, font);
// finish loading assets from disk
manager.finishLoading();
// cache our texture regions
level = TextureRegion.split(manager.get("sprites/level.png", Texture.class), 8, 8);
chase = TextureRegion.split(manager.get("sprites/chase.png", Texture.class), 16, 16);
death = TextureRegion.split(manager.get("sprites/death.png", Texture.class), 16, 16);
ghosts = TextureRegion.split(manager.get("sprites/ghosts.png", Texture.class), 16, 16);
pacman = TextureRegion.split(manager.get("sprites/pacman.png", Texture.class), 16, 16);
points = TextureRegion.split(manager.get("sprites/points.png", Texture.class), 16, 16);
volume = TextureRegion.split(manager.get("sprites/volume.png", Texture.class), 16, 16);
}
public Texture getLevelBackground() {
return manager.get("level_background.png", Texture.class);
}
public Texture getLogo() {
return manager.get("logo.png", Texture.class);
}
public BitmapFont getFont() {
return manager.get("fonts/joystix.ttf", BitmapFont.class);
}
public void dispose() {
manager.dispose();
}
}

View File

@ -2,7 +2,6 @@ package com.me.pacman;
import com.badlogic.gdx.Game; import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.assets.AssetManager;
import com.badlogic.gdx.graphics.GL20; import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera; import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.SpriteBatch;
@ -19,7 +18,7 @@ public class PacDude extends Game {
public static final int LEVEL_WIDTH = 224; public static final int LEVEL_WIDTH = 224;
public static final int LEVEL_HEIGHT = 288; public static final int LEVEL_HEIGHT = 288;
public AssetManager assets; public Assets assets;
public SpriteBatch batch; public SpriteBatch batch;
public ShapeRenderer sr; public ShapeRenderer sr;
@ -32,7 +31,9 @@ public class PacDude extends Game {
viewport = new FitViewport(LEVEL_WIDTH, LEVEL_HEIGHT, cam); viewport = new FitViewport(LEVEL_WIDTH, LEVEL_HEIGHT, cam);
viewport.apply(true); viewport.apply(true);
assets = new AssetManager(); assets = new Assets();
assets.loadAssets();
batch = new SpriteBatch(); batch = new SpriteBatch();
sr = new ShapeRenderer(); sr = new ShapeRenderer();

View File

@ -6,19 +6,26 @@ import com.me.pacman.PacDude;
public class Level { public class Level {
private PacDude game;
private TextureRegion pellet;
private TextureRegion powerPellet;
// Grid of components, [rows][columns] // Grid of components, [rows][columns]
public LevelComponent[][] components; public LevelComponent[][] components;
public Level(String level) {
public Level(PacDude game, String level) {
this.game = game;
pellet = game.assets.level[0][7];
powerPellet = game.assets.level[0][6];
LevelLoader loader = new LevelLoader(level); LevelLoader loader = new LevelLoader(level);
components = loader.loadLevel(); components = loader.loadLevel();
} }
public void render(PacDude game, int offsetX, int offsetY) { public void render(int offsetX, int offsetY) {
Texture sprites = game.assets.get("sprites/sprites_1.png");
TextureRegion powerPellet = new TextureRegion(sprites, 16, 16, 8, 8);
TextureRegion pellet = new TextureRegion(sprites, 24, 16, 8, 8);
for (int i = 0; i < components.length; i++) { for (int i = 0; i < components.length; i++) {
LevelComponent[] row = components[i]; LevelComponent[] row = components[i];
for (int j = 0; j < row.length; j++) { for (int j = 0; j < row.length; j++) {

View File

@ -1,6 +1,13 @@
package com.me.pacman.state; package com.me.pacman.state;
import com.badlogic.gdx.assets.loaders.FileHandleResolver;
import com.badlogic.gdx.assets.loaders.resolvers.InternalFileHandleResolver;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGeneratorLoader;
import com.badlogic.gdx.graphics.g2d.freetype.FreetypeFontLoader;
import com.me.pacman.PacDude; import com.me.pacman.PacDude;
import com.me.pacman.level.Level; import com.me.pacman.level.Level;
@ -8,6 +15,7 @@ public class MenuState extends State {
private Texture levelBackground; private Texture levelBackground;
private Texture logo; private Texture logo;
private BitmapFont font;
private Level level; private Level level;
@ -17,22 +25,18 @@ public class MenuState extends State {
@Override @Override
public void setup() { public void setup() {
game.assets.load("level.png", Texture.class); levelBackground = game.assets.getLevelBackground();
game.assets.load("logo.png", Texture.class); logo = game.assets.getLogo();
game.assets.load("sprites/sprites_1.png", Texture.class);
game.assets.finishLoading();
levelBackground = game.assets.get("level.png", Texture.class);
logo = game.assets.get("logo.png", Texture.class);
logo.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear); logo.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
font = game.assets.getFont();
level = new Level("level"); level = new Level(game,"level");
} }
@Override @Override
public void render() { public void render() {
game.batch.draw(levelBackground, 0, 16); game.batch.draw(levelBackground, 0, 16);
level.render(game, 0, 16); level.render(0, 16);
game.batch.draw(logo, 0, 124, 224, 120); game.batch.draw(logo, 0, 124, 224, 120);
} }

View File

@ -1,5 +1,6 @@
#Tue Dec 24 14:05:31 GET 2019
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME