Friday, January 15, 2016

How to install LWJGL 3 on Intellij IDEA 15

Maybe some of you may already know what was actually LWJGL (Light Weight Java Gaming Library) is, well its an open source java programming language library for Developing games like the most popular game Minecraft.

And today we're going to add the lwjgl library or probably we can say "installing LWJGL"  into Intellij IDEA IDE(Integrated development environment).

So for those of you who want to make games with the Java programming language, you can start by using this library. Before we start, all you have to do is of  download the latest LWJGL library package from the website lwjgl website:  https://www.lwjgl.org/download

Installing The Library

Once you have downloaded the library extract it into a folder then open the IDE :
Intellij IDEA 15
  • Create a New Project
    Open the IDE and you should be prompted into a New Project window that will allow you make an Java Application project. And let's just set name of the project "LWJGL 3".
    New Project
     
  • When you ready on your workspace press Ctrl+Shift+S or click File -> Project Structure...
  • On the Project Structure window click Libraries then click the green plus button to create a new Project Library then click Java section

  • Browse the LWJGL Folder that you had been extract then locate it to "your extracted lwjgl package folder\lwjgl\jar\lwjgl.jar" then click OK, another confirmation window should be appear, then just click OK



  • Congrats...you just include the LWJGL library into your Intellij IDEA IDE, but we're not done yet we have to add some natives library to the IDE.

  • Click Add button or just press Alt + Insert do the same thing like above locate to \lwjgl\native\ then click OK
  •  
  • The next step do the same thing above but locate to the \lwjgl\src.zip
  • The next step is same, do the same thing as above but locate to the \lwjgl\doc then another windows will appear, then just click Javadoc
  • When everything done like the picture below click OK


Finalization

Here you go... you has been install the LWJGL Library. Now go create a new java class you can name the class anything you want but we going to use "HelloWorld" for now


You can copy this source code to begin testing the LWJGL and to see if the LWJGL work properly
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryUtil.*;
public class HelloWorld {
    // We need to strongly reference callback instances.
    private GLFWErrorCallback errorCallback;
    private GLFWKeyCallback   keyCallback;
    // The window handle
    private long window;
    public void run() {
        System.out.println("Hello LWJGL " + Version.getVersion() + "!");
        try {
            init();
            loop();
            // Release window and window callbacks
            glfwDestroyWindow(window);
            keyCallback.release();
        } finally {
            // Terminate GLFW and release the GLFWErrorCallback
            glfwTerminate();
            errorCallback.release();
        }
    }
    private void init() {
        // Setup an error callback. The default implementation
        // will print the error message in System.err.
        glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
        // Initialize GLFW. Most GLFW functions will not work before doing this.
        if ( glfwInit() != GLFW_TRUE )
            throw new IllegalStateException("Unable to initialize GLFW");
        // Configure our window
        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
        int WIDTH = 300;
        int HEIGHT = 300;
        // Create the window
        window = glfwCreateWindow(WIDTH, HEIGHT, "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, keyCallback = new GLFWKeyCallback() {
            @Override
            public void invoke(long window, int key, int scancode, int action, int mods) {
                if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE )
                    glfwSetWindowShouldClose(window, GLFW_TRUE); // We will detect this in our rendering loop
            }
        });
        // Get the resolution of the primary monitor
        GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
        // Center our window
        glfwSetWindowPos(
            window,
            (vidmode.width() - WIDTH) / 2,
            (vidmode.height() - HEIGHT) / 2
        );
        // Make the OpenGL context current
        glfwMakeContextCurrent(window);
        // Enable v-sync
        glfwSwapInterval(1);
        // Make the window visible
        glfwShowWindow(window);
    }
    private void loop() {
        // This line is critical for LWJGL's interoperation with GLFW's
        // OpenGL context, or any context that is managed externally.
        // LWJGL detects the context that is current in the current thread,
        // creates the GLCapabilities instance and makes the OpenGL
        // bindings available for use.
        GL.createCapabilities();
        // Set the clear color
        glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
        // Run the rendering loop until the user has attempted to close
        // the window or has pressed the ESCAPE key.
        while ( glfwWindowShouldClose(window) == GLFW_FALSE ) {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer
            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 HelloWorld().run();
    }
} // source code from https://www.lwjgl.org/guide



Press Alt+Shift+F10 or click Run -> Run... to run the program then it should working perfectly.
if you have problem or have something error when running the program please ask in the comment.

8 comments:

  1. Thanks a lot! I thought I only had to add the JAR file, now I know better. 10/10

    ReplyDelete
  2. Error:(3, 1) java: package org.lwjgl does not exist

    ReplyDelete
  3. Error:(3, 1) java: package org.lwjgl does not exist :s

    ReplyDelete
    Replies
    1. it's probably you haven't add the lwjgl library, follow the steps carefully again.

      Delete
  4. You did mention something about the Edit configurations to recognize native values, but you did not specify what to do.
    The source code you posted did not compile for me, since the method release() could not be resolved. Any idea what I have done wrong?

    ReplyDelete
  5. You did mention something about Edit configurations to recognize native values, but you did not specify what to do.
    I have added the libraries as you described, but I cant get the code you posted to compile. The method release() can not be resolved.
    Do you have any idea about what I have done wrong? Could it be something about the Edit configurations?

    ReplyDelete
    Replies
    1. the native folder should be on your extracted lwjgl folder I think I will make video for that so u can follow my steps

      Delete