diff --git a/plutostatic/src/main/java/cz/tefek/pluto/engine/buffer/BufferHelper.java b/plutostatic/src/main/java/cz/tefek/pluto/engine/buffer/BufferHelper.java index a84addf..a613037 100644 --- a/plutostatic/src/main/java/cz/tefek/pluto/engine/buffer/BufferHelper.java +++ b/plutostatic/src/main/java/cz/tefek/pluto/engine/buffer/BufferHelper.java @@ -3,11 +3,15 @@ package cz.tefek.pluto.engine.buffer; import org.apache.commons.io.IOUtils; import org.lwjgl.BufferUtils; -import java.io.FileInputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.FloatBuffer; import java.nio.IntBuffer; +import java.nio.channels.FileChannel; +import java.nio.file.Files; +import java.nio.file.Path; + +import cz.tefek.pluto.io.asl.resource.ResourceAddress; /** * A utility class to handle primitive native buffers. @@ -110,9 +114,83 @@ public final class BufferHelper */ public static ByteBuffer readToFlippedByteBuffer(String path) throws IOException { - try (var fis = new FileInputStream(path)) + try (var fc = FileChannel.open(Path.of(path))) { - var ba = IOUtils.toByteArray(fis); + var size = fc.size(); + + if (size > Integer.MAX_VALUE) + { + throw new IOException("File ' + pah + ' is too big to be read into a ByteBuffer!"); + } + + ByteBuffer buf = BufferUtils.createByteBuffer((int) size); + fc.read(buf); + buf.flip(); + return buf; + } + } + + /** + * Loads a file denoted by the specified {@link Path} and fills the input + * {@link ByteBuffer} with the read bytes. + * + *

+ * Make sure the buffer can hold the entire file. + *

+ * + * @param path The file's path. + * @param buf The input buffer to be filled with data. + * + * @return The input {@link ByteBuffer}. + * @throws IOException Upon standard I/O errors. + * + * @author 493msi + * @since 0.3 + */ + public static ByteBuffer readToByteBuffer(Path path, ByteBuffer buf) throws IOException + { + try (var fc = FileChannel.open(path)) + { + fc.read(buf); + buf.flip(); + return buf; + } + } + + /** + * {@link ResourceAddress} version of + * {@link BufferHelper#readToByteBuffer(Path path, ByteBuffer buf)}. + * + * @param addr The file's {@link ResourceAddress}. + * @param buf The input buffer to be filled with data. + * + * @return The input {@link ByteBuffer}. + * @throws IOException Upon standard I/O errors. + * + * @author 493msi + * @since 0.3 + */ + public static ByteBuffer readToByteBuffer(ResourceAddress addr, ByteBuffer buf) throws IOException + { + return readToByteBuffer(addr.toNIOPath(), buf); + } + + /** + * Loads a file denoted by the specified {@link ResourceAddress} and returns + * a {@link ByteBuffer} containing the read bytes. + * + * @param path The file's path. + * @return A {@link ByteBuffer} containing the file's contents. + * @throws IOException Upon standard I/O errors. + * + * @author 493msi + * @since 0.3 + */ + public static ByteBuffer readToFlippedByteBuffer(ResourceAddress path) throws IOException + { + try (var is = Files.newInputStream(path.toNIOPath())) + { + var ba = IOUtils.toByteArray(is); return flippedByteBuffer(ba); } } diff --git a/plutostatic/src/main/java/cz/tefek/pluto/engine/display/Display.java b/plutostatic/src/main/java/cz/tefek/pluto/engine/display/Display.java index 41febe3..9e78db1 100644 --- a/plutostatic/src/main/java/cz/tefek/pluto/engine/display/Display.java +++ b/plutostatic/src/main/java/cz/tefek/pluto/engine/display/Display.java @@ -27,6 +27,7 @@ public class Display int width; int height; boolean debugMode; + boolean coreProfile = true; private boolean wasResized; @@ -190,7 +191,7 @@ public class Display public void createOpenGLCapabilities() { - var glCapabilities = GL.createCapabilities(true); + var glCapabilities = GL.createCapabilities(this.coreProfile); GLDebugInfo.printDebugInfo(glCapabilities); diff --git a/plutostatic/src/main/java/cz/tefek/pluto/engine/display/DisplayBuilder.java b/plutostatic/src/main/java/cz/tefek/pluto/engine/display/DisplayBuilder.java index a3bc32a..0212fb2 100644 --- a/plutostatic/src/main/java/cz/tefek/pluto/engine/display/DisplayBuilder.java +++ b/plutostatic/src/main/java/cz/tefek/pluto/engine/display/DisplayBuilder.java @@ -50,11 +50,26 @@ public class DisplayBuilder public DisplayBuilder hintOpenGLVersion(int major, int minor) { + GLFW.glfwWindowHint(GLFW.GLFW_CLIENT_API, GLFW.GLFW_OPENGL_API); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, major); GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, minor); GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GLFW.GLFW_TRUE); GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE); + this.display.coreProfile = true; + + return this; + } + + public DisplayBuilder hintOpenGLVersionLegacy(int major, int minor) + { + GLFW.glfwWindowHint(GLFW.GLFW_CLIENT_API, GLFW.GLFW_OPENGL_API); + GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_ANY_PROFILE); + GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, major); + GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, minor); + + this.display.coreProfile = false; + return this; } diff --git a/plutostatic/src/main/java/cz/tefek/pluto/engine/math/ViewMatrix.java b/plutostatic/src/main/java/cz/tefek/pluto/engine/math/ViewMatrix.java index 43078e9..9fc3f44 100644 --- a/plutostatic/src/main/java/cz/tefek/pluto/engine/math/ViewMatrix.java +++ b/plutostatic/src/main/java/cz/tefek/pluto/engine/math/ViewMatrix.java @@ -11,10 +11,10 @@ import org.joml.Matrix3x2f; public class ViewMatrix { /** - * Create a 2D + * Create a 2D view matrix. * - * @param x The X camera translation - * @param y The Y camera translation + * @param x The X camera translation + * @param y The Y camera translation * @param zoom The zoom * * @return the view matrix