I’ve used this class to death in the last couple of my LibGDX projects, and it’s been extremely helpful! I’ve even used it in a pure LWJGL project also, with some reworking. Here’s the class:
package com.nishu.balancedEnergy.entity;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
public class SpriteStore {
public static type getType(String tileType) {
String str = tileType;
switch (str) {
case "Grass":
return type.Grass;
case "Base":
return type.Base;
case "Stone":
return type.Stone;
case "light":
return type.Light;
case "bluePlayer":
return type.bluePlayer;
case "buttonDefault":
return type.buttonDefault;
case "buttonClicked":
return type.buttonClicked;
case "bullet-test":
return type.Bullet;
case "hotBar":
return type.hotBar;
case "hotBarSelection":
return type.hotBarSelection;
case "void":
return type.voidTile;
case "flowerGrass":
return type.flowerGrass;
case "water":
return type.Water;
case "lava":
return type.lava;
default:
return type.Grass;
}
}
public static Sprite createSprite(String type) {
type tiletype = getType(type);
if (tiletype != null) {
Sprite s = new Sprite(getTexLoc(tiletype));
return s;
}
return null;
}
private static Texture getTexLoc(type TileType) {
switch (TileType) {
case Base:
return new Texture(Gdx.files.internal("res/tiles/base.png"));
case Grass:
return new Texture(Gdx.files.internal("res/tiles/grass.png"));
case voidTile:
return new Texture(Gdx.files.internal("res/tiles/void.png"));
case flowerGrass:
return new Texture(Gdx.files.internal("res/tiles/flowerGrass.png"));
case Water:
return new Texture(Gdx.files.internal("res/tiles/water.png"));
case lava:
return new Texture(Gdx.files.internal("res/tiles/lava.png"));
case Stone:
return new Texture(Gdx.files.internal("res/tiles/stone.png"));
case bluePlayer:
return new Texture(Gdx.files.internal("res/players/playerBlue.png"));
case buttonClicked:
return new Texture(Gdx.files.internal("res/gui/buttonClicked.png"));
case buttonDefault:
return new Texture(Gdx.files.internal("res/gui/buttonDefault.png"));
case Light:
return new Texture(Gdx.files.internal("res/tiles/light.png"));
case Bullet:
return new Texture(Gdx.files.internal("res/bullets/bullet-test.png"));
case hotBar:
return new Texture(Gdx.files.internal("res/ui/hotBar.png"));
case hotBarSelection:
return new Texture(Gdx.files.internal("res/ui/selectionSquare.png"));
default:
break;
}
return null;
}
public static enum type {
Grass, Base, Stone, Light, Water, flowerGrass, lava, voidTile, bluePlayer, buttonDefault, buttonClicked, Bullet, hotBar, hotBarSelection;
}
}
Basically I sort through a bunch of types in an enum, and then create a new sprite with a texture based off of the enum type. There can be some modifications, but for now, its quick and easy to create a new sprite! For instance, I can create a bunch of tiles like this:
Sprite tile = SpriteStore.createSprite(type);
And the spritestore will create a new sprite and send the texture back with it! I can definitely do more with it, like sorting textures or using texture atlases, but I like it