Trick is a library for GBA image conversion and more.
import trick # import everything import trick/[common, gfxconvert] # import individual modules
See below for an API overview of each module.
common
This module contains some general utilitites for working with data.
Types
Name | Type | Summary |
---|---|---|
View[T] | object | Allows you to treat arbitrary memory like an array of some type, without copying. |
Procedures
Name | Parameters | Returns | Summary |
---|---|---|---|
viewSeq | seq[T] | View[T] | Interpret a sequence as a view |
viewSeqAs | seq[A] | View[B] | Interpret a sequence as a view of a different type |
viewBytesAs | string | View[T] | Interpret a string of binary data as a view of some type |
toSeq | View[T] | seq[T] | Convert a view to a sequence |
toBytes | View[T] | string | Convert a view to binary data |
toBytes | seq[T] | string | Convert a sequence to binary data |
makeCString | string | string | Convert a string of binary data to an escaped C string literal |
toCamelCase | name, firstUpper=false | string | Convert a string from snake_case to camelCase |
fileToVarName | name, firstUpper=false | string | Convert a filename to a variable name |
gfxconvert
Types and procedures to convert between PNG and raw binary image/palette data.
For reading and writing PNGs, Trick uses the nimPNG library by jangko.
Types
Name | Type | Summary |
---|---|---|
GfxInfo | object | Configuration object to encode/decode an image |
GfxColor | uint16 | 15-bit BGR color |
GfxPalette | seq[GfxColor] | List of colors |
GfxBpp | enum | Bits per pixel specifier |
GfxLayout | enum | Whether the image is arranged into 8x8 tiles |
Constants
Name | Type | Summary |
---|---|---|
clrEmpty | GfxColor | Special 'transparent' color |
Procedures
Name | Parameters | Returns | Summary |
---|---|---|---|
binToPng | data, conf | PNG | Convert raw GBA/NDS image data to a PNG object |
writeFile | filename, png | Write a PNG object to a file | |
pngToBin | filename, conf, buildPal | string | Load a PNG and convert to GBA/NDS image data |
readPal | filename | GfxPalette | Read a palette from a binary file |
writePal | filename, pal | Write a palette to a binary file | |
rgb5 | r, g, b | GfxColor | Create a color using 5-bit components |
rgb8 | r, g, b | GfxColor | Create a color using 8-bit components |
rgb8 | hex | GfxColor | Create a color using an 0xRRGGBB value |
swap4bpp | ab | ba | Swap the low and high nybbles within a byte |
swap2bpp | abcd | dcba | Swap the four pairs of bits within a byte |
Iterators
Name | Parameters | Returns | Summary |
---|---|---|---|
tileEncode | width, height, bpp=8 | (srcIndex, destIndex) | Unravel a tiled space into a bitmap space or vice versa |
bgconvert
Procedures for working with tiled backgrounds.
Types
Name | Type | Summary |
---|---|---|
Tile4 | array[32, uint8] | 8x8 pixels at 4bpp (paletted) |
Tile8 | array[64, uint8] | 8x8 pixels at 8bpp (paletted) |
Tile16 | array[32, GfxColor] | 8x8 pixels at 15bpp |
SomeTile | Tile4 | Tile8 | Tile16 | Typeclass for any kind of tile |
ScrEntry | distinct uint16 | Tile index with palette and flipping flags (via getters/setters) |
Screenblock | array[1024, ScrEntry] | A block of 32x32 screen entries |
Bg4 | object | 4bpp tiled background with a list of palettes |
Bg16 | object | 16bpp high-color tiled background (used for processing/conversion) |
Procedures
Name | Parameters | Returns | Summary |
---|---|---|---|
loadBg4 | filename | Bg4 | Load a PNG as a 4bpp tiled background |
loadBg8 | filename | Bg8 | Load a PNG as a 8bpp tiled background |
loadBg16 | filename | Bg16 | Load a PNG as a 15bpp (direct color) tiled background |
toBg4 | Bg8 | Bg4 | Convert a 8bpp background to 4bpp with strict rules |
toBg4 | Bg16 | Bg4 | Convert a 15bpp (direct color) background to 4bpp (paletted) |
reduce | tiles | (tiles, map) | Remove duplicates from a list of tiles, and build a tile map |
getPalettesFromTiles | tiles16 | seq[IntSet] | Get a list of palettes from a list of 15bpp tiles |
toScreenBlocks | map, w | seq[Screenblock] | Arrange a map into screenblocks (chunks of 32x32 tiles) |
flipX | tile | tile | Flip a tile horizontally |
flipY | tile | tile | Flip a tile vertically |
clear | tile | Erase all pixels in a tile |
palbuilder
This module implements a palette reduction algorithm: Given a list of palettes (of which many are duplicates or at least have colors in common), it attempts to merge them down into a reduced list of palettes.
This also produces a list of indexes, mapping palettes from the old list to the new list.
Note: Throughout this module, IntSet is used in place of GfxPalette, for performance reasons.
Procedures
Name | Parameters | Returns | Summary |
---|---|---|---|
reducePalettes | seq[IntSet] | (seq[GfxPalette], seq[int]) | Try to produce a minimal list of 16-color palettes |
toPalette | IntSet | GfxPalette | Convert an IntSet to a list of colors |
toIntSet | GfxPalette | IntSet | Convert a list of colors to an IntSet |
joinPalettes | seq[GfxPalette] | GfxPalette | Concatenate several palettes into one (with padding) |
mmutil
Note: This module is on the chopping block, best not to use.
Uses the Maxmod utility program (mmutil) to produce a soundbank, which is then converted to C. The soundbank header is converted to Nim, for use in your game.
Procedures
Name | Parameters | Returns | Summary |
---|---|---|---|
makeSoundbank | nimOutputPath, cOutputPath, | Create a soundbank from a list of audio files |
Variables
Name | Type | Summary |
---|---|---|
mmutilPath | string | Location of the mmutil executable. You shouldn't need to change this. |
Exports
-
viewSeqAs, viewBytesAs, viewSeq, []=, toBytes, toSeq, toBytes, items, fileToVarName, toCamelCase, makeCString, View, [], rgb5, rgb15, b=, GfxColor, g, pngToBin, clrEmpty, swap4bpp, swap2bpp, rgb8, binToPng, rgb8, writeFile, b, GfxPalette, readPng, GfxLayout, $, r, GfxInfo, readPal, ==, g=, r=, GfxBpp, tileEncode, writePal, palbank, clear, toBg4, vflip=, palbank=, Bg8, SomeTile, Bg4, Screenblock, Tile8, hflip, loadBg16, tid=, Bg16, toScreenBlocks, hash, toBg4, flipY, vflip, tid, Tile4, Tile16, loadBg8, toBg8, flipX, loadBg4, hflip=, reduce, ScrEntry, getPalettesFromTiles, toIntSet, reducePalettes, toPalette, joinPalettes, makeSoundbank, mmutilPath