162 lines
4.3 KiB
Java
162 lines
4.3 KiB
Java
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.opengl.GL;
|
|
import org.lwjgl.opengl.GL11;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
import static org.lwjgl.glfw.GLFW.*;
|
|
import static org.lwjgl.opengl.GL46.*;
|
|
import static org.lwjgl.system.MemoryUtil.NULL;
|
|
|
|
public class Window extends BaseWindow {
|
|
private boolean debug;
|
|
private final Program program;
|
|
private final RenderContext renderContext;
|
|
private boolean shouldClose;
|
|
private final Vec2i size;
|
|
private final TextureAtlas textureAtlas;
|
|
private final long window;
|
|
|
|
public Window(Vec2i size, String title, Engine engine) {
|
|
super(engine);
|
|
GLFWErrorCallback.createPrint(System.err).set();
|
|
if(!glfwInit()) throw new IllegalStateException("Unable to initialize GLFW");
|
|
|
|
glfwDefaultWindowHints();
|
|
this.debug = false;
|
|
this.size = size;
|
|
|
|
window = glfwCreateWindow(size.getX(), size.getY(), title, NULL, NULL);
|
|
if(window == NULL) throw new RuntimeException("Failed to create GLFW window");
|
|
|
|
|
|
glfwShowWindow(window);
|
|
glfwMakeContextCurrent(window);
|
|
glfwSwapInterval(1); //vsync
|
|
GL.createCapabilities();
|
|
glMatrixMode(GL_PROJECTION);
|
|
glLoadIdentity();
|
|
glOrtho(0.0f, size.getX(), size.getY(), 0.0f, 0.0f, 1.0f);
|
|
glEnable(GL_COLOR);
|
|
glEnable(GL_TEXTURE_2D);
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
glEnable(GL_BLEND);
|
|
|
|
textureAtlas = new 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;
|
|
|
|
System.out.println("Skunkworks window initialised");
|
|
System.out.print("Maximum Texture Size: ");
|
|
System.out.println(GL11.glGetInteger(GL_MAX_TEXTURE_SIZE));
|
|
}
|
|
|
|
@Override
|
|
public ITexture loadTexture(Image image) {
|
|
return textureAtlas.addTexture(image);
|
|
}
|
|
|
|
@Override
|
|
public ITexture loadTexture(String path) {
|
|
var img = engine.loadImage(path);
|
|
return textureAtlas.addTexture(img);
|
|
}
|
|
|
|
@Override
|
|
public List<ITexture> loadTextureArray(Image image, Vec2i tileSize) {
|
|
var tileCount = Vec2i.div(image.getSize(), tileSize);
|
|
var tiles = new ArrayList<ITexture>();
|
|
|
|
for(int y = 0; y < tileCount.getY(); ++y) {
|
|
for(int x = 0; x < tileCount.getX(); ++x) {
|
|
var rect = new Recti(Vec2i.mul(new Vec2i(x, y), tileSize), tileSize);
|
|
var img = image.getSubImage(rect);
|
|
tiles.add(loadTexture(img));
|
|
}
|
|
}
|
|
|
|
return tiles;
|
|
}
|
|
|
|
@Override
|
|
public List<ITexture> loadTextureArray(String path, Vec2i tileSize) {
|
|
var img = engine.loadImage(path);
|
|
return loadTextureArray(img, tileSize);
|
|
}
|
|
|
|
@Override
|
|
public void renderFinish(IRenderContext context) {
|
|
if(debug) {
|
|
context.drawTextureRectangle(new Recti(Vec2i.ZERO, size), textureAtlas.getAtlasTexture());
|
|
}
|
|
glEnd();
|
|
glfwSwapBuffers(window);
|
|
}
|
|
|
|
@Override
|
|
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);
|
|
program.use();
|
|
glBegin(GL_TRIANGLES);
|
|
return renderContext;
|
|
}
|
|
|
|
@Override
|
|
public void setDebug(boolean on) {
|
|
this.debug = on;
|
|
}
|
|
|
|
@Override
|
|
public boolean shouldClose() {
|
|
return shouldClose;
|
|
}
|
|
|
|
@Override
|
|
public void tick() {
|
|
glfwMakeContextCurrent(window);
|
|
if(glfwWindowShouldClose(window)) {
|
|
shouldClose = true;
|
|
return;
|
|
}
|
|
|
|
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);
|
|
}
|
|
""";
|
|
}
|