Getting Started
Requirements
- PHP 8.5+ (
composer.jsonrequiresphp >= 8.5andext-vio) - Composer
- php-vio extension - current release recommended (2.34+ for Ogg Opus/Vorbis audio and the system clipboard). Newer rendering features are enabled per php-vio feature flag, so an older php-vio runs with those features off.
- Optional fallbacks when php-vio is not loaded: php-glfw (OpenGL 3.0-4.6 + NanoVG) or php-vulkan (standalone Vulkan 3D)
- Optional:
ext-gdfor visual regression tests,ext-parallel(ZTS) for threading
Recommended: php-vio
php-vio picks the best GPU backend for your platform (Metal on macOS, D3D12/D3D11 on Windows, Vulkan on Linux, OpenGL as fallback) and also provides window, input, audio, text shaping and textures. Install via PIE: pie install phpolygon/php-vio, or take a prebuilt binary from the releases. Its documentation lives at phpolygon.github.io/php-vio-docs.
Installation
Create a new game project and require PHPolygon:
mkdir my-game && cd my-game
composer init
composer require phpolygon/phpolygonMinimal 2D Game
<?php
use PHPolygon\Engine;
use PHPolygon\EngineConfig;
use PHPolygon\Rendering\Color;
$engine = new Engine(new EngineConfig(
width: 1280,
height: 720,
title: 'My First Game',
));
$engine->onRender(function (Engine $e, float $interpolation) {
$r = $e->renderer2D;
$r->drawRect(100, 100, 200, 150, Color::blue());
$r->drawText('Hello PHPolygon!', 120, 160, 24, Color::white());
});
$engine->run();Splash Screen
The engine displays a splash screen on startup showing the PHPolygon logo and active renderer info (e.g. "Vio 2D - Vio 3D"):
- Fade in: 0.4s, hold, fade out: 0.5s, total: ~2.5s
- Logo loaded from
resources/branding/logo.png - Skipped automatically in headless mode
Disable or customize it via EngineConfig:
$engine = new Engine(new EngineConfig(
title: 'My Game',
skipSplash: true, // disable the splash screen entirely
splashDuration: 2.5, // total duration in seconds (default)
));Cooperative init (generators)
For long startup work, onInit may be a generator function. Each yield lets the engine render one splash frame and pump window events, so Linux compositors (Mutter/KWin) don't flag the window as "not responding" on slow GPUs:
$engine->onInit(function (Engine $engine) {
loadFonts();
yield; // splash redraws, events pump
buildWorld();
yield;
});Void-returning callbacks still run synchronously – generators are opt-in.
Minimal 3D Game
<?php
use PHPolygon\Engine;
use PHPolygon\EngineConfig;
use PHPolygon\Component\Transform3D;
use PHPolygon\Component\MeshRenderer;
use PHPolygon\Component\Camera3DComponent;
use PHPolygon\Component\DirectionalLight;
use PHPolygon\Geometry\BoxMesh;
use PHPolygon\Geometry\MeshRegistry;
use PHPolygon\Math\Vec3;
use PHPolygon\Rendering\Color;
use PHPolygon\Rendering\Material;
use PHPolygon\Rendering\MaterialRegistry;
use PHPolygon\Rendering\RenderCommandList;
use PHPolygon\System\Camera3DSystem;
use PHPolygon\System\Renderer3DSystem;
$engine = new Engine(new EngineConfig(
width: 1280,
height: 720,
title: 'My 3D Game',
is3D: true,
));
$engine->onInit(function () use ($engine): void {
// Register geometry and materials
MeshRegistry::register('box', BoxMesh::generate(1.0, 1.0, 1.0));
MaterialRegistry::register('red', Material::color(Color::red()));
// Register required 3D systems
$commandList = $engine->commandList3D ?? new RenderCommandList();
$engine->world->addSystem(new Camera3DSystem($commandList, 1280, 720));
$engine->world->addSystem(new Renderer3DSystem($engine->renderer3D, $commandList));
// Camera
$engine->world->createEntity()
->attach(new Transform3D(position: new Vec3(0, 2, 5)))
->attach(new Camera3DComponent());
// Sun light
$engine->world->createEntity()
->attach(new Transform3D())
->attach(new DirectionalLight(
direction: new Vec3(-1.0, -1.0, -0.5),
color: new Color(1.0, 1.0, 1.0),
intensity: 1.2,
));
// A red box
$engine->world->createEntity()
->attach(new Transform3D(position: new Vec3(0, 0, 0)))
->attach(new MeshRenderer('box', 'red'));
});
$engine->run();Project Structure
my-game/
├── composer.json
├── game.php ← entry point
├── src/
│ ├── Scene/ ← scene PHP files
│ ├── Component/ ← custom components
│ ├── System/ ← custom systems
│ └── World/ ← procedural geometry
├── assets/ ← sounds, UI layouts
├── resources/
│ └── shaders/source/ ← custom GLSL shaders
└── build.json ← build configuration (optional)Using Facades
PHPolygon provides Laravel-style facades for convenient static access:
use PHPolygon\Support\Facades\World;
use PHPolygon\Support\Facades\Input;
use PHPolygon\Support\Facades\Shader;
use PHPolygon\Support\Facades\Events;
// Create entities
$entity = World::createEntity();
// Check input
if (Input::isKeyPressed(VIO_KEY_SPACE)) { // key codes are GLFW-compatible; php-vio defines VIO_KEY_*
// jump
}
// Switch shaders for performance testing
Shader::use('unlit');Key edges are consumed on read
Input::isKeyPressed() and isKeyReleased() report the edge (this frame's press / release) and consume it on read – only the first system that reads a given key in a frame sees true; later reads that frame get false. So if two systems both react to the same key, register the higher-priority one first. For continuous "is it held down" checks use isKeyDown(), which does not consume. When handing control back from a modal (e.g. closing a menu), call Input::clearKeyEdges() so a stale press doesn't leak into gameplay.