Skip to content

2. Pixel Game Window

João Alves edited this page Jan 17, 2022 · 16 revisions

Introduction

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

📖 Want to jump right in? Learn with a code example.

Modules

To use the implementations refered on this section you must include the following module(s):

  • pixel-core

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 with 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.commons.DeltaTime;
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() {
       super.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 issues or want to know more on how to startup a game window, take a look at our code examples.

Closing a Pixel Game Window

To gracefully close the game, call the window close() function. This action stops the game loop, calls the window dispose() function and terminates the native window (including the associated resources).

gameWindow.close();
Clone this wiki locally