Main Menu Custom Cursor - UQcsse3200/2024-studio-2 GitHub Wiki

Custom Cursor in LibGDX

Introduction

LibGDX allows you to replace the default system cursor with a custom one, enhancing the game's UI and player experience. This guide explains how to set up a custom cursor using Pixmap and Gdx.graphics.newCursor().

Steps

1. Prepare the Image

Use a PNG image for the custom cursor, preferably around 32x32 or 64x64 pixels.

image

2. Load the Image

Pixmap pixmap = new Pixmap(Gdx.files.internal("images/CustomCursor.png"));

3. Create the Cursor

customCursor = Gdx.graphics.newCursor(pixmap, pixmap.getWidth() / 4, pixmap.getHeight() / 4);

4. Set the Custom Cursor

Gdx.graphics.setCursor(customCursor);
pixmap.dispose();  // Clean up resources

5. Dispose Resources

@Override
public void dispose() {
    customCursor.dispose();
}

Best Practices

  1. Use small, appropriately sized images.
  2. Set the hotspot correctly for intuitive clicking.
  3. Dispose of resources to avoid memory leaks.

UML Diagram

image