sdl3/examples/threaded_framework_windows.php
2025-10-25 22:04:24 +02:00

154 lines
4.1 KiB
PHP

<?php
/**
* Example with 2 framework windows running in parallel threads
* Requires: parallel extension
*
* Note: Each window runs in its own thread with its own event loop
*/
require_once __DIR__ . '/../vendor/autoload.php';
use parallel\Runtime;
use PHPNative\Framework\Application;
use PHPNative\Ui\Widget\Button;
use PHPNative\Ui\Widget\Container;
use PHPNative\Ui\Widget\Label;
// Check if parallel extension is loaded
if (!extension_loaded('parallel')) {
die("Error: parallel extension is not loaded. Install with: pecl install parallel\n");
}
echo "Starting 2 framework windows in parallel threads...\n\n";
/**
* Function to run a window with the framework in a thread
*/
$windowRunner = function(string $title, int $x, int $y, string $bgColor) {
require_once __DIR__ . '/../vendor/autoload.php';
// Create application for this thread
$app = new \PHPNative\Framework\Application();
// Create window
$window = $app->createWindow($title, 400, 350, $x, $y);
// Create UI
$container = new \PHPNative\Ui\Widget\Container("flex flex-col p-6 gap-4 bg-$bgColor-100");
$titleLabel = new \PHPNative\Ui\Widget\Label(
text: $title,
style: "text-2xl text-$bgColor-900"
);
$infoLabel = new \PHPNative\Ui\Widget\Label(
text: 'Running in separate thread',
style: "text-sm text-$bgColor-700"
);
$frameLabel = new \PHPNative\Ui\Widget\Label(
text: 'Frame: 0',
style: "text-base text-$bgColor-600"
);
$button = new \PHPNative\Ui\Widget\Button(
text: 'Click Me!',
style: "bg-$bgColor-500 hover:bg-$bgColor-700 text-white p-4 rounded-lg"
);
$clickCount = 0;
$button->setOnClick(function() use (&$clickCount, $button, $title) {
$clickCount++;
$button->setText("Clicked $clickCount times");
echo "$title - Button clicked! Count: $clickCount\n";
});
$closeButton = new \PHPNative\Ui\Widget\Button(
text: 'Close Window',
style: "bg-red-500 hover:bg-red-700 text-white p-3 rounded"
);
$closeButton->setOnClick(function() use ($window) {
$window->close();
});
$container->addComponent($titleLabel);
$container->addComponent($infoLabel);
$container->addComponent($frameLabel);
$container->addComponent($button);
$container->addComponent($closeButton);
$window->setRoot($container);
// Custom run loop with frame counter
$frameCount = 0;
while (!$window->shouldClose()) {
$frameCount++;
$frameLabel->setText("Frame: $frameCount");
$window->layout();
// Poll events
rgfw_pollEvents();
$window->handleEvents();
// Update
\PHPNative\Async\TaskManager::getInstance()->update();
$window->update();
// Render
$window->render();
// ~60 FPS
usleep(16666);
}
// Cleanup
$window->cleanup();
return "$title closed after $frameCount frames";
};
try {
// Launch Window 1 (Blue) in thread
echo "Launching Window 1 (Blue) in thread...\n";
$runtime1 = new Runtime();
$future1 = $runtime1->run($windowRunner, [
'Window 1 - Blue',
100,
100,
'blue'
]);
// Launch Window 2 (Green) in thread
echo "Launching Window 2 (Green) in thread...\n";
$runtime2 = new Runtime();
$future2 = $runtime2->run($windowRunner, [
'Window 2 - Green',
550,
100,
'green'
]);
echo "\nBoth windows launched in parallel threads!\n";
echo "Each window has its own event loop\n";
echo "Try clicking the buttons and closing windows\n";
echo "Main thread waiting for windows to close...\n\n";
// Wait for both windows to complete
echo "Waiting for Window 1...\n";
$result1 = $future1->value();
echo $result1 . "\n";
echo "Waiting for Window 2...\n";
$result2 = $future2->value();
echo $result2 . "\n";
echo "\nAll windows closed. Application exiting.\n";
} catch (\Throwable $e) {
echo "Error: " . $e->getMessage() . "\n";
echo "Stack trace:\n" . $e->getTraceAsString() . "\n";
}