Using Slick Utilities Without the Framework

From Slick2D Wiki
Jump to: navigation, search

This page covers all information related to using the individual parts of the Slick library instead as opposed to the library as a whole, by means of the Slick-Util library, in order to interface directly with LWJGL. You can grab the Slick-Util library from here.

Remember: Slick is not a game engine. It doesn't intend to take over how you want to work or render. It's intended as a set of utilities that mesh together to form the library. Each of these utilities can be used in their own right. It can act like an engine if you use the GameContainer utilities - but this isn't an absolute. If you simple need something to get images on to the screen and want to manage the game loop yourself - fine! Just pick up the library and use Image. Input, Sound and Fonts among other things are all intended to be useable this way.

So, if you want a complete solution, you can use GameContainer and have Slick manage alot for you. If you just want some utilities, Slick can help there too. More so, if you want some source code to look at to get an idea where to start with something - go ahead. The license for Slick is loose - the code running everything is intentionally simplistic so it should be easy to pick up and understand.

Overall, let us know what you do with things - it's really fun to find a new game to play in any way!

Texture Handling[edit]

Texture loading in Slick is designed to work directly with the Image classes. When using Slick as a set of utilities within your LWJGL game you may find it required to load textures and bind them outside of Slick (for certain custom effects, different data sources). There is one gotcha here, Slick caches the last texture it bound - so that textures aren't rebound if they're already current. This means that should you bind a different texture outside of the Slick framework it may confuse the rendering in Slick.

To get round this, before you start doing your own thing with rendering in LWJGL, call the following on texture:

texture.unbind();

This will clear the internal cacheing state and allow you to do whatever you need with textures through OpenGL without contaminating your Slick rendering.

Sound with Slick-Util[edit]

The slick-util library provides a simple and easy to use mechanism to load and play sounds via openAL.

It supports the following formats:

  • wav
  • aif
  • ogg
  • xm

Sounds can be loaded and played as sfx and music can be streamed.

Basic Example[edit]

Here's some basic code that loads an ogg file and plays it.

Audio sound = AudioLoader.getAudio("OGG",
             new FileInputStream("sfx/enemyHit.ogg"));
sound.playAsSoundEffect(1.0f, 1.0f, false);

This is a simple example, typically you would load your sounds in some sort of initialization phase, then play the sounds at points during your main game loop. It's still this easy though.

Native libraries and Java dependancies[edit]

You need to include the openAL native libraries from the lwjgl distribution in your runtime environment for openAL to work.

If you use ogg audio, you need to include the following jar files from the slick-util package:

  • jogg-0.0.7.jar
  • jorbis-0.0.15.jar

If you use Mod or XM audio you'll need to include the following jar file:

  • ibxm.jar

Loading and Binding GL Textures[edit]

The slick-util library provides a simple and easy to use mechanism to load images into openGL textures, and a way to bind those textures while rendering.

The texture loader supports the following graphics formats:

  • tga
  • png

Basic Example[edit]

Here's some basic code that shows how to load a texture file and bind it.

// init gl
Texture tex = TextureLoader.getTexture("PNG",
                new FileInputStream("example.png"));
 
// in your render loop:
tex.bind();
 
// gl rendering code etc..

TrueType Fonts with Slick-Util[edit]

Slick has the capability to easily display True Type fonts on screen.

True Type Fonts are loaded and rendered using the org.newdawn.slick.TrueTypeFont class.

import org.newdawn.slick.TrueTypeFont;

You can find the full javadoc to the TrueTypeFont class here.

Basic Usage[edit]

// initialise the font
Font font = new Font("Verdana", Font.BOLD, 20);
TrueTypeFont trueTypeFont = new TrueTypeFont(font, true);
 
// render some text to the screen
trueTypeFont.drawString(20.0f, 20.0f, "Slick displaying True Type Fonts", Color.green);


Additional Usage[edit]

Anti Aliasing[edit]

The second parameter to the TrueTypeFont constructor controls whether the font is antialiased or not.

Loading fonts from the classpath[edit]

There are a number of true type fonts available on any java system. For reference, see:

http://java.sun.com/j2se/1.5.0/docs/api/index.html?java/awt/Font.html

Most people will also want to load custom fonts for display. Below is a short example which loads a ttf font that has been included in the programs classpath.

import org.newdawn.slick.Color;
import org.newdawn.slick.TrueTypeFont;
 
import java.awt.Font;
import java.io.BufferedInputStream;
import java.io.IOException;
 
public class TTFTest {
 
    public static void main(String[] args)
        throws LWJGLException, FontFormatException, IOException {
 
        Font font;
        TrueTypeFont trueTypeFont;
 
        // not included for brevity
        initGL();
 
        Font startFont = Font.createFont(Font.TRUETYPE_FONT,
    new BufferedInputStream(TTFTest.class.getClassLoader().getResourceAsStream(
    "technoid.ttf")));
        Font baseFont = startFont.deriveFont(Font.PLAIN, 20);
        trueTypeFont = new TrueTypeFont(baseFont, true);
 
        while (true) {
            GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
            trueTypeFont.drawString(20.0f, 20.0f, "this is a test", Color.gray);
 
            Display.update();
            if (Display.isCloseRequested()) {
                System.exit(0);
            }
        }
    }
}