Skip to content
PanJohnny edited this page Nov 8, 2022 · 14 revisions

Welcome to the PJGL wiki! Enjoy your stay here.

Design principles

Main api methods are placed in PJGL class. This class provides you way to configure the library, request some data and run it. There is also class PJGLEvents which contains events that you can 'listen' to. Upon listening to events you can modify behaviour, register things, and more in the correct time. There are also PJGLRegistries where you can register object managers or modules.

Hello World!

First you'll start with creating class that extends GameApplication

import com.panjohnny.pjgl.api.GameApplication;
import com.panjohnny.pjgl.api.event.OperationInterceptor;
import com.panjohnny.pjgl.core.rendering.GLFWWindow;

public class HelloWorld extends GameApplication {
    /**
     * Method where everything should be initialized and your data loaded. Called before {@link #modifyWindow(GLFWWindow)}
     *
     * @apiNote Beware that you should not do any GL operations here. That includes loading models and shaders!
     */
    @Override
    public void init() {
        System.out.println("Hello PJGL World!");
    }

    // other methods
    ...
}

This GameApplication actually hooks you directly into the api. The init function is called before window is visible and graphics are not initialized so it should be used with caution. Now we need to register our application and run the game.

    public static void main(String[] args) {
        PJGL.registerApplication(new HelloWorld());
        PJGL.run();
    }

This would hook up our app with the api and run the game. You should see Hello PJGL World pop up and window open, that means you created your first program.

Creating game object

First you need to create class that extends it:

import com.panjohnny.pjgl.api.object.GameObject;
import com.panjohnny.pjgl.api.utils.TextureLoader;
import com.panjohnny.pjgl.core.helpers.Model;

public class ImageStuff extends GameObject {
    public ImageStuff(int x, int y, int width, int height) {
        super(x, y, width, height, Model.rectangle(), TextureLoader.loadTexture(TextureLoader.loadImageAndHandleException("/image.png")));
    }

    @Override
    public void update(long delta) {

    }
}

This game object would be rectangle shaped and it would have the texture of image.png in resources folder. Now we need to get our GameObject to the screen.

In the last example we coded simple hello world. Return to the GameApplication and in load add the object.

PJGLRegistries.GAME_OBJECT_MANAGER_REGISTRY.get(GameObject.class).add(new ImageStuff(100, 100, 100, 100));

This basically gets the registry with managers. Managers handle objects. ImageStuff doesn't have its own object manager, so we would register it to the defaul one because we haven't modified the registry. After running the code your image should appear on the screen.

Handling input

In modifyWindow method (in the GameApplication) you can set key and mouse listener. To do it just run:

        window.setKeyListener(new WindowKeyListener() {
            @Override
            public void onKeyPress(long window, int key, int scancode, List<KeyEventMod> mods) {
                
            }

            @Override
            public void onKeyRelease(long window, int key, int scancode, List<KeyEventMod> mods) {

            }
        });
        
        window.setMouseListener(new WindowMouseListener());

Now you have registered key and mouse listeners and use them. But wait WindowMouseListener is not abstract. In order to listen to mouse events just ovveride individual methods.

For example:

        window.setMouseListener(new WindowMouseListener() {
            @Override
            public void onButtonPress(long window, int button, List<WindowKeyListener.KeyEventMod> mods) {
                if (button == GLFW.GLFW_MOUSE_BUTTON_1) {
                    System.out.println("Click!");
                }
            }
        });
Clone this wiki locally