Search code examples
javalwjgl

the constructor (int, int, string) is undefined; creating display in LWJGL


Not sure if this is too specific, or too much example code, but I really have no idea what I have done wrong. I am trying to make a basic display window in LWJGL following this tutorial. In line 17:

window = new DisplayManager(WIDTH, HEIGHT, "game");

I get the error: The constructor DisplayManager(int, int, String) is undefined. I have a general idea of what the error is, but I have no idea why it is happening. I am new to Java, so don't entirely understand what is going on, but I have taken a close look at both the files in the video and can not spot any discrepancies other than the differences in the names of the files and packages that I have created compared to his.

This is MainGame.java:

package gameEngine;

import renderEngine.DisplayManager;

public class MainGame implements Runnable {
    public Thread game;
    public static DisplayManager window;
    public static final int WIDTH = 1280, HEIGHT = 760;

    public void start() {
        game = new Thread(this, "game");
        game.start();
    }

    public static void init() {
        System.out.println("Initialising Game...");
        window = new DisplayManager(WIDTH, HEIGHT, "game");
        window.create();
    }

    public void run() {
        init();
        while (true) {
            update();
            render();
        }
    }

    private void update() {
        System.out.println("Updating Game...");
    }

    private void render() {
        System.out.println("Rendering Game...");
    }

    public static void main(String[] args) {
         new MainGame().start();

    }

}

And this is DisplayManager.java:

package renderEngine;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWVidMode;


public class DisplayManager {
    private int width, height;
    private String title;
    private long window;

    public void Window(int width, int height, String Game) {
        this.width = width;
        this.height = height;
        this.title = title;
    }
    public void create() {
        if (!GLFW.glfwInit()) {
            System.err.println("ERROR: GLFW wasn't initialised!");
            return;
        }
        window = GLFW.glfwCreateWindow(width, height, title, 0, 0);
        if (window == 0) {
            System.err.println("ERROR: Window wasn't created!");
        }
        GLFWVidMode videoMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
        GLFW.glfwSetWindowPos(window, videoMode.width() - width / 2, videoMode.height() - height / 2);
        GLFW.glfwShowWindow(window);
    }
}

Sorry about all the unused junk code. A lot of it is setup for further tutorial, as far as I know. If anyone could help me with this, that would be greatly appreciated. Cheers!


Solution

  • Your DisplayManager does not define a constructor. Thus it only hat a default constructor that does not accept any arguments.

    To fix this you could replace your Window method or simply add a constructor.

    public DisplayManager(int width, int height, String Game) { 
        this.width = width; 
        this.height = height; 
        this.title = title; 
    }