# Renderers

> The engine ships WebGPU and WebGL2, and the game never learns which one draws. Switch RENDERER and everything comes out the same.

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

The engine ships two renderers, WebGPU and WebGL2, and the game never learns which one is drawing. `renderer: 'AUTO'`, the default, takes WebGPU when it starts and WebGL2 when it does not, including a browser that reports WebGPU and then cannot hand over a device. Naming one explicitly never falls back to the other.

Switch `RENDERER` between `'WEBGPU'` and `'WEBGL2'`: sprites, transparency, a textured sprite, text and a camera with a pinned HUD come out the same.

## Code

`index.ts`:

```ts
import { createGame } from 'nacatamalon';
import * as Settings from './settings';
import { Level } from './scenes/demo';

const game = createGame('#app', {
    renderer: Settings.RENDERER,
    width: Settings.WIDTH,
    height: Settings.HEIGHT,
    background: '#0a0a12',
});

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

`components/hud.ts`:

```ts
import { createSprite, createText, useLoadBitmapFont, useScreenSpace } from 'nacatamalon';
import * as Settings from '../settings';

export const Hud = () => {
    useScreenSpace();

    const font = useLoadBitmapFont(Settings.ARCADE);

    createSprite({
        tint: 'rgba(10, 10, 18, 0.8)',
        anchor: {
            x: 0,
            y: 0
        },
        width: 250,
        height: 44,
        zIndex: 100,
        transform: {
            x: 8,
            y: 8,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });
    createText({
        text: 'RENDERER\n' + Settings.RENDERER,
        font,
        style: {
            fontSize: 16,
            lineSpacing: 4
        },
        tint: '#8ee6a0',
        zIndex: 101,
        transform: {
            x: 16,
            y: 14,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });
};
```

`components/player.ts`:

```ts
import { createSprite, useLoadTexture, useUpdate } from 'nacatamalon';

export const Player = () => {
    const texture = useLoadTexture({
        src: '/example-assets/assets/gammix.png',
        key: 'gammix'
    });

    const player = createSprite({
        texture,
        width: 64,
        height: 64,
        zIndex: 10,
        transform: {
            x: 400,
            y: 240,
            rotation: 0,
            scaleX: 1,
            scaleY: 1
        },
    });

    useUpdate((_delta, elapsed) => {
        player.transform.x = 400 + Math.cos(elapsed * 0.8) * 220;
        player.transform.y = 240 + Math.sin(elapsed * 1.2) * 120;
    });

    return player;
};
```

`components/world.ts`:

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

export const World = () => {
    const colours = ['rgba(255, 85, 102, 0.6)', 'rgba(85, 221, 119, 0.6)', 'rgba(85, 153, 255, 0.6)'];

    for (let gy = 0; gy < 6; gy++) {
        for (let gx = 0; gx < 10; gx++) {
            createSprite({
                tint: getColor(colours[(gx + gy) % colours.length]),
                width: 40,
                height: 40,
                transform: {
                    x: gx * 90,
                    y: gy * 90,
                    rotation: (gx + gy) * 0.2,
                    scaleX: 1,
                    scaleY: 1
                },
            });
        }
    }
};
```

`scenes/demo.ts`:

```ts
import { createScene, useCamera2d, useSpawn, useCameraFollow } from 'nacatamalon';

import { World } from '../components/world';
import { Player } from '../components/player';
import { Hud } from '../components/hud';

export const Level = () => {
    useCamera2d();
    useSpawn(World)();

    const player = useSpawn(Player)().api;

    useCameraFollow(player, {
        round: false
    });
    useSpawn(Hud)();

    return createScene();
};
```

`settings.ts`:

```ts
export const RENDERER = 'WEBGL2';

export const WIDTH = 480;
export const HEIGHT = 320;

export const ARCADE = {
    json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
    atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png'
};

// Something to look at through a camera: markers, see-through colours and a textured sprite.
```
