I am trying to switch from using JFrame from a 2D game to using lwjgl for performance reasons. I added lwjgl and I am brand new to using it and the rendering just seems completely broken. I initialize everything properly but when I try to draw a simple rectangle on the screen I use a drawQuads method I found on google and it seems just broken like none of the values do what they are supposed to, I can't put the x and y coordinates in since the screen seems to render very small values like -0.8 to 0.8 and since I used JFrame I just used the pixel coordinates like 200,400. How do I fix this, what am I doing wrong? This is the main class that renders and ticks everything and right now it just renders a broken rectangle in the render method.
private void lwjglInit() {
GLFWErrorCallback.createPrint(System.err).set();
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
window = glfwCreateWindow(WIDTH, HEIGHT, title, 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
});
try ( MemoryStack stack = stackPush() ) {
IntBuffer pWidth = stack.mallocInt(1);
IntBuffer pHeight = stack.mallocInt(1);
// 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
);
}
glfwMakeContextCurrent(window);
// Enable v-sync
glfwSwapInterval(1);
// Make the window visible
glfwShowWindow(window);
GL.createCapabilities();
// Set the clear color
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
private void init() {
handler = new Handler();
input = new KeyInput();
new Camera(0, 0);
level = new Level(this, handler);
handler.setLevel(level);
minput = new MouseInput(this);
// this.addKeyListener(input);
// this.addMouseListener(minput);
// this.addMouseMotionListener(minput);
screen = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
}
private synchronized void start() {
if (isRunning)
return;
thread = new Thread(this);
thread.start();
isRunning = true;
}
private synchronized void stop() {
if (!(isRunning))
return;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
isRunning = false;
// Free the window callbacks and destroy the window
glfwFreeCallbacks(window);
glfwDestroyWindow(window);
// Terminate GLFW and free the error callback
glfwTerminate();
glfwSetErrorCallback(null).free();
}
public void run() {
lwjglInit();
init();
level.load("res/levels/default/level1.png");
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int updates = 0;
int frames = 0;
while(isRunning) {
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1) {
tick();
updates++;
delta--;
}
render();
frames++;
if(System.currentTimeMillis() - timer > 1000) {
timer += 1000;
System.out.println("FPS: " + frames + " TICKS: " + updates);
frames = 0;
updates = 0;
}
}
stop();
}
private void tick() {
if (glfwWindowShouldClose(window)) {
isRunning = false;
return;
}
glfwPollEvents();
handler.tick();
Camera.tick();
}
private void render() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Renderer.drawQuad(-0.7, 0, 0.3, 0.3, 255, 0, 0);
glfwSwapBuffers(window);
}
And this is the render method for the drawQuads:
public static void drawQuad(double x, double y, double width, double height, int red, int green, int blue) {
GL11.glColor3f(red, green, blue);
glBegin(GL_QUADS);
glVertex2f((float)-x, (float)y);
glVertex2f((float)width, (float)y);
glVertex2f((float)width, (float)-height);
glVertex2f((float)-x, (float)-height);
glEnd();
}
I try putting different values for x or y and it seems to just stretch the rectangle, if I try to change the width and height it just moves it or stretches it.