diff --git a/com/danitheskunk/skunkworks/BaseGame.java b/com/danitheskunk/skunkworks/BaseGame.java index c9555a1..f710d8f 100644 --- a/com/danitheskunk/skunkworks/BaseGame.java +++ b/com/danitheskunk/skunkworks/BaseGame.java @@ -3,17 +3,20 @@ package com.danitheskunk.skunkworks; import com.danitheskunk.skunkworks.gfx.Color; import com.danitheskunk.skunkworks.gfx.IRenderContext; import com.danitheskunk.skunkworks.gfx.font.IFont; +import com.danitheskunk.skunkworks.nodes.Node; public abstract class BaseGame { protected Engine engine; protected IWindow window; protected IFont debugFont; + protected Node rootNode; public BaseGame(Vec2i windowSize, String windowTitle) { this.engine = new Engine(); this.window = engine.openWindow(windowSize, windowTitle); //todo: load from .jar this.debugFont = window.loadFontTileset("fonts/ega-8x14.png"); + this.rootNode = new Node(); init(); } @@ -23,6 +26,8 @@ public abstract class BaseGame { //todo: frame rate control update(1000.0 / 60.0); var rc = window.renderStart(); + renderPre(rc); + rootNode.render(rc); render(rc); window.renderFinish(rc); } @@ -32,8 +37,12 @@ public abstract class BaseGame { } + protected void renderPre(IRenderContext rc) { + + } + protected void render(IRenderContext rc) { - rc.drawString(new Vec2i(8, 8), "Welcome to Skunkworks, please overide the render method to get started", debugFont, Color.LIGHT_GRAY); + //rc.drawString(new Vec2i(8, 8), "Welcome to Skunkworks, please overide the render method to get started", debugFont, Color.LIGHT_GRAY); } protected void update(double delta) { diff --git a/com/danitheskunk/skunkworks/TestNode.java b/com/danitheskunk/skunkworks/TestNode.java new file mode 100644 index 0000000..2c23087 --- /dev/null +++ b/com/danitheskunk/skunkworks/TestNode.java @@ -0,0 +1,19 @@ +package com.danitheskunk.skunkworks; + +import com.danitheskunk.skunkworks.nodes.NodeSprite; + +public class TestNode extends BaseGame { + NodeSprite sprite; + + public TestNode() { + super(new Vec2i(1280, 720), "Skunkworks"); + sprite = new NodeSprite(); + sprite.setTexture(window.loadTexture("demoassets/test.png")); + sprite.setPos(new Vec2f(100, 100)); + rootNode.add(sprite); + } + + public static void main(String[] args) { + new TestNode().run(); + } +} diff --git a/com/danitheskunk/skunkworks/Vec2f.java b/com/danitheskunk/skunkworks/Vec2f.java index 8a4e7c1..5d8b99c 100644 --- a/com/danitheskunk/skunkworks/Vec2f.java +++ b/com/danitheskunk/skunkworks/Vec2f.java @@ -1,6 +1,7 @@ package com.danitheskunk.skunkworks; public final class Vec2f { + public final static Vec2f ZERO = new Vec2f(0, 0); private final double x, y; //constructors diff --git a/com/danitheskunk/skunkworks/nodes/Node.java b/com/danitheskunk/skunkworks/nodes/Node.java new file mode 100644 index 0000000..0e15209 --- /dev/null +++ b/com/danitheskunk/skunkworks/nodes/Node.java @@ -0,0 +1,59 @@ +package com.danitheskunk.skunkworks.nodes; + +import com.danitheskunk.skunkworks.Vec2f; +import com.danitheskunk.skunkworks.gfx.IRenderContext; + +import java.sql.Array; +import java.util.*; + +//todo: figure out the whole translation position thingie + +public class Node implements Iterable { + protected List children; + protected Node parent; + protected Vec2f pos; + + public Node() { + children = new ArrayList<>(); + parent = null; + pos = Vec2f.ZERO; + } + + public Vec2f getPos() { + return pos; + } + + public void setPos(Vec2f pos) { + this.pos = pos; + } + + public void add(Node node) { + if(node.parent != null) { + throw new RuntimeException("Can't add node to two nodes"); + } + node.parent = this; + children.add(node); + } + + public void remove(Node node) { + node.parent = null; + children.remove(node); + } + + public void render(IRenderContext rc) { + for(var child : this) { + child.render(rc); + } + } + + public Vec2f getAbsolutePos() { + if(parent == null) + return pos; + return Vec2f.add(parent.getAbsolutePos(), pos); + } + + @Override + public Iterator iterator() { + return children.iterator(); + } +} diff --git a/com/danitheskunk/skunkworks/nodes/NodeSprite.java b/com/danitheskunk/skunkworks/nodes/NodeSprite.java new file mode 100644 index 0000000..5d8acce --- /dev/null +++ b/com/danitheskunk/skunkworks/nodes/NodeSprite.java @@ -0,0 +1,27 @@ +package com.danitheskunk.skunkworks.nodes; + +import com.danitheskunk.skunkworks.Vec2f; +import com.danitheskunk.skunkworks.gfx.IRenderContext; +import com.danitheskunk.skunkworks.gfx.ITexture; + +public class NodeSprite extends Node { + //todo: support animations + private ITexture texture; + + public NodeSprite() { + texture = null; + } + + public ITexture getTexture() { + return texture; + } + + public void setTexture(ITexture texture) { + this.texture = texture; + } + + @Override + public void render(IRenderContext rc) { + rc.drawTexture(getAbsolutePos().toVec2i(), texture); + } +} diff --git a/demoassets/test.png b/demoassets/test.png new file mode 100644 index 0000000..f00f1a3 Binary files /dev/null and b/demoassets/test.png differ