# Style gallery

> One courtyard in every render style, from the NES and the Game Boy to the N64, the Dreamcast and CGI, one key apart.

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

One courtyard, turning slowly, in every render style in `RENDER.STYLES`: the NES, the Super Nintendo, the Mega Drive, the Game Boy and the Game Boy Advance, the PlayStation, the Saturn, the Nintendo 64, the Dreamcast and CGI. Each brings how its materials are drawn, its colours (a palette or the bits it had a channel, dithered down to them), its resolution, the shape of its pixels and whether its pictures and its screen are smoothed.

It starts with no style, at the game's own 320 × 240, and the scene picks one with `useRenderStyle`. The picture stays as large as fits whatever resolution the style brings, so only its pixels change. **Space** goes to the next style.

## Code

`index.ts`:

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

// No style for the game: the scene picks one, and with none it is drawn at the game's 320 x 240.
const game = createGame('#app', {
    width: WIDTH,
    height: HEIGHT,
    // As large as fits, whatever resolution the style brings, so only its pixels change.
    scaling: 'contain',
    fullscreenScaling: 'contain',
    background: SKY,
    actions: ACTIONS
});

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

`components/hud.ts`:

```ts
import { createText, useAction, useGame, useLoadBitmapFont, useScreenSpace, useUpdate } from 'nacatamalon';
import type { TRenderStyleHandle } from 'nacatamalon';
import { STYLES } from '../settings';

export const Hud = (style: TRenderStyleHandle) => {
    useScreenSpace();

    const font = useLoadBitmapFont({
        json: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.json',
        atlas: '/example-assets/fonts/bitmaps/nacatamal-arcade/nacatamal-arcade.png'
    });

    const name = createText({
        text: STYLES[0]!.name,
        font,
        tint: '#ffffff',
        transform: {
            x: 8,
            y: 8
        }
    });

    const says = createText({
        text: STYLES[0]!.says,
        font,
        tint: '#1c2440',
        transform: {
            x: 8,
            y: 20
        }
    });

    const size = createText({
        text: '',
        font,
        tint: '#ffffff'
    });

    const hint = createText({
        text: 'SPACE NEXT STYLE',
        font,
        tint: '#ffe08a'
    });

    const game = useGame();

    // The style can bring its own resolution: what is placed against the screen's edges is placed
    // again when it changes, and the corner says how many pixels the game has now.
    const place = () => {
        size.text = `${game.getWidth()}X${game.getHeight()}`;
        size.transform.x = game.getWidth() - 8 - size.text.length * 8;
        size.transform.y = 8;
        hint.transform.x = 8;
        hint.transform.y = game.getHeight() - 16;
    };
    place();
    game.onResized(place);

    const next = useAction('next');
    let current = 0;

    // One call changes the whole scene: its materials, its effects, its resolution and filtering.
    useUpdate(() => {
        if (next.justPressed()) {
            current = (current + 1) % STYLES.length;
            style.set(STYLES[current]!.style);
            name.text = STYLES[current]!.name;
            says.text = STYLES[current]!.says;
        }
    });
};
```

`components/scenery.ts`:

```ts
import { chrome, createMaterial, createMesh, createModel, useCubeGeometry, useLoadModel, useLoadTexture, usePlaneGeometry, useSphereGeometry, useUpdate } from 'nacatamalon';
import { createPixels, createPixelTexture, fillRect, studioEnvironment } from 'nacatamalon/pixels';

/**
 * Bricks painted in code: rows of red with light mortar, every other row moved half a brick.
 */
const bricks = () => {
    const pixels = createPixels(64, 32, '#9a4a32');
    for (let row = 0; row < 4; row++) {
        const y = row * 8;
        fillRect(pixels, 0, y, 64, 1, '#d8c8a8');
        const from = row % 2 === 0 ? 0 : 8;
        for (let x = from; x < 64; x += 16) {
            fillRect(pixels, x, y, 1, 8, '#d8c8a8');
        }
    }
    return pixels;
};

// What every style is seen on: a floor of big tiles, where pictures swim; brick walls; a textured
// duck, where filtering shows; and a ball of chrome. None says how it is drawn except the chrome, a
// recipe that says it is lit at every pixel, so the style decides the rest.
export const Scenery = () => {
    const tiles = useLoadTexture({
        src: '/example-assets/assets/3d/textures/tiles.png'
    });

    createMesh({
        geometry: usePlaneGeometry({
            width: 14,
            depth: 14,
            // Big triangles, as the consoles drew floors: where swimming pictures show.
            widthSegments: 4,
            depthSegments: 4
        }),
        texture: tiles,
        tint: '#e0d8c8',
        specular: '#ffffff',
        shininess: 12
    });

    const wall = createPixelTexture(bricks());
    for (const [x, z, turn] of [[0, -4, 0], [-4, 0, Math.PI / 2], [4, 0, Math.PI / 2]] as const) {
        createMesh({
            geometry: useCubeGeometry({
                width: 6,
                height: 1.6,
                depth: 0.6
            }),
            texture: wall,
            transform: {
                x,
                y: 0.8,
                z,
                rotationY: turn
            }
        });
    }

    const duck = useLoadModel({
        src: '/example-assets/assets/3d/gltf/Duck/Duck.gltf'
    });
    const bird = createModel({
        model: duck,
        transform: {
            x: -0.9,
            scaleX: 1.2,
            scaleY: 1.2,
            scaleZ: 1.2
        }
    });

    const ball = createMesh({
        geometry: useSphereGeometry({
            radius: 0.7,
            segments: 16,
            rings: 12
        }),
        material: createMaterial(chrome({
            environment: createPixelTexture(studioEnvironment({
                top: '#cfe4ff',
                horizon: '#e8eef4',
                ground: '#6a5a3a'
            }))
        })),
        transform: {
            x: 1.4,
            y: 0.7,
            z: 0.4
        }
    });

    useUpdate((delta, time) => {
        bird.transform.rotationY += delta * 0.5;
        ball.transform.y = 0.8 + Math.sin(time * 1.5) * 0.15;
    });
};
```

