Huge refactoring, cleanup, constraints

This commit is contained in:
Ondřej Hruška
2014-04-02 11:47:24 +02:00
parent acc8dffad7
commit cf4e6e0a6d
25 changed files with 249 additions and 208 deletions
+4 -4
View File
@@ -16,7 +16,7 @@ import mightypork.rogue.sounds.SoundSystem;
import mightypork.rogue.tasks.TaskTakeScreenshot;
import mightypork.rogue.util.Utils;
import mightypork.utils.control.Destroyable;
import mightypork.utils.control.bus.MessageBus;
import mightypork.utils.control.bus.EventBus;
import mightypork.utils.control.timing.TimerDelta;
import mightypork.utils.control.timing.UpdateEvent;
import mightypork.utils.control.timing.Updateable;
@@ -39,7 +39,7 @@ public class App implements Destroyable, AppAccess {
private InputSystem input;
private SoundSystem sounds;
private DisplaySystem display;
private MessageBus events;
private EventBus events;
/** current screen */
private Screen screen;
@@ -172,7 +172,7 @@ public class App implements Destroyable, AppAccess {
*/
private void initBus()
{
events = new MessageBus();
events = new EventBus();
events.subscribe(this);
events.createChannel(UpdateEvent.class, Updateable.class);
@@ -335,7 +335,7 @@ public class App implements Destroyable, AppAccess {
* @return event bus
*/
@Override
public MessageBus bus()
public EventBus bus()
{
return events;
}
+2 -2
View File
@@ -4,7 +4,7 @@ package mightypork.rogue;
import mightypork.rogue.display.DisplaySystem;
import mightypork.rogue.input.InputSystem;
import mightypork.rogue.sounds.SoundSystem;
import mightypork.utils.control.bus.MessageBus;
import mightypork.utils.control.bus.EventBus;
/**
@@ -35,7 +35,7 @@ public interface AppAccess {
/**
* @return event bus
*/
abstract MessageBus bus();
abstract EventBus bus();
/**
+2 -2
View File
@@ -4,7 +4,7 @@ package mightypork.rogue;
import mightypork.rogue.display.DisplaySystem;
import mightypork.rogue.input.InputSystem;
import mightypork.rogue.sounds.SoundSystem;
import mightypork.utils.control.bus.MessageBus;
import mightypork.utils.control.bus.EventBus;
/**
@@ -46,7 +46,7 @@ public class AppAdapter implements AppAccess {
@Override
public final MessageBus bus()
public final EventBus bus()
{
return app.bus();
}
+3 -3
View File
@@ -7,13 +7,13 @@ import java.util.Set;
import mightypork.rogue.AppAccess;
import mightypork.rogue.AppAdapter;
import mightypork.utils.control.bus.MessageBus;
import mightypork.utils.control.bus.EventBus;
import mightypork.utils.control.bus.clients.DelegatingClient;
import mightypork.utils.control.bus.clients.ToggleableClient;
/**
* Client that can be attached to the {@link MessageBus}, or added as a child
* Client that can be attached to the {@link EventBus}, or added as a child
* client to another {@link DelegatingClient}
*
* @author MightyPork
@@ -51,7 +51,7 @@ public class ChildClient extends AppAdapter implements DelegatingClient, Togglea
/**
* Add a child subscriber to the {@link MessageBus}.<br>
* Add a child subscriber to the {@link EventBus}.<br>
*
* @param client
*/
+21 -16
View File
@@ -9,8 +9,8 @@ import java.nio.ByteBuffer;
import mightypork.rogue.AppAccess;
import mightypork.rogue.bus.Subsystem;
import mightypork.rogue.bus.events.ScreenChangeEvent;
import mightypork.rogue.display.constraints.RenderContext;
import mightypork.utils.logging.Log;
import mightypork.utils.math.constraints.ConstraintContext;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect;
@@ -20,7 +20,7 @@ import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class DisplaySystem extends Subsystem implements RenderContext {
public class DisplaySystem extends Subsystem implements ConstraintContext {
private DisplayMode windowDisplayMode;
private int targetFps;
@@ -109,7 +109,7 @@ public class DisplaySystem extends Subsystem implements RenderContext {
}
public BufferedImage takeScreenshot()
public Screenshot takeScreenshot()
{
glReadBuffer(GL_FRONT);
int width = Display.getDisplayMode().getWidth();
@@ -119,20 +119,9 @@ public class DisplaySystem extends Subsystem implements RenderContext {
ByteBuffer buffer = BufferUtils.createByteBuffer(width * height * bpp);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Screenshot sc = new Screenshot(width, height, bpp, buffer);
// convert to a buffered image
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int i = (x + (width * y)) * bpp;
int r = buffer.get(i) & 0xFF;
int g = buffer.get(i + 1) & 0xFF;
int b = buffer.get(i + 2) & 0xFF;
image.setRGB(x, height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
}
}
return image;
return sc;
}
@@ -197,4 +186,20 @@ public class DisplaySystem extends Subsystem implements RenderContext {
{
return new Rect(getSize());
}
public static class Screenshot {
public int width;
public int height;
public int bpp;
public ByteBuffer bytes;
public Screenshot(int width, int height, int bpp, ByteBuffer buffer) {
this.width = width;
this.height = height;
this.bpp = bpp;
this.bytes = buffer;
}
}
}
+2 -4
View File
@@ -5,11 +5,11 @@ import static org.lwjgl.opengl.GL11.*;
import mightypork.rogue.AppAccess;
import mightypork.rogue.bus.Subsystem;
import mightypork.rogue.bus.events.ScreenChangeEvent;
import mightypork.rogue.display.constraints.RenderContext;
import mightypork.rogue.input.KeyBinder;
import mightypork.rogue.input.KeyBindingPool;
import mightypork.rogue.input.KeyStroke;
import mightypork.utils.control.timing.Updateable;
import mightypork.utils.math.constraints.ConstraintContext;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect;
@@ -21,14 +21,12 @@ import mightypork.utils.math.coord.Rect;
*
* @author MightyPork
*/
public abstract class Screen extends Subsystem implements Updateable, KeyBinder, RenderContext, ScreenChangeEvent.Listener {
public abstract class Screen extends Subsystem implements Updateable, KeyBinder, ConstraintContext, ScreenChangeEvent.Listener {
private final KeyBindingPool keybindings = new KeyBindingPool();
private boolean active;
private boolean inited = false;
public Screen(AppAccess app) {
super(app);
+2 -12
View File
@@ -2,9 +2,9 @@ package mightypork.rogue.display;
import mightypork.rogue.bus.ChildClient;
import mightypork.rogue.display.constraints.RenderContext;
import mightypork.rogue.display.constraints.Renderable;
import mightypork.utils.control.timing.Updateable;
import mightypork.utils.math.constraints.ConstraintContext;
import mightypork.utils.math.coord.Rect;
@@ -13,7 +13,7 @@ import mightypork.utils.math.coord.Rect;
*
* @author MightyPork
*/
public abstract class ScreenLayer extends ChildClient implements Renderable, Updateable, RenderContext {
public abstract class ScreenLayer extends ChildClient implements Renderable, Updateable, ConstraintContext {
private Screen screen;
@@ -39,16 +39,6 @@ public abstract class ScreenLayer extends ChildClient implements Renderable, Upd
}
/**
* UNSUPPORTED
*/
@Override
public final void setContext(RenderContext context)
{
throw new UnsupportedOperationException("ScreenLayer uses screen as it's context.");
}
@Override
public Rect getRect()
{
@@ -1,58 +0,0 @@
package mightypork.rogue.display.constraints;
import mightypork.utils.math.coord.Coord;
/**
* A constraint based on a given {@link RenderContext}
*
* @author MightyPork
*/
public abstract class BaseConstraint implements WithContext {
protected RenderContext context = null;
public BaseConstraint(RenderContext context) {
this.context = context;
}
@Override
public void setContext(RenderContext context)
{
this.context = context;
}
/**
* @return context
*/
public RenderContext getContext()
{
return context;
}
/**
* @return context rect origin
*/
protected Coord getOrigin()
{
if (context == null) return Coord.zero();
return context.getRect().getOrigin();
}
/**
* @return context rect size
*/
protected Coord getSize()
{
if (context == null) return Coord.zero();
return context.getRect().getSize();
}
}
@@ -1,394 +0,0 @@
package mightypork.rogue.display.constraints;
import mightypork.utils.control.timing.animation.AnimDouble;
import mightypork.utils.math.coord.Coord;
import mightypork.utils.math.coord.Rect;
/**
* Constraint factory.<br>
* Import statically for best experience.
*
* @author MightyPork
*/
public class ConstraintFactory {
public static NumConstraint c_min(final NumConstraint a, final NumConstraint b)
{
return new NumConstraint(null) {
@Override
public double getValue()
{
return Math.min(a.getValue(), b.getValue());
}
};
}
public static NumConstraint c_max(final NumConstraint a, final NumConstraint b)
{
return new NumConstraint(null) {
@Override
public double getValue()
{
return Math.max(a.getValue(), b.getValue());
}
};
}
public static NumConstraint c_abs(final NumConstraint a)
{
return new NumConstraint(null) {
@Override
public double getValue()
{
return Math.abs(a.getValue());
}
};
}
public static NumConstraint c_round(final NumConstraint a)
{
return new NumConstraint(null) {
@Override
public double getValue()
{
return Math.round(a.getValue());
}
};
}
public static RectConstraint c_round(RenderContext context)
{
return new RectConstraint(context) {
@Override
public Rect getRect()
{
return context.getRect().round();
}
};
}
public static NumConstraint c_ceil(final NumConstraint a)
{
return new NumConstraint(null) {
@Override
public double getValue()
{
return Math.ceil(a.getValue());
}
};
}
public static NumConstraint c_floor(final NumConstraint a)
{
return new NumConstraint(null) {
@Override
public double getValue()
{
return Math.floor(a.getValue());
}
};
}
public static NumConstraint c_neg(final NumConstraint a)
{
return new NumConstraint(null) {
@Override
public double getValue()
{
return -a.getValue();
}
};
}
public static NumConstraint c_add(final NumConstraint a, final NumConstraint b)
{
return new NumConstraint(null) {
@Override
public double getValue()
{
return a.getValue() + b.getValue();
}
};
}
public static NumConstraint c_sub(final NumConstraint a, final NumConstraint b)
{
return new NumConstraint(null) {
@Override
public double getValue()
{
return a.getValue() - b.getValue();
}
};
}
public static NumConstraint c_mul(final NumConstraint a, final NumConstraint b)
{
return new NumConstraint(null) {
@Override
public double getValue()
{
return a.getValue() * b.getValue();
}
};
}
public static NumConstraint c_div(final NumConstraint a, final NumConstraint b)
{
return new NumConstraint(null) {
@Override
public double getValue()
{
return a.getValue() / b.getValue();
}
};
}
public static NumConstraint c_percent(final NumConstraint whole, final NumConstraint percent)
{
return new NumConstraint(null) {
@Override
public double getValue()
{
return whole.getValue() * (percent.getValue() / 100);
}
};
}
public static NumConstraint c_n(final double a)
{
return new NumConstraint(null) {
@Override
public double getValue()
{
return a;
}
};
}
public static NumConstraint c_n(final AnimDouble a)
{
return new NumConstraint(null) {
@Override
public double getValue()
{
return a.getCurrentValue();
}
};
}
public static NumConstraint c_width(final RenderContext context)
{
return new NumConstraint(context) {
@Override
public double getValue()
{
return getSize().x;
}
};
}
public static NumConstraint c_height(final RenderContext context)
{
return new NumConstraint(context) {
@Override
public double getValue()
{
return getSize().y;
}
};
}
public static RectConstraint c_row(RenderContext context, final int rows, final int index)
{
return new RectConstraint(context) {
@Override
public Rect getRect()
{
double height = context.getRect().getSize().y;
double perRow = height / rows;
return Rect.fromSize(getOrigin().add(0, perRow * (rows - index - 1)), getSize().setY(perRow));
}
};
}
public static RectConstraint c_column(RenderContext context, final int columns, final int index)
{
return new RectConstraint(context) {
@Override
public Rect getRect()
{
double width = context.getRect().getSize().x;
double perCol = width / columns;
return Rect.fromSize(getOrigin().add(perCol * index, 0), getSize().setX(perCol));
}
};
}
public static RectConstraint c_shrink(RenderContext context, NumConstraint shrink)
{
return c_shrink(context, shrink, shrink, shrink, shrink);
}
public static RectConstraint c_shrink(RenderContext context, NumConstraint horiz, NumConstraint vert)
{
return c_shrink(context, horiz, vert, horiz, vert);
}
public static RectConstraint c_shrink(RenderContext context, final NumConstraint left, final NumConstraint top, final NumConstraint right, final NumConstraint bottom)
{
return new RectConstraint(context) {
@Override
public Rect getRect()
{
return context.getRect().shrink(left.getValue(), top.getValue(), right.getValue(), bottom.getValue());
}
};
}
public static RectConstraint c_grow(RenderContext context, NumConstraint grow)
{
return c_grow(context, grow, grow, grow, grow);
}
public static RectConstraint c_grow(RenderContext context, NumConstraint horiz, NumConstraint vert)
{
return c_grow(context, horiz, vert, horiz, vert);
}
public static RectConstraint c_grow(RenderContext context, final NumConstraint left, final NumConstraint top, final NumConstraint right, final NumConstraint bottom)
{
return new RectConstraint(context) {
@Override
public Rect getRect()
{
return context.getRect().grow(left.getValue(), top.getValue(), right.getValue(), bottom.getValue());
}
};
}
public static RectConstraint c_tile(RenderContext context, final int rows, final int cols, final int left, final int top)
{
return new RectConstraint(context) {
@Override
public Rect getRect()
{
double height = getSize().y;
double width = getSize().y;
double perRow = height / rows;
double perCol = width / cols;
return Rect.fromSize(getOrigin().add(perCol * left, perRow * (rows - top - 1)), perCol, perRow);
}
};
}
public static RectConstraint c_sizedBox(RenderContext context, final NumConstraint left, final NumConstraint bottom, final NumConstraint width, final NumConstraint height)
{
return new RectConstraint(context) {
@Override
public Rect getRect()
{
Coord origin = getOrigin();
//@formatter:off
return Rect.fromSize(
origin.x + left.getValue(),
origin.y + bottom.getValue(),
width.getValue(),
height.getValue()
);
//@formatter:on
}
};
}
public static RectConstraint c_posBox(RenderContext context, final NumConstraint left, final NumConstraint bottom, final NumConstraint right, final NumConstraint top)
{
return new RectConstraint(context) {
@Override
public Rect getRect()
{
Coord origin = getOrigin();
//@formatter:off
return new Rect(
origin.x + left.getValue(),
origin.y + bottom.getValue(),
origin.x + right.getValue(),
origin.y + top.getValue()
);
//@formatter:on
}
};
}
public static RectConstraint c_move(RenderContext context, final NumConstraint x, final NumConstraint y)
{
return new RectConstraint(context) {
@Override
public Rect getRect()
{
return context.getRect().add(x.getValue(), y.getValue());
}
};
}
}
@@ -5,13 +5,15 @@ import java.util.LinkedList;
import mightypork.rogue.AppAccess;
import mightypork.rogue.bus.ChildClient;
import mightypork.utils.math.constraints.ConstraintContext;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Rect;
public class ElementHolder extends ChildClient implements RenderContext, Renderable {
public class ElementHolder extends ChildClient implements ConstraintContext, RenderableWithContext {
private LinkedList<Renderable> elements = new LinkedList<Renderable>();
private RenderContext context;
private LinkedList<RenderableWithContext> elements = new LinkedList<RenderableWithContext>();
private ConstraintContext context;
public ElementHolder(AppAccess app) {
@@ -19,64 +21,19 @@ public class ElementHolder extends ChildClient implements RenderContext, Rendera
}
public ElementHolder(AppAccess app, RenderContext context) {
public ElementHolder(AppAccess app, ConstraintContext context) {
super(app);
this.context = context;
}
@Override
public void setContext(RenderContext context)
public void setContext(ConstraintContext context)
{
this.context = context;
}
/**
* Add element to the holder.
*
* @param elem
*/
public void add(Renderable elem)
{
if (elem == null) return;
elem.setContext(this);
elements.add(elem);
addChildClient(elem);
}
/**
* Add element to the holder.
*
* @param elem
* @param constraint
*/
public void add(Renderable elem, RectConstraint constraint)
{
if (elem == null) return;
constraint.setContext(this);
elem.setContext(constraint);
elements.add(elem);
addChildClient(elem);
}
/**
* Remove element from the holder
*
* @param elem
*/
public void remove(Renderable elem)
{
if (elem == null) return;
elements.remove(elem);
removeChildClient(elem);
}
@Override
public void render()
{
@@ -92,4 +49,49 @@ public class ElementHolder extends ChildClient implements RenderContext, Rendera
return context.getRect();
}
/**
* Add element to the holder.
*
* @param elem
*/
public void add(RenderableWithContext elem)
{
if (elem == null) return;
elem.setContext(this);
elements.add(elem);
addChildClient(elem);
}
/**
* Add element to the holder.
*
* @param elem
* @param constraint
*/
public void add(RenderableWithContext elem, RectConstraint constraint)
{
if (elem == null) return;
constraint.setContext(this);
elem.setContext(constraint);
elements.add(elem);
addChildClient(elem);
}
/**
* Remove element from the holder
*
* @param elem
*/
public void remove(RenderableWithContext elem)
{
if (elem == null) return;
elements.remove(elem);
removeChildClient(elem);
}
}
@@ -1,18 +0,0 @@
package mightypork.rogue.display.constraints;
/**
* Constraint that provides size
*
* @author MightyPork
*/
public abstract class NumConstraint extends BaseConstraint {
public NumConstraint(RenderContext context) {
super(context);
}
public abstract double getValue();
}
@@ -1,22 +0,0 @@
package mightypork.rogue.display.constraints;
import mightypork.utils.math.coord.Rect;
/**
* Constraint that provides a rect (RenderContext)
*
* @author MightyPork
*/
public abstract class RectConstraint extends BaseConstraint implements RenderContext {
public RectConstraint(RenderContext context) {
super(context);
}
@Override
public abstract Rect getRect();
}
@@ -1,18 +0,0 @@
package mightypork.rogue.display.constraints;
import mightypork.utils.math.coord.Rect;
/**
* Bounding box provider - context for {@link RectConstraint}
*
* @author MightyPork
*/
public interface RenderContext {
/**
* @return bounding rectangle
*/
public Rect getRect();
}
@@ -1,12 +1,16 @@
package mightypork.rogue.display.constraints;
public interface Renderable extends WithContext {
/**
* Can be rendered
*
* @author MightyPork
*/
public interface Renderable {
/**
* Render on screen
*/
public void render();
@Override
public void setContext(RenderContext context);
}
@@ -0,0 +1,21 @@
package mightypork.rogue.display.constraints;
import mightypork.utils.math.constraints.ConstraintContext;
import mightypork.utils.math.constraints.SettableContext;
/**
* {@link Renderable} with {@link SettableContext}
*
* @author MightyPork
*/
public interface RenderableWithContext extends Renderable, SettableContext {
@Override
public void render();
@Override
public void setContext(ConstraintContext context);
}
@@ -1,12 +0,0 @@
package mightypork.rogue.display.constraints;
public interface WithContext {
/**
* Assign a context
*
* @param context
*/
public void setContext(RenderContext context);
}
@@ -1,27 +1,27 @@
package mightypork.rogue.display.screens.screenBouncy;
import static mightypork.rogue.display.constraints.ConstraintFactory.*;
import static mightypork.utils.math.constraints.ConstraintFactory.*;
import java.util.Random;
import mightypork.rogue.display.constraints.NumConstraint;
import mightypork.rogue.display.constraints.RectConstraint;
import mightypork.rogue.display.constraints.RenderContext;
import mightypork.rogue.display.constraints.Renderable;
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.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 Renderable, Updateable, RenderContext {
public class BouncyBox implements RenderableWithContext, Updateable, ConstraintContext {
private Random rand = new Random();
private RenderContext context;
private ConstraintContext context;
private RectConstraint box;
@@ -30,17 +30,11 @@ public class BouncyBox implements Renderable, Updateable, RenderContext {
public BouncyBox() {
NumConstraint side = c_height(this);
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;
}
@@ -60,7 +54,7 @@ public class BouncyBox implements Renderable, Updateable, RenderContext {
@Override
public void setContext(RenderContext context)
public void setContext(ConstraintContext context)
{
this.context = context;
}
@@ -1,7 +1,7 @@
package mightypork.rogue.display.screens.screenBouncy;
import static mightypork.rogue.display.constraints.ConstraintFactory.*;
import static mightypork.utils.math.constraints.ConstraintFactory.*;
import java.util.ArrayList;
import java.util.List;
@@ -12,22 +12,37 @@ import javax.imageio.ImageIO;
import mightypork.rogue.Paths;
import mightypork.rogue.display.DisplaySystem;
import mightypork.rogue.display.DisplaySystem.Screenshot;
import mightypork.utils.logging.Log;
public class TaskTakeScreenshot implements Runnable {
private BufferedImage image;
private Screenshot scr;
public TaskTakeScreenshot(DisplaySystem disp) {
this.image = disp.takeScreenshot();
scr = disp.takeScreenshot();
}
@Override
public void run()
{
BufferedImage image = new BufferedImage(scr.width, scr.height, BufferedImage.TYPE_INT_RGB);
// convert to a buffered image
for (int x = 0; x < scr.width; x++) {
for (int y = 0; y < scr.height; y++) {
int i = (x + (scr.width * y)) * scr.bpp;
int r = scr.bytes.get(i) & 0xFF;
int g = scr.bytes.get(i + 1) & 0xFF;
int b = scr.bytes.get(i + 2) & 0xFF;
image.setRGB(x, scr.height - (y + 1), (0xFF << 24) | (r << 16) | (g << 8) | b);
}
}
String fname = getUniqueScreenshotName();
// generate unique filename