# Handheld LCD

> lcd() is the screen of a handheld: a grid between the pixels, a tinted panel, and ghosts behind anything that moves.

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

`lcd()` is the screen of a handheld: a `grid` between the pixels, a `tint` the panel could not help, and pixels slow to change, so anything moving leaves a ghost (`ghosting`). The ghost reads what the effect showed **last frame**, which costs two pictures the size of the canvas while it runs.

The grid is one real pixel wide around each game pixel, so it needs at least two real pixels per game pixel. This example runs at `pixelRatio: 'device'`; on a screen of density 1 there is no gap to draw and the grid is left out.

The green panel is **two effects**: `colorAdjust({ saturation: 0 })` takes the colour out, and `lcd()` tints what is left. **Click** to change panel, **↑** **↓** for the ghosting and **Space** to switch it off.

## 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,
    pixelRatio: 'device',
    background: '#0b0710',
});

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

`components/effect_controls.ts`:

```ts
import { createText, useKeyboard, useLoadBitmapFont, usePointer, useUpdate, useScreenSpace } from 'nacatamalon';
import { clamp } from 'nacatamalon/math';
import { colorAdjust, lcd, usePostProcess } from 'nacatamalon/effects';

import { PANELS, MONO_SETTINGS, EFFECT_SETTINGS } from '../settings';

export const EffectControls = () => {
    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 label = createText({
        text: '',
        font,
        style: {
            fontSize: 8,
        },
        tint: '#ffffff',
        transform: {
            x: 12,
            y: 14,
        },
    });

    createText({
        text: 'CLICK PANEL   UP DOWN GHOSTING   SPACE OFF',
        font,
        style: {
            fontSize: 8,
        },
        tint: '#8c82a6',
        transform: {
            x: 12,
            y: 30,
        },
    });

    const mono = usePostProcess(colorAdjust(MONO_SETTINGS));

    const effect = usePostProcess(lcd(EFFECT_SETTINGS));

    let panel = 0;

    const show = () => {
        if (!effect.enabled) {
            label.text = 'OFF  THE FRAME AS DRAWN';

            return;
        }

        label.text = PANELS[panel].name + '  GHOSTING ' + Math.round(effect.uniforms.ghosting * 100);
    };

    show();

    usePointer().onDown(() => {
        panel = (panel + 1) % PANELS.length;
        mono.enabled = PANELS[panel].mono;
        effect.uniforms.tint = PANELS[panel].tint;
        show();
    });

    const keys = useKeyboard();

    useUpdate(() => {
        const ghosting = keys.justPressed('ArrowUp') ? 0.1 : keys.justPressed('ArrowDown') ? -0.1 : 0;

        if (ghosting !== 0) {
            effect.uniforms.ghosting = clamp(effect.uniforms.ghosting + ghosting, 0, 0.9);
            show();
        }

        // Switched off, not removed: it keeps its place in the chain for when it comes back.
        if (keys.justPressed(' ')) {
            effect.enabled = !effect.enabled;
            show();
        }
    });
};
```

`components/stage.ts`:

```ts
import { WIDTH, HEIGHT } from '../settings';
import { createMaterial, createMesh, createSprite, getColor, useCamera3d, useDirectionalLight, useLoadTexture, useUpdate, useSphereGeometry } from 'nacatamalon';

// The test card every post-processing example draws, so the only thing that changes between them
// is the effect. Each part is here because it shows something a screen effect does:
//
//   lit balls   smooth gradients that nobody drew, made by the light as it turns. Banding shows
//               there first, because a gradient is exactly what a few steps cannot hold.
//   hue ramp    every colour at once, which is where a palette has to make its hardest choices.
//   grey ramp   sixteen even steps: whether an effect keeps them even is visible at a glance.
//   gammix      pixel art, already limited, so it shows what an effect does to a sprite that was
//               drawn for a small palette in the first place.

export const Stage = () => {
    useCamera3d({
        projection: 'perspective',
        fov: 50,
        transform: {
            z: 5,
            y: 0.4
        },
    });

    const light = useDirectionalLight({
        color: '#ffffff',
        intensity: 1.2,
        ambient: 0.25,
        transform: {
            rotationX: -0.5
        },
    });

    const geometry = useSphereGeometry();

    ['#ff7a3d', '#3dd6c6', '#9b5cff'].forEach((hex, i) => {
        const material = createMaterial({
            name: 'post-card-' + i,
            shader: 'mesh3d',
            tint: getColor(hex),
            specular: '#ffffff',
            shininess: 24,
        });

        createMesh({
            geometry,
            material,
            transform: {
                x: (i - 1) * 2.1,
                y: 0.6,
            },
        });
    });

    // The light swings from side to side in front of the balls, so the gradients move and a
    // static effect cannot hide in one lucky frame.
    useUpdate((_, time) => {
        light.transform.rotationY = Math.sin(time * 0.8) * 0.9;
    });

    for (let i = 0; i < 48; i++) {
        createSprite({
            width: WIDTH / 48,
            height: 18,
            tint: getColor('hsl(' + Math.round((i / 48) * 360) + ', 85%, 55%)'),
            transform: {
                x: (i + 0.5) * WIDTH / 48,
                y: HEIGHT - 38,
            },
        });
    }

    for (let i = 0; i < 16; i++) {
        const v = Math.round((i / 15) * 255);

        createSprite({
            width: WIDTH / 16,
            height: 18,
            tint: getColor('rgb(' + v + ',' + v + ',' + v + ')'),
            transform: {
                x: (i + 0.5) * WIDTH / 16,
                y: HEIGHT - 18,
            },
        });
    }

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

    createSprite({
        texture: gammix,
        width: 48,
        height: 48,
        transform: {
            x: WIDTH - 34,
            y: 34,
        },
    });
};
```

`components/trail_ball.ts`:

```ts
import { createSprite, useScreenSpace, useUpdate } from 'nacatamalon';
import { WIDTH, BALL } from '../settings';

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

    const ball = createSprite({
        width: BALL.size,
        height: BALL.size,
        tint: '#ffffff',
        transform: {
            x: WIDTH / 2,
            y: BALL.y
        }
    });

    useUpdate((_delta, time) => {
        ball.transform.x = WIDTH / 2 + Math.sin(time * BALL.speed) * BALL.range;
    });
};
```

`scenes/demo.ts`:

```ts
import { createScene, useSpawn } from 'nacatamalon';
import { Stage } from '../components/stage';
import { TrailBall } from '../components/trail_ball';
import { EffectControls } from '../components/effect_controls';

export const Game = () => {
    useSpawn(Stage)();
    useSpawn(TrailBall)();
    useSpawn(EffectControls)();

    return createScene();
};
```

`settings.ts`:

```ts
import type { lcd } from 'nacatamalon/effects';

export const WIDTH = 480;

export const HEIGHT = 300;

// lcd() is the screen of a handheld: a grid between the pixels, a tint the panel could not help,
// and pixels slow to change, so anything moving leaves a ghost. The ghost reads what the effect
// showed LAST frame, which costs two pictures the size of the canvas while it runs.
//
// The grid is one real pixel wide around each game pixel, so it needs at least two real pixels per
// game pixel: this game runs at pixelRatio 'device', and on a screen of density 1 there is no gap
// to draw.
//
// The green panel is two effects: colorAdjust() takes the colour out first, and lcd() tints what
// is left. The order they were installed in is the order they run in.

export const PANELS = [
    {
        name: 'GREEN',
        mono: true,
        tint: [0.61, 0.73, 0.06]
    },
    {
        name: 'POCKET',
        mono: true,
        tint: [0.85, 0.85, 0.78]
    },
    {
        name: 'COLOUR',
        mono: false,
        tint: [1, 0.97, 0.9]
    },
];

export const MONO_SETTINGS = {
    saturation: 0,
};
export const EFFECT_SETTINGS = {
    tint: [0.61, 0.73, 0.06],
    ghosting: 0.5,
} satisfies Parameters<typeof lcd>[0];

export const BALL = {
    size: 14,
    range: 200,
    speed: 3,
    y: 205
};
```
