Intro
Hello, first of all. This is the first official JA2DAPI blog entry. We'll go ahead and jump into some basic things about the API...
Okay, so to start things off, there's a kick-off class called Frame2D that is a simple way to create a window (or full screen display), as well as an animation and render environment. To create the Frame2D, you specify the size of the window, whether it is to be full screen or not, and the type of Painter you want. The Painter is what handles the rendering and animation of things. There's currently two Painters that are available: The GamePainter is designed for applications that have a lot of continuous animation (most games) and EventDrivenPainter is designed for applications where updates (and render calls) are driven by specific, typically user input-driven, events.
Here's a way to create a Frame2D that is a 1024X768 window with a GamePainter:
import twodapi.core.*;
import java.awt.Graphics;
public class StarterTest
{
public static void main(String[] args)
{
Frame2D gameFrame = new Frame2D (Frame2D.REG_PAINTER, 1024, 768, false);
GamePainter painter = (GamePainter)gameFrame.getPainter();
}
}
Notice that we immediately have a GamePainter ready to go. The GamePainter is what we use to add objects to the window. To demonstrate, we add this code to main():
Sprite2D ship= new Sprite2D("/images/ship.gif");
painter.addToPaintList(ship);
ship.setLocation(500, 400);
Here we created a Sprite2D object based on the "ship.gif" image and added it to the GamePainter's paint list. Then, we moved the ship sprite to be closer to the center of the window.
Animation
So the animation framework I've adopted is a typical "update and render" loop. Under the hood, there's an AnimationThread running hard at work to maintain the frame rate you have specified. FYI, all sprite objects (Sprite2D, FlipbookSprite, and MouseSprite) automatically receive updates from the Painter after they are added to the paint list. We can access these updates by extending a sprite class and override updateState(float interpolation). See below:
class ShipSprite extends Sprite2D
{
public ShipSprite()
{
super("/images/ship.gif");
}
public void updateState(float interpolation)
{
rotate(Math.PI/16);
}
}
In the constructor (public ShipSprite()), we are merely calling the Sprite2D constructor and passing in the filename for the ship image as we did above. What's new is the override of the method updateState(). This method is being called by the GamePainter every frame update (50 frame per second, by default), and here we are rotating the ship Pi/16 radians per update.
Well, that's it for now! There are a couple of other ways to animate in JA2DAPI, and I will explore those in the next blog. Stay tuned!
No comments:
Post a Comment