Added init tasks for resources.

master
Ondřej Hruška 11 years ago
parent 6d901ddfe5
commit af8535620b
  1. 6
      src/junk/AppInitOptions.java
  2. 4
      src/junk/BaseApp.java
  3. 4
      src/mightypork/gamecore/audio/DeferredAudio.java
  4. 5
      src/mightypork/gamecore/audio/IAudio.java
  5. 53
      src/mightypork/gamecore/audio/SoundRegistry.java
  6. 12
      src/mightypork/gamecore/audio/players/AudioPlayer.java
  7. 2
      src/mightypork/gamecore/audio/players/LoopPlayer.java
  8. 4
      src/mightypork/gamecore/core/App.java
  9. 2
      src/mightypork/gamecore/core/config/InitTaskConfig.java
  10. 5
      src/mightypork/gamecore/core/init/InitTask.java
  11. 1
      src/mightypork/gamecore/core/init/InitTaskCrashHandler.java
  12. 1
      src/mightypork/gamecore/core/init/InitTaskDisplay.java
  13. 1
      src/mightypork/gamecore/core/init/InitTaskIonizables.java
  14. 1
      src/mightypork/gamecore/core/init/InitTaskLog.java
  15. 2
      src/mightypork/gamecore/core/init/InitTaskLogHeader.java
  16. 40
      src/mightypork/gamecore/core/init/InitTaskResourceLoader.java
  17. 30
      src/mightypork/gamecore/core/init/InitTaskResourceLoaderAsync.java
  18. 21
      src/mightypork/gamecore/core/init/InitTaskResourceLoaderNone.java
  19. 34
      src/mightypork/gamecore/core/init/InitTaskResources.java
  20. 1
      src/mightypork/gamecore/core/init/InitTaskWorkdir.java
  21. 4
      src/mightypork/gamecore/core/init/OptionalInitTask.java
  22. 5
      src/mightypork/gamecore/core/plugins/AppPlugin.java
  23. 2
      src/mightypork/gamecore/core/plugins/screenshot/InitTaskPluginScreenshot.java
  24. 2
      src/mightypork/gamecore/core/plugins/screenshot/ScreenshotPlugin.java
  25. 30
      src/mightypork/gamecore/graphics/fonts/DeferredFont.java
  26. 5
      src/mightypork/gamecore/graphics/fonts/FontRegistry.java
  27. 31
      src/mightypork/gamecore/graphics/fonts/FontStyle.java
  28. 19
      src/mightypork/gamecore/graphics/textures/TextureRegistry.java
  29. 2
      src/mightypork/gamecore/gui/screens/impl/CrossfadeOverlay.java
  30. 104
      src/mightypork/gamecore/resources/Res.java
  31. 2
      src/mightypork/gamecore/resources/ResourceInitializer.java
  32. 24
      src/mightypork/gamecore/resources/loading/AsyncResourceLoader.java
  33. 2
      src/mightypork/gamecore/resources/loading/ResourceLoader.java

@ -7,7 +7,7 @@ import java.util.List;
import java.util.logging.Level;
import mightypork.gamecore.core.AppBackend;
import mightypork.gamecore.resources.ResourceSetup;
import mightypork.gamecore.resources.ResourceInitializer;
import mightypork.gamecore.resources.loading.AsyncResourceLoader;
import mightypork.gamecore.resources.loading.ResourceLoader;
@ -27,7 +27,7 @@ public class AppInitOptions {
String configFile = "settings.cfg";
String configComment = "Main config file";
final List<ResourceSetup> resourceLists = new ArrayList<>();
final List<ResourceInitializer> resourceLists = new ArrayList<>();
final List<KeySetup> keyLists = new ArrayList<>();
final List<ConfigSetup> configLists = new ArrayList<>();
@ -56,7 +56,7 @@ public class AppInitOptions {
}
public void addResources(ResourceSetup res)
public void addResources(ResourceInitializer res)
{
resourceLists.add(res);
}

@ -10,7 +10,7 @@ import mightypork.gamecore.core.config.Config;
import mightypork.gamecore.gui.screens.ScreenRegistry;
import mightypork.gamecore.gui.screens.impl.CrossfadeOverlay;
import mightypork.gamecore.resources.Res;
import mightypork.gamecore.resources.ResourceSetup;
import mightypork.gamecore.resources.ResourceInitializer;
import mightypork.utils.files.WorkDir;
import mightypork.utils.logging.Log;
@ -140,7 +140,7 @@ public abstract class BaseApp extends App implements UncaughtExceptionHandler {
Res.setBaseDir(this);
for (final ResourceSetup rl : opt.resourceLists) {
for (final ResourceInitializer rl : opt.resourceLists) {
Res.load(rl);
}

@ -29,14 +29,14 @@ public abstract class DeferredAudio extends BaseDeferredResource implements IAud
@Override
public void play(double gain, double pitch, boolean loop)
{
play(gain, pitch, loop, App.audio().getListenerPos());
play(gain, pitch, loop, App.sound().getListenerPos());
}
@Override
public void play(double gain, double pitch, boolean loop, double x, double y)
{
play(gain, pitch, loop, x, y, App.audio().getListenerPos().z());
play(gain, pitch, loop, x, y, App.sound().getListenerPos().z());
}

@ -19,7 +19,7 @@ public interface IAudio extends Destroyable {
/**
* Resume loop (if was paused)
* Resume loop (if was looping and paused)
*/
void resumeLoop();
@ -34,7 +34,8 @@ public interface IAudio extends Destroyable {
/**
* Stop audio playback, free source.
* Stop audio playback, free source. Meaningful for loops, may not work
* properly for effects.
*/
void stop();

@ -6,6 +6,8 @@ import java.util.Map;
import mightypork.gamecore.audio.players.EffectPlayer;
import mightypork.gamecore.audio.players.LoopPlayer;
import mightypork.gamecore.core.App;
import mightypork.utils.exceptions.KeyAlreadyExistsException;
/**
@ -19,6 +21,28 @@ public class SoundRegistry {
private final Map<String, LoopPlayer> loops = new HashMap<>();
/**
* Register effect resource
*
* @param key sound key
* @param resourcePath path to the effect resource
* @param gain gain adjustment
* @param pitch pitch adjustment
* @return the just created effect player
*/
public EffectPlayer addEffect(String key, String resourcePath, double gain, double pitch)
{
final EffectPlayer effect = App.sound().createEffect(resourcePath);
effect.setPitch(pitch);
effect.setGain(gain);
addEffect(key, effect);
return effect;
}
/**
* Register effect resource
*
@ -27,10 +51,37 @@ public class SoundRegistry {
*/
public void addEffect(String key, EffectPlayer effect)
{
if (effects.containsKey(key)) throw new KeyAlreadyExistsException();
effects.put(key, effect);
}
/**
* Register loop resource (music / effect loop)
*
* @param key sound key
* @param resourcePath path to the effect resource
* @param gain gain adjustment
* @param pitch pitch adjustment
* @param fadeIn fadeIn time (s)
* @param fadeOut fadeOut time (s)
* @return the just created loop player
*/
public LoopPlayer addLoop(String key, String resourcePath, double gain, double pitch, double fadeIn, double fadeOut)
{
final LoopPlayer loop = App.sound().createLoop(resourcePath);
loop.setPitch(pitch);
loop.setGain(gain);
loop.setFadeTimes(fadeIn, fadeOut);
addLoop(key, loop);
return loop;
}
/**
* Register loop resource (music / effect loop)
*
@ -39,6 +90,8 @@ public class SoundRegistry {
*/
public void addLoop(String key, LoopPlayer loop)
{
if (loops.containsKey(key)) throw new KeyAlreadyExistsException();
loops.put(key, loop);
}

@ -94,22 +94,22 @@ public abstract class AudioPlayer implements Destroyable {
/**
* Set base gain. 1 is original volume, 0 is silence.
*
* @param baseGain base gain
* @param gain base gain
*/
public void setGain(double baseGain)
public void setGain(double gain)
{
this.baseGain = baseGain;
this.baseGain = gain;
}
/**
* Set base pitch. 1 is original pitch, less is deeper, more is higher.
*
* @param basePitch base pitch
* @param pitch base pitch
*/
public void setPitch(double basePitch)
public void setPitch(double pitch)
{
this.basePitch = basePitch;
this.basePitch = pitch;
}
}

@ -9,7 +9,7 @@ import mightypork.utils.math.animation.NumAnimated;
/**
* Audio loop player (with fading, good for music)
* Audio loop player (with fading, for music)
*
* @author Ondřej Hruška (MightyPork)
*/

@ -7,6 +7,8 @@ import java.util.List;
import mightypork.gamecore.audio.AudioModule;
import mightypork.gamecore.core.config.Config;
import mightypork.gamecore.core.events.ShutdownEvent;
import mightypork.gamecore.core.init.InitTask;
import mightypork.gamecore.core.plugins.AppPlugin;
import mightypork.gamecore.graphics.GraphicsModule;
import mightypork.gamecore.input.InputModule;
import mightypork.utils.annotations.Stub;
@ -236,7 +238,7 @@ public class App extends BusNode {
*
* @return audio module
*/
public static AudioModule audio()
public static AudioModule sound()
{
return instance.backend.getAudio();
}

@ -1,7 +1,7 @@
package mightypork.gamecore.core.config;
import mightypork.gamecore.core.InitTask;
import mightypork.gamecore.core.init.InitTask;
import mightypork.utils.annotations.Stub;

@ -1,4 +1,4 @@
package mightypork.gamecore.core;
package mightypork.gamecore.core.init;
import java.util.ArrayList;
@ -7,6 +7,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Set;
import mightypork.gamecore.core.App;
import mightypork.utils.Reflect;
import mightypork.utils.annotations.Stub;
import mightypork.utils.logging.Log;
@ -30,7 +31,7 @@ public abstract class InitTask {
*
* @param app app
*/
final void bind(App app)
public final void bind(App app)
{
this.app = app;
}

@ -4,7 +4,6 @@ package mightypork.gamecore.core.init;
import java.lang.Thread.UncaughtExceptionHandler;
import mightypork.gamecore.core.App;
import mightypork.gamecore.core.InitTask;
import mightypork.utils.annotations.Stub;
import mightypork.utils.logging.Log;

@ -1,7 +1,6 @@
package mightypork.gamecore.core.init;
import mightypork.gamecore.core.InitTask;
import mightypork.gamecore.graphics.GraphicsModule;

@ -3,7 +3,6 @@ package mightypork.gamecore.core.init;
import java.io.IOException;
import mightypork.gamecore.core.InitTask;
import mightypork.utils.ion.Ion;
import mightypork.utils.ion.IonInput;
import mightypork.utils.ion.IonOutput;

@ -4,7 +4,6 @@ package mightypork.gamecore.core.init;
import java.io.File;
import java.util.logging.Level;
import mightypork.gamecore.core.InitTask;
import mightypork.utils.files.WorkDir;
import mightypork.utils.logging.Log;
import mightypork.utils.logging.writers.LogWriter;

@ -3,8 +3,6 @@ package mightypork.gamecore.core.init;
import java.io.IOException;
import mightypork.gamecore.core.InitTask;
import mightypork.gamecore.core.OptionalInitTask;
import mightypork.utils.files.WorkDir;
import mightypork.utils.logging.Log;

@ -0,0 +1,40 @@
package mightypork.gamecore.core.init;
import mightypork.gamecore.resources.loading.ResourceLoader;
/**
* Task to add a resource loader.<br>
* By default the async resource loader is used
*
* @author Ondřej Hruška (MightyPork)
*/
public abstract class InitTaskResourceLoader extends InitTask {
/** The loader. */
protected ResourceLoader loader;
@Override
public void run()
{
loader = getLoaderImpl();
if (loader != null) loader.init();
}
/**
* Create a loader impl
*
* @return loader
*/
protected abstract ResourceLoader getLoaderImpl();
@Override
public String getName()
{
return "resource_loader";
}
}

@ -0,0 +1,30 @@
package mightypork.gamecore.core.init;
import mightypork.gamecore.resources.loading.AsyncResourceLoader;
import mightypork.gamecore.resources.loading.ResourceLoader;
/**
* Task to add a resource loader.<br>
* By default the async resource loader is used
*
* @author Ondřej Hruška (MightyPork)
*/
public class InitTaskResourceLoaderAsync extends InitTaskResourceLoader {
/**
* Create a loader impl
*
* @return loader
*/
@Override
protected ResourceLoader getLoaderImpl()
{
final AsyncResourceLoader loader = new AsyncResourceLoader();
// could now configure the loader
return loader;
}
}

@ -0,0 +1,21 @@
package mightypork.gamecore.core.init;
import mightypork.gamecore.resources.loading.ResourceLoader;
/**
* Task to add a resource loader.<br>
* By default the async resource loader is used
*
* @author Ondřej Hruška (MightyPork)
*/
public class InitTaskResourceLoaderNone extends InitTaskResourceLoader {
@Override
protected ResourceLoader getLoaderImpl()
{
return null;
}
}

@ -0,0 +1,34 @@
package mightypork.gamecore.core.init;
import mightypork.gamecore.resources.Res;
import mightypork.gamecore.resources.ResourceInitializer;
/**
* Task to initialize resources
*
* @author Ondřej Hruška (MightyPork)
*/
public abstract class InitTaskResources extends InitTask implements ResourceInitializer {
@Override
public void run()
{
Res.load(this);
}
@Override
public String getName()
{
return "resources";
}
@Override
public String[] getDependencies()
{
return new String[] { "resource_loader" };
}
}

@ -9,7 +9,6 @@ import java.util.Map.Entry;
import javax.swing.JOptionPane;
import mightypork.gamecore.core.App;
import mightypork.gamecore.core.InitTask;
import mightypork.utils.annotations.Stub;
import mightypork.utils.files.InstanceLock;
import mightypork.utils.files.WorkDir;

@ -1,4 +1,4 @@
package mightypork.gamecore.core;
package mightypork.gamecore.core.init;
import java.lang.annotation.Documented;
@ -20,5 +20,5 @@ import java.lang.annotation.Target;
@Documented
@Inherited
public @interface OptionalInitTask {
//
}

@ -1,6 +1,7 @@
package mightypork.gamecore.core;
package mightypork.gamecore.core.plugins;
import mightypork.gamecore.core.App;
import mightypork.utils.annotations.Stub;
import mightypork.utils.eventbus.clients.BusNode;
@ -23,7 +24,7 @@ public class AppPlugin extends BusNode {
*
* @param app app
*/
void bind(App app)
public void bind(App app)
{
this.app = app;
}

@ -1,7 +1,7 @@
package mightypork.gamecore.core.plugins.screenshot;
import mightypork.gamecore.core.InitTask;
import mightypork.gamecore.core.init.InitTask;
import mightypork.utils.files.WorkDir;

@ -2,8 +2,8 @@ package mightypork.gamecore.core.plugins.screenshot;
import mightypork.gamecore.core.App;
import mightypork.gamecore.core.AppPlugin;
import mightypork.gamecore.core.events.MainLoopRequest;
import mightypork.gamecore.core.plugins.AppPlugin;
import mightypork.utils.Support;

@ -12,36 +12,6 @@ import mightypork.gamecore.resources.BaseDeferredResource;
*/
public abstract class DeferredFont extends BaseDeferredResource implements IFont {
/**
* Font style enum
*/
public static enum FontStyle
{
/** Plan style */
PLAIN(0),
/** Bold style */
BOLD(1),
/** Italic style */
ITALIC(2),
/** Bond and italic together */
BOLD_ITALIC(1 + 2);
/** Number associated with the style */
public int numval;
/**
* Font style
*
* @param style style index as in awt Font. Not using constants to be
* independent on awt.
*/
private FontStyle(int style)
{
this.numval = style;
}
}
/**
* Requested font size. For bitmap fonts, this should match the actual font
* size (in pixels). The font can be scaled after loaded, but it may be

@ -46,7 +46,10 @@ public class FontRegistry extends BusNode {
/**
* Add a font alias.
* Add a font alias. Useful to specify fonts to use for various parts of the
* app, without having to change the aliases throughout the app whenever the
* font alias is changed.<br>
* It is, however, NOT possible to make alias to alias.
*
* @param alias_key alias key
* @param font_key font key

@ -0,0 +1,31 @@
package mightypork.gamecore.graphics.fonts;
/**
* Font style enum
*/
public enum FontStyle
{
/** Plan style */
PLAIN(0),
/** Bold style */
BOLD(1),
/** Italic style */
ITALIC(2),
/** Bond and italic together */
BOLD_ITALIC(1 + 2);
/** Number associated with the style */
public int numval;
/**
* Font style
*
* @param style style index as in awt Font. Not using constants to be
* independent on awt.
*/
private FontStyle(int style)
{
this.numval = style;
}
}

@ -23,17 +23,22 @@ public class TextureRegistry {
/**
* Load a texture from resource, without a key. This texture will not be
* added to the bank.
* Load a texture from resource.
*
* @param resourcePath resource path of the texture
* @param filter filtering mode
* @param wrap wrapping mode
* @return texture reference
*/
public ITexture addTexture(String resourcePath, FilterMode filter, WrapMode wrap)
public ITexture loadTexture(String resourcePath, FilterMode filter, WrapMode wrap)
{
return addTexture(resourcePath, resourcePath, filter, wrap);
final DeferredTexture texture = App.gfx().createTextureResource(resourcePath);
texture.setFilter(filter);
texture.setWrap(wrap);
App.bus().send(new ResourceLoadRequest(texture));
return texture;
}
@ -51,11 +56,7 @@ public class TextureRegistry {
{
if (key != null) if (textures.containsKey(key)) throw new KeyAlreadyExistsException();
final DeferredTexture texture = App.gfx().createTextureResource(resourcePath);
texture.setFilter(filter);
texture.setWrap(wrap);
App.bus().send(new ResourceLoadRequest(texture));
final ITexture texture = loadTexture(resourcePath, filter, wrap);
if (key != null) {
textures.put(key, texture);

@ -76,7 +76,7 @@ public class CrossfadeOverlay extends Overlay {
if (screen == null) {
// going for halt
App.audio().fadeOutAllLoops();
App.sound().fadeOutAllLoops();
}
if (fromDark) {

@ -19,30 +19,18 @@ import mightypork.gamecore.graphics.textures.TxSheet;
*/
public final class Res {
private static TextureRegistry textures;
private static SoundRegistry sounds;
private static FontRegistry fonts;
private static boolean initialized = false;
private static TextureRegistry textures = new TextureRegistry();
private static SoundRegistry sounds = new SoundRegistry();
private static FontRegistry fonts = new FontRegistry();
/**
* Load on behalf of given base app
* Get a texture by key
*
* @param app app access
* @param key the key
* @return texture
*/
public static void init()
{
if (initialized) return;
initialized = true;
textures = new TextureRegistry();
sounds = new SoundRegistry();
fonts = new FontRegistry();
}
public static ITexture getTexture(String key)
public static ITexture texture(String key)
{
return textures.getTexture(key);
}
@ -51,10 +39,10 @@ public final class Res {
/**
* Get a texture sheet by key
*
* @param key
* @param key the key
* @return sheet
*/
public static TxSheet getTxSheet(String key)
public static TxSheet txSheet(String key)
{
return textures.getSheet(key);
}
@ -63,38 +51,94 @@ public final class Res {
/**
* Get a texture quad by key
*
* @param key
* @param key the key
* @return quad
*/
public static TxQuad getTxQuad(String key)
public static TxQuad txQuad(String key)
{
return textures.getQuad(key);
}
public static LoopPlayer getSoundLoop(String key)
/**
* Get a sound loop player by key
*
* @param key the key
* @return loop player
*/
public static LoopPlayer loop(String key)
{
return sounds.getLoop(key);
}
public static EffectPlayer getSoundEffect(String key)
/**
* Get a sound effect player by key
*
* @param key the key
* @return effect player
*/
public static EffectPlayer sound(String key)
{
return sounds.getEffect(key);
}
public static IFont getFont(String key)
/**
* Get a font by key
*
* @param key the key
* @return font
*/
public static IFont font(String key)
{
return fonts.getFont(key);
}
public static void load(ResourceSetup binder)
/**
* Get internal texture registry
*
* @return registry
*/
public static TextureRegistry getTextureRegistry()
{
return textures;
}
/**
* Get internal font registry
*
* @return registry
*/
public static FontRegistry getFontRegistry()
{
return fonts;
}
/**
* Get internal sound registry
*
* @return registry
*/
public static SoundRegistry getSoundRegistry()
{
return sounds;
}
/**
* Load resources by a resource initializer.
*
* @param initializer the resource initializer
*/
public static void load(ResourceInitializer initializer)
{
binder.addFonts(fonts);
binder.addTextures(textures);
binder.addSounds(sounds);
initializer.addFonts(fonts);
initializer.addTextures(textures);
initializer.addSounds(sounds);
}
}

@ -11,7 +11,7 @@ import mightypork.gamecore.graphics.textures.TextureRegistry;
*
* @author Ondřej Hruška (MightyPork)
*/
public interface ResourceSetup {
public interface ResourceInitializer {
/**
* Add fonts to load.

@ -28,27 +28,37 @@ public class AsyncResourceLoader extends Thread implements ResourceLoader, Destr
private volatile boolean mainLoopQueuing = true;
/**
* Create a resource loader.
*/
public AsyncResourceLoader()
{
super("Deferred loader");
}
@Override
public synchronized void init()
{
App.bus().subscribe(this); // FIXME bad
App.bus().subscribe(this);
setDaemon(true);
super.start();
}
/**
* True to queue resources that must load in rendering context to main loop.
* May cause lag at the beginning, but results in smoother performance later
* (better than load-on-demand)
*
* @param yes true to allow queuing
*/
public void enableMainLoopQueuing(boolean yes)
{
mainLoopQueuing = yes;
}
public AsyncResourceLoader()
{
super("Deferred loader");
}
@Override
public void loadResource(final DeferredResource resource)
{

@ -20,7 +20,7 @@ public interface ResourceLoader {
/**
* Initialize the loader (Join the bus, start a stread etc)
* Initialize the loader (Join the bus, start a thread etc)
*
* @param app app the loader works for. The event bus must already be
* initialized.

Loading…
Cancel
Save