# Texture loading

> useLoadTexture hands the texture back at once and fills it in when the image arrives: sprites pop in as they land.

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

`useLoadTexture({ src, key })` hands back a texture immediately (`status: 'loading'`) and fills it in when the image arrives. Every sprite here exists from the first frame: the renderer skips each one until its texture is ready, so they pop in as they land and nothing checks a status.

The bar underneath has no texture, which is exactly why it can draw while the rest is still coming, and `useLoadProgress` is what tells it how far along it is.

## Code

`index.ts`:

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

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

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

`components/gallery.ts`:

```ts
import { createSprite } from 'nacatamalon';
import type { TTexture } from 'nacatamalon';
import { WIDTH } from '../settings';

type TGalleryProps = {
    logo: TTexture;
    icons: TTexture[];
};

export const Gallery = ({ logo, icons }: TGalleryProps) => {
    // Each sprite appears when its own image arrives; no status check controls visibility.
    createSprite({
        texture: logo,
        transform: {
            x: WIDTH / 2,
            y: 120
        }
    });

    icons.forEach((texture, index) => {
        createSprite({
            texture,
            width: 48,
            height: 48,
            transform: {
                x: 144 + index * 64,
                y: 210
            }
        });
    });
};
```

`components/progress_bar.ts`:

```ts
import { createSprite, useUpdate } from 'nacatamalon';
import type { TLoadProgress } from 'nacatamalon';
import { BAR } from '../settings';

export const ProgressBar = (progress: TLoadProgress) => {
    createSprite({
        width: BAR.width + 4,
        height: 10,
        tint: '#1f2b44',
        transform: {
            x: BAR.x + BAR.width / 2,
            y: BAR.y
        }
    });

    const fill = createSprite({
        width: 0,
        height: 6,
        tint: '#4ade80',
        anchor: {
            x: 0,
            y: 0.5
        },
        transform: {
            x: BAR.x,
            y: BAR.y
        }
    });

    let announced = false;

    useUpdate(() => {
        // A left anchor keeps the edge fixed while the width grows.
        fill.width = progress.progress * BAR.width;

        if (progress.done && !announced) {
            announced = true;
            console.log('[useLoadProgress] ' + progress.loaded + '/' + progress.total + ' settled');
        }
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useLoadProgress, useLoadTexture, useSpawn } from 'nacatamalon';
import { Gallery } from '../components/gallery';
import { ProgressBar } from '../components/progress_bar';
import { ICON_COUNT } from '../settings';

export const Game = () => {
    const logo = useLoadTexture({
        src: '/example-assets/assets/gammix.png'
    });

    const icons = Array.from({
        length: ICON_COUNT
    }, (_, index) => {
        return useLoadTexture({
            src: '/example-assets/assets/mushroom/Icon' + (index + 1) + '.png'
        });
    });

    const progress = useLoadProgress([logo, ...icons]);

    useSpawn(Gallery)({
        logo,
        icons
    });
    useSpawn(ProgressBar)(progress);

    return createScene();
};
```

`settings.ts`:

```ts
export const WIDTH = 480;
export const HEIGHT = 320;
export const ICON_COUNT = 4;

export const BAR = {
    x: 120,
    y: 272,
    width: 240
};
```
