Skip to content

2. Pixel Game Window

João Alves edited this page Dec 18, 2021 · 16 revisions

Introduction

This section explores the starting point of the Pixel framework, the game window.

Game Window

The PixelWindow is the principal instance of your game. It is responsible for initiating the rendering engine (OpenGL) and associated dependencies (for example, the audio engine). In addition, it provides the groundwork for updating game logic and drawing pixels into the screen by providing a structured game loop:


pixel window game loop

Let's start by creating a new class which extends the PixelWindow like the example bellow:

import org.pixel.core.PixelWindow;
import org.pixel.core.WindowSettings;

public class BasicGameWindow extends PixelWindow {

    /**
     * Constructor
     *
     * @param settings
     */
    public BasicGameWindow(WindowSettings settings) {
        super(settings);
    }

    /**
     * Where the content of the game is loaded.
     */
    @Override
    public void load() {

    }

    /**
     * Where the game logic is updated.
     *
     * @param delta The elapsed time since the last frame
     */
    @Override
    public void update(DeltaTime delta) {

    }

    /**
     * Where the drawing of textures/text/primitives/etc happens.
     *
     * @param delta The elapsed time since the last frame
     */
    @Override
    public void draw(DeltaTime delta) {

    }

    /**
     * Where native instances are released from memory.
     */
    @Override
    public void dispose() {

    }
}

Window Settings

One of the requirements to create a PixelWindow is the WindowSettings. This configuration data structure contains runtime settings such as screen width, height, title, vsync and others.

Setup example:

WindowSettings settings = new WindowSettings(800, 600);
settings.setWindowTitle("My Pixel Game Window");
settings.setWindowResizable(true);
settings.setVsync(true);
settings.setMultisampling(2);
settings.setWindowMode(WindowMode.WINDOWED);

Starting a Pixel Game Window

After completing the previous steps (Window class and Settings), the game window can be initialized as follows:

PixelWindow gameWindow = new BasicGameWindow(settings);
gameWindow.start(); 

All done! If you run your application you should see a window similar to the one bellow:

game cycle

If you are facing troubles or want to know more on how to startup a game window, take a look at our code examples.

Clone this wiki locally