Added asynchronous asset loading and related state.

This commit is contained in:
2014-10-09 12:35:51 -04:30
parent b2bea761de
commit 4d7d988787
10 changed files with 518 additions and 124 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 397 KiB

View File

@@ -29,9 +29,12 @@ import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.gamejolt.mikykr5.poukemon.interfaces.AssetsLoadedListener;
import com.gamejolt.mikykr5.poukemon.states.BaseState;
import com.gamejolt.mikykr5.poukemon.states.LogoScreen;
import com.gamejolt.mikykr5.poukemon.states.LoadingState;
import com.gamejolt.mikykr5.poukemon.states.LogoScreenState;
import com.gamejolt.mikykr5.poukemon.states.MainMenuState;
import com.gamejolt.mikykr5.poukemon.utils.AsyncAssetLoader;
public class GameCore extends Game {
private static final String TAG = "GAME_CORE";
@@ -71,6 +74,8 @@ public class GameCore extends Game {
@Override
public void create () {
AsyncAssetLoader loader = AsyncAssetLoader.getInstance();
// Set up rendering fields and settings.
ShaderProgram.pedantic = false;
batch = new SpriteBatch();
@@ -95,10 +100,10 @@ public class GameCore extends Game {
states = new BaseState[game_states_t.getNumStates()];
try{
states[game_states_t.LOGO_SCREEN.getValue()] = new LogoScreen(this);
states[game_states_t.LOGO_SCREEN.getValue()] = new LogoScreenState(this);
states[game_states_t.MAIN_MENU.getValue()] = new MainMenuState(this);
states[game_states_t.IN_GAME.getValue()] = null;
states[game_states_t.LOADING.getValue()] = null;
states[game_states_t.LOADING.getValue()] = new LoadingState(this);
states[game_states_t.QUIT.getValue()] = null;
}catch(IllegalArgumentException e){
Gdx.app.error(TAG, CLASS_NAME + ".create(): Illegal argument caught creating states: ", e);
@@ -106,6 +111,13 @@ public class GameCore extends Game {
return;
}
for(BaseState state : states){
if(state != null && state instanceof AssetsLoadedListener)
loader.addListener((AssetsLoadedListener)state);
}
AsyncAssetLoader.freeInstance();
loader = null;
// Set the initial current and next states.
currState = game_states_t.LOGO_SCREEN;
nextState = null;

View File

@@ -22,7 +22,4 @@ public abstract class ProjectConstants{
public static final boolean DEBUG = false;
public static final int[] POWERS_OF_2 = {64, 128, 256, 512, 1024, 2048};
public static final int MENU_BUTTON_FONT_SIZE = 40;
public static final String FONT_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890:,";
}

View File

@@ -0,0 +1,125 @@
/*
* Copyright (C) 2014 Miguel Angel Astor Romero
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gamejolt.mikykr5.poukemon.effects;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.utils.Disposable;
import com.gamejolt.mikykr5.poukemon.interfaces.AssetsLoadedListener;
import com.gamejolt.mikykr5.poukemon.utils.AsyncAssetLoader;
public class ScrollingBackground implements Disposable, AssetsLoadedListener{
private static final String TAG = "SCROLLING_BACKGROUND";
private static final String CLASS_NAME = ScrollingBackground.class.getSimpleName();
private static final String SHADER_PATH = "shaders/movingBckg/movingBckg";
private AsyncAssetLoader loader;
private Texture backgroundTexture;
private Sprite background;
private ShaderProgram shader;
private int u_scaling;
private int u_displacement;
private float scaling;
private float displacement;
private String texturePath;
public ScrollingBackground(String texturePath){
this(texturePath, 2.0f, 0.0f, true);
}
public ScrollingBackground(String texturePath, boolean loadAsync){
this(texturePath, 2.0f, 0.0f, loadAsync);
}
public ScrollingBackground(String texturePath, float scaling){
this(texturePath, scaling, 0.0f, true);
}
public ScrollingBackground(String texturePath, float scaling, boolean loadAsync){
this(texturePath, scaling, 0.0f, loadAsync);
}
public ScrollingBackground(String texturePath, float scaling, float displacement, boolean loadAsync){
if(loadAsync){
loader = AsyncAssetLoader.getInstance();
loader.addAssetToLoad(texturePath, Texture.class);
loader.addListener(this);
}else{
backgroundTexture = new Texture(Gdx.files.internal(texturePath));
initGraphics();
}
shader = new ShaderProgram(Gdx.files.internal(SHADER_PATH + "_vert.glsl"), Gdx.files.internal(SHADER_PATH + "_frag.glsl"));
if(!shader.isCompiled()){
Gdx.app.error(TAG, CLASS_NAME + ".ScrollingBackground() :: Failed to compile the shader.");
Gdx.app.error(TAG, CLASS_NAME + shader.getLog());
shader = null;
}
u_scaling = shader.getUniformLocation("u_scaling");
u_displacement = shader.getUniformLocation("u_displacement");
this.texturePath = texturePath;
this.scaling = scaling;
this.displacement = displacement;
}
public void render(SpriteBatch batch) throws IllegalStateException{
if(!batch.isDrawing())
throw new IllegalStateException("Must be called between SpriteBatch.begin() and SpriteBatch.end()");
if(shader != null){
batch.setShader(shader);
shader.setUniformf(u_scaling, scaling);
shader.setUniformf(u_displacement, displacement);
}
background.draw(batch);
if(shader != null) batch.setShader(null);
displacement = displacement < 0.0f ? 1.0f : displacement - 0.0005f;
}
@Override
public void dispose(){
backgroundTexture.dispose();
if(shader != null) shader.dispose();
}
@Override
public void onAssetsLoaded(){
backgroundTexture = loader.getAsset(texturePath, Texture.class);
initGraphics();
AsyncAssetLoader.freeInstance();
}
private void initGraphics(){
// Set up the texture.
backgroundTexture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
backgroundTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
// Set the sprite for rendering.
background = new Sprite(backgroundTexture);
background.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
background.setPosition(-(Gdx.graphics.getWidth() / 2), -(Gdx.graphics.getHeight() / 2));
}
}

View File

@@ -0,0 +1,20 @@
/*
* Copyright (C) 2014 Miguel Angel Astor Romero
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gamejolt.mikykr5.poukemon.interfaces;
public interface AssetsLoadedListener {
public void onAssetsLoaded();
}

View File

@@ -0,0 +1,93 @@
/*
* Copyright (C) 2014 Miguel Angel Astor Romero
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gamejolt.mikykr5.poukemon.states;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.BitmapFont.TextBounds;
import com.gamejolt.mikykr5.poukemon.GameCore;
import com.gamejolt.mikykr5.poukemon.GameCore.game_states_t;
import com.gamejolt.mikykr5.poukemon.effects.ScrollingBackground;
import com.gamejolt.mikykr5.poukemon.utils.AsyncAssetLoader;
import com.gamejolt.mikykr5.poukemon.utils.CachedFontManager;
public class LoadingState extends BaseState{
private static final String CLASS_NAME = MainMenuState.class.getSimpleName();
// Helper fields.
private AsyncAssetLoader loader;
private CachedFontManager fontManager;
// Graphic data.
private BitmapFont font;
private ScrollingBackground scrollingBckg;
public LoadingState(final GameCore core) throws IllegalArgumentException{
super();
loader = AsyncAssetLoader.getInstance();
fontManager = CachedFontManager.getInstance();
if(core == null)
throw new IllegalArgumentException(CLASS_NAME + ": Core is null.");
this.core = core;
// Create the start button font.
font = fontManager.loadFont("data/fonts/d-puntillas-B-to-tiptoe.ttf", CachedFontManager.BASE_FONT_SIZE * 3);
// Set up the background.
scrollingBckg = new ScrollingBackground("data/gfx/textures/floortiles.png", false);
stateEnabled = false;
}
@Override
public void render(float delta){
TextBounds bounds = font.getBounds("Loading");
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
core.batch.setProjectionMatrix(pixelPerfectCamera.combined);
core.batch.begin();{
// Render background.
scrollingBckg.render(core.batch);
font.setColor(Color.BLACK);
font.draw(core.batch, "Loading", -(bounds.width / 2), -(bounds.height / 2));
}core.batch.end();
if(loader != null){
if(loader.loadAssets()){
loader.notifyListeners();
core.nextState = game_states_t.MAIN_MENU;
}
}
}
@Override
public void dispose(){
scrollingBckg.dispose();
CachedFontManager.freeInstance();
AsyncAssetLoader.freeInstance();
loader = null;
}
}

View File

@@ -21,12 +21,12 @@ import com.badlogic.gdx.graphics.Texture;
import com.gamejolt.mikykr5.poukemon.GameCore;
import com.gamejolt.mikykr5.poukemon.GameCore.game_states_t;
public class LogoScreen extends BaseState {
private static final String CLASS_NAME = LogoScreen.class.getSimpleName();
public class LogoScreenState extends BaseState{
private static final String CLASS_NAME = LogoScreenState.class.getSimpleName();
private Texture logo;
private long then;
public LogoScreen(final GameCore core){
public LogoScreenState(final GameCore core){
if(core == null)
throw new IllegalArgumentException(CLASS_NAME + ": Core is null.");
@@ -51,7 +51,7 @@ public class LogoScreen extends BaseState {
now = System.currentTimeMillis();
delta = now - then;
if(delta > 8000L){
core.nextState = game_states_t.MAIN_MENU;
core.nextState = game_states_t.LOADING;
then = now;
}
}
@@ -61,7 +61,6 @@ public class LogoScreen extends BaseState {
logo.dispose();
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer, int button){
then = 0L;

View File

@@ -20,52 +20,41 @@ import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.Texture.TextureWrap;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.NinePatch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.NinePatchDrawable;
import com.gamejolt.mikykr5.poukemon.GameCore;
import com.gamejolt.mikykr5.poukemon.GameCore.game_states_t;
import com.gamejolt.mikykr5.poukemon.ProjectConstants;
import com.gamejolt.mikykr5.poukemon.effects.ScrollingBackground;
import com.gamejolt.mikykr5.poukemon.interfaces.AssetsLoadedListener;
import com.gamejolt.mikykr5.poukemon.utils.AsyncAssetLoader;
import com.gamejolt.mikykr5.poukemon.utils.CachedFontManager;
public class MainMenuState extends BaseState{
private static final String TAG = "MAIN_MENU";
public class MainMenuState extends BaseState implements AssetsLoadedListener{
private static final String CLASS_NAME = MainMenuState.class.getSimpleName();
private static final String SHADER_PATH = "shaders/movingBckg/movingBckg";
// Helper fields.
private float u_scaling[];
private float u_displacement;
private AsyncAssetLoader loader;
private CachedFontManager fontManager;
private boolean assetsLoaded;
// Buttons and other GUI components.
private TextButton startButton;
private Rectangle startButtonBBox;
private TextButton quitButton;
private Rectangle quitButtonBBox;
private Sprite background;
// Graphic data for the start button.
private Texture menuButtonEnabledTexture;
private Texture menuButtonDisabledTexture;
private Texture menuButtonPressedTexture;
private NinePatch menuButtonEnabled9p;
private NinePatch menuButtonDisabled9p;
private NinePatch menuButtonPressed9p;
private BitmapFont font;
// Other graphics.
private Texture backgroundTexture;
private ShaderProgram backgroundShader;
private TextButton startButton;
private Rectangle startButtonBBox;
private TextButton quitButton;
private Rectangle quitButtonBBox;
private Texture menuButtonEnabledTexture;
private Texture menuButtonDisabledTexture;
private Texture menuButtonPressedTexture;
private NinePatch menuButtonEnabled9p;
private NinePatch menuButtonDisabled9p;
private NinePatch menuButtonPressed9p;
private BitmapFont font;
private ScrollingBackground scrollingBckg;
// Button touch helper fields.
private boolean startButtonTouched;
@@ -76,76 +65,22 @@ public class MainMenuState extends BaseState{
public MainMenuState(final GameCore core) throws IllegalArgumentException{
super();
TextButtonStyle textButtonStyle;
FreeTypeFontGenerator fontGenerator;
FreeTypeFontParameter fontParameters;
if(core == null)
throw new IllegalArgumentException(CLASS_NAME + ": Core is null.");
this.core = core;
loader = AsyncAssetLoader.getInstance();
fontManager = CachedFontManager.getInstance();
assetsLoaded = false;
// Create the start button background.
menuButtonEnabledTexture = new Texture(Gdx.files.internal("data/gfx/gui/Anonymous_Pill_Button_Yellow.png"));
menuButtonEnabled9p = new NinePatch(new TextureRegion(menuButtonEnabledTexture, 0, 0, menuButtonEnabledTexture.getWidth(), menuButtonEnabledTexture.getHeight()), 49, 49, 45, 45);
menuButtonDisabledTexture = new Texture(Gdx.files.internal("data/gfx/gui/Anonymous_Pill_Button_Cyan.png"));
menuButtonDisabled9p = new NinePatch(new TextureRegion(menuButtonDisabledTexture, 0, 0, menuButtonDisabledTexture.getWidth(), menuButtonDisabledTexture.getHeight()), 49, 49, 45, 45);
menuButtonPressedTexture = new Texture(Gdx.files.internal("data/gfx/gui/Anonymous_Pill_Button_Blue.png"));
menuButtonPressed9p = new NinePatch(new TextureRegion(menuButtonPressedTexture, 0, 0, menuButtonPressedTexture.getWidth(), menuButtonPressedTexture.getHeight()), 49, 49, 45, 45);
// Create the start button font.
fontParameters = new FreeTypeFontParameter();
fontParameters.characters = ProjectConstants.FONT_CHARS;
fontParameters.size = ProjectConstants.MENU_BUTTON_FONT_SIZE;
fontParameters.flip = false;
fontGenerator = new FreeTypeFontGenerator(Gdx.files.internal("data/fonts/d-puntillas-B-to-tiptoe.ttf"));
font = fontGenerator.generateFont(fontParameters);
fontGenerator.dispose();
// Create the buttons.
textButtonStyle = new TextButtonStyle();
textButtonStyle.font = font;
textButtonStyle.up = new NinePatchDrawable(menuButtonEnabled9p);
textButtonStyle.checked = new NinePatchDrawable(menuButtonPressed9p);
textButtonStyle.disabled = new NinePatchDrawable(menuButtonDisabled9p);
textButtonStyle.fontColor = new Color(Color.BLACK);
textButtonStyle.downFontColor = new Color(Color.WHITE);
textButtonStyle.disabledFontColor = new Color(Color.BLACK);
startButton = new TextButton("Start game", textButtonStyle);
startButton.setText("Start game");
startButtonBBox = new Rectangle(0, 0, startButton.getWidth(), startButton.getHeight());
quitButton = new TextButton("Quit", textButtonStyle);
quitButton.setText("quit");
quitButtonBBox = new Rectangle(0, 0, quitButton.getWidth(), quitButton.getHeight());
// Set buttons.
startButton.setPosition(-(startButton.getWidth() / 2), -(startButton.getHeight() / 2));
startButtonBBox.setPosition(startButton.getX(), startButton.getY());
quitButton.setPosition(-(quitButton.getWidth() / 2), (startButton.getY() - startButton.getHeight()) - 10);
quitButtonBBox.setPosition(quitButton.getX(), quitButton.getY());
// Load graphic resources.
loader.addAssetToLoad("data/gfx/gui/Anonymous_Pill_Button_Yellow.png", Texture.class);
loader.addAssetToLoad("data/gfx/gui/Anonymous_Pill_Button_Cyan.png", Texture.class);
loader.addAssetToLoad("data/gfx/gui/Anonymous_Pill_Button_Blue.png", Texture.class);
font = fontManager.loadFont("data/fonts/d-puntillas-B-to-tiptoe.ttf");
// Set up the background.
backgroundTexture = new Texture(Gdx.files.internal("data/gfx/textures/grass.png"));
backgroundTexture.setWrap(TextureWrap.Repeat, TextureWrap.Repeat);
backgroundTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
background = new Sprite(backgroundTexture);
background.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
background.setPosition(-(Gdx.graphics.getWidth() / 2), -(Gdx.graphics.getHeight() / 2));
backgroundShader = new ShaderProgram(Gdx.files.internal(SHADER_PATH + "_vert.glsl"), Gdx.files.internal(SHADER_PATH + "_frag.glsl"));
if(!backgroundShader.isCompiled()){
Gdx.app.error(TAG, CLASS_NAME + ".MainMenuStateBase() :: Failed to compile the background shader.");
Gdx.app.error(TAG, CLASS_NAME + backgroundShader.getLog());
backgroundShader = null;
}
u_scaling = new float[2];
u_scaling[0] = Gdx.graphics.getWidth() > Gdx.graphics.getHeight() ? 16.0f : 9.0f;
u_scaling[1] = Gdx.graphics.getHeight() > Gdx.graphics.getWidth() ? 16.0f : 9.0f;
u_displacement = 1.0f;
scrollingBckg = new ScrollingBackground("data/gfx/textures/grass.png");
startButtonTouched = false;
startButtonTouchPointer = -1;
@@ -156,17 +91,17 @@ public class MainMenuState extends BaseState{
}
@Override
public void render(float delta){
public void render(float delta) throws IllegalStateException{
if(!assetsLoaded)
throw new IllegalStateException("Attempted to render before assets were loaded.");
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
core.batch.setProjectionMatrix(pixelPerfectCamera.combined);
core.batch.begin();{
// Render background.
core.batch.disableBlending();
drawBackground(core.batch);
core.batch.enableBlending();
scrollingBckg.render(core.batch);
// Render buttons.
startButton.draw(core.batch, 1.0f);
@@ -180,20 +115,8 @@ public class MainMenuState extends BaseState{
menuButtonEnabledTexture.dispose();
menuButtonDisabledTexture.dispose();
menuButtonPressedTexture.dispose();
backgroundTexture.dispose();
if(backgroundShader != null) backgroundShader.dispose();
font.dispose();
}
private void drawBackground(SpriteBatch batch){
if(backgroundShader != null){
batch.setShader(backgroundShader);
backgroundShader.setUniformf("u_scaling", 2.0f);
backgroundShader.setUniformf("u_displacement", u_displacement);
}
background.draw(batch);
if(backgroundShader != null) batch.setShader(null);
u_displacement = u_displacement < 0.0f ? 1.0f : u_displacement - 0.0005f;
scrollingBckg.dispose();
CachedFontManager.freeInstance();
}
/*;;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -261,4 +184,45 @@ public class MainMenuState extends BaseState{
}
return false;
}
@Override
public void onAssetsLoaded() {
TextButtonStyle textButtonStyle;
// Create the button backgrounds.
menuButtonEnabledTexture = loader.getAsset("data/gfx/gui/Anonymous_Pill_Button_Yellow.png", Texture.class);
menuButtonDisabledTexture = loader.getAsset("data/gfx/gui/Anonymous_Pill_Button_Cyan.png", Texture.class);
menuButtonPressedTexture = loader.getAsset("data/gfx/gui/Anonymous_Pill_Button_Blue.png", Texture.class);
menuButtonEnabled9p = new NinePatch(new TextureRegion(menuButtonEnabledTexture, 0, 0, menuButtonEnabledTexture.getWidth(), menuButtonEnabledTexture.getHeight()), 49, 49, 45, 45);
menuButtonDisabled9p = new NinePatch(new TextureRegion(menuButtonDisabledTexture, 0, 0, menuButtonDisabledTexture.getWidth(), menuButtonDisabledTexture.getHeight()), 49, 49, 45, 45);
menuButtonPressed9p = new NinePatch(new TextureRegion(menuButtonPressedTexture, 0, 0, menuButtonPressedTexture.getWidth(), menuButtonPressedTexture.getHeight()), 49, 49, 45, 45);
// Create the buttons.
textButtonStyle = new TextButtonStyle();
textButtonStyle.font = font;
textButtonStyle.up = new NinePatchDrawable(menuButtonEnabled9p);
textButtonStyle.checked = new NinePatchDrawable(menuButtonPressed9p);
textButtonStyle.disabled = new NinePatchDrawable(menuButtonDisabled9p);
textButtonStyle.fontColor = new Color(Color.BLACK);
textButtonStyle.downFontColor = new Color(Color.WHITE);
textButtonStyle.disabledFontColor = new Color(Color.BLACK);
startButton = new TextButton("Start game", textButtonStyle);
startButton.setText("Start game");
startButtonBBox = new Rectangle(0, 0, startButton.getWidth(), startButton.getHeight());
quitButton = new TextButton("Quit", textButtonStyle);
quitButton.setText("quit");
quitButtonBBox = new Rectangle(0, 0, quitButton.getWidth(), quitButton.getHeight());
// Set buttons.
startButton.setPosition(-(startButton.getWidth() / 2), -(startButton.getHeight() / 2));
startButtonBBox.setPosition(startButton.getX(), startButton.getY());
quitButton.setPosition(-(quitButton.getWidth() / 2), (startButton.getY() - startButton.getHeight()) - 10);
quitButtonBBox.setPosition(quitButton.getX(), quitButton.getY());
assetsLoaded = true;
AsyncAssetLoader.freeInstance();
}
}

View File

@@ -0,0 +1,95 @@
/*
* Copyright (C) 2014 Miguel Angel Astor Romero
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gamejolt.mikykr5.poukemon.utils;
import java.util.LinkedList;
import com.badlogic.gdx.assets.AssetManager;
import com.gamejolt.mikykr5.poukemon.interfaces.AssetsLoadedListener;
public final class AsyncAssetLoader {
private LinkedList<AssetsLoadedListener> listeners;
private AssetManager manager;
private AsyncAssetLoader(){
listeners = new LinkedList<AssetsLoadedListener>();
manager = new AssetManager();
}
private static final class SingletonHolder{
public static int REF_COUNT = 0;
public static AsyncAssetLoader INSTANCE;
}
public static AsyncAssetLoader getInstance(){
if(SingletonHolder.REF_COUNT == 0)
SingletonHolder.INSTANCE = new AsyncAssetLoader();
SingletonHolder.REF_COUNT++;
return SingletonHolder.INSTANCE;
}
public static void freeInstance(){
SingletonHolder.REF_COUNT--;
if(SingletonHolder.REF_COUNT <= 0) SingletonHolder.INSTANCE = null;
}
public void addListener(AssetsLoadedListener listener) throws IllegalArgumentException{
try{
checkParametes(listener, "listener");
}catch(IllegalArgumentException e){
throw e;
}
listeners.add(listener);
}
public <T> void addAssetToLoad(String path, Class<T> assetClass) throws IllegalArgumentException{
try{
checkParametes(path, "path");
checkParametes(path, "assetClass");
}catch(IllegalArgumentException e){
throw e;
}
manager.load(path, assetClass);
}
public <T> T getAsset(String path, Class<T> assetClass) throws IllegalArgumentException{
try{
checkParametes(path, "path");
checkParametes(path, "assetClass");
}catch(IllegalArgumentException e){
throw e;
}
return manager.get(path, assetClass);
}
public boolean loadAssets(){
return manager.update();
}
public void notifyListeners(){
for(AssetsLoadedListener listener : listeners)
listener.onAssetsLoaded();
listeners.clear();
}
private void checkParametes(Object parameter, String paramName) throws IllegalArgumentException{
if(parameter == null) throw new IllegalArgumentException("Parameter: " + paramName + " is null.");
}
}

View File

@@ -0,0 +1,89 @@
/*
* Copyright (C) 2014 Miguel Angel Astor Romero
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.gamejolt.mikykr5.poukemon.utils;
import java.util.HashMap;
import java.util.Map;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
public class CachedFontManager{
public static final String FONT_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890:,";
public static final int BASE_FONT_SIZE = 40;
private Map<String, BitmapFont> fonts;
private CachedFontManager(){
fonts = new HashMap<String, BitmapFont>();
}
private static final class SingletonHolder{
public static int REF_COUNT = 0;
public static CachedFontManager INSTANCE;
}
public static CachedFontManager getInstance(){
if(SingletonHolder.REF_COUNT == 0)
SingletonHolder.INSTANCE = new CachedFontManager();
SingletonHolder.REF_COUNT++;
return SingletonHolder.INSTANCE;
}
public static void freeInstance(){
SingletonHolder.REF_COUNT--;
if(SingletonHolder.REF_COUNT <= 0){
SingletonHolder.INSTANCE.dispose();
SingletonHolder.INSTANCE = null;
}
}
public BitmapFont loadFont(String path){
return loadFont(path, BASE_FONT_SIZE);
}
public BitmapFont loadFont(String path, int size){
FreeTypeFontGenerator fontGenerator;
FreeTypeFontParameter fontParameters;
BitmapFont font;
if(fonts.containsKey(path))
return fonts.get(path);
fontParameters = new FreeTypeFontParameter();
fontParameters.characters = FONT_CHARS;
fontParameters.size = size;
fontParameters.flip = false;
fontGenerator = new FreeTypeFontGenerator(Gdx.files.internal(path));
font = fontGenerator.generateFont(fontParameters);
fonts.put(path, font);
fontGenerator.dispose();
return font;
}
private void dispose(){
for(BitmapFont font : fonts.values())
font.dispose();
fonts.clear();
}
}