Java for SDL is a binding for SDL written in java.
Table Of Contents
-
Java for SDL
- About JSDL - what is it?
- About SDL
- Core Features - what can it
do?
- Extensions
-
Javadoc structure
- (J)SDL Architecture and API
- Package Examples
- Test Examples
-
Developing in JSDL
- JSDL Basics
- Environment
-
API Overview
- Initilializing SDL - sdl.core.Main
- Video - sdl.video.SDLSurface
- Video - sdl.video.SDLPixelFormat
- Events - sdl.event
- Audio - sdl.audio
Java for SDL
About JSDL
Java for SDL is a binding for SDL written in java. Now you can
use SDL and still program with Java. For more information, refer to
the project website at http://jsdl.sourceforge.net
Java's bindings are written in C to support JNI and Linux. This C part
should be easily portable to other platforms. SDL itself (not JSDL) currently
supports Linux, Win32, BeOS, MaxOS, Solaris, IRIX and FreeBSD.
About SDL ( copied from SDL
toc)
- SDL is a free cross-platform multi-media
development API
- Used for games
- Used for game SDKs
- Used for emulators
- Used for demos
- Used for multimedia applications
The project homepage of SDL is located at
http://www.libsdl.org/.
JSDL Core Features
Like core SDL, JSDL provides a set of interfaces to do the following
- Video : maniplate the graphics framebuffer
- query and set the state of the framebuffer
- create and render pixels to the graphics framebuffer
- work with offscreen image buffer that can be blitted to the framebuffer.
- Audio: play sound on an audio device.
- Events: and handle events
JSDL Extensions
A number of multimedia extensions have been added to the SDL API. Currently
the following SDL extensions have been wrapped by JSDL bindings.
- SDLMpeg: a high performance API set for loading, decoding
and displaying MPEG streams. (Mpeg 1, MP3 audio, etc..)
- SDLcd: API for opening and playing audio CDs.
- SDLImage: API for loading various bitmap file formats.
- SFont: API for rudimentary bitmap font manipulation.
Javadoc Structure - where to look for API and samples
(J)SDL Architecture
SDL is composed of eight subsystems
- Audio,
- CDROM,
- Event Handling,
- File I/O, *
- Joystick Handling,
- Threading *
- Timers *
- Video
Each subsytem has a corresponding API and must be initialized before use.
* Subsystem is handled by Java + JSDL
runtime, developer need not interact with SDL API's directly.
Package Examples
Each subsystem correponds to a java package. The naming convention
is sdl.subsystem or sdl.extension. Typically, each package
overview will describe the SDL subsystem and provide some sample code on
how to use the subsystem. For example, the Package overview
for sdl.audio describes how to work with the SDL Audio subsystem.
Test Examples
The unit tests and demos are located in the sdl.test package. The
have been added to the JSDL java docs to provide further examples on how
to code with JSDL.
Developing in JSDL
JSDL Development Basics
JSDL development mirrors standard SDL coding. The SDL official
web site has API documentation and tutorials that are esstential in understanding
how to develop using the Java SDL bindings.
Because of the similarity in syntax and implementation, the
SDL "C" APIs / tutorials can be easily
translated into JSDL java code.
JSDL Environment
JSDL requires Java and SDL to run. JSDL is comprised of
- a native dynamic library (typically libSDL4Java.so)
- a jar file (typically jsdl.jar)
libSDL4Java.so must be added to the java.library.path.
The easiest way to do this is to run java with the following arguments.
java -Djava.library.path=$JSDL_BASE
where $JSDL_BASE is the directory containing this library.
jsdl.jar must be added to the classpath
when compiling and testing your JSDL application.
A typical set of build / test commands would look something like
JSDL_BASE=/home/sstraw/build/jsdl
cd sdl/test
javac -classpath .:$JSDL_BASE TestVideo.java
cd ../..
java -Djava.library.path=$JSDL_BASE:/usr/lib -classpath .:$JSDL_BASE/build
sdl.test.TestVideo.java
API Overview
JSDL Runtime Interface
- sdl.core.Main
The core of any JSDL app starts with an instance of the sdl.core.Main
class to initialize the JSDL runtime environment.
Main sdl = new Main();
int result = sdl.SDLInit(Main.SDL_INIT_VIDEO);
Next, we need to initialize the video buffer through the java.video.Video
class, and retreive a reference to the "SDLSurface" object. The "surface"
object is our interface to the framebuffer itself. (We will discuss surfaces
later)
Video video = new Video();
SDLSurface surface = video.setVideoMode(640,480,16);
When are finished with our SDL routines, we (should) free whatever resources
we've consumed, unlock the framebufffer, etc. This is done through
the following method in the "Main" object.
sdl.SDLQuit()
Surfaces
Surfaces represent the buffer you will read and write "raw" pixels to.
A surface may refer to the acutal graphics device,
SDLSurface surface = video.setVideoMode(640,480,16);
or may refer to an offscreen image buffer.
SDLSurface image = SDLImage.createFromFile("/tmp/icon23.jpg");
Since surfaces in JSDL have built in buffering, it is necessary to "update"
the surface so the changes are pushed to the actual graphics device.
surface.updateRawPixels();
surface.updateRect(0,0,0,0);
Pixels and PixelFormat
Low level drawing to the surface is done by defining pixels. Each
Pixel is an integer (int, long or short) that represents a RGB color
value. The number of bits used by this long is determined by the color
depth. Attributes like color depth and bitmasks used to extract color
component information are stored in the SDLPixelFormat class. It is
very helpful to review the JSDL test examples and the SDL documentation.
The following code creates a pixel with a pixel depth greater than 8 bits.
(8 bit color is a special case)
public static short createHiColor(SDLPixelFormat
fmt, byte red, byte green, byte blue)
{
short value = (short) (((red
>> fmt.m_RLoss) << fmt.m_RShift) + ((green >> fmt.m_GLoss)
<< fmt.m_GShift) + ((blue >> fmt.m_BLoss) << fmt.m_BShift));
return value;
}
An alternative convenience routine creates a pixel value based on the
color depth of an arbitrary surface.
int colorkey = surface.mapRGB((byte)0,(byte)0,(byte)0)
;
Events
JSDL presents SDL events as standard Java events. However the JSDL Event
mechanism is based on the SDL Event subsystem. Thus JSDL Event development
incorporates aspects of both Standard Java Events and SDL Events.
JSDL supports the following types of events.
- SDLKeyboardEvent
- SDLMouseButtonEvent
- SDLMouseMotionEvent
- SDLQuitEvent
- SDLCustomEvent
- SDLUserEvent
Classes that will listen to JSDL events must implement the sdl.event.SDLEventListener
interface and create this single method,
public boolean incomingEvents(SDLCustomEvent
event) throws SDLEventException;
Audio
JSDL provides a set of classes to control the audio device and to load/play
audio streams. Refer to the javadocs on use of the following classes
- SDLAudio: wrapper class that controls the audio device.
- SDLAudioSpec: class that describes the attributes of the audio device.
- AudioPlay: represents an individual audio file.
- AudioCallBack: interface of the class that controls the playing
of an audio file.
jsdl4scott 09/22/02