Module atlases

Overview

An Atlas, like a Texture, represents raw image data in GPU memory. However, an Atlas also has an associated two-dimensional "partition size" which must evenly partition the underlying texture. Instead of blitting the entire texture, specific sub-regions can be drawn by referencing them numerically. The indexes are assigned left-to-right and top-to-bottom:

Example sub-region layouts
+---+---+---+      +---+---+
|   |   |   |      |   |   |
| 0 | 1 | 2 |      | 0 | 1 |
|   |   |   |      |   |   |
+-----------+      +-------+     +---+---+---+---+---+---+
|   |   |   |      |   |   |     |   |   |   |   |   |   |
| 3 | 4 | 5 |      | 2 | 3 |     | 0 | 1 | 2 | 3 | 4 | 5 |
|   |   |   |      |   |   |     |   |   |   |   |   |   |
+---+---+---+      +-------+     +---+---+---+---+---+---+
                   |   |   |
                   | 4 | 5 |
                   |   |   |
                   +---+---+

Types

AtlasInfo = object
  filename*: string            ## filename used to load the Atlas
  width*: int                  ## the width of the Atlas partition
  height*: int                 ## the height of the Atlas partition
  name*: string                ## name of the Atlas in an AtlasManager
  description*: string         ## description of the Atlas
  authors*: seq[string]        ## authors of the Atlas
  
Meta-data describing an Atlas
Atlas = ref object
  info*: AtlasInfo             ## meta-data describing the Atlas
  width*: int                  ## Atlas width in sub-regions
  height*: int                 ## Atlas height in sub-regions
  texture*: Texture            ## Texture underlying the Atlas
  
Used for rendering sub-regions of a Texture
AtlasManager = ref object
  textures: TextureManager     ## textures backing managed Atlases
  registry: Table[string, Atlas] ## loaded Atlases by name
  
Used for loading and managing Atlases

Procs

proc newAtlasManager(window: WindowPtr; display: RendererPtr): AtlasManager {.
    raises: [], tags: [].}
proc load(am: AtlasManager; name, filename: string; width, height: int;
         description: string = nil; authors: seq[string] = nil): Atlas {.
    raises: [KeyError, NoSuchResourceError, ValueError, InvalidResourceError],
    tags: [ReadDirEffect].}
Load an image resource from disk partitioned into sub-regions of width and height. Once loaded sub-regions may be rendered by index. The Atlas can be retrieved from the AtlasManager using the provided name.
proc loadPack(am: AtlasManager; filename: string) {.raises: [ValueError, IOError,
    Exception, JsonParsingError, IndexError, KeyError, NoSuchResourceError,
    InvalidResourceError], tags: [ReadIOEffect, RootEffect, ReadDirEffect].}

Load a resource-pack of Altases. Assets inside of an Atlas resource-pack should be unmarshalable by the AtlasAsset type.

Example AtlasAsset JSON

"example_atlas_asset": {
  "filename": "atlases/example_atlas.png",
  "width": 32, "height": 32
}
proc get(am: AtlasManager; name: string): Atlas {.
    raises: [NoSuchResourceError, KeyError], tags: [].}
Get a loaded Atlas by name
proc render(display: RendererPtr; atlas: Atlas; rx, ry, dx, dy: int) {.raises: [], tags: [].}
Render the sub-region rx, ry of atlas to the destination dx, dy of display
proc render(display: RendererPtr; atlas: Atlas; n, dx, dy: int) {.
    raises: [DivByZeroError], tags: [].}
Render the indexed sub-region n of atlas to the destination dx, dy of display