`scenes/demo.ts`:

```ts
import { cameraLookAt, createScene, useAmbientLight, useCamera3d, useDirectionalLight, useRenderStyle, useSpawn, useUpdate } from 'nacatamalon';
import { Hud } from '../components/hud';
import { Scenery } from '../components/scenery';

export const Gallery = () => {
    const camera = useCamera3d({
        projection: 'perspective',
        fov: 55,
        transform: {
            y: 2.2,
            z: 6.5
        }
    });

    // No style to begin with: the game's own way. The HUD changes it.
    const style = useRenderStyle(null);

    // A sky above and grass below, and a sun.
    useAmbientLight({
        color: '#cfe0ff',
        ground: '#6a7a4a',
        intensity: 0.45
    });
    useDirectionalLight({
        color: '#fff4e0',
        intensity: 0.9,
        // Turned to shine down from up to the right.
        transform: {
            rotationX: -0.8,
            rotationY: 0.6
        }
    });

    useSpawn(Scenery)();
    useSpawn(Hud)(style);

    // Slowly round, so pictures swim and corners shiver where a style makes them.
    useUpdate((_delta, time) => {
        const angle = Math.sin(time * 0.3) * 0.6;
        camera.transform.x = Math.sin(angle) * 6.5;
        camera.transform.z = Math.cos(angle) * 6.5;
        cameraLookAt(camera, { x: 0, y: 0.6, z: 0 });
    });

    return createScene();
};
```

`settings.ts`:

```ts
import { getColor, RENDER } from 'nacatamalon';
import type { TActionMap, TRenderStyle } from 'nacatamalon';

// The game's own size and look: what "no style" is. Each style brings its own resolution and
// filtering, and the game's come back when the scene goes back to none.
export const WIDTH = 320;
export const HEIGHT = 240;
export const SKY = getColor('#7fa8d8');

// The styles the gallery goes through, in order, with what each changes, short enough for the 160
// pixels of the Game Boy's screen.
export const STYLES: readonly { name: string; says: string; style: TRenderStyle | null }[] = [
    {
        name: 'NO STYLE',
        says: 'AS EACH SAYS',
        style: null
    },
    {
        name: 'NES',
        says: '54 COLOURS 8:7',
        style: RENDER.STYLES.nes
    },
    {
        name: 'SNES',
        says: 'DITHER 8:7',
        style: RENDER.STYLES.snes
    },
    {
        name: 'GENESIS',
        says: '512 COLOURS',
        style: RENDER.STYLES.genesis
    },
    {
        name: 'GAME BOY',
        says: 'FOUR GREENS',
        style: RENDER.STYLES.gameBoy
    },
    {
        name: 'GBA',
        says: '240X160',
        style: RENDER.STYLES.gba
    },
    {
        name: 'PS1',
        says: 'SWIMS AND SHIVERS',
        style: RENDER.STYLES.ps1
    },
    {
        name: 'SATURN',
        says: 'SWIMS PIXELATED',
        style: RENDER.STYLES.saturn
    },
    {
        name: 'N64',
        says: 'SMOOTH AND BLUR',
        style: RENDER.STYLES.n64
    },
    {
        name: 'DREAMCAST',
        says: 'SMOOTH AND SHARP',
        style: RENDER.STYLES.dreamcast
    },
    {
        name: 'CGI',
        says: 'PIXEL LIGHT BLOOM',
        style: RENDER.STYLES.cgi
    }
];

export const ACTIONS: TActionMap = [
    {
        name: 'next',
        bindings: [
            {
                type: 'key',
                key: ' '
            },
            {
                type: 'button',
                button: 'a'
            }
        ]
    }
];
```
