Update pom to support profiles + Remove idea project

This commit is contained in:
Florian RICHER 2024-10-13 19:13:12 +02:00
parent 888c438b55
commit c4bd9d2558
15 changed files with 494 additions and 272 deletions

View file

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

View file

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

View file

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

View file

@ -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<Integer, Integer> state = new HashMap<Integer, Integer>();
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<Integer, Integer> 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<Integer, Integer> getState() {
return state;
}
public static void setState(HashMap<Integer, Integer> 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;
}
}