# Bitmap text

> useLoadBitmapFont loads a bitmap font and createText writes with it, live score counter included.

Example `bitmap-text`, running at https://nacatamalon.com/en/examples/bitmap-text. The whole catalogue: https://nacatamalon.com/llms/examples.md.

`useLoadBitmapFont` loads a bitmap font (an image plus a file saying where each character is) and `createText` writes with it. `style.fontSize` is the height of a line in pixels: the arcade font is 8 px tall, so 32 draws it four times as big and stays crisp.

The score is plain data, so `score.text = ...` shows on the next frame. The last line is written in lowercase, and this font has lowercase letters, so it is drawn as written; a font without them would draw its capitals instead.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { Scene } from './scenes/demo';
import { WIDTH, HEIGHT } from './settings';

const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    background: '#0a0a18'
});

// Declare Scenes
game({
    Scene
});
```

`components/score.ts`:

```ts
import { createText, useLoadBitmapFont, useUpdate } from 'nacatamalon';
import { ARCADE, SCORE } from '../settings';

export const Score = () => {
    const font = useLoadBitmapFont(ARCADE);

    // The text comes back as plain data: change .text and the next frame shows it.
    const score = createText({
        text: 'SCORE 0',
        font,
        style: {
            fontSize: 24
        },
        tint: '#4cff91',
        transform: {
            x: 16,
            y: 86,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    let points = 0;
    let elapsed = 0;

    useUpdate((delta) => {
        elapsed += delta;

        if (elapsed < SCORE.interval) {
            return;
        }

        elapsed = 0;
        points += SCORE.increment;
        score.text = 'SCORE ' + points;
    });
};
```

`components/specimen.ts`:

```ts
import { createText, useLoadBitmapFont } from 'nacatamalon';
import { ARCADE } from '../settings';

export const Specimen = () => {
    const font = useLoadBitmapFont(ARCADE);

    createText({
        text: 'NACATAMAL ON',
        font,
        style: {
            fontSize: 32
        },
        tint: '#f5c518',
        transform: {
            x: 16,
            y: 20,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    createText({
        text: 'INSERT COIN',
        font,
        // letterSpacing is in final pixels, like in CSS.
        style: {
            fontSize: 16,
            letterSpacing: 4
        },
        transform: {
            x: 16,
            y: 140,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    // The font contains lowercase glyphs; fonts without them fall back to capitals.
    createText({
        text: '0123456789 abcdefghijklmnopqrstuvwxyz',
        font,
        tint: '#667799',
        transform: {
            x: 16,
            y: 188,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { Specimen } from '../components/specimen';
import { Score } from '../components/score';

export const Scene = () => {
    useSpawn(Specimen)();
    useSpawn(Score)();

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 220;
export const SCORE = {
    interval: 0.08,
    increment: 10
};
export const ARCADE = {
    json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
    atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png'
};
```
