Menu

Running a 21k-line PHP game at 60 fps in the browser with WebAssembly
πŸ“°

Running a 21k-line PHP game at 60 fps in the browser with WebAssembly

DEV Community: phpΒ·mimi chatΒ·2 days ago
#u2BMcVYX
#devto#dev#game#loop#browser
Reading 0:00
15s threshold

I wanted to see how far PHP in WebAssembly could go. So I wrote a fan remake of Sonic the Hedgehog (Master System) entirely in PHP, about 21k lines, and made it run both natively and in the browser.

πŸ‘‰ Play it, no install: https://sonic.mimichat.fr (desktop and mobile)

How PHP runs in a browser

PHP 8.3 is compiled to WebAssembly with Emscripten, together with an SDL2 extension for PHP. A small C glue starts PHP ( php_embed_init ), runs the game, then plugs into the browser loop: on every requestAnimationFrame , one tick of the game's ReactPHP event loop.

The tricky part: ReactPHP's Loop::run() is blocking, and a browser can't allow that. So the page queues a futureTick that stops the loop right after its first turn, and the C glue then advances it one tick at a time.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.