Skip to content

Legacy Examples ​

This directory retains the 16 demo entry points from the legacy example/ directory, preserving filenames with a .cjs extension. 14 entry points are adapted to the current API; 2 asynchronous drawing entry points report UNSUPPORTED and exit with code 2.

All examples · API guide · Development guide

These scripts show how legacy scenes behave with the current API. For new applications, start with Quick start.

Running ​

Run from the project root:

sh
# The initial build includes the native module
npm run build

# Open the combined demo window
npm run demo:ported

# Run any ported script
node examples/ported/texture.cjs
node examples/ported/sound_buffer.cjs

# Verify results, reporting passes and unsupported cases separately
npm run test:ported

Example entry points ​

FileDemonstration
color.cjsColor construction, component writes and JSON output
vector2.cjs2D vectors, scalar conversion, copying and named operations
vector3.cjs3D vectors, scalar conversion, copying and named operations
time.cjsTime factories, bigint microseconds and Time.Zero
rect.cjsposition/size, containment and intersections, preserving the original invalid-input demonstration
video_mode.cjsVideo modes, sizes, NaN input, desktop and fullscreen mode queries
vertex.cjsVertex member views and VertexArray access
joystick.cjsJoystick connection, identification, button counts and axis queries
sensor.cjsAvailability, enabling and reading of all sensors
touch.cjsTouch state and position queries
font.cjsFailed loading, font loading, glyph at code point 1/character size 10 and line spacing
texture.cjsFailed loading, GIF loading, cropping, texture copying and Sprite
sound_buffer.cjsWAV decoding, PCM copying, saving and reloading
render_window.cjsThe original combined scene's shapes, vertex lines, gradient pixels, cropping, masks, Sprite, Chinese text and audio
render_window_display_async.cjsUnsupported: original synchronous/asynchronous display comparison; displayAsync is missing
render_window_display_draw_async.cjsUnsupported: original combined asynchronous drawing scene; drawAsync and displayAsync are missing

Window interaction ​

render_window.cjs uses the original VideoMode.getFullscreenModes()[3], title hello and style 5. It preserves the red background, original scene layout, default volume, a single Sound playback at startup, and Music configured to loop without starting playback. Scheduling remains setFramerateLimit(60) and setImmediate(frame).

KeyAction
Esc/close eventClose the window; Esc also retains real-time querying
LeftAdd 1 to both x and y of the window position
LControlRecreate at 800 x 600 while held and enable vertical sync
LAltPrint context settings
RAltChange the window title
SRequest the virtual keyboard, subject to system support

The original mouse logic is also preserved: read desktop and window-relative coordinates every frame, clamp to the original mode bounds, then call Mouse.setPosition. The original key-repeat behavior remains.

On non-Windows systems, the combined example still saves /tmp/xxx.png. The SoundBuffer example still saves .game.wav under os.tmpdir(), then reloads it without automatically deleting it.

Porting conventions ​

Adaptations cover import paths, SFML 3 arguments and method names, value types and asynchronous loading. Scene parameters, animation formulas, draw order, input, file output and frame scheduling preserve the original logic. Images, fonts and audio are in assets/.

The two entry points with async in their names do not emulate legacy APIs with synchronous drawing or fixed sleeps. The display comparison retains 10 frames, a 50 ms heartbeat, the setImmediate wrapper, displayAsync and error checks after closing; the combined asynchronous scene retains sequential await drawAsync/displayAsync calls.

Coordinates and sizes use Vector2, rotations use Angle, colors use Color, and events are read through is/getIf; numeric enum iteration filters reverse mappings. Loading operations check booleans; the original missing paths abcdefg/abc.jpg are preserved. console.log/JSON.stringify receive new objects directly, with output following the new types' own representation. Integer conversion and invalid-value handling also follow the new value types, without emulating legacy validation in the examples.

Each script contains its demo logic independently, no longer using shared scenes or output-formatting helpers. Formerly synchronous resource loads now use await in the original order, not parallel preloading.

Automated verification ​

test:ported uses a separate test preload script that normal example runs do not load. During testing, it selects a null audio device, checks mouse settings without moving the system pointer, and redirects file output to an isolated directory. If the virtual display offers fewer than four fullscreen modes, the test fills the remaining options with the desktop mode.

The test script closes the combined window after 8 frames, checking layout, colors, point counts, scale, text and vertex lines per frame. Tests count 14 runnable entry points and 2 unsupported entry points separately; unsupported cases do not count as successful ports. Null-audio-device testing does not prove actual speaker output works.

Icons made by Lorc, Delapouite & Skoll · game-icons.net · CC BY 3.0