# Fonts across scenes

> One scene loads every font under a key; the rest of the game names them with font: 'title'.

Example `fonts-across-scenes`, running at https://nacatamalon.com/en/examples/fonts-across-scenes. The whole catalogue: https://nacatamalon.com/llms/examples.md.

One scene loads every font, the rest only name them. `Loading` calls `useLoadFont` with a `key` for each, shows the progress with `useLoadProgress`, and moves on when it is done.

`Title` and `Level` never load anything: `createText({ font: 'title' })` takes the font the game already holds under that key, instead of the font record. A key nobody loaded throws, naming it, rather than quietly drawing with another font.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import { WIDTH, HEIGHT } from './settings';
import { Loading } from './scenes/loading';
import { Title } from './scenes/title';
import { Level } from './scenes/level';

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

// Declare Scenes
game({
    Loading,
    Title,
    Level
});
```

`scenes/level.ts`:

```ts
import { createScene, createText, useScene, useUpdate } from 'nacatamalon';

import { YELLOW, WHITE, CENTRED } from '../settings';

export const Level = () => {
    const scene = useScene();

    createText({
        text: 'Level 1',
        font: 'title',
        style: {
            fontSize: 48
        },
        tint: YELLOW,
        anchor: CENTRED,
        transform: {
            x: 240,
            y: 30
        }
    });

    const score = createText({
        text: 'SCORE 0',
        font: 'ui',
        style: {
            fontSize: 16
        },
        tint: WHITE,
        transform: {
            x: 24,
            y: 140
        }
    });

    createText({
        text: 'BACK',
        font: 'ui',
        style: {
            fontSize: 16
        },
        tint: WHITE,
        anchor: CENTRED,
        transform: {
            x: 240,
            y: 210
        },
        onClick: () => scene.change('Title'),
    });

    let points = 0;

    useUpdate((delta) => {
        points += Math.round(delta * 300);
        score.text = `SCORE ${points}`;
    });

    return createScene();
};
```

`scenes/loading.ts`:

```ts
import { createScene, createSprite, createText, useLoadFont, useLoadProgress, useScene, useUpdate } from 'nacatamalon';

// The ONLY place the game's fonts are loaded. Each goes under a key, and from here on every scene
// names it by that key alone: no useLoadFont anywhere else.
export const Loading = () => {
    const fonts = [
        useLoadFont({
            src: '/example-assets/fonts/vector/Bangers-Regular.ttf',
            key: 'title'
        }),
        // 'chars' draws these letters while loading, so the first screens that show them do not.
        useLoadFont({
            src: '/example-assets/fonts/vector/PressStart2P-Regular.ttf',
            key: 'ui',
            chars: '0123456789 SCORECLICKTOPLAYBACKLEVEL'
        }),
    ];

    const loader = useLoadProgress(fonts);
    const scene = useScene();

    // Nothing of ours is loaded yet, so this screen uses the built-in font, which needs no loading.
    createText({
        text: 'LOADING FONTS',
        style: {
            fontSize: 8
        },
        tint: '#8a8fa8',
        anchor: {
            x: 0.5,
            y: 0
        },
        transform: {
            x: 240,
            y: 110
        }
    });
    createSprite({
        width: 200,
        height: 6,
        tint: '#22263a',
        anchor: {
            x: 0,
            y: 0.5
        },
        transform: {
            x: 140,
            y: 130
        }
    });

    const bar = createSprite({
        width: 0,
        height: 6,
        tint: '#f5c518',
        anchor: {
            x: 0,
            y: 0.5
        },
        transform: {
            x: 140,
            y: 130
        }
    });

    useUpdate(() => {
        bar.width = 200 * loader.progress;

        if (loader.done) {
            scene.change('Title');
        }
    });

    return createScene();
};
```

`scenes/title.ts`:

```ts
import { createScene, createText, useScene, useUpdate } from 'nacatamalon';

import { YELLOW, WHITE, CENTRED } from '../settings';

export const Title = () => {
    const scene = useScene();

    // No useLoadFont: each font is named by the key Loading gave it.
    createText({
        text: 'FONTS ACROSS SCENES',
        font: 'title',
        style: {
            fontSize: 40
        },
        tint: YELLOW,
        anchor: CENTRED,
        transform: {
            x: 240,
            y: 50
        }
    });

    const play = createText({
        text: 'CLICK TO PLAY',
        font: 'ui',
        style: {
            fontSize: 16
        },
        tint: WHITE,
        anchor: CENTRED,
        transform: {
            x: 240,
            y: 170
        },
        onClick: () => scene.change('Level'),
    });

    useUpdate((_delta, time) => {
        // Blinks by its colour rather than by hiding it: a hidden text cannot be clicked.
        play.tint = {
            ...WHITE,
            a: Math.sin(time * 6) > -0.5 ? 1 : 0.25
        };
    });

    return createScene();
};
```

`settings.ts`:

```ts
import { getColor } from 'nacatamalon';

export const WIDTH = 480;
export const HEIGHT = 260;
export const YELLOW = getColor('#f5c518');
export const WHITE = getColor('#ffffff');
export const CENTRED = {
    x: 0.5,
    y: 0
};
```
