Heaps is a cross platform graphics engine designed for high performance games.
Heaps has been designed to leverage modern GPU that are now commonly available on both desktop and mobile devices.
Heaps currently supports HTML5 WebGL, Flash Stage3D, native Mobile (iOS and Android) and Desktop with OpenGL.
Heaps consists in several top level packages, which are:
h2d used for 2D display on screen (for 2D games and user interface)h3d used for rendering 3D modelshxd contains cross platform classes such as Bitmaps, and a complete resource loading and management frameworkhxsl is the Heaps Shader Language implementationIn order to get started with Heaps, you need first to:
haxelib install heapsYou should then be ready to run your first example:
class Main extends hxd.App {
var bmp : h2d.Bitmap;
override function initialize() {
var tile = h2d.Tile.fromColor(0xFF0000, 100, 100);
bmp = new h2d.Bitmap(tile, s2d);
bmp.x = s2d.width * 0.5;
bmp.y = s2d.height * 0.5;
}
override function update(dt:Float) {
bmp.rotation += 0.1;
}
static function main() {
new Main();
}
}
In order to compile, make sure you have included the Heaps library or have added -lib heaps to your compilation parameters.
index.html that includes your .js haxe output-swf-version 11.8) which is required for HeapsYou should now be able to compile and display the example. It will show you a rotating red square.
Several examples for both 2D and 3D are available in the heaps samples directory.
Before entering into the details of h2d, let's introduce a few concepts that we will use in the documentation:
hxd.Bitmap type) is a picture stored in local memory which you can modify and access its individual pixels. In Heaps, before being displayed, a Bitmap need to be turned into a Textureh3d.mat.Texture type) is a picture that is allocated into the GPU memory. You can no longer access its pixels or modify it in an efficient way. But it can be used to display 3D models or 2D pictures.h2d.Tile) is a sub part of a Texture. For instance a 256x256 Texture might contain several graphics, such as different the frames of an animated sprite. A Tile will be a part of this texture, it has a (x,y) position and a (width,height) size in pixels. It can also have a pivot position (dx,dy).(-tile.width,-tile.height), it will now be at the bottom right of the Tile. Changing the pivot affects the way Bitmap Sprites are displayed and the way local transformations (such as rotations) are performed.h2d.Sprite) is the base class of all of H2D displayable objects. A Sprite has a position (x,y), a scale (scaleX,scaleY), a rotation. It can contain other Sprites which will inherit its transformations, creating a scene treeh2d.Scene) is a special Sprite which is at the root of the scene tree. In hxd.App is it accessible with the s2d variable. You will need to add your Sprites to the scene before they can be displayed. The Scene also handles events such as clicks, touch, and keyboard keys.h2d.Bitmap) is a Sprite that allows you to display an unique Tile at the sprite position, such as in the previous example.Now that the basic concepts have been introduced, let's get back to our previous example, this time with comments:
class Main extends hxd.App {
var bmp : h2d.Bitmap;
override function initialize() {
// allocate a Texture with red color and creates a 100x100 Tile from it
var tile = h2d.Tile.fromColor(0xFF0000, 100, 100);
// create a Bitmap sprite, which will display the tile
// and will be added to our 2D scene (s2d)
bmp = new h2d.Bitmap(tile, s2d);
// modify the display position of the Bitmap sprite
bmp.x = s2d.width * 0.5;
bmp.y = s2d.height * 0.5;
}
// on each frame
override function update(dt:Float) {
// increment the display bitmap rotation by 0.1 radians
bmp.rotation += 0.1;
}
static function main() {
new Main();
}
}
We can easily make the Bitmap rotate around its center by changing the tile pivot, by adding the following lines:
bmp.tile.dx = -50; bmp.tile.dy = -50;
The following properties and methods can be accessed on any Sprite:
x and y : the position in pixels relative to the parent Sprite (or in the Scene)rotation : the rotation of the sprite in radiansscaleX and scaleY : the horizontal and vertical scaling values for this Sprite (default to (1,1)). You can uniformaly increase the current scales by calling sprite.scale(1.1) or set them to give value by using sprite.setScale(value).visible : when visible is set to false, a sprite is still updated (position is calculated and animation still plays) but the sprite and all its children are not displayedparent : the current parent Sprite, or null if it has not been addedremove() : remove the sprite from its parent. This will prevent it from being updated and displayedaddChild(s) : adds the specified sprite to the children listfor( s in sprite ) {...} : iterates over all the current children
Sprite have other properties and methods which can be discovered by visiting the h2d.Sprite API section.
H2D classes that can display something on screen usually extends the h2d.Drawable class.
Each Drawable (including h2d.Bitmap) has then several properties that can be manipulated:
alpha : this will change the amount of transparency your drawable is displayed with. For instance a value of 0.5 will display a Tile with 50% opacity.color : color is the color multiplier of the drawable. You can access its individual channels with (r,g,b,a) components. It is initialy set to white (all components are set to 1.). Setting for instance the (r,g,b) components to 0.5 will make the tile appear more dark.
blendMode : the blend mode tells how the drawable gets composited with the background color when its drawn on the screen.
The background color refers to the screen content at the time the sprite is being drawn. This can be another sprite content if it was drawn before at the same position.
The following blend mode values are available :Alpha (default) : the drawable blends itself with the background using its alpha value. An opaque pixel will erase the background, a fully transparent one will be ignored.None : this disable the blending with the background. Alpha channel is ignored and the color is written as-it. This offers the best display performances for large backgrounds that have nothing showing behind themAdd : the drawable color will be added to the background, useful for creating explosions effects or particles for instance.SoftAdd : similar to Add but will prevent over saturationMultiply : the sprite color is multiplied by the background colorErase : the sprite color is substracted to the background colorfilter : when a sprite is scaled (upscaled or downscaled), by default Heaps will use the nearest pixel in the Tile to display it. This will create a nice pixelated effect for some games, but might not looks good on your game. You can try to set the filter value to true, which will enable bilinear filtering on the sprite, making it looks less sharp and more smooth/blurry.shaders : each Drawable can have shaders added to modify their display. Shaders will be introduced later in this manual.
Drawable have other properties which can be discovered by visiting the h2d.Drawable API section.
Creating an animated sprite in H2D is quite easy.
Instead of using h2d.Bitmap to display a single Tile, you can use h2d.Anim to display a list of tiles that will automatically be played:
// creates three tiles with different color var t1 = h2d.Tile.fromColor(0xFF0000, 30, 30); var t2 = h2d.Tile.fromColor(0x00FF00, 30, 40); var t3 = h2d.Tile.fromColor(0x0000FF, 30, 50); // creates an animation for these tiles var anim = new h2d.Anim([t1,t2,t3],s2d);
The following properties and methods can be accessed on h2d.Anim:
speed : changes the playback speed of the animation, in frames per seconds.loop : tells if the animation will loop after it reaches the last frame.onAnimEnd : this dynamic method can be set to be informed when we have reached the end of the animation :
anim.onAnimEnd = function() {
trace("END!");
}
Anim have other properties which can be discovered by visiting the h2d.Anim API section.
Before start with text manipulation, we must know the h2d.Font class:
h2d.Font is a manager who links each character with his h2d.Tile: it is used to display each letter of a text;h2d.Font;h2d.Font we must have:
Do not forget to add to your compilation parameters the location of your resource files -D resourcesPath=yourPath
Copy the two font files in your resources directory:
You can use h2d.Font like this:
// compilation parameter: -D resourcesPath=res // location of the files: res/customFont.fnt and res/customFont.png // load a bitmap font Resource: var font = hxd.Res.customFont.toFont();
h2d.Text extends h2d.Drawable to display text with H2D.
h2d.Text is a text field whose you can easily change the textual content with its property text.
To manipulate easily h2d.Text you can use the following properties:
text: change the textual content of the text field.textColor: the color in hexadecimal format. This property also modify the color property (see h2d.Drawable).maxWidth: width limit of the text field. The text field is multi line: the textual content length exceeding the maxWidth is added with a new line. The wordwrap does not cut the words: if the length of a word exceed the maxWidth, it does not cut the word.font: the font used to display the text. See h2d.Font to use it.letterSpacing: integer in pixels representing the amount of space that is distributed between all characters. Default value is letterSpacing = 0;lineSpacing: integer in pixels representing the amount of vertical space between lines. Default value is lineSpacing = 0;, it corresponds to the basic spacing between lines.dropShadow: a solid drop shadow of your text. To disable it, use the value dropShadow = null;.
To configure dropShadow you can use a Typedef with these properties:
dx and dy: define the distance in pixel between the text and his drop shadow. For instance the values : dx = 2.5; and dy = 5.0; moves the shadow from the text to 2.5 pixel to the right and 5 pixel to the bottom.color: the hexadecimal color of the shadow (like color = 0x111133;).alpha: this will change the amount of transparency of your drop shadow. For instance a value of alpha = 0.25; will display a drop shadow with 25% opacity.textWidth and textHeight: get the size of the text field in pixel. You can't edit it. If you would change the width, use the maxWidth property.You should then be ready to display your first text:
// load a bitmap font Resource
var font = hxd.Res.customFont.toFont();
// adds a red shadow
tf.dropShadow = { dx : 3, dy : 3, color : 0xFF0000, alpha : 0.8 };
// creates a text field with this font
var tf = new h2d.Text(font, s2d);
tf.text = "Hello h2d!";