Compare commits

..

2 Commits

Author SHA1 Message Date
DaniTheSkunk 661fc6d275 started writing node system 2022-10-11 02:15:14 +00:00
DaniTheSkunk e81af5e193 vec2f and vec2i conversion functions 2022-10-11 01:52:59 +00:00
7 changed files with 152 additions and 24 deletions

View File

@ -3,17 +3,20 @@ package com.danitheskunk.skunkworks;
import com.danitheskunk.skunkworks.gfx.Color; import com.danitheskunk.skunkworks.gfx.Color;
import com.danitheskunk.skunkworks.gfx.IRenderContext; import com.danitheskunk.skunkworks.gfx.IRenderContext;
import com.danitheskunk.skunkworks.gfx.font.IFont; import com.danitheskunk.skunkworks.gfx.font.IFont;
import com.danitheskunk.skunkworks.nodes.Node;
public abstract class BaseGame { public abstract class BaseGame {
protected Engine engine; protected Engine engine;
protected IWindow window; protected IWindow window;
protected IFont debugFont; protected IFont debugFont;
protected Node rootNode;
public BaseGame(Vec2i windowSize, String windowTitle) { public BaseGame(Vec2i windowSize, String windowTitle) {
this.engine = new Engine(); this.engine = new Engine();
this.window = engine.openWindow(windowSize, windowTitle); this.window = engine.openWindow(windowSize, windowTitle);
//todo: load from .jar //todo: load from .jar
this.debugFont = window.loadFontTileset("fonts/ega-8x14.png"); this.debugFont = window.loadFontTileset("fonts/ega-8x14.png");
this.rootNode = new Node();
init(); init();
} }
@ -23,6 +26,8 @@ public abstract class BaseGame {
//todo: frame rate control //todo: frame rate control
update(1000.0 / 60.0); update(1000.0 / 60.0);
var rc = window.renderStart(); var rc = window.renderStart();
renderPre(rc);
rootNode.render(rc);
render(rc); render(rc);
window.renderFinish(rc); window.renderFinish(rc);
} }
@ -32,8 +37,12 @@ public abstract class BaseGame {
} }
protected void renderPre(IRenderContext rc) {
}
protected void render(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) { protected void update(double delta) {

View File

@ -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();
}
}

View File

@ -1,6 +1,7 @@
package com.danitheskunk.skunkworks; package com.danitheskunk.skunkworks;
public final class Vec2f { public final class Vec2f {
public final static Vec2f ZERO = new Vec2f(0, 0);
private final double x, y; private final double x, y;
//constructors //constructors
@ -9,15 +10,6 @@ public final class Vec2f {
this.y = y; this.y = y;
} }
//getters and setters
public double getX() {
return x;
}
public double getY() {
return y;
}
//static functions //static functions
public static Vec2f add(Vec2f a, Vec2f b) { public static Vec2f add(Vec2f a, Vec2f b) {
return new Vec2f(a.x + b.x, a.y + b.y); return new Vec2f(a.x + b.x, a.y + b.y);
@ -34,4 +26,17 @@ public final class Vec2f {
public static Vec2f div(Vec2f a, double b) { public static Vec2f div(Vec2f a, double b) {
return new Vec2f(a.x / b, a.y / b); return new Vec2f(a.x / b, a.y / b);
} }
//getters and setters
public double getX() {
return x;
}
public double getY() {
return y;
}
public Vec2i toVec2i() {
return new Vec2i((int) x, (int) y);
}
} }

View File

@ -2,9 +2,8 @@ package com.danitheskunk.skunkworks;
@SuppressWarnings("SpellCheckingInspection") @SuppressWarnings("SpellCheckingInspection")
public final class Vec2i { public final class Vec2i {
private final int x, y;
public final static Vec2i ZERO = new Vec2i(0, 0); public final static Vec2i ZERO = new Vec2i(0, 0);
private final int x, y;
//constructors //constructors
@ -15,16 +14,6 @@ public final class Vec2i {
//getters and setters //getters and setters
public int getX() {
return x;
}
public int getY() {
return y;
}
//static functions
public static Vec2i add(Vec2i a, Vec2i b) { public static Vec2i add(Vec2i a, Vec2i b) {
return new Vec2i(a.x + b.x, a.y + b.y); return new Vec2i(a.x + b.x, a.y + b.y);
} }
@ -33,6 +22,8 @@ public final class Vec2i {
return new Vec2i(a.x + b.x + c.x, a.y + b.y + c.y); return new Vec2i(a.x + b.x + c.x, a.y + b.y + c.y);
} }
//static functions
public static Vec2i add(Vec2i a, Vec2i b, Vec2i c, Vec2i d) { public static Vec2i add(Vec2i a, Vec2i b, Vec2i c, Vec2i d) {
return new Vec2i(a.x + b.x + c.x + d.x, a.y + b.y + c.y + d.y); return new Vec2i(a.x + b.x + c.x + d.x, a.y + b.y + c.y + d.y);
} }
@ -44,6 +35,7 @@ public final class Vec2i {
public static Vec2i mul(Vec2i a, int b) { public static Vec2i mul(Vec2i a, int b) {
return new Vec2i(a.x * b, a.y * b); return new Vec2i(a.x * b, a.y * b);
} }
public static Vec2i mul(Vec2i a, Vec2i b) { public static Vec2i mul(Vec2i a, Vec2i b) {
return new Vec2i(a.x * b.x, a.y * b.y); return new Vec2i(a.x * b.x, a.y * b.y);
} }
@ -51,19 +43,36 @@ public final class Vec2i {
public static Vec2i div(Vec2i a, int b) { public static Vec2i div(Vec2i a, int b) {
return new Vec2i(a.x / b, a.y / b); return new Vec2i(a.x / b, a.y / b);
} }
public static Vec2i div(Vec2i a, Vec2i b) { public static Vec2i div(Vec2i a, Vec2i b) {
return new Vec2i(a.x / b.x, a.y / b.y); return new Vec2i(a.x / b.x, a.y / b.y);
} }
public static Vec2f divf(Vec2i a, double b) { public static Vec2f divf(Vec2i a, double b) {
return new Vec2f(a.x / b, a.y / b); return new Vec2f(a.x / b, a.y / b);
} }
public static Vec2f divf(Vec2i a, Vec2f b) { public static Vec2f divf(Vec2i a, Vec2f b) {
return new Vec2f(a.x / b.getX(), a.y / b.getY()); return new Vec2f(a.x / b.getX(), a.y / b.getY());
} }
public static Vec2f divf(Vec2i a, int b) { public static Vec2f divf(Vec2i a, int b) {
return new Vec2f(a.x / (double)b, a.y / (double)b); return new Vec2f(a.x / (double) b, a.y / (double) b);
} }
public static Vec2f divf(Vec2i a, Vec2i b) { public static Vec2f divf(Vec2i a, Vec2i b) {
return new Vec2f(a.x / (double)b.x, a.y / (double)b.y); return new Vec2f(a.x / (double) b.x, a.y / (double) b.y);
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Vec2f toVec2f() {
return new Vec2f((double) x, (double) y);
} }
} }

View File

@ -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<Node> {
protected List<Node> 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<Node> iterator() {
return children.iterator();
}
}

View File

@ -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);
}
}

BIN
demoassets/test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB