stuff before modern opengl rewrite

This commit is contained in:
DaniTheSkunk 2022-10-05 00:36:19 +00:00
parent ed672439a4
commit 9c992aa27e
10 changed files with 169 additions and 14 deletions

View File

@ -1,3 +0,0 @@
Manifest-Version: 1.0
Main-Class: Test

View File

@ -9,6 +9,7 @@ import java.util.ArrayList;
public class Test { public class Test {
public static void main(String[] args) { public static void main(String[] args) {
/*
var windows = Window.getAllVisible(); var windows = Window.getAllVisible();
System.out.printf("%d Open Windows:\n", windows.size()); System.out.printf("%d Open Windows:\n", windows.size());
@ -23,22 +24,23 @@ public class Test {
Window.onKey(); Window.onKey();
Window.onNewWindow(); Window.onNewWindow();
Window.messageLoop(); Window.messageLoop();
/* */
var engine = new Engine(); var engine = new Engine();
var window = engine.openWindow(new Vec2i(1280, 720), "Skunkworks"); 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 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 tex2 = window.loadTexture("C:\\art\\pixel stuff.png");
var tileset = window.loadTextureArray("C:\\stream\\coding\\rlc\\tilemap.png", new Vec2i(16, 16)); //var tileset = window.loadTextureArray("C:\\stream\\coding\\rlc\\tilemap.png", new Vec2i(16, 16));
var font = window.loadFontTileset("EGA8x14.png"); //var font = window.loadFontTileset("EGA8x14.png");
var font2 = window.loadFontTTF("fonts\\LiberationSans-Regular.ttf", 16*8.f); //var font2 = window.loadFontTTF("fonts\\LiberationSans-Regular.ttf", 16*8.f);
//img.drawImage(img2, Vec2i.ZERO); //var tex = window.loadTexture(img);
var tex = window.loadTexture(img); var slice = window.loadNineSlice("demoassets\\9slice-1.png");
window.setDebug(true); //window.setDebug(true);
while(!window.shouldClose()) { while(!window.shouldClose()) {
window.tick(); window.tick();
var renderContext = window.renderStart(); var renderContext = window.renderStart();
renderContext.drawTextureRectangle( /*renderContext.drawTextureRectangle(
new Recti(0, 0, 1280, 720), new Recti(0, 0, 1280, 720),
tex tex
); );
@ -54,8 +56,11 @@ public class Test {
renderContext.drawString(new Vec2i(100, 100), "hello world mew", font); renderContext.drawString(new Vec2i(100, 100), "hello world mew", font);
renderContext.drawString(new Vec2i(710, 140), "hello world mew", font2); renderContext.drawString(new Vec2i(710, 140), "hello world mew", font2);
*/
renderContext.drawNineSlice(slice, new Recti(100, 100, 1080, 520));
window.renderFinish(renderContext); window.renderFinish(renderContext);
} }
*/
} }
} }

View File

@ -31,4 +31,67 @@ abstract class BaseWindow implements IWindow {
return new FontTTF(bytes, size, this); 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));
}
} }

View File

@ -8,6 +8,17 @@ class GLRenderContext extends BaseRenderContext implements IRenderContext{
public GLRenderContext(GLTextureAtlas atlas) { public GLRenderContext(GLTextureAtlas atlas) {
this.atlas = 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 @Override
public void drawRectangle(Recti rect, Color color) { public void drawRectangle(Recti rect, Color color) {
var tl = rect.getTopLeft(); var tl = rect.getTopLeft();
@ -34,6 +45,16 @@ class GLRenderContext extends BaseRenderContext implements IRenderContext{
glVertex2i(br.getX(), bl.getY()); 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 @Override
public void drawTextureRectangle(Recti rect, ITexture texture) { public void drawTextureRectangle(Recti rect, ITexture texture) {
drawTextureRectangle(rect, texture, Color.WHITE); drawTextureRectangle(rect, texture, Color.WHITE);

View File

@ -24,6 +24,8 @@ class GLTextureAtlas {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
doubleAtlasSize(); //to init
} }
ITexture addTexture(Image img) { ITexture addTexture(Image img) {

View File

@ -100,6 +100,7 @@ class GLWindow extends BaseWindow {
public IRenderContext renderStart() { public IRenderContext renderStart() {
textureAtlas.update(); textureAtlas.update();
glClearColor(0.1f, 0.2f, 0.3f, 1.0f); 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); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES); glBegin(GL_TRIANGLES);
return renderContext; return renderContext;

View File

@ -1,8 +1,12 @@
package com.danitheskunk.skunkworks; package com.danitheskunk.skunkworks;
public interface IRenderContext { public interface IRenderContext {
void drawNineSlice(NineSlice slice, Recti rect); //todo: add rounding mode
void drawRectangle(Recti rect, Color color); void drawRectangle(Recti rect, Color color);
void drawString(Vec2i pos, String string, IFont font); 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);
void drawTextureRectangle(Recti rect, ITexture texture, Color color); void drawTextureRectangle(Recti rect, ITexture texture, Color color);
//todo: drawTextureRectangleRepeat
} }

View File

@ -6,6 +6,8 @@ public interface IWindow {
Engine getEngine(); Engine getEngine();
IFont loadFontTileset(String path); IFont loadFontTileset(String path);
IFont loadFontTTF(String path, float size); IFont loadFontTTF(String path, float size);
NineSlice loadNineSlice(Image image);
NineSlice loadNineSlice(String path);
ITexture loadTexture(Image image); ITexture loadTexture(Image image);
ITexture loadTexture(String path); ITexture loadTexture(String path);
List<ITexture> loadTextureArray(Image image, Vec2i tileSize); List<ITexture> loadTextureArray(Image image, Vec2i tileSize);

View File

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

BIN
demoassets/9slice-1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 B