Skip to content

Commit

Permalink
Working with Font/Bitmap/ByteArray assets
Browse files Browse the repository at this point in the history
  • Loading branch information
joshtynjala committed Oct 11, 2024
1 parent 3442940 commit 278f095
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
3 changes: 3 additions & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
* [Compressing bitmap data](working-with-bitmaps\compressing-bitmap-data.md)
* [Making textures with noise functions](working-with-bitmaps\making-textures-with-noise-functions.md)
* [Scrolling bitmaps](working-with-bitmaps\scrolling-bitmaps.md)
* [Working with bitmap assets](working-with-bitmaps\working-with-bitmap-assets.md)
# * [Taking advantage of mipmapping](working-with-bitmaps\taking-advantage-of-mipmapping.md)
# * [Bitmap example: Animated spinning moon](working-with-bitmaps\bitmap-example-animated-spinning-moon.md)
# * [Asynchronous decoding of bitmap images](working-with-bitmaps\asynchronous-decoding-of-bitmap-images.md)
Expand Down Expand Up @@ -94,6 +95,7 @@
* [Formatting text](using-the-textfield-class\formatting-text.md)
* [Advanced text rendering](using-the-textfield-class\advanced-text-rendering.md)
* [Working with static text](using-the-textfield-class\working-with-static-text.md)
* [Working with font assets](working-with-bitmaps\working-with-font-assets.md)
# * [TextField Example: Newspaper-style text formatting](using-the-textfield-class\textfield-example-newspaper-style-text-formatting.md)
* [Chapter 24: Working with sound](working-with-sound\README.md)
* [Basics of working with sound](working-with-sound\basics-of-working-with-sound.md)
Expand Down Expand Up @@ -165,6 +167,7 @@
# * [Encrypted local storage](storing-local-data\encrypted-local-storage.md)
* [Chapter 41: Working with byte arrays](working-with-byte-arrays\README.md)
* [Reading a writing a byte array](working-with-byte-arrays\reading-and-writing-a-byte-array.md)
* [Working with byte array assets](working-with-bitmaps\working-with-byte-array-assets.md)
# * [ByteArray example: Reading a .zip file](working-with-byte-arrays\bytearray-example-reading-a-zip-file.md)
#* [Chapter 42: Basics of networking and communication](basics-of-networking-and-communication\README.md)
# * [Network connectivity changes](basics-of-networking-and-communication\network-connectivity-changes.md)
Expand Down
54 changes: 54 additions & 0 deletions using-the-textfield-class/working-with-font-assets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Working with font assets {#working-with-font-assets}

Using font assets allows custom fonts to be included with your OpenFL project. In some cases, font assets will be embedded in the
generated binary, and in others, they will be included as separate files.
However, the API for working with either option is the same, allowing you to
write code once and target many platforms.

## Specifying font assets

In a
[Lime _project.xml_ file](https://lime.openfl.org/docs/project-files/xml-format/),
include font image files using the
[`<assets>` element](https://lime.openfl.org/docs/project-files/xml-format/#assets).
Font assets may be of the `font` type only.

- font

```xml
<assets path="assets">
<font path="fnt/MyFont.ttf" id="MyFont" />
<font path="fnt/AnotherFont.otf" />
</assets>
```

## Using font assets

The following code works with a font asset with the id "fnt". The code gets
an instance of the font asset and uses it with a TextField:

```haxe
import openfl.utils.Assets;
import openfl.display.Sprite;
import openfl.text.Font;
import openfl.text.TextFormat;
import openfl.text.TextField;
class FontAssetExample extends Sprite
{
public function new()
{
super();
var fnt:Font = Assets.getFont("MyFont");
var tf:TextField = new TextField();
tf.defaultTextFormat = new TextFormat(fnt.getName());
tf.text = "Hello world";
addChild(tf);
}
}
```

## More Help topics

- [openfl.utils.Assets class](https://api.openfl.org/openfl/utils/Assets.html)
- [project.xml `<assets>` element](https://lime.openfl.org/docs/project-files/xml-format/#assets)
53 changes: 53 additions & 0 deletions working-with-bitmaps/working-with-bitmap-assets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Working with bitmap assets {#working-with-bitmap-assets}

Using bitmap assets, instead of loading bitmaps from a web URL, a file system
path, or generating the bitmap data manually, is another possibility for
rendering bitmap images. In some cases, bitmap assets will be embedded in the
generated binary, and in others, they will be included as separate files.
However, the API for working with either option is the same, allowing you to
write code once and target many platforms.

## Specifying bitmap assets

In a
[Lime _project.xml_ file](https://lime.openfl.org/docs/project-files/xml-format/),
include bitmap image files using the
[`<assets>` element](https://lime.openfl.org/docs/project-files/xml-format/#assets).
Bitmap assets may be of the `bitmap` type only.

- image

```xml
<assets path="assets">
<image path="img/MyImage.png" id="MyImage" />
<image path="img/AnotherImage.jpg" />
</assets>
```

## Using bitmap assets

The following code works with a bitmap asset with the id "MyImage". The code
gets an instance of the bitmap asset and passes it to a new Bitmap display
object:

```haxe
import openfl.utils.Assets;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.Sprite;
class BitmapAssetExample extends Sprite
{
public function new()
{
super();
var bmd:BitmapData = Assets.getBitmapData("MyImage");
addChild(new Bitmap(bmd));
}
}
```

## More Help topics

- [openfl.utils.Assets class](https://api.openfl.org/openfl/utils/Assets.html)
- [project.xml `<assets>` element](https://lime.openfl.org/docs/project-files/xml-format/#assets)
51 changes: 51 additions & 0 deletions working-with-byte-arrays/working-with-byte-array-assets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Working with byte array assets {#working-with-byte-array-assets}

Using byte array assets, instead of loading binary data from a web URL or a file
system pat is another possibility for reading byte arrays. In some cases, byte
array assets will be embedded in the generated binary, and in others, they will
be included as separate files. However, the API for working with either option
is the same, allowing you to write code once and target many platforms.

## Specifying byte array assets

In a
[Lime _project.xml_ file](https://lime.openfl.org/docs/project-files/xml-format/),
include binary files using the
[`<assets>` element](https://lime.openfl.org/docs/project-files/xml-format/#assets).
Byte array assets may be of the `binary` type only.

- image

```xml
<assets path="assets">
<binary path="img/MyBytes.dat" id="MyBytes" />
<binary path="img/AnotherFile.bin" />
</assets>
```

## Using byte array assets

The following code works with a byte array asset with the id "MyBytes". The code
gets an instance of the byte array asset and prints its length to the debug
console:

```haxe
import openfl.utils.Assets;
import openfl.display.Sprite;
import openfl.utils.ByteArray;
class BinaryAssetExample extends Sprite
{
public function new()
{
super();
var bytes:ByteArray = Assets.getBytes("MyBytes");
trace(bytes.length);
}
}
```

## More Help topics

- [openfl.utils.Assets class](https://api.openfl.org/openfl/utils/Assets.html)
- [project.xml `<assets>` element](https://lime.openfl.org/docs/project-files/xml-format/#assets)

0 comments on commit 278f095

Please sign in to comment.