diff --git a/META-INF/MANIFEST.MF b/META-INF/MANIFEST.MF deleted file mode 100644 index 21b13d8..0000000 --- a/META-INF/MANIFEST.MF +++ /dev/null @@ -1,3 +0,0 @@ -Manifest-Version: 1.0 -Main-Class: Test - diff --git a/Test.java b/Test.java index 0324c27..177840b 100644 --- a/Test.java +++ b/Test.java @@ -9,6 +9,7 @@ import java.util.ArrayList; public class Test { public static void main(String[] args) { + /* var windows = Window.getAllVisible(); System.out.printf("%d Open Windows:\n", windows.size()); @@ -23,22 +24,23 @@ public class Test { Window.onKey(); Window.onNewWindow(); Window.messageLoop(); - /* + */ + var engine = new Engine(); var window = engine.openWindow(new Vec2i(1280, 720), "Skunkworks"); - var img = engine.loadImage("C:\\Users\\dani\\Videos\\Screenshot 2022-06-25 17-00-59.png"); - var tex2 = window.loadTexture("C:\\art\\pixel stuff.png"); - var tileset = window.loadTextureArray("C:\\stream\\coding\\rlc\\tilemap.png", new Vec2i(16, 16)); - var font = window.loadFontTileset("EGA8x14.png"); - var font2 = window.loadFontTTF("fonts\\LiberationSans-Regular.ttf", 16*8.f); - //img.drawImage(img2, Vec2i.ZERO); - var tex = window.loadTexture(img); - window.setDebug(true); + //var img = engine.loadImage("C:\\Users\\dani\\Videos\\Screenshot 2022-06-25 17-00-59.png"); + //var tex2 = window.loadTexture("C:\\art\\pixel stuff.png"); + //var tileset = window.loadTextureArray("C:\\stream\\coding\\rlc\\tilemap.png", new Vec2i(16, 16)); + //var font = window.loadFontTileset("EGA8x14.png"); + //var font2 = window.loadFontTTF("fonts\\LiberationSans-Regular.ttf", 16*8.f); + //var tex = window.loadTexture(img); + var slice = window.loadNineSlice("demoassets\\9slice-1.png"); + //window.setDebug(true); while(!window.shouldClose()) { window.tick(); var renderContext = window.renderStart(); - renderContext.drawTextureRectangle( + /*renderContext.drawTextureRectangle( new Recti(0, 0, 1280, 720), tex ); @@ -54,8 +56,11 @@ public class Test { renderContext.drawString(new Vec2i(100, 100), "hello world mew", font); renderContext.drawString(new Vec2i(710, 140), "hello world mew", font2); + + */ + renderContext.drawNineSlice(slice, new Recti(100, 100, 1080, 520)); window.renderFinish(renderContext); } - */ + } } diff --git a/com/danitheskunk/skunkworks/BaseWindow.java b/com/danitheskunk/skunkworks/BaseWindow.java index 418ade3..315abfc 100644 --- a/com/danitheskunk/skunkworks/BaseWindow.java +++ b/com/danitheskunk/skunkworks/BaseWindow.java @@ -31,4 +31,67 @@ abstract class BaseWindow implements IWindow { return new FontTTF(bytes, size, this); } + @Override + public NineSlice loadNineSlice(Image image) { + int x1 = -1; + int x2 = -1; + int y1 = -1; + int y2 = -1; + + for(int i = 1; i < image.getWidth(); ++i) { + if(image.getPixel(new Vec2i(i, 0)).getA() != 0) { + x1 = i; + break; + } + } + for(int i = x1; i < image.getWidth(); ++i) { + if(image.getPixel(new Vec2i(i, 0)).getA() == 0) { + x2 = i; + break; + } + } + for(int i = 1; i < image.getHeight(); ++i) { + if(image.getPixel(new Vec2i(0, i)).getA() != 0) { + y1 = i; + break; + } + } + for(int i = y1; i < image.getHeight(); ++i) { + if(image.getPixel(new Vec2i(i, 0)).getA() == 0) { + y2 = i; + break; + } + } + + if(x1 == -1 || x2 == -1 || y1 == -1 || y2 == -1) { + throw new RuntimeException("NineSlice error"); + } + var tl = image.getSubImage(new Recti(1, 1, x1-1, y1-1)); + var top = image.getSubImage(new Recti(x1, 1, x2-x1, y1-1)); + var tr = image.getSubImage(new Recti(x2, 1, image.getWidth()-x2, y1-1)); + var left = image.getSubImage(new Recti(1, y1, x1-1, y2-y1)); + var center = image.getSubImage(new Recti(x1, y1, x2-x1, y2-y1)); + var right = image.getSubImage(new Recti(x2, y1, image.getWidth()-x2, y2-y1)); + var bl = image.getSubImage(new Recti(1, y2, x1-1, image.getHeight()-y2)); + var bottom = image.getSubImage(new Recti(x1, y2, x2-x1, image.getHeight()-y2)); + var br = image.getSubImage(new Recti(x2, y2, image.getWidth()-x2, image.getHeight()-y2)); + var slice = new NineSlice( + loadTexture(tl), + loadTexture(tr), + loadTexture(bl), + loadTexture(br), + loadTexture(top), + loadTexture(right), + loadTexture(bottom), + loadTexture(left), + loadTexture(center) + ); + return slice; + } + + @Override + public NineSlice loadNineSlice(String path) { + return loadNineSlice(engine.loadImage(path)); + } + } diff --git a/com/danitheskunk/skunkworks/GLRenderContext.java b/com/danitheskunk/skunkworks/GLRenderContext.java index f3e5d30..ecd3d14 100644 --- a/com/danitheskunk/skunkworks/GLRenderContext.java +++ b/com/danitheskunk/skunkworks/GLRenderContext.java @@ -8,6 +8,17 @@ class GLRenderContext extends BaseRenderContext implements IRenderContext{ public GLRenderContext(GLTextureAtlas atlas) { this.atlas = atlas; } + + @Override + public void drawNineSlice(NineSlice slice, Recti rect) { + var centerWidth = Math.min(0, rect.getWidth() - slice.getLeft().getSize().getX() - slice.getRight().getSize().getX()); + var centerHeight = Math.min(0, rect.getHeight() - slice.getTop().getSize().getY() - slice.getBottom().getSize().getY()); + var pos = rect.getPos(); + var size = rect.getSize(); + + drawTexture(pos, slice.getTopLeft()); + } + @Override public void drawRectangle(Recti rect, Color color) { var tl = rect.getTopLeft(); @@ -34,6 +45,16 @@ class GLRenderContext extends BaseRenderContext implements IRenderContext{ glVertex2i(br.getX(), bl.getY()); } + @Override + public void drawTexture(Vec2i pos, ITexture texture) { + drawTextureRectangle(new Recti(pos, texture.getSize()), texture); + } + + @Override + public void drawTexture(Vec2i pos, ITexture texture, Color color) { + drawTextureRectangle(new Recti(pos, texture.getSize()), texture, color); + } + @Override public void drawTextureRectangle(Recti rect, ITexture texture) { drawTextureRectangle(rect, texture, Color.WHITE); diff --git a/com/danitheskunk/skunkworks/GLTextureAtlas.java b/com/danitheskunk/skunkworks/GLTextureAtlas.java index c164851..3b91edf 100644 --- a/com/danitheskunk/skunkworks/GLTextureAtlas.java +++ b/com/danitheskunk/skunkworks/GLTextureAtlas.java @@ -24,6 +24,8 @@ class GLTextureAtlas { glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + doubleAtlasSize(); //to init + } ITexture addTexture(Image img) { diff --git a/com/danitheskunk/skunkworks/GLWindow.java b/com/danitheskunk/skunkworks/GLWindow.java index 5bdc803..762e62f 100644 --- a/com/danitheskunk/skunkworks/GLWindow.java +++ b/com/danitheskunk/skunkworks/GLWindow.java @@ -100,6 +100,7 @@ class GLWindow extends BaseWindow { public IRenderContext renderStart() { textureAtlas.update(); glClearColor(0.1f, 0.2f, 0.3f, 1.0f); + glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBegin(GL_TRIANGLES); return renderContext; diff --git a/com/danitheskunk/skunkworks/IRenderContext.java b/com/danitheskunk/skunkworks/IRenderContext.java index 93a7286..ce255ad 100644 --- a/com/danitheskunk/skunkworks/IRenderContext.java +++ b/com/danitheskunk/skunkworks/IRenderContext.java @@ -1,8 +1,12 @@ package com.danitheskunk.skunkworks; public interface IRenderContext { + void drawNineSlice(NineSlice slice, Recti rect); //todo: add rounding mode void drawRectangle(Recti rect, Color color); void drawString(Vec2i pos, String string, IFont font); + void drawTexture(Vec2i pos, ITexture texture); + void drawTexture(Vec2i pos, ITexture texture, Color color); void drawTextureRectangle(Recti rect, ITexture texture); void drawTextureRectangle(Recti rect, ITexture texture, Color color); + //todo: drawTextureRectangleRepeat } diff --git a/com/danitheskunk/skunkworks/IWindow.java b/com/danitheskunk/skunkworks/IWindow.java index 1e89e77..8b8fe25 100644 --- a/com/danitheskunk/skunkworks/IWindow.java +++ b/com/danitheskunk/skunkworks/IWindow.java @@ -6,6 +6,8 @@ public interface IWindow { Engine getEngine(); IFont loadFontTileset(String path); IFont loadFontTTF(String path, float size); + NineSlice loadNineSlice(Image image); + NineSlice loadNineSlice(String path); ITexture loadTexture(Image image); ITexture loadTexture(String path); List loadTextureArray(Image image, Vec2i tileSize); diff --git a/com/danitheskunk/skunkworks/NineSlice.java b/com/danitheskunk/skunkworks/NineSlice.java new file mode 100644 index 0000000..17791e0 --- /dev/null +++ b/com/danitheskunk/skunkworks/NineSlice.java @@ -0,0 +1,60 @@ +package com.danitheskunk.skunkworks; + +public class NineSlice { + private ITexture topLeft; + private ITexture topRight; + private ITexture bottomLeft; + private ITexture bottomRight; + private ITexture top; + private ITexture right; + private ITexture bottom; + private ITexture left; + private ITexture center; + NineSlice(ITexture topLeft, ITexture topRight, ITexture bottomLeft, ITexture bottomRight, ITexture top, ITexture right, ITexture bottom, ITexture left, ITexture center) { + this.topLeft = topLeft; + this.topRight = topRight; + this.bottomLeft = bottomLeft; + this.bottomRight = bottomRight; + this.top = top; + this.right = right; + this.bottom = bottom; + this.left = left; + this.center = center; + } + + ITexture getTopLeft() { + return topLeft; + } + + ITexture getTopRight() { + return topRight; + } + + ITexture getBottomLeft() { + return bottomLeft; + } + + ITexture getBottomRight() { + return bottomRight; + } + + ITexture getTop() { + return top; + } + + ITexture getRight() { + return right; + } + + ITexture getBottom() { + return bottom; + } + + ITexture getLeft() { + return left; + } + + ITexture getCenter() { + return center; + } +} diff --git a/demoassets/9slice-1.png b/demoassets/9slice-1.png new file mode 100644 index 0000000..b1c8ba8 Binary files /dev/null and b/demoassets/9slice-1.png differ