Preparation for textures, improved EventBus & events

This commit is contained in:
Ondřej Hruška
2014-04-02 18:43:07 +02:00
parent cf4e6e0a6d
commit 7f35d1d316
39 changed files with 598 additions and 394 deletions
+96 -121
View File
@@ -14,12 +14,12 @@ import mightypork.rogue.input.InputSystem;
import mightypork.rogue.input.KeyStroke;
import mightypork.rogue.sounds.SoundSystem;
import mightypork.rogue.tasks.TaskTakeScreenshot;
import mightypork.rogue.textures.TextureRegistry;
import mightypork.rogue.util.Utils;
import mightypork.utils.control.Destroyable;
import mightypork.utils.control.bus.EventBus;
import mightypork.utils.control.bus.events.DestroyEvent;
import mightypork.utils.control.bus.events.UpdateEvent;
import mightypork.utils.control.timing.TimerDelta;
import mightypork.utils.control.timing.UpdateEvent;
import mightypork.utils.control.timing.Updateable;
import mightypork.utils.logging.Log;
import mightypork.utils.logging.LogInstance;
@@ -31,15 +31,17 @@ import org.lwjgl.input.Keyboard;
*
* @author MightyPork
*/
public class App implements Destroyable, AppAccess {
public class App implements AppAccess {
/** instance pointer */
private static App inst;
private InputSystem input;
private SoundSystem sounds;
private DisplaySystem display;
private EventBus events;
// modules
private InputSystem inputSystem;
private SoundSystem soundSystem;
private DisplaySystem displaySystem;
private EventBus eventBus;
private TextureRegistry textureRegistry;
/** current screen */
private Screen screen;
@@ -50,6 +52,9 @@ public class App implements Destroyable, AppAccess {
/** Log instance; accessible as static via Log. */
private LogInstance log;
/** timer */
private TimerDelta timerRender;
/**
* @param args
@@ -71,6 +76,43 @@ public class App implements Destroyable, AppAccess {
}
/**
* Start the application
*/
private void start()
{
initialize();
mainLoop();
shutdown();
}
/**
* App main loop
*/
private void mainLoop()
{
screen = new TestLayeredScreen(this);
screen.setActive(true);
timerRender = new TimerDelta();
while (!displaySystem.isCloseRequested()) {
displaySystem.beginFrame();
eventBus.broadcast(new UpdateEvent(timerRender.getDelta()));
if (scheduledScreenshot) {
takeScreenshot();
scheduledScreenshot = false;
}
displaySystem.endFrame();
}
}
/**
* Handle a crash
*
@@ -87,7 +129,8 @@ public class App implements Destroyable, AppAccess {
@Override
public void shutdown()
{
destroy();
bus().broadcast(new DestroyEvent());
System.exit(0);
}
@@ -95,21 +138,33 @@ public class App implements Destroyable, AppAccess {
public void initialize()
{
Log.i("Initializing subsystems");
// lock working directory
initLock();
initBus();
initLogger();
initDisplay();
initSound();
initInput();
}
@Override
public void destroy()
{
if (sounds != null) sounds.destroy();
if (input != null) input.destroy();
if (display != null) display.destroy();
// setup logging
log = Log.create("runtime", Paths.LOGS, 10);
log.enable(Config.LOGGING_ENABLED);
log.enableSysout(Config.LOG_TO_STDOUT);
// event bus
eventBus = new EventBus();
// Subsystems
textureRegistry = new TextureRegistry(this);
displaySystem = new DisplaySystem(this);
displaySystem.createMainWindow(Const.WINDOW_W, Const.WINDOW_H, true, Config.START_IN_FS, Const.TITLEBAR);
displaySystem.setTargetFps(Const.FPS_RENDER);
soundSystem = new SoundSystem(this);
soundSystem.setMasterVolume(1);
inputSystem = new InputSystem(this);
setupGlobalKeystrokes();
// load resources
Resources.load(this);
}
@@ -170,34 +225,9 @@ public class App implements Destroyable, AppAccess {
/**
* initialize inputs
*/
private void initBus()
private void setupGlobalKeystrokes()
{
events = new EventBus();
events.subscribe(this);
events.createChannel(UpdateEvent.class, Updateable.class);
}
/**
* initialize sound system
*/
private void initSound()
{
sounds = new SoundSystem(this);
sounds.setMasterVolume(1);
}
/**
* initialize inputs
*/
private void initInput()
{
input = new InputSystem(this);
input.bindKeyStroke(new KeyStroke(Keyboard.KEY_F2), new Runnable() {
inputSystem.bindKeyStroke(new KeyStroke(Keyboard.KEY_F2), new Runnable() {
@Override
public void run()
@@ -207,17 +237,17 @@ public class App implements Destroyable, AppAccess {
}
});
input.bindKeyStroke(new KeyStroke(false, Keyboard.KEY_F11), new Runnable() {
inputSystem.bindKeyStroke(new KeyStroke(false, Keyboard.KEY_F11), new Runnable() {
@Override
public void run()
{
Log.f3("F11, toggling fullscreen.");
display.switchFullscreen();
displaySystem.switchFullscreen();
}
});
input.bindKeyStroke(new KeyStroke(Keyboard.KEY_LCONTROL, Keyboard.KEY_Q), new Runnable() {
inputSystem.bindKeyStroke(new KeyStroke(Keyboard.KEY_LCONTROL, Keyboard.KEY_Q), new Runnable() {
@Override
public void run()
@@ -229,75 +259,13 @@ public class App implements Destroyable, AppAccess {
}
/**
* initialize display
*/
private void initDisplay()
{
display = new DisplaySystem(this);
display.createMainWindow(Const.WINDOW_W, Const.WINDOW_H, true, Config.START_IN_FS, Const.TITLEBAR);
display.setTargetFps(Const.FPS_RENDER);
}
/**
* initialize main logger
*/
private void initLogger()
{
log = Log.create("runtime", Paths.LOGS, 10);
log.enable(Config.LOGGING_ENABLED);
log.enableSysout(Config.LOG_TO_STDOUT);
}
private void start()
{
initialize();
mainLoop();
shutdown();
}
/** timer */
private TimerDelta timerRender;
/**
* App main loop
*/
private void mainLoop()
{
screen = new TestLayeredScreen(this);
screen.setActive(true);
timerRender = new TimerDelta();
while (!display.isCloseRequested()) {
display.beginFrame();
events.broadcast(new UpdateEvent(timerRender.getDelta()));
if (scheduledScreenshot) {
takeScreenshot();
scheduledScreenshot = false;
}
display.endFrame();
}
}
/**
* Do take a screenshot
*/
private void takeScreenshot()
{
sounds.getEffect("gui.shutter").play(1);
Utils.runAsThread(new TaskTakeScreenshot(display));
soundSystem.getEffect("gui.shutter").play(1);
Utils.runAsThread(new TaskTakeScreenshot(displaySystem));
}
@@ -307,7 +275,7 @@ public class App implements Destroyable, AppAccess {
@Override
public SoundSystem snd()
{
return sounds;
return soundSystem;
}
@@ -317,7 +285,7 @@ public class App implements Destroyable, AppAccess {
@Override
public InputSystem input()
{
return input;
return inputSystem;
}
@@ -327,7 +295,7 @@ public class App implements Destroyable, AppAccess {
@Override
public DisplaySystem disp()
{
return display;
return displaySystem;
}
@@ -337,7 +305,14 @@ public class App implements Destroyable, AppAccess {
@Override
public EventBus bus()
{
return events;
return eventBus;
}
@Override
public TextureRegistry tx()
{
return textureRegistry;
}
}
+7
View File
@@ -4,6 +4,7 @@ package mightypork.rogue;
import mightypork.rogue.display.DisplaySystem;
import mightypork.rogue.input.InputSystem;
import mightypork.rogue.sounds.SoundSystem;
import mightypork.rogue.textures.TextureRegistry;
import mightypork.utils.control.bus.EventBus;
@@ -38,6 +39,12 @@ public interface AppAccess {
abstract EventBus bus();
/**
* @return texture registry
*/
abstract TextureRegistry tx();
/**
* Quit to OS<br>
* Destroy app & exit VM
+8
View File
@@ -4,6 +4,7 @@ package mightypork.rogue;
import mightypork.rogue.display.DisplaySystem;
import mightypork.rogue.input.InputSystem;
import mightypork.rogue.sounds.SoundSystem;
import mightypork.rogue.textures.TextureRegistry;
import mightypork.utils.control.bus.EventBus;
@@ -58,4 +59,11 @@ public class AppAdapter implements AppAccess {
app.shutdown();
}
@Override
public TextureRegistry tx()
{
return app.tx();
}
}
+33
View File
@@ -0,0 +1,33 @@
package mightypork.rogue;
public class Resources {
public static void load(AppAccess app)
{
loadSounds(app);
loadTextures(app);
loadFonts(app);
}
private static void loadFonts(AppAccess app)
{
}
private static void loadTextures(AppAccess app)
{
}
private static void loadSounds(AppAccess app)
{
}
}
+1 -3
View File
@@ -2,9 +2,9 @@ package mightypork.rogue.bus;
import mightypork.rogue.AppAccess;
import mightypork.utils.control.Destroyable;
import mightypork.utils.control.bus.clients.DelegatingClient;
import mightypork.utils.control.bus.clients.ToggleableClient;
import mightypork.utils.control.interf.Destroyable;
/**
@@ -27,8 +27,6 @@ public abstract class Subsystem extends ChildClient implements DelegatingClient,
{
deinit();
setListening(false);
bus().unsubscribe(this);
}
@@ -3,7 +3,6 @@ package mightypork.rogue.display;
import static org.lwjgl.opengl.GL11.*;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import mightypork.rogue.AppAccess;
@@ -4,7 +4,7 @@ package mightypork.rogue.display;
import java.util.LinkedList;
import mightypork.rogue.AppAccess;
import mightypork.utils.control.timing.Updateable;
import mightypork.utils.control.interf.Updateable;
public abstract class LayeredScreen extends Screen {
+1 -1
View File
@@ -8,7 +8,7 @@ import mightypork.rogue.bus.events.ScreenChangeEvent;
import mightypork.rogue.input.KeyBinder;
import mightypork.rogue.input.KeyBindingPool;
import mightypork.rogue.input.KeyStroke;
import mightypork.utils.control.timing.Updateable;
import mightypork.utils.control.interf.Updateable;
import mightypork.utils.math.constraints.ConstraintContext;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect;
@@ -3,7 +3,7 @@ package mightypork.rogue.display;
import mightypork.rogue.bus.ChildClient;
import mightypork.rogue.display.constraints.Renderable;
import mightypork.utils.control.timing.Updateable;
import mightypork.utils.control.interf.Updateable;
import mightypork.utils.math.constraints.ConstraintContext;
import mightypork.utils.math.coord.Rect;
@@ -5,11 +5,18 @@ import java.util.LinkedList;
import mightypork.rogue.AppAccess;
import mightypork.rogue.bus.ChildClient;
import mightypork.utils.control.bus.EventBus;
import mightypork.utils.math.constraints.ConstraintContext;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Rect;
/**
* Bag for {@link RenderableWithContext} elements with constraints.<br>
* Elements are exposed to {@link EventBus}.
*
* @author MightyPork
*/
public class ElementHolder extends ChildClient implements ConstraintContext, RenderableWithContext {
private LinkedList<RenderableWithContext> elements = new LinkedList<RenderableWithContext>();
@@ -67,8 +74,9 @@ public class ElementHolder extends ChildClient implements ConstraintContext, Ren
/**
* Add element to the holder.
*
* @param elem
* @param constraint
* @param elem element; it's context will be set to the constraint.
* @param constraint Constraint to be used for the element. It's context
* will be set to this {@link ElementHolder}
*/
public void add(RenderableWithContext elem, RectConstraint constraint)
{
@@ -0,0 +1,22 @@
package mightypork.rogue.display.screens;
import mightypork.rogue.AppAccess;
import mightypork.rogue.bus.Subsystem;
public class ScreenRegistry extends Subsystem {
public ScreenRegistry(AppAccess app) {
super(app);
}
@Override
protected void deinit()
{
// TODO Auto-generated method stub
}
}
@@ -1,186 +0,0 @@
package mightypork.rogue.display.screens;
import java.util.Random;
import mightypork.rogue.AppAccess;
import mightypork.rogue.bus.events.MouseButtonEvent;
import mightypork.rogue.display.Screen;
import mightypork.rogue.input.KeyStroke;
import mightypork.rogue.textures.Render;
import mightypork.utils.control.timing.animation.AnimDouble;
import mightypork.utils.control.timing.animation.AnimDoubleDeg;
import mightypork.utils.math.Polar;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.easing.Easing;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
public class ScreenTestAnimations extends Screen implements MouseButtonEvent.Listener {
public ScreenTestAnimations(AppAccess app) {
super(app);
bindKeyStroke(new KeyStroke(Keyboard.KEY_RIGHT), new Runnable() {
@Override
public void run()
{
for (AnimDouble a : anims) {
a.animate(0, 1, 1 + rand.nextDouble() * 1);
}
}
});
bindKeyStroke(new KeyStroke(Keyboard.KEY_LEFT), new Runnable() {
@Override
public void run()
{
for (AnimDouble a : anims) {
a.animate(1, 0, 1 + rand.nextDouble() * 1);
}
}
});
}
private Random rand = new Random();
private AnimDoubleDeg degAnim = new AnimDoubleDeg(0, Easing.ELASTIC_OUT);
//@formatter:off
private AnimDouble[] anims = new AnimDouble[] {
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
new AnimDouble(0, Easing.BOUNCE_OUT),
// new AnimDouble(0, Easing.NONE),
// new AnimDouble(0, Easing.LINEAR),
//
// new AnimDouble(0, Easing.QUADRATIC_IN),
// new AnimDouble(0, Easing.QUADRATIC_OUT),
// new AnimDouble(0, Easing.QUADRATIC_IN_OUT),
//
// new AnimDouble(0, Easing.CUBIC_IN),
// new AnimDouble(0, Easing.CUBIC_OUT),
// new AnimDouble(0, Easing.CUBIC_IN_OUT),
//
// new AnimDouble(0, Easing.QUADRATIC_IN),
// new AnimDouble(0, Easing.QUADRATIC_OUT),
// new AnimDouble(0, Easing.QUADRATIC_IN_OUT),
//
// new AnimDouble(0, Easing.QUINTIC_IN),
// new AnimDouble(0, Easing.QUINTIC_OUT),
// new AnimDouble(0, Easing.QUINTIC_IN_OUT),
//
// new AnimDouble(0, Easing.EXPO_IN),
// new AnimDouble(0, Easing.EXPO_OUT),
// new AnimDouble(0, Easing.EXPO_IN_OUT),
//
// new AnimDouble(0, Easing.SINE_IN),
// new AnimDouble(0, Easing.SINE_OUT),
// new AnimDouble(0, Easing.SINE_IN_OUT),
//
// new AnimDouble(0, Easing.CIRC_IN),
// new AnimDouble(0, Easing.CIRC_OUT),
// new AnimDouble(0, Easing.CIRC_IN_OUT),
//
// new AnimDouble(0, Easing.BOUNCE_IN),
// new AnimDouble(0, Easing.BOUNCE_OUT),
// new AnimDouble(0, Easing.BOUNCE_IN_OUT),
//
// new AnimDouble(0, Easing.BACK_IN),
// new AnimDouble(0, Easing.BACK_OUT),
// new AnimDouble(0, Easing.BACK_IN_OUT),
//
// new AnimDouble(0, Easing.ELASTIC_IN),
// new AnimDouble(0, Easing.ELASTIC_OUT),
// new AnimDouble(0, Easing.ELASTIC_IN_OUT),
};
//@formatter:on
@Override
protected void deinitScreen()
{
// no impl
}
@Override
protected void onScreenEnter()
{
// no impl
}
@Override
protected void onScreenLeave()
{
// no impl
}
@Override
protected void updateScreen(double delta)
{
degAnim.update(delta);
for (AnimDouble a : anims) {
a.update(delta);
}
}
@Override
protected void renderScreen()
{
double screenH = Display.getHeight();
double screenW = Display.getWidth();
double perBoxH = screenH / anims.length;
double padding = perBoxH * 0.1;
double boxSide = perBoxH - padding * 2;
for (int i = 0; i < anims.length; i++) {
AnimDouble a = anims[i];
Render.setColor(RGB.GREEN);
Render.quadSize(padding + a.getCurrentValue() * (screenW - perBoxH), screenH - perBoxH * i - perBoxH + padding, boxSide, boxSide);
}
Render.setColor(RGB.YELLOW);
Render.translate(new Coord(Display.getWidth() / 2, Display.getHeight() / 2));
Render.rotateZ(degAnim.getCurrentValue());
Render.quadSize(-10, -10, 20, 200);
}
@Override
public void receive(MouseButtonEvent event)
{
if (event.isDown()) {
Coord vec = disp().getSize().half().vecTo(event.getPos());
Polar p = Polar.fromCoord(vec);
degAnim.fadeTo(p.getAngleDeg() - 90, 1.5);
}
}
}
@@ -7,14 +7,14 @@ import java.util.Random;
import mightypork.rogue.display.constraints.RenderableWithContext;
import mightypork.rogue.textures.Render;
import mightypork.utils.control.timing.Updateable;
import mightypork.utils.control.timing.animation.AnimDouble;
import mightypork.utils.control.interf.Updateable;
import mightypork.utils.math.animation.AnimDouble;
import mightypork.utils.math.animation.Easing;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.constraints.ConstraintContext;
import mightypork.utils.math.constraints.NumConstraint;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Rect;
import mightypork.utils.math.easing.Easing;
public class BouncyBox implements RenderableWithContext, Updateable, ConstraintContext {
@@ -29,13 +29,19 @@ public class BouncyBox implements RenderableWithContext, Updateable, ConstraintC
public BouncyBox() {
// create box
NumConstraint side = c_height(this);
RectConstraint abox = c_box_sized(this, side, side);
// move
NumConstraint move_length = c_sub(c_width(this), side);
NumConstraint offset = c_mul(move_length, c_n(pos));
RectConstraint abox = c_sizedBox(this, offset, c_n(0), side, side);
NumConstraint margin = c_percent(side, c_n(10));
RectConstraint with_margin = c_shrink(abox, margin);
box = with_margin;
abox = c_move(abox, offset, c_n(0));
// add padding
abox = c_shrink(abox, c_percent(side, c_n(10)));
box = abox;
}
@@ -9,6 +9,7 @@ import java.util.List;
import mightypork.rogue.display.Screen;
import mightypork.rogue.display.ScreenLayer;
import mightypork.rogue.display.constraints.ElementHolder;
import mightypork.utils.math.constraints.RectConstraint;
public class LayerBouncyBoxes extends ScreenLayer {
@@ -20,7 +21,9 @@ public class LayerBouncyBoxes extends ScreenLayer {
public LayerBouncyBoxes(Screen screen) {
super(screen);
layout = new ElementHolder(screen, c_shrink(this, c_percent(c_height(this), c_n(8))));
RectConstraint holder_rect = c_shrink(this, c_percent(c_height(this), c_n(8)));
layout = new ElementHolder(screen, holder_rect);
addChildClient(layout);
for (int i = 0; i < 32; i++) {
@@ -0,0 +1,6 @@
package mightypork.rogue.display.screens.screenTextures;
public class ScreenTextureTest {
}
+1 -1
View File
@@ -6,7 +6,7 @@ import mightypork.rogue.bus.Subsystem;
import mightypork.rogue.bus.events.KeyboardEvent;
import mightypork.rogue.bus.events.MouseButtonEvent;
import mightypork.rogue.bus.events.MouseMotionEvent;
import mightypork.utils.control.timing.Updateable;
import mightypork.utils.control.interf.Updateable;
import mightypork.utils.math.coord.Coord;
import org.lwjgl.LWJGLException;
+1 -1
View File
@@ -1,7 +1,7 @@
package mightypork.rogue.sounds;
import mightypork.utils.control.Destroyable;
import mightypork.utils.control.interf.Destroyable;
import mightypork.utils.files.FileUtils;
import mightypork.utils.logging.Log;
import mightypork.utils.math.coord.Coord;
+2 -2
View File
@@ -1,9 +1,9 @@
package mightypork.rogue.sounds;
import mightypork.utils.control.interf.Updateable;
import mightypork.utils.control.timing.Pauseable;
import mightypork.utils.control.timing.Updateable;
import mightypork.utils.control.timing.animation.AnimDouble;
import mightypork.utils.math.animation.AnimDouble;
import mightypork.utils.objects.Mutable;
import org.lwjgl.openal.AL10;
+1 -1
View File
@@ -9,7 +9,7 @@ import java.util.Set;
import mightypork.rogue.AppAccess;
import mightypork.rogue.bus.Subsystem;
import mightypork.utils.control.timing.Updateable;
import mightypork.utils.control.interf.Updateable;
import mightypork.utils.logging.Log;
import mightypork.utils.math.Calc.Buffers;
import mightypork.utils.math.coord.Coord;
@@ -0,0 +1,140 @@
package mightypork.rogue.textures;
import mightypork.utils.math.coord.Rect;
import org.newdawn.slick.opengl.Texture;
public class MultiTexture implements Texture {
private Texture backingTexture;
private String resourcePath;
public MultiTexture(String resourcePath) {
this.resourcePath = resourcePath;
}
public TxQuad getQuad(Rect rect)
{
return new TxQuad(this, rect);
}
/**
* Attempt to load the texture.
*/
private void load()
{
if (backingTexture == null) {
backingTexture = Render.loadTexture(resourcePath);
}
}
@Override
public boolean hasAlpha()
{
load();
return backingTexture.hasAlpha();
}
@Override
public String getTextureRef()
{
load();
return backingTexture.getTextureRef();
}
@Override
public void bind()
{
load();
backingTexture.bind();
}
@Override
public int getImageHeight()
{
load();
return backingTexture.getImageHeight();
}
@Override
public int getImageWidth()
{
load();
return backingTexture.getImageWidth();
}
@Override
public float getHeight()
{
load();
return backingTexture.getHeight();
}
@Override
public float getWidth()
{
load();
return backingTexture.getWidth();
}
@Override
public int getTextureHeight()
{
load();
return backingTexture.getTextureHeight();
}
@Override
public int getTextureWidth()
{
load();
return backingTexture.getTextureWidth();
}
@Override
public void release()
{
if (backingTexture == null) return;
backingTexture.release();
}
@Override
public int getTextureID()
{
load();
return backingTexture.getTextureID();
}
@Override
public byte[] getTextureData()
{
load();
return backingTexture.getTextureData();
}
@Override
public void setTextureFilter(int textureFilter)
{
load();
backingTexture.setTextureFilter(textureFilter);
}
}
+10 -9
View File
@@ -13,6 +13,7 @@ import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureImpl;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
@@ -28,7 +29,6 @@ public class Render {
private static final Coord AXIS_Y = new Coord(0, 1, 0);
private static final Coord AXIS_Z = new Coord(0, 0, 1);
public static Texture lastBinded = null;
private static boolean inited = false;
@@ -998,7 +998,7 @@ public class Render {
} catch (IOException e) {
Log.e("Loading of texture " + resourcePath + " failed.", e);
throw new RuntimeException(e);
throw new RuntimeException("Could not load texture " + resourcePath + ".", e);
}
}
@@ -1012,11 +1012,12 @@ public class Render {
*/
public static void bindTexture(Texture texture) throws RuntimeException
{
if (texture != lastBinded) {
glBindTexture(GL_TEXTURE_2D, 0);
glBindTexture(GL_TEXTURE_2D, texture.getTextureID());
lastBinded = texture;
}
texture.bind();
// if (texture != lastBinded) {
// glBindTexture(GL_TEXTURE_2D, 0);
// glBindTexture(GL_TEXTURE_2D, texture.getTextureID());
// lastBinded = texture;
// }
}
@@ -1025,7 +1026,7 @@ public class Render {
*/
public static void unbindTexture()
{
glBindTexture(GL_TEXTURE_2D, 0);
lastBinded = null;
TextureImpl.bindNone();
// glBindTexture(GL_TEXTURE_2D, 0);
}
}
@@ -0,0 +1,124 @@
package mightypork.rogue.textures;
import java.util.HashMap;
import mightypork.rogue.AppAccess;
import mightypork.rogue.bus.Subsystem;
import mightypork.utils.control.interf.Destroyable;
import mightypork.utils.math.coord.Rect;
import org.newdawn.slick.opengl.Texture;
/**
* Texture loader and quad registry
*
* @author MightyPork
*/
public class TextureRegistry extends Subsystem implements Destroyable {
public TextureRegistry(AppAccess app) {
super(app);
}
private HashMap<String, MultiTexture> textures = new HashMap<String, MultiTexture>();
private HashMap<String, TxQuad> quads = new HashMap<String, TxQuad>();
private MultiTexture lastTx;
/**
* Load a {@link Texture} from resource
*
* @param key texture key
* @param resourcePath texture resource path
*/
public void loadTexture(String key, String resourcePath)
{
MultiTexture tx = new MultiTexture(resourcePath);
textures.put(key, tx);
lastTx = tx;
}
/**
* Create a {@link TxQuad} in a texture
*
* @param quadKey quad key
* @param textureKey texture key
* @param quad quad rectangle (absolute pixel coordinates) *
*/
public void makeQuad(String quadKey, String textureKey, Rect quad)
{
MultiTexture tx = textures.get(textureKey);
if (tx == null) throw new RuntimeException("Texture with key " + textureKey + " not defined!");
TxQuad txquad = tx.getQuad(quad);
quads.put(quadKey, txquad);
}
/**
* Create a {@link TxQuad} in the last loaded texture
*
* @param quadKey quad key
* @param quad quad rectangle (absolute pixel coordinates)
*/
public void makeQuad(String quadKey, Rect quad)
{
MultiTexture tx = lastTx;
if (tx == null) throw new RuntimeException("There's no texture loaded yet, can't define quads!");
TxQuad txquad = tx.getQuad(quad);
quads.put(quadKey, txquad);
}
/**
* Get a {@link TxQuad} for key
*
* @param key quad key
* @return the quad
*/
public TxQuad getQuad(String key)
{
TxQuad q = quads.get(key);
if (q == null) throw new RuntimeException("There's no quad called " + key + "!");
return q;
}
/**
* Get a loaded {@link Texture}
*
* @param key texture key
* @return the texture
*/
public Texture getTexture(String key)
{
Texture t = textures.get(key);
if (t == null) throw new RuntimeException("There's no texture called " + key + "!");
return t;
}
@Override
protected void deinit()
{
for (Texture tx : textures.values()) {
tx.release();
}
textures.clear();
quads.clear();
}
}
+14 -16
View File
@@ -38,6 +38,20 @@ public class TxQuad {
}
/**
* Make of coords
*
* @param tx texture
* @param x1 x1
* @param y1 y1
* @param x2 x2
* @param y2 y2
*/
public TxQuad(Texture tx, int x1, int y1, int x2, int y2) {
this(tx, new Rect(x1, y1, x2, y2));
}
/**
* @param tx Texture
* @param uvs Rect of texturwe UVs (pixels - from left top)
@@ -49,22 +63,6 @@ public class TxQuad {
}
/**
* Make of coords
*
* @param tx texture
* @param x1 x1
* @param y1 y1
* @param x2 x2
* @param y2 y2
*/
public TxQuad(Texture tx, int x1, int y1, int x2, int y2) {
this.tx = tx;
this.uvs = new Rect(x1, y1, x2, y2);
this.size = uvs.getSize();
}
public TxQuad copy()
{
return new TxQuad(tx, uvs);