Cleaned up coord, rect and constraint system.

This commit is contained in:
Ondřej Hruška
2014-04-12 21:13:16 +02:00
parent 65bfbbd16a
commit 4221d5430a
72 changed files with 2018 additions and 1788 deletions
+7 -11
View File
@@ -12,7 +12,6 @@ import mightypork.gamecore.control.bus.clients.RootBusNode;
import mightypork.gamecore.control.bus.events.ResourceLoadRequest;
import mightypork.gamecore.control.timing.Updateable;
import mightypork.utils.math.Calc.Buffers;
import mightypork.utils.math.coord.MutableCoord;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecMutable;
import mightypork.utils.math.coord.VecView;
@@ -32,9 +31,8 @@ public class SoundSystem extends RootBusNode implements Updateable {
private static final Vec INITIAL_LISTENER_POS = Vec.ZERO;
private static final int MAX_SOURCES = 256;
private static VecMutable listener = new MutableCoord(0, 0, 0);
private static boolean inited;
private static VecMutable listener = VecMutable.zero();
private static boolean soundSystemInited = false;
/**
@@ -45,10 +43,10 @@ public class SoundSystem extends RootBusNode implements Updateable {
public static void setListener(Vec pos)
{
listener.setTo(pos);
FloatBuffer buf3 = Buffers.alloc(3);
FloatBuffer buf6 = Buffers.alloc(6);
final FloatBuffer buf3 = Buffers.alloc(3);
final FloatBuffer buf6 = Buffers.alloc(6);
buf3.clear();
Buffers.fill(buf3, pos.xf(), pos.yf(), pos.zf());
Buffers.fill(buf3, (float) pos.x(), (float) pos.y(), (float) pos.z());
AL10.alListener(AL10.AL_POSITION, buf3);
buf3.clear();
Buffers.fill(buf3, 0, 0, 0);
@@ -56,7 +54,6 @@ public class SoundSystem extends RootBusNode implements Updateable {
buf6.clear();
Buffers.fill(buf6, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f);
AL10.alListener(AL10.AL_ORIENTATION, buf6);
buf3 = buf6 = null;
}
@@ -84,13 +81,12 @@ public class SoundSystem extends RootBusNode implements Updateable {
public SoundSystem(AppAccess app) {
super(app);
if (!inited) {
if (!soundSystemInited) {
SoundStore.get().setMaxSources(MAX_SOURCES);
SoundStore.get().init();
setListener(INITIAL_LISTENER_POS);
inited = true;
soundSystemInited = true;
}
}
@@ -293,7 +293,7 @@ final public class EventBus implements Destroyable {
@Override
public int compareTo(Delayed o)
{
return -Long.valueOf(o.getDelay(TimeUnit.MILLISECONDS)).compareTo(getDelay(TimeUnit.MILLISECONDS));
return Long.valueOf(getDelay(TimeUnit.MILLISECONDS)).compareTo(o.getDelay(TimeUnit.MILLISECONDS));
}
@@ -14,8 +14,8 @@ import mightypork.utils.math.coord.VecView;
@UnloggedEvent
public class MouseMotionEvent implements Event<MouseMotionEvent.Listener> {
private final Vec move;
private final Vec pos;
private final VecView move;
private final VecView pos;
/**
@@ -23,8 +23,8 @@ public class MouseMotionEvent implements Event<MouseMotionEvent.Listener> {
* @param move move vector
*/
public MouseMotionEvent(Vec pos, Vec move) {
this.move = move;
this.pos = pos;
this.move = move.value();
this.pos = pos.value();
}
@@ -33,7 +33,7 @@ public class MouseMotionEvent implements Event<MouseMotionEvent.Listener> {
*/
public VecView getMove()
{
return move.view();
return move;
}
@@ -42,7 +42,7 @@ public class MouseMotionEvent implements Event<MouseMotionEvent.Listener> {
*/
public VecView getPos()
{
return pos.view();
return pos;
}
@@ -23,7 +23,7 @@ public class TimerFps {
* @param fps target FPS
*/
public TimerFps(long fps) {
FRAME = Math.round(SECOND / fps);
FRAME = Math.round(SECOND / (double) fps);
lastFrame = System.nanoTime();
nextFrame = System.nanoTime() + FRAME;
@@ -1,9 +1,9 @@
package mightypork.gamecore.gui.components;
import mightypork.utils.math.constraints.PluggableContext;
import mightypork.utils.math.constraints.PluggableRect;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.rect.RectView;
import mightypork.utils.math.rect.RectValue;
/**
@@ -11,14 +11,14 @@ import mightypork.utils.math.rect.RectView;
*
* @author MightyPork
*/
public interface PluggableRenderable extends Renderable, PluggableContext {
public interface PluggableRenderable extends Renderable, PluggableRect {
@Override
void render();
@Override
RectView getRect();
RectValue getRect();
@Override
@@ -3,7 +3,7 @@ package mightypork.gamecore.gui.components;
import mightypork.utils.math.constraints.ContextAdapter;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.rect.RectView;
import mightypork.utils.math.rect.RectValue;
/**
@@ -18,7 +18,7 @@ public abstract class PluggableRenderer extends ContextAdapter implements Plugga
@Override
public RectView getRect()
public RectValue getRect()
{
return super.getRect();
}
@@ -10,7 +10,7 @@ import mightypork.gamecore.gui.components.PluggableRenderable;
import mightypork.gamecore.gui.components.PluggableRenderer;
import mightypork.gamecore.gui.components.Renderable;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.rect.RectView;
import mightypork.utils.math.rect.RectValue;
/**
@@ -60,7 +60,7 @@ public abstract class ElementHolder extends BusNode implements PluggableRenderab
@Override
public RectView getRect()
public RectValue getRect()
{
return context.getRect();
}
@@ -6,10 +6,9 @@ import mightypork.gamecore.render.fonts.FontRenderer;
import mightypork.gamecore.render.fonts.FontRenderer.Align;
import mightypork.gamecore.render.fonts.GLFont;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.MutableCoord;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecMutable;
import mightypork.utils.math.rect.RectView;
import mightypork.utils.math.rect.RectValue;
import mightypork.utils.string.StringProvider;
import mightypork.utils.string.StringProvider.StringWrapper;
@@ -30,7 +29,7 @@ public class TextPainter extends PluggableRenderer {
private boolean shadow;
private RGB shadowColor = RGB.BLACK;
private final VecMutable shadowOffset = new MutableCoord(1, 1);
private final VecMutable shadowOffset = VecMutable.make(1, 1);
/**
@@ -86,7 +85,7 @@ public class TextPainter extends PluggableRenderer {
if (text == null) return;
final String str = text.getString();
final RectView rect = getRect();
final RectValue rect = getRect();
if (shadow) {
font.draw(str, rect.move(shadowOffset), align, shadowColor);
@@ -12,7 +12,7 @@ import mightypork.gamecore.input.KeyStroke;
import mightypork.gamecore.render.Render;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.rect.RectView;
import mightypork.utils.math.rect.RectValue;
/**
@@ -104,7 +104,7 @@ public abstract class Screen extends AppSubModule implements Renderable, KeyBind
@Override
public RectView getRect()
public RectValue getRect()
{
return getDisplay().getRect();
}
@@ -10,7 +10,7 @@ import mightypork.gamecore.input.KeyStroke;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView;
import mightypork.utils.math.rect.RectView;
import mightypork.utils.math.rect.RectValue;
/**
@@ -26,7 +26,9 @@ public abstract class ScreenLayer extends AppSubModule implements Comparable<Scr
private final KeyBindingPool keybindings = new KeyBindingPool();
/** Mouse position constraint */
protected final VecView cMousePos = getInput().getMousePos();
/** Screen size constraint */
protected final VecView cScreenSize = getDisplay().getSize();
@@ -65,7 +67,7 @@ public abstract class ScreenLayer extends AppSubModule implements Comparable<Scr
@Override
public RectView getRect()
public RectValue getRect()
{
return screen.getRect();
}
+16 -13
View File
@@ -9,8 +9,6 @@ import mightypork.gamecore.control.bus.events.MouseMotionEvent;
import mightypork.gamecore.control.timing.Updateable;
import mightypork.rogue.events.ActionRequest;
import mightypork.rogue.events.ActionRequest.RequestType;
import mightypork.utils.math.coord.MutableCoord;
import mightypork.utils.math.coord.SynthCoord2D;
import mightypork.utils.math.coord.VecMutable;
import mightypork.utils.math.coord.VecView;
@@ -30,7 +28,8 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
// listeners
private final KeyBindingPool keybindings;
private final VecView mousePos = new SynthCoord2D() {
/** Current mouse position */
private final VecView mousePos = new VecView() {
@Override
public double x()
@@ -103,9 +102,13 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
keybindings.unbindKeyStroke(stroke);
}
// counters as fields to save memory.
private final VecMutable mouseMove = VecMutable.zero();
private final VecMutable mouseLastPos = VecMutable.zero();
@Override
public void update(double delta)
public synchronized void update(double delta)
{
// was destroyed
if (!Display.isCreated()) return;
@@ -114,17 +117,17 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
Display.processMessages();
final VecMutable moveSum = new MutableCoord();
final VecMutable lastPos = new MutableCoord();
// sum the moves
mouseMove.reset();
mouseLastPos.reset();
boolean wasMouse = false;
while (Mouse.next()) {
onMouseEvent(moveSum, lastPos);
onMouseEvent(mouseMove, mouseLastPos);
wasMouse = true;
}
if (wasMouse && !moveSum.isZero()) {
getEventBus().send(new MouseMotionEvent(lastPos, moveSum));
if (wasMouse && !mouseMove.isZero()) {
getEventBus().send(new MouseMotionEvent(mouseLastPos, mouseMove));
}
while (Keyboard.next()) {
@@ -142,8 +145,8 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
final int button = Mouse.getEventButton();
final boolean down = Mouse.getEventButtonState();
final VecMutable pos = new MutableCoord(Mouse.getEventX(), Mouse.getEventY());
final VecMutable move = new MutableCoord(Mouse.getEventDX(), Mouse.getEventDY());
final VecMutable pos = VecMutable.make(Mouse.getEventX(), Mouse.getEventY());
final VecMutable move = VecMutable.make(Mouse.getEventDX(), Mouse.getEventDY());
final int wheeld = Mouse.getEventDWheel();
@@ -152,7 +155,7 @@ public class InputSystem extends RootBusNode implements Updateable, KeyBinder {
move.mul(1, -1, 1);
if (button != -1 || wheeld != 0) {
getEventBus().send(new MouseButtonEvent(pos.copy(), button, down, wheeld));
getEventBus().send(new MouseButtonEvent(pos.value(), button, down, wheeld));
}
moveSum.add(move);
@@ -11,10 +11,8 @@ import mightypork.gamecore.control.bus.events.ScreenChangeEvent;
import mightypork.gamecore.control.timing.FpsMeter;
import mightypork.utils.logging.Log;
import mightypork.utils.math.constraints.RectConstraint;
import mightypork.utils.math.coord.SynthCoord2D;
import mightypork.utils.math.coord.VecView;
import mightypork.utils.math.rect.FixedRect;
import mightypork.utils.math.rect.RectView;
import mightypork.utils.math.rect.RectValue;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
@@ -33,7 +31,8 @@ public class DisplaySystem extends AppModule implements RectConstraint {
private int targetFps;
private FpsMeter fpsMeter;
private final VecView cScreenSize = new SynthCoord2D() {
/** Current screen size */
private final VecView screenSize = new VecView() {
@Override
public double y()
@@ -191,7 +190,7 @@ public class DisplaySystem extends AppModule implements RectConstraint {
*/
public VecView getSize()
{
return cScreenSize;
return screenSize;
}
@@ -200,7 +199,7 @@ public class DisplaySystem extends AppModule implements RectConstraint {
*/
public int getWidth()
{
return cScreenSize.xi();
return screenSize.xi();
}
@@ -209,7 +208,7 @@ public class DisplaySystem extends AppModule implements RectConstraint {
*/
public int getHeight()
{
return cScreenSize.yi();
return screenSize.yi();
}
@@ -240,9 +239,9 @@ public class DisplaySystem extends AppModule implements RectConstraint {
@Override
public RectView getRect()
public RectValue getRect()
{
return new FixedRect(getSize());
return RectValue.make(getSize());
}
+4 -4
View File
@@ -9,7 +9,6 @@ import mightypork.gamecore.render.textures.TxQuad;
import mightypork.utils.files.FileUtils;
import mightypork.utils.logging.Log;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.FixedCoord;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecView;
import mightypork.utils.math.rect.Rect;
@@ -28,9 +27,9 @@ import org.newdawn.slick.util.ResourceLoader;
*/
public class Render {
public static final Vec AXIS_X = new FixedCoord(1, 0, 0);
public static final Vec AXIS_Y = new FixedCoord(0, 1, 0);
public static final Vec AXIS_Z = new FixedCoord(0, 0, 1);
public static final VecView AXIS_X = VecView.make(1, 0, 0);
public static final VecView AXIS_Y = VecView.make(0, 1, 0);
public static final VecView AXIS_Z = VecView.make(0, 0, 1);
/**
@@ -541,6 +540,7 @@ public class Render {
/**
* Setup Ortho projection for 2D graphics
*
* @param size viewport size (screen size)
*/
public static void setupOrtho(VecView size)
@@ -4,6 +4,7 @@ package mightypork.gamecore.render.fonts;
import mightypork.gamecore.render.Render;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Vec;
import mightypork.utils.math.coord.VecMutable;
import mightypork.utils.math.coord.VecView;
import mightypork.utils.math.rect.Rect;
@@ -71,7 +72,7 @@ public class FontRenderer {
private double getScale(double height)
{
return height / font.getHeight();
return height / font.getLineHeight();
}
@@ -109,7 +110,7 @@ public class FontRenderer {
{
Render.pushMatrix();
Render.translate(pos.view().round());
Render.translate(pos.value().round());
Render.scaleXY(getScale(height));
font.draw(text, color);
@@ -192,20 +193,19 @@ public class FontRenderer {
final double w = getWidth(text, height);
final Vec start;
final VecMutable start = pos.mutable();
switch (align) {
case LEFT:
start = pos.view();
break;
case CENTER:
start = pos.view().sub(w / 2D, 0);
start.sub(w / 2D, 0);
break;
case RIGHT:
default:
start = pos.view().sub(w, 0);
start.sub(w, 0);
break;
}
@@ -33,7 +33,7 @@ public interface GLFont {
/**
* @return font height
*/
int getHeight();
int getLineHeight();
/**
@@ -46,5 +46,5 @@ public interface GLFont {
/**
* @return specified font size
*/
int getSize();
int getFontSize();
}
@@ -23,7 +23,6 @@ import mightypork.gamecore.render.fonts.GLFont;
import mightypork.gamecore.render.textures.FilterMode;
import mightypork.utils.logging.Log;
import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.FixedCoord;
import mightypork.utils.math.coord.VecView;
import org.lwjgl.BufferUtils;
@@ -376,14 +375,14 @@ public class CachedFont implements GLFont {
@Override
public int getHeight()
public int getLineHeight()
{
return fontHeight;
}
@Override
public int getSize()
public int getFontSize()
{
return fontSize;
}
@@ -425,7 +424,7 @@ public class CachedFont implements GLFont {
@Override
public VecView getNeededSpace(String text)
{
return new FixedCoord(getWidth(text), getHeight());
return VecView.make(getWidth(text), getLineHeight());
}
}
@@ -79,37 +79,31 @@ public class DeferredFont extends DeferredResource implements GLFont {
}
public void setFont(GLFont font)
{
this.font = font;
}
public void setSize(double size)
public synchronized void setSize(double size)
{
this.size = size;
}
public void setStyle(FontStyle style)
public synchronized void setStyle(FontStyle style)
{
this.style = style;
}
public void setChars(String chars)
public synchronized void setChars(String chars)
{
this.chars = chars;
}
public void setFilter(FilterMode filter)
public synchronized void setFilter(FilterMode filter)
{
this.filter = filter;
}
public void setAntialias(boolean antialias)
public synchronized void setAntialias(boolean antialias)
{
this.antialias = antialias;
}
@@ -182,20 +176,20 @@ public class DeferredFont extends DeferredResource implements GLFont {
* @return font height
*/
@Override
public int getHeight()
public int getLineHeight()
{
if (!ensureLoaded()) return 0;
return font.getHeight();
return font.getLineHeight();
}
@Override
public int getSize()
public int getFontSize()
{
if (!ensureLoaded()) return 0;
return font.getSize();
return font.getFontSize();
}
@@ -212,11 +206,4 @@ public class DeferredFont extends DeferredResource implements GLFont {
// this will have to suffice
font = null;
}
public void setFiltering(FilterMode filter)
{
this.filter = filter;
}
}
@@ -30,7 +30,7 @@ public class NullFont implements GLFont {
@Override
public int getHeight()
public int getLineHeight()
{
return 0;
}
@@ -44,7 +44,7 @@ public class NullFont implements GLFont {
@Override
public int getSize()
public int getFontSize()
{
return 0;
}
@@ -1,8 +1,8 @@
package mightypork.gamecore.render.textures;
import mightypork.utils.math.rect.FixedRect;
import mightypork.utils.math.rect.Rect;
import mightypork.utils.math.rect.RectValue;
import org.newdawn.slick.opengl.Texture;
@@ -65,7 +65,7 @@ public class TxQuad {
* @param y2 right bottom Y (0-1)
*/
public TxQuad(Texture tx, double x1, double y1, double x2, double y2) {
this(tx, new FixedRect(x1, y1, x2, y2));
this(tx, RectValue.make(x1, y1, x2, y2));
}