From c4bd9d25581fbaa39d0231779125cd15b150be45 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Sun, 13 Oct 2024 19:13:12 +0200 Subject: [PATCH] Update pom to support profiles + Remove idea project --- .gitignore | 4 +- .idea/.gitignore | 8 - .idea/compiler.xml | 13 - .idea/encodings.xml | 7 - .idea/jarRepositories.xml | 20 -- .idea/misc.xml | 12 - .idea/modules.xml | 8 - .idea/vcs.xml | 6 - pom.xml | 136 +++++----- .../java/fr/mrdev023/vulkan_java/App.java | 110 +------- .../mrdev023/vulkan_java/window/Display.java | 124 +++++++++ .../vulkan_java/window/DisplayMode.java | 51 ++++ .../fr/mrdev023/vulkan_java/window/Input.java | 239 ++++++++++++++++++ .../java/fr/mrdev023/vulkan_java/AppTest.java | 19 -- vulkan_java.iml | 9 - 15 files changed, 494 insertions(+), 272 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/compiler.xml delete mode 100644 .idea/encodings.xml delete mode 100644 .idea/jarRepositories.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml create mode 100644 src/main/java/fr/mrdev023/vulkan_java/window/Display.java create mode 100644 src/main/java/fr/mrdev023/vulkan_java/window/DisplayMode.java create mode 100644 src/main/java/fr/mrdev023/vulkan_java/window/Input.java delete mode 100644 src/test/java/fr/mrdev023/vulkan_java/AppTest.java delete mode 100644 vulkan_java.iml diff --git a/.gitignore b/.gitignore index b6f4823..5cf9556 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .direnv .mvn -target \ No newline at end of file +target +.idea/ +*.iml \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml deleted file mode 100644 index 3f02ced..0000000 --- a/.idea/compiler.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index aa00ffa..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml deleted file mode 100644 index 712ab9d..0000000 --- a/.idea/jarRepositories.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index bf58084..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index ec6e86d..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index c3f2b43..7cc7d5e 100644 --- a/pom.xml +++ b/pom.xml @@ -8,90 +8,59 @@ 1.0-SNAPSHOT vulkan_java - - http://www.example.com + https://mrdev023.fr UTF-8 - 22 - 3.2.3 + 21 + 3.3.4 + 1.10.8 - - - - org.junit - junit-bom - 5.11.0 - pom - import - - - - - - org.junit.jupiter - junit-jupiter-api - test - - - - org.junit.jupiter - junit-jupiter-params - test - org.lwjgl lwjgl ${lwjgl.version} - - org.lwjgl - lwjgl - ${lwjgl.version} - natives-windows - - - org.lwjgl - lwjgl - ${lwjgl.version} - natives-macos - - - org.lwjgl - lwjgl - ${lwjgl.version} - natives-linux - org.lwjgl lwjgl-glfw ${lwjgl.version} - - org.lwjgl - lwjgl-glfw - ${lwjgl.version} - natives-windows - - - org.lwjgl - lwjgl-glfw - ${lwjgl.version} - natives-macos - - - org.lwjgl - lwjgl-glfw - ${lwjgl.version} - natives-linux - org.lwjgl lwjgl-vulkan ${lwjgl.version} + + org.joml + joml + ${joml.version} + + + + + org.lwjgl + lwjgl + ${lwjgl.version} + ${native.target} + runtime + + + org.lwjgl + lwjgl-glfw + ${lwjgl.version} + ${native.target} + runtime + + + + + + + + @@ -127,11 +96,6 @@ maven-deploy-plugin 3.1.2 - - - maven-site-plugin - 3.12.1 - maven-project-info-reports-plugin 3.6.1 @@ -139,4 +103,40 @@ + + + + windows-profile + + + Windows + + + + natives-windows + + + + linux-profile + + + Linux + + + + natives-linux + + + + OSX-profile + + + mac + + + + natives-macos + + + diff --git a/src/main/java/fr/mrdev023/vulkan_java/App.java b/src/main/java/fr/mrdev023/vulkan_java/App.java index 6785f53..d677ae2 100644 --- a/src/main/java/fr/mrdev023/vulkan_java/App.java +++ b/src/main/java/fr/mrdev023/vulkan_java/App.java @@ -1,108 +1,16 @@ package fr.mrdev023.vulkan_java; -import org.lwjgl.*; -import org.lwjgl.glfw.*; -import org.lwjgl.system.*; +import fr.mrdev023.vulkan_java.window.Display; -import java.nio.*; - -import static org.lwjgl.glfw.Callbacks.*; -import static org.lwjgl.glfw.GLFW.*; -import static org.lwjgl.system.MemoryStack.*; -import static org.lwjgl.system.MemoryUtil.*; - -/** - * Hello world! - */ public class App { - // The window handle - private long window; - - public void run() { - System.out.println("Hello LWJGL " + Version.getVersion() + "!"); - - init(); - loop(); - - // Free the window callbacks and destroy the window - glfwFreeCallbacks(window); - glfwDestroyWindow(window); - - // Terminate GLFW and free the error callback - glfwTerminate(); - glfwSetErrorCallback(null).free(); - } - - private void init() { - // Setup an error callback. The default implementation - // will print the error message in System.err. - GLFWErrorCallback.createPrint(System.err).set(); - - // Initialize GLFW. Most GLFW functions will not work before doing this. - if ( !glfwInit() ) - throw new IllegalStateException("Unable to initialize GLFW"); - - // Configure GLFW - glfwDefaultWindowHints(); // optional, the current window hints are already the default - glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation - glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable - - // Create the window - window = glfwCreateWindow(300, 300, "Hello World!", NULL, NULL); - if ( window == NULL ) - throw new RuntimeException("Failed to create the GLFW window"); - - // Setup a key callback. It will be called every time a key is pressed, repeated or released. - glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> { - if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE ) - glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop - }); - - // Get the thread stack and push a new frame - try ( MemoryStack stack = stackPush() ) { - IntBuffer pWidth = stack.mallocInt(1); // int* - IntBuffer pHeight = stack.mallocInt(1); // int* - - // Get the window size passed to glfwCreateWindow - glfwGetWindowSize(window, pWidth, pHeight); - - // Get the resolution of the primary monitor - GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); - - // Center the window - glfwSetWindowPos( - window, - (vidmode.width() - pWidth.get(0)) / 2, - (vidmode.height() - pHeight.get(0)) / 2 - ); - } // the stack frame is popped automatically - - // Make the OpenGL context current - glfwMakeContextCurrent(window); - // Enable v-sync - glfwSwapInterval(1); - - // Make the window visible - glfwShowWindow(window); - } - - private void loop() { - - - // Run the rendering loop until the user has attempted to close - // the window or has pressed the ESCAPE key. - while ( !glfwWindowShouldClose(window) ) { - // Replace by clear framebuffer on vulkan - - glfwSwapBuffers(window); // swap the color buffers - - // Poll for window events. The key callback above will only be - // invoked during this call. - glfwPollEvents(); - } - } - public static void main(String[] args) { - new App().run(); + Display.create("My first application", 800, 600); + + while (!Display.isCloseRequested()) { + Display.updateEvent(); + Display.updateFrame(); + } + + Display.destroy(); } } diff --git a/src/main/java/fr/mrdev023/vulkan_java/window/Display.java b/src/main/java/fr/mrdev023/vulkan_java/window/Display.java new file mode 100644 index 0000000..3c85ea0 --- /dev/null +++ b/src/main/java/fr/mrdev023/vulkan_java/window/Display.java @@ -0,0 +1,124 @@ +package fr.mrdev023.vulkan_java.window; + +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.system.MemoryUtil.*; + +import java.nio.*; + +import org.lwjgl.*; +import org.lwjgl.glfw.*; + +public class Display { + + private static DisplayMode displayMode; + private static String TITLE = ""; + private static long window; + private static boolean hasResized = false; + + public static void create(String title, int width, int height) { + if (!glfwInit()) + throw new IllegalStateException("Unable to initialize GLFW"); + + TITLE = title; + displayMode = new DisplayMode(width, height); + window = glfwCreateWindow(displayMode.getWidth(), displayMode.getHeight(), TITLE, NULL, NULL); + } + + public static void setMouseGrabbed(boolean a) { + if (a) { + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); + } else { + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); + } + } + + public static void setVSync(boolean a) { + if (a) glfwSwapInterval(1); + else glfwSwapInterval(0); + } + + public static void setSample(int sample) { + glfwWindowHint(GLFW_SAMPLES, sample); + } + + public static void setResizable(boolean a) { + if (a) glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); + else glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + } + + public static void setTitle(String title) { + TITLE = title; + glfwSetWindowTitle(window, TITLE); + } + + public static String getTitle() { + return TITLE; + } + + public static boolean wasResized() { + IntBuffer w = BufferUtils.createIntBuffer(1); + IntBuffer h = BufferUtils.createIntBuffer(1); + glfwGetWindowSize(window, w, h); + int width = w.get(0); + int height = h.get(0); + + if (Display.getDisplayMode().getWidth() != width || Display.getDisplayMode().getHeight() != height || hasResized) { + setDisplayMode(new DisplayMode(width, height)); + hasResized = false; + return true; + } else { + return false; + } + } + + public static void printMonitorsInfo() { + PointerBuffer monitors = glfwGetMonitors(); + GLFWVidMode m; + if (monitors == null) { + System.out.println("No monitor detected !"); + return; + } + for (int i = 0; i < monitors.capacity(); i++) { + m = glfwGetVideoMode(monitors.get(i)); + System.out.println(glfwGetMonitorName(monitors.get(i)) + "(" + i + ") : " + m.width() + "x" + m.height() + ":" + m.refreshRate() + "Hz"); + } + + } + + public static boolean isCloseRequested() { + return glfwWindowShouldClose(window); + } + + public static void createContext() { + glfwMakeContextCurrent(window); + } + + public static void updateEvent() { + glfwPollEvents(); + } + + public static void updateFrame() { + glfwSwapBuffers(window); + } + + public static DisplayMode getDisplayMode() { + return displayMode; + } + + public static void setDisplayMode(DisplayMode displayMode) { + if (Display.displayMode == null || displayMode == null) return; + Display.displayMode.setDisplayMode(displayMode); + hasResized = true; + } + + public static void destroy() { + glfwDestroyWindow(window); + glfwTerminate(); + } + + public static long getWindow() { + return window; + } + + +} \ No newline at end of file diff --git a/src/main/java/fr/mrdev023/vulkan_java/window/DisplayMode.java b/src/main/java/fr/mrdev023/vulkan_java/window/DisplayMode.java new file mode 100644 index 0000000..5f6f7bc --- /dev/null +++ b/src/main/java/fr/mrdev023/vulkan_java/window/DisplayMode.java @@ -0,0 +1,51 @@ +package fr.mrdev023.vulkan_java.window; + + +import static org.lwjgl.glfw.GLFW.*; + +public class DisplayMode { + + private int width = 0, height = 0; + private boolean fullscreen = false; + + public DisplayMode(int width, int height) { + this.width = width; + this.height = height; + } + + public void setDisplayMode(DisplayMode displayMode) { + this.width = displayMode.getWidth(); + this.height = displayMode.getHeight(); + this.fullscreen = displayMode.isFullscreen(); + setDisplayMode(); + } + + public void setDisplayMode() { + glfwSetWindowSize(Display.getWindow(), width, height); + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public boolean isFullscreen() { + return fullscreen; + } + + public void setFullscreen(boolean fullscreen) { + this.fullscreen = fullscreen; + setDisplayMode(); + } +} diff --git a/src/main/java/fr/mrdev023/vulkan_java/window/Input.java b/src/main/java/fr/mrdev023/vulkan_java/window/Input.java new file mode 100644 index 0000000..99a19cf --- /dev/null +++ b/src/main/java/fr/mrdev023/vulkan_java/window/Input.java @@ -0,0 +1,239 @@ +package fr.mrdev023.vulkan_java.window; + +import static org.lwjgl.glfw.GLFW.*; + +import java.util.*; +import java.util.Map.*; + +import org.joml.Vector2f; +import org.lwjgl.glfw.*; + +public class Input { + + public static GLFWScrollCallback scroll; + public static GLFWCursorPosCallback mousePos; + + private static Vector2f mousePosition = new Vector2f(); + private static Vector2f dMouse = new Vector2f(); + private static Vector2f previousDMouse = new Vector2f(); + + public static final int NONE = 0, PRESSED = 1, RELEASED = 2, REPEATED = 3, UP = 4, DOWN = 5, + NBRE_KEY = 0x15D, NBRE_BUTTON = 10, + MOUSE_OFFSET = NBRE_KEY + 1, MOUSE_WHEEL_OFFSET = MOUSE_OFFSET + 1; + + private static HashMap state = new HashMap(); + + private static double ywheel = 0; + + public static void init() { + glfwSetScrollCallback(Display.getWindow(), scroll = new GLFWScrollCallback() { + public void invoke(long window, double xoffset, double yoffset) { + scroll(window, xoffset, yoffset); + } + }); + glfwSetCursorPosCallback(Display.getWindow(), mousePos = new GLFWCursorPosCallback() { + public void invoke(long window, double xpos, double ypos) { + mousepos(window, xpos, ypos); + } + }); + for (int i = 0; i < NBRE_KEY; i++) { + state.put(i, NONE); + } + for (int i = 0; i < NBRE_BUTTON; i++) { + state.put(i + MOUSE_OFFSET, NONE); + } + state.put(MOUSE_WHEEL_OFFSET, NONE); + } + + public static void update() { + for (Entry set : state.entrySet()) { + int i = set.getKey(); + int st = set.getValue(); + if (i > -1 && i < NBRE_KEY) { + if (glfwGetKey(Display.getWindow(), i) == 0 && st == NONE) continue; + if (glfwGetKey(Display.getWindow(), i) == 1 && st == NONE) { + state.replace(i, PRESSED); + } else if (glfwGetKey(Display.getWindow(), i) == 1 && st == PRESSED) { + state.replace(i, REPEATED); + } else if (glfwGetKey(Display.getWindow(), i) == 0 && (st == PRESSED || st == REPEATED)) { + state.replace(i, RELEASED); + } else if (glfwGetKey(Display.getWindow(), i) == 0 && st == RELEASED) { + state.replace(i, NONE); + } + } else if (i >= MOUSE_OFFSET && i < MOUSE_OFFSET + NBRE_BUTTON) { + if (glfwGetMouseButton(Display.getWindow(), i - MOUSE_OFFSET) == 0 && st == NONE) continue; + if (glfwGetMouseButton(Display.getWindow(), i - MOUSE_OFFSET) == 1 && st == NONE) { + state.replace(i, PRESSED); + } else if (glfwGetMouseButton(Display.getWindow(), i - MOUSE_OFFSET) == 1 && st == PRESSED) { + state.replace(i, REPEATED); + } else if (glfwGetMouseButton(Display.getWindow(), i - MOUSE_OFFSET) == 0 && (st == PRESSED || st == REPEATED)) { + state.replace(i, RELEASED); + } else if (glfwGetMouseButton(Display.getWindow(), i - MOUSE_OFFSET) == 0 && st == RELEASED) { + state.replace(i, NONE); + } + } + } + int st = state.get(MOUSE_WHEEL_OFFSET); + if (ywheel > 0 && (st == NONE || st == UP)) { + state.replace(MOUSE_WHEEL_OFFSET, UP); + } else if (ywheel < 0 && (st == NONE || st == DOWN)) { + state.replace(MOUSE_WHEEL_OFFSET, DOWN); + } else if (ywheel == 0 && (st == DOWN || st == UP)) { + state.replace(MOUSE_WHEEL_OFFSET, NONE); + } + ywheel = 0; + if (dMouse.equals(previousDMouse)) { + dMouse = new Vector2f(); + } else { + previousDMouse = dMouse; + } + } + + public static void destroy() { + mousePos.free(); + scroll.free(); + } + + public static void scroll(long window, double xoffset, double yoffset) { + ywheel = yoffset; + } + + public static void mousepos(long window, double xpos, double ypos) { + dMouse.x = (float) (xpos - mousePosition.x); + dMouse.y = (float) (ypos - mousePosition.y); + mousePosition.x = (float) xpos; + mousePosition.y = (float) ypos; + } + + public static boolean isButtonDown(int button) { + return state.get(button + MOUSE_OFFSET) == PRESSED; + } + + public static boolean isButtonUp(int button) { + return state.get(button + MOUSE_OFFSET) == RELEASED; + } + + public static boolean isButton(int button) { + return state.get(button + MOUSE_OFFSET) == PRESSED || state.get(button + MOUSE_OFFSET) == REPEATED; + } + + public static int isButtonState(int button) { + return state.get(button + MOUSE_OFFSET); + } + + public static boolean isKeyDown(int key) { + return state.get(key) == PRESSED; + } + + public static boolean isKeyUp(int key) { + return state.get(key) == RELEASED; + } + + public static boolean isKey(int key) { + return state.get(key) == PRESSED || state.get(key) == REPEATED; + } + + public static int isKeyState(int key) { + return state.get(key); + } + + public static int isMouseWheelState() { + return state.get(MOUSE_WHEEL_OFFSET); + } + + public static boolean isMouseWheelUp() { + return state.get(MOUSE_WHEEL_OFFSET) == UP; + } + + public static boolean isMouseWheelDown() { + return state.get(MOUSE_WHEEL_OFFSET) == DOWN; + } + + public static GLFWScrollCallback getScroll() { + return scroll; + } + + public static void setScroll(GLFWScrollCallback scroll) { + Input.scroll = scroll; + } + + public static GLFWCursorPosCallback getMousePos() { + return mousePos; + } + + public static void setMousePos(GLFWCursorPosCallback mousePos) { + Input.mousePos = mousePos; + } + + public static Vector2f getMousePosition() { + return mousePosition; + } + + public static void setMousePosition(Vector2f mousePosition) { + Input.mousePosition = mousePosition; + } + + public static Vector2f getDMouse() { + return dMouse; + } + + public static void setDMouse(Vector2f dMouse) { + Input.dMouse = dMouse; + } + + public static HashMap getState() { + return state; + } + + public static void setState(HashMap state) { + Input.state = state; + } + + public static double getYwheel() { + return ywheel; + } + + public static void setYwheel(double ywheel) { + Input.ywheel = ywheel; + } + + public static int getNone() { + return NONE; + } + + public static int getPressed() { + return PRESSED; + } + + public static int getReleased() { + return RELEASED; + } + + public static int getRepeated() { + return REPEATED; + } + + public static int getUp() { + return UP; + } + + public static int getDown() { + return DOWN; + } + + public static int getNbreKey() { + return NBRE_KEY; + } + + public static int getNbreButton() { + return NBRE_BUTTON; + } + + public static int getMouseOffset() { + return MOUSE_OFFSET; + } + + public static int getMouseWheelOffset() { + return MOUSE_WHEEL_OFFSET; + } +} diff --git a/src/test/java/fr/mrdev023/vulkan_java/AppTest.java b/src/test/java/fr/mrdev023/vulkan_java/AppTest.java deleted file mode 100644 index 9940692..0000000 --- a/src/test/java/fr/mrdev023/vulkan_java/AppTest.java +++ /dev/null @@ -1,19 +0,0 @@ -package fr.mrdev023.vulkan_java; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.junit.jupiter.api.Test; - -/** - * Unit test for simple App. - */ -public class AppTest { - - /** - * Rigorous Test :-) - */ - @Test - public void shouldAnswerWithTrue() { - assertTrue(true); - } -} diff --git a/vulkan_java.iml b/vulkan_java.iml deleted file mode 100644 index be3cc78..0000000 --- a/vulkan_java.iml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file