refactored, and changed gl backend to use shaders
This commit is contained in:
parent
9c992aa27e
commit
721185c413
23
Test.java
23
Test.java
|
@ -1,11 +1,4 @@
|
||||||
import com.danitheskunk.skunkworks.*;
|
import com.danitheskunk.skunkworks.*;
|
||||||
import com.danitheskunk.skunkworks.windows.*;
|
|
||||||
import com.sun.jna.Pointer;
|
|
||||||
import com.sun.jna.platform.win32.User32;
|
|
||||||
import com.sun.jna.platform.win32.WinDef;
|
|
||||||
import com.sun.jna.platform.win32.WinUser;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class Test {
|
public class Test {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -28,19 +21,19 @@ public class Test {
|
||||||
|
|
||||||
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);
|
||||||
//var tex = window.loadTexture(img);
|
var tex = window.loadTexture(img);
|
||||||
var slice = window.loadNineSlice("demoassets\\9slice-1.png");
|
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
|
||||||
);
|
);
|
||||||
|
@ -57,7 +50,7 @@ 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));
|
renderContext.drawNineSlice(slice, new Recti(100, 100, 1080, 520));
|
||||||
window.renderFinish(renderContext);
|
window.renderFinish(renderContext);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks;
|
||||||
|
|
||||||
import org.lwjgl.stb.STBTTFontinfo;
|
import com.danitheskunk.skunkworks.gfx.Image;
|
||||||
import static org.lwjgl.stb.STBTruetype.*;
|
import com.danitheskunk.skunkworks.gfx.NineSlice;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.font.FontTTF;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.font.FontTileset;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.font.IFont;
|
||||||
|
|
||||||
abstract class BaseWindow implements IWindow {
|
abstract public class BaseWindow implements IWindow {
|
||||||
protected final Engine engine;
|
protected final Engine engine;
|
||||||
|
|
||||||
BaseWindow(Engine engine) {
|
public BaseWindow(Engine engine) {
|
||||||
this.engine = engine;
|
this.engine = engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks;
|
||||||
|
|
||||||
|
import com.danitheskunk.skunkworks.backends.gl.Window;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.GraphicsBackend;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.Image;
|
||||||
import org.lwjgl.BufferUtils;
|
import org.lwjgl.BufferUtils;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
import static com.danitheskunk.skunkworks.GraphicsBackend.*;
|
import static com.danitheskunk.skunkworks.gfx.GraphicsBackend.*;
|
||||||
|
|
||||||
public class Engine {
|
public class Engine {
|
||||||
private final GraphicsBackend graphicsBackend;
|
private final GraphicsBackend graphicsBackend;
|
||||||
|
@ -40,7 +43,7 @@ public class Engine {
|
||||||
|
|
||||||
public IWindow openWindow(Vec2i size, String title) {
|
public IWindow openWindow(Vec2i size, String title) {
|
||||||
return switch(graphicsBackend) {
|
return switch(graphicsBackend) {
|
||||||
case OPENGL -> new GLWindow(size, title, this);
|
case OPENGL -> new Window(size, title, this);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
package com.danitheskunk.skunkworks;
|
|
||||||
|
|
||||||
public interface ITexture {
|
|
||||||
Vec2i getSize();
|
|
||||||
}
|
|
|
@ -1,5 +1,11 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks;
|
||||||
|
|
||||||
|
import com.danitheskunk.skunkworks.gfx.IRenderContext;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.ITexture;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.Image;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.NineSlice;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.font.IFont;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface IWindow {
|
public interface IWindow {
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
package com.danitheskunk.skunkworks.backends.gl;
|
||||||
|
|
||||||
|
import static org.lwjgl.opengl.GL46.*;
|
||||||
|
|
||||||
|
public class Program {
|
||||||
|
Shader vertex;
|
||||||
|
Shader fragment;
|
||||||
|
int program;
|
||||||
|
public Program(String vertexSource, String fragmentSource) {
|
||||||
|
vertex = new Shader(vertexSource, GL_VERTEX_SHADER);
|
||||||
|
fragment = new Shader(fragmentSource, GL_FRAGMENT_SHADER);
|
||||||
|
program = glCreateProgram();
|
||||||
|
glAttachShader(program, vertex.getShader());
|
||||||
|
glAttachShader(program, fragment.getShader());
|
||||||
|
|
||||||
|
glLinkProgram(program);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void use() {
|
||||||
|
glUseProgram(program);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getAttribLocation(String attrib) {
|
||||||
|
return glGetAttribLocation(program, attrib);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getUniformLocation(String attrib) {
|
||||||
|
return glGetUniformLocation(program, attrib);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Shader getVertex() {
|
||||||
|
return vertex;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Shader getFragment() {
|
||||||
|
return fragment;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,12 +1,17 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks.backends.gl;
|
||||||
|
|
||||||
import static org.lwjgl.opengl.GL11.*;
|
import com.danitheskunk.skunkworks.*;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.*;
|
||||||
|
|
||||||
class GLRenderContext extends BaseRenderContext implements IRenderContext{
|
import static org.lwjgl.opengl.GL46.*;
|
||||||
private final GLTextureAtlas atlas;
|
|
||||||
|
|
||||||
public GLRenderContext(GLTextureAtlas atlas) {
|
class RenderContext extends BaseRenderContext implements IRenderContext {
|
||||||
|
private final TextureAtlas atlas;
|
||||||
|
private final int texCoordIndex;
|
||||||
|
|
||||||
|
public RenderContext(TextureAtlas atlas, int texCoordIndex) {
|
||||||
this.atlas = atlas;
|
this.atlas = atlas;
|
||||||
|
this.texCoordIndex = texCoordIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -62,7 +67,7 @@ class GLRenderContext extends BaseRenderContext implements IRenderContext{
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawTextureRectangle(Recti rect, ITexture texture, Color color) {
|
public void drawTextureRectangle(Recti rect, ITexture texture, Color color) {
|
||||||
var tex = (GLTexture)texture;
|
var tex = (Texture)texture;
|
||||||
|
|
||||||
var tl = rect.getTopLeft();
|
var tl = rect.getTopLeft();
|
||||||
var tr = rect.getTopRight();
|
var tr = rect.getTopRight();
|
||||||
|
@ -82,18 +87,18 @@ class GLRenderContext extends BaseRenderContext implements IRenderContext{
|
||||||
);
|
);
|
||||||
|
|
||||||
//counterclockwise triangles
|
//counterclockwise triangles
|
||||||
glTexCoord2d(tbl.getX(), tbl.getY());
|
glVertexAttrib2d(texCoordIndex, tbl.getX(), tbl.getY());
|
||||||
glVertex2i(bl.getX(), bl.getY());
|
glVertex2i(bl.getX(), bl.getY());
|
||||||
glTexCoord2d(ttr.getX(), ttr.getY());
|
glVertexAttrib2d(texCoordIndex, ttr.getX(), ttr.getY());
|
||||||
glVertex2i(tr.getX(), tr.getY());
|
glVertex2i(tr.getX(), tr.getY());
|
||||||
glTexCoord2d(ttl.getX(), ttl.getY());
|
glVertexAttrib2d(texCoordIndex, ttl.getX(), ttl.getY());
|
||||||
glVertex2i(tl.getX(), tl.getY());
|
glVertex2i(tl.getX(), tl.getY());
|
||||||
|
|
||||||
glTexCoord2d(ttr.getX(), ttr.getY());
|
glVertexAttrib2d(texCoordIndex, ttr.getX(), ttr.getY());
|
||||||
glVertex2i(tr.getX(), tr.getY());
|
glVertex2i(tr.getX(), tr.getY());
|
||||||
glTexCoord2d(tbl.getX(), tbl.getY());
|
glVertexAttrib2d(texCoordIndex, tbl.getX(), tbl.getY());
|
||||||
glVertex2i(bl.getX(), bl.getY());
|
glVertex2i(bl.getX(), bl.getY());
|
||||||
glTexCoord2d(tbr.getX(), tbr.getY());
|
glVertexAttrib2d(texCoordIndex, tbr.getX(), tbr.getY());
|
||||||
glVertex2i(br.getX(), bl.getY());
|
glVertex2i(br.getX(), bl.getY());
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package com.danitheskunk.skunkworks.backends.gl;
|
||||||
|
|
||||||
|
import static org.lwjgl.opengl.GL46.*;
|
||||||
|
|
||||||
|
public class Shader {
|
||||||
|
private int shader;
|
||||||
|
private int type;
|
||||||
|
|
||||||
|
public Shader(String source, int type) {
|
||||||
|
this.type = type;
|
||||||
|
|
||||||
|
shader = glCreateShader(type);
|
||||||
|
glShaderSource(shader, source);
|
||||||
|
glCompileShader(shader);
|
||||||
|
|
||||||
|
int[] ret = {0};
|
||||||
|
glGetShaderiv(shader, GL_COMPILE_STATUS, ret);
|
||||||
|
if(ret[0] == GL_FALSE) {
|
||||||
|
var errmsg = glGetShaderInfoLog(shader);
|
||||||
|
glDeleteShader(shader);
|
||||||
|
throw new RuntimeException("Shader error:\n" + errmsg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getShader() {
|
||||||
|
return shader;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,12 +1,17 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks.backends.gl;
|
||||||
|
|
||||||
class GLTexture implements ITexture {
|
import com.danitheskunk.skunkworks.gfx.ITexture;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.Image;
|
||||||
|
import com.danitheskunk.skunkworks.Recti;
|
||||||
|
import com.danitheskunk.skunkworks.Vec2i;
|
||||||
|
|
||||||
|
class Texture implements ITexture {
|
||||||
private Recti texArea;
|
private Recti texArea;
|
||||||
|
|
||||||
|
|
||||||
private final Image img; //for re-blitting onto texture atlas
|
private final Image img; //for re-blitting onto texture atlas
|
||||||
|
|
||||||
GLTexture(Recti texArea, Image img) {
|
Texture(Recti texArea, Image img) {
|
||||||
this.texArea = texArea;
|
this.texArea = texArea;
|
||||||
this.img = img;
|
this.img = img;
|
||||||
}
|
}
|
|
@ -1,5 +1,9 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks.backends.gl;
|
||||||
|
|
||||||
|
import com.danitheskunk.skunkworks.gfx.ITexture;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.Image;
|
||||||
|
import com.danitheskunk.skunkworks.Recti;
|
||||||
|
import com.danitheskunk.skunkworks.Vec2i;
|
||||||
import org.lwjgl.BufferUtils;
|
import org.lwjgl.BufferUtils;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -8,13 +12,13 @@ import java.util.List;
|
||||||
|
|
||||||
import static org.lwjgl.opengl.GL11.*;
|
import static org.lwjgl.opengl.GL11.*;
|
||||||
|
|
||||||
class GLTextureAtlas {
|
class TextureAtlas {
|
||||||
private GLTexture atlasTexture; //for debugging
|
private Texture atlasTexture; //for debugging
|
||||||
private Image img;
|
private Image img;
|
||||||
private boolean shouldUpdate;
|
private boolean shouldUpdate;
|
||||||
private final List<GLTexture> textures;
|
private final List<Texture> textures;
|
||||||
|
|
||||||
GLTextureAtlas() {
|
TextureAtlas() {
|
||||||
img = new Image(new Vec2i(32, 32));
|
img = new Image(new Vec2i(32, 32));
|
||||||
int textureID = glGenTextures();
|
int textureID = glGenTextures();
|
||||||
textures = new ArrayList<>();
|
textures = new ArrayList<>();
|
||||||
|
@ -31,7 +35,7 @@ class GLTextureAtlas {
|
||||||
ITexture addTexture(Image img) {
|
ITexture addTexture(Image img) {
|
||||||
//todo: do the actual texture stuff
|
//todo: do the actual texture stuff
|
||||||
//this.img = img;
|
//this.img = img;
|
||||||
var texture = new GLTexture(new Recti(new Vec2i(0, 0), img.getSize()), img);
|
var texture = new Texture(new Recti(new Vec2i(0, 0), img.getSize()), img);
|
||||||
textures.add(texture);
|
textures.add(texture);
|
||||||
shouldUpdate = true;
|
shouldUpdate = true;
|
||||||
return texture;
|
return texture;
|
||||||
|
@ -40,7 +44,7 @@ class GLTextureAtlas {
|
||||||
void doubleAtlasSize() {
|
void doubleAtlasSize() {
|
||||||
img = new Image(Vec2i.mul(img.getSize(), 2));
|
img = new Image(Vec2i.mul(img.getSize(), 2));
|
||||||
System.out.printf("Resized atlas to %dx%d\n", img.getSize().getX(), img.getSize().getY());
|
System.out.printf("Resized atlas to %dx%d\n", img.getSize().getX(), img.getSize().getY());
|
||||||
atlasTexture = new GLTexture(new Recti(Vec2i.ZERO, img.getSize()), img);
|
atlasTexture = new Texture(new Recti(Vec2i.ZERO, img.getSize()), img);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
|
@ -114,7 +118,7 @@ class GLTextureAtlas {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public GLTexture getAtlasTexture() {
|
public Texture getAtlasTexture() {
|
||||||
return atlasTexture;
|
return atlasTexture;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,9 +126,9 @@ class GLTextureAtlas {
|
||||||
return img.getSize();
|
return img.getSize();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class TextureHeightComparator implements Comparator<GLTexture> {
|
private static class TextureHeightComparator implements Comparator<Texture> {
|
||||||
@Override
|
@Override
|
||||||
public int compare(GLTexture o1, GLTexture o2) {
|
public int compare(Texture o1, Texture o2) {
|
||||||
return o1.getImg().getHeight() - o2.getImg().getHeight();
|
return o1.getImg().getHeight() - o2.getImg().getHeight();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,5 +1,9 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks.backends.gl;
|
||||||
|
|
||||||
|
import com.danitheskunk.skunkworks.*;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.IRenderContext;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.ITexture;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.Image;
|
||||||
import org.lwjgl.glfw.GLFWErrorCallback;
|
import org.lwjgl.glfw.GLFWErrorCallback;
|
||||||
import org.lwjgl.opengl.GL;
|
import org.lwjgl.opengl.GL;
|
||||||
import org.lwjgl.opengl.GL11;
|
import org.lwjgl.opengl.GL11;
|
||||||
|
@ -8,18 +12,19 @@ import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.lwjgl.glfw.GLFW.*;
|
import static org.lwjgl.glfw.GLFW.*;
|
||||||
import static org.lwjgl.opengl.GL11.*;
|
import static org.lwjgl.opengl.GL46.*;
|
||||||
import static org.lwjgl.system.MemoryUtil.NULL;
|
import static org.lwjgl.system.MemoryUtil.NULL;
|
||||||
|
|
||||||
class GLWindow extends BaseWindow {
|
public class Window extends BaseWindow {
|
||||||
private boolean debug;
|
private boolean debug;
|
||||||
private final GLRenderContext renderContext;
|
private final Program program;
|
||||||
|
private final RenderContext renderContext;
|
||||||
private boolean shouldClose;
|
private boolean shouldClose;
|
||||||
private final Vec2i size;
|
private final Vec2i size;
|
||||||
private final GLTextureAtlas textureAtlas;
|
private final TextureAtlas textureAtlas;
|
||||||
private final long window;
|
private final long window;
|
||||||
|
|
||||||
public GLWindow(Vec2i size, String title, Engine engine) {
|
public Window(Vec2i size, String title, Engine engine) {
|
||||||
super(engine);
|
super(engine);
|
||||||
GLFWErrorCallback.createPrint(System.err).set();
|
GLFWErrorCallback.createPrint(System.err).set();
|
||||||
if(!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW");
|
if(!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW");
|
||||||
|
@ -44,8 +49,11 @@ class GLWindow extends BaseWindow {
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
glEnable(GL_BLEND);
|
glEnable(GL_BLEND);
|
||||||
|
|
||||||
textureAtlas = new GLTextureAtlas();
|
textureAtlas = new TextureAtlas();
|
||||||
renderContext = new GLRenderContext(textureAtlas);
|
program = new Program(vertexSource, fragmentSource);
|
||||||
|
program.use();
|
||||||
|
renderContext = new RenderContext(textureAtlas, program.getAttribLocation("texCoord"));
|
||||||
|
glProgramUniform2f(program.program, program.getUniformLocation("windowSize"), size.getX(), size.getY());
|
||||||
|
|
||||||
shouldClose = false;
|
shouldClose = false;
|
||||||
|
|
||||||
|
@ -102,6 +110,7 @@ class GLWindow extends BaseWindow {
|
||||||
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);
|
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);
|
||||||
|
program.use();
|
||||||
glBegin(GL_TRIANGLES);
|
glBegin(GL_TRIANGLES);
|
||||||
return renderContext;
|
return renderContext;
|
||||||
}
|
}
|
||||||
|
@ -126,4 +135,27 @@ class GLWindow extends BaseWindow {
|
||||||
|
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String vertexSource = """
|
||||||
|
#version 450
|
||||||
|
layout(location = 0) in vec2 pos;
|
||||||
|
layout(location = 1) in vec2 texCoord;
|
||||||
|
layout(location = 2) uniform vec2 windowSize;
|
||||||
|
layout(location = 1) out vec2 out_texCoord;
|
||||||
|
void main() {
|
||||||
|
gl_Position = vec4(pos / windowSize * vec2(2.0f, -2.0f) + vec2(-1.0f, 1.0f), 0.0f, 1.0f);
|
||||||
|
out_texCoord = texCoord;
|
||||||
|
}
|
||||||
|
""";
|
||||||
|
|
||||||
|
private static String fragmentSource = """
|
||||||
|
#version 450
|
||||||
|
layout(location = 1) in vec2 texCoord;
|
||||||
|
layout(binding = 0) uniform sampler2D tex;
|
||||||
|
out vec4 color;
|
||||||
|
void main() {
|
||||||
|
//color = vec4(texCoord.x, texCoord.y, 0.7f, 1.0f);
|
||||||
|
color = texture(tex, texCoord);
|
||||||
|
}
|
||||||
|
""";
|
||||||
}
|
}
|
|
@ -1,10 +1,14 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks.gfx;
|
||||||
|
|
||||||
|
import com.danitheskunk.skunkworks.gfx.font.IFont;
|
||||||
|
import com.danitheskunk.skunkworks.Recti;
|
||||||
|
import com.danitheskunk.skunkworks.Vec2i;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.nio.IntBuffer;
|
import java.nio.IntBuffer;
|
||||||
|
|
||||||
abstract class BaseRenderContext implements IRenderContext {
|
abstract public class BaseRenderContext implements IRenderContext {
|
||||||
public void drawString(Vec2i pos, String string, IFont font) {
|
public void drawString(Vec2i pos, String string, IFont font) {
|
||||||
int x = pos.getX();
|
int x = pos.getX();
|
||||||
int y = pos.getY();
|
int y = pos.getY();
|
|
@ -1,4 +1,4 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks.gfx;
|
||||||
|
|
||||||
public final class Color {
|
public final class Color {
|
||||||
private final int r, g, b, a;
|
private final int r, g, b, a;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks.gfx;
|
||||||
|
|
||||||
public enum GraphicsBackend {
|
public enum GraphicsBackend {
|
||||||
OPENGL
|
OPENGL
|
|
@ -1,4 +1,7 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks.gfx;
|
||||||
|
|
||||||
|
import com.danitheskunk.skunkworks.*;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.font.IFont;
|
||||||
|
|
||||||
public interface IRenderContext {
|
public interface IRenderContext {
|
||||||
void drawNineSlice(NineSlice slice, Recti rect); //todo: add rounding mode
|
void drawNineSlice(NineSlice slice, Recti rect); //todo: add rounding mode
|
|
@ -0,0 +1,7 @@
|
||||||
|
package com.danitheskunk.skunkworks.gfx;
|
||||||
|
|
||||||
|
import com.danitheskunk.skunkworks.Vec2i;
|
||||||
|
|
||||||
|
public interface ITexture {
|
||||||
|
Vec2i getSize();
|
||||||
|
}
|
|
@ -1,5 +1,7 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks.gfx;
|
||||||
|
|
||||||
|
import com.danitheskunk.skunkworks.Recti;
|
||||||
|
import com.danitheskunk.skunkworks.Vec2i;
|
||||||
import org.lwjgl.stb.STBImage;
|
import org.lwjgl.stb.STBImage;
|
||||||
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
@ -9,7 +11,7 @@ public class Image {
|
||||||
private final Vec2i size;
|
private final Vec2i size;
|
||||||
|
|
||||||
//constructors
|
//constructors
|
||||||
Image(ByteBuffer buffer) { //png or similar
|
public Image(ByteBuffer buffer) { //png or similar
|
||||||
//todo: resource system
|
//todo: resource system
|
||||||
int[] x = {0}, y = {0}, n = {0};
|
int[] x = {0}, y = {0}, n = {0};
|
||||||
var img = STBImage.stbi_load_from_memory(buffer, x, y, n, 4);
|
var img = STBImage.stbi_load_from_memory(buffer, x, y, n, 4);
|
||||||
|
@ -19,12 +21,12 @@ public class Image {
|
||||||
img.get(data);
|
img.get(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
Image(Vec2i size) {
|
public Image(Vec2i size) {
|
||||||
this.size = size;
|
this.size = size;
|
||||||
data = new byte[size.getX() * size.getY() * 4];
|
data = new byte[size.getX() * size.getY() * 4];
|
||||||
}
|
}
|
||||||
|
|
||||||
Image(ByteBuffer buffer, Vec2i size) {
|
public Image(ByteBuffer buffer, Vec2i size) {
|
||||||
data = new byte[buffer.remaining()];
|
data = new byte[buffer.remaining()];
|
||||||
buffer.get(data);
|
buffer.get(data);
|
||||||
this.size = size;
|
this.size = size;
|
|
@ -1,4 +1,6 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks.gfx;
|
||||||
|
|
||||||
|
import com.danitheskunk.skunkworks.gfx.ITexture;
|
||||||
|
|
||||||
public class NineSlice {
|
public class NineSlice {
|
||||||
private ITexture topLeft;
|
private ITexture topLeft;
|
||||||
|
@ -10,7 +12,7 @@ public class NineSlice {
|
||||||
private ITexture bottom;
|
private ITexture bottom;
|
||||||
private ITexture left;
|
private ITexture left;
|
||||||
private ITexture center;
|
private ITexture center;
|
||||||
NineSlice(ITexture topLeft, ITexture topRight, ITexture bottomLeft, ITexture bottomRight, ITexture top, ITexture right, ITexture bottom, ITexture left, ITexture center) {
|
public NineSlice(ITexture topLeft, ITexture topRight, ITexture bottomLeft, ITexture bottomRight, ITexture top, ITexture right, ITexture bottom, ITexture left, ITexture center) {
|
||||||
this.topLeft = topLeft;
|
this.topLeft = topLeft;
|
||||||
this.topRight = topRight;
|
this.topRight = topRight;
|
||||||
this.bottomLeft = bottomLeft;
|
this.bottomLeft = bottomLeft;
|
||||||
|
@ -22,39 +24,39 @@ public class NineSlice {
|
||||||
this.center = center;
|
this.center = center;
|
||||||
}
|
}
|
||||||
|
|
||||||
ITexture getTopLeft() {
|
public ITexture getTopLeft() {
|
||||||
return topLeft;
|
return topLeft;
|
||||||
}
|
}
|
||||||
|
|
||||||
ITexture getTopRight() {
|
public ITexture getTopRight() {
|
||||||
return topRight;
|
return topRight;
|
||||||
}
|
}
|
||||||
|
|
||||||
ITexture getBottomLeft() {
|
public ITexture getBottomLeft() {
|
||||||
return bottomLeft;
|
return bottomLeft;
|
||||||
}
|
}
|
||||||
|
|
||||||
ITexture getBottomRight() {
|
public ITexture getBottomRight() {
|
||||||
return bottomRight;
|
return bottomRight;
|
||||||
}
|
}
|
||||||
|
|
||||||
ITexture getTop() {
|
public ITexture getTop() {
|
||||||
return top;
|
return top;
|
||||||
}
|
}
|
||||||
|
|
||||||
ITexture getRight() {
|
public ITexture getRight() {
|
||||||
return right;
|
return right;
|
||||||
}
|
}
|
||||||
|
|
||||||
ITexture getBottom() {
|
public ITexture getBottom() {
|
||||||
return bottom;
|
return bottom;
|
||||||
}
|
}
|
||||||
|
|
||||||
ITexture getLeft() {
|
public ITexture getLeft() {
|
||||||
return left;
|
return left;
|
||||||
}
|
}
|
||||||
|
|
||||||
ITexture getCenter() {
|
public ITexture getCenter() {
|
||||||
return center;
|
return center;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,10 +1,11 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks.gfx.font;
|
||||||
|
|
||||||
import org.lwjgl.BufferUtils;
|
import com.danitheskunk.skunkworks.IWindow;
|
||||||
|
import com.danitheskunk.skunkworks.Vec2i;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.ITexture;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.Image;
|
||||||
import org.lwjgl.stb.STBTTFontinfo;
|
import org.lwjgl.stb.STBTTFontinfo;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
@ -19,7 +20,7 @@ public class FontTTF implements IFont {
|
||||||
STBTTFontinfo info;
|
STBTTFontinfo info;
|
||||||
IWindow window;
|
IWindow window;
|
||||||
|
|
||||||
FontTTF(ByteBuffer buffer, float size, IWindow window) {
|
public FontTTF(ByteBuffer buffer, float size, IWindow window) {
|
||||||
int[] ascent = {0};
|
int[] ascent = {0};
|
||||||
int[] descent = {0};
|
int[] descent = {0};
|
||||||
int[] lineGap = {0};
|
int[] lineGap = {0};
|
|
@ -1,12 +1,15 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks.gfx.font;
|
||||||
|
|
||||||
|
import com.danitheskunk.skunkworks.Vec2i;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.ITexture;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
class FontTileset implements IFont {
|
public class FontTileset implements IFont {
|
||||||
private List<ITexture> textures;
|
private List<ITexture> textures;
|
||||||
private Vec2i charSize;
|
private Vec2i charSize;
|
||||||
|
|
||||||
FontTileset(List<ITexture> textures) {
|
public FontTileset(List<ITexture> textures) {
|
||||||
this.textures = textures;
|
this.textures = textures;
|
||||||
this.charSize = textures.get(0).getSize();
|
this.charSize = textures.get(0).getSize();
|
||||||
}
|
}
|
|
@ -1,4 +1,7 @@
|
||||||
package com.danitheskunk.skunkworks;
|
package com.danitheskunk.skunkworks.gfx.font;
|
||||||
|
|
||||||
|
import com.danitheskunk.skunkworks.Vec2i;
|
||||||
|
import com.danitheskunk.skunkworks.gfx.ITexture;
|
||||||
|
|
||||||
public interface IFont {
|
public interface IFont {
|
||||||
int getLineHeight(int ch);
|
int getLineHeight(int ch);
|
|
@ -1,4 +1,4 @@
|
||||||
package com.danitheskunk.skunkworks.windows;
|
package com.danitheskunk.skunkworks.os.windows;
|
||||||
|
|
||||||
import com.sun.jna.Library;
|
import com.sun.jna.Library;
|
||||||
import com.sun.jna.Native;
|
import com.sun.jna.Native;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.danitheskunk.skunkworks.windows;
|
package com.danitheskunk.skunkworks.os.windows;
|
||||||
|
|
||||||
import com.sun.jna.Structure;
|
import com.sun.jna.Structure;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package com.danitheskunk.skunkworks.windows;
|
package com.danitheskunk.skunkworks.os.windows;
|
||||||
|
|
||||||
import com.sun.jna.Structure;
|
import com.sun.jna.Structure;
|
||||||
import com.sun.jna.platform.win32.WinDef;
|
import com.sun.jna.platform.win32.WinDef;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.danitheskunk.skunkworks.windows;
|
package com.danitheskunk.skunkworks.os.windows;
|
||||||
|
|
||||||
import com.sun.jna.Native;
|
import com.sun.jna.Native;
|
||||||
import com.sun.jna.platform.win32.User32;
|
import com.sun.jna.platform.win32.User32;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.danitheskunk.skunkworks.windows;
|
package com.danitheskunk.skunkworks.os.windows;
|
||||||
|
|
||||||
import com.sun.jna.Native;
|
import com.sun.jna.Native;
|
||||||
import com.sun.jna.platform.win32.WinUser;
|
import com.sun.jna.platform.win32.WinUser;
|
|
@ -1,4 +1,4 @@
|
||||||
package com.danitheskunk.skunkworks.windows;
|
package com.danitheskunk.skunkworks.os.windows;
|
||||||
|
|
||||||
|
|
||||||
import com.danitheskunk.skunkworks.Recti;
|
import com.danitheskunk.skunkworks.Recti;
|
Loading…
Reference in New Issue