之前一直看到php文档中有一个做GUI扩展,虽然这不是php擅长的,印象中这就不是它应该做的。同时文档很少,有点难看懂。今天记录下初次运行。
测试环境 W in
php 版本如下
$ php71.exe -v
PHP 7.1.10 (cli) (built: Sep 26 2017 20:07:25) ( NTS MSVC14 (Visual C++ 2015) x86 )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
extension=php_ui.dll
$ php71.exe -m
[PHP Modules]
bcmath
...
ui
...
[Zend Modules]
扩展包里面还带了4个示例文件,分别是
snake.php
starfield.php
gallery.php
histogram.php
php71.exe snake.php
该示例我是无法正常运行的,点击空格后,蛇只会移动一步,之后卡住。多次点击空格可以继续移动一个,鼠标滚轮一直滚动时也会一点点移动。
该示例同贪吃蛇,一样会卡住。再连续滚动鼠标滑轮时,可以到20桢,停止时界面也随之停止。
该示例正常运行,包括界面二的进度条加载。体验还是挺丝滑的。
该示例正常运行
<?php
--- define (\"PHP_UI_SECOND\", 1000000);
+++ define (\"PHP_UI_SECOND\", 30);
define (\"PHP_UI_SNAKE_FPS\", 30);
--- $animator = new class(1000000/60, $stars) extends Executor {
+++ $animator = new class(1000000, $stars) extends Executor {
以下为部分源码
if ($char == \" \") {
$this->pause = !$this->pause;
if ($this->pause) {
/* this allows the CPU to idle while paused */
$this->executor->setInterval(0);
} else {
/* this will (re)start the game */
$this->executor->setInterval(PHP_UI_SECOND/PHP_UI_SNAKE_FPS);
}
}
这里的 PHP_UI_SECOND/PHP_UI_SNAKE_FPS 应该设置每一帧刷新的时间,也就是控制帧率。但实际情况并没有按设想的运行。
以下时本机测试结果反向推测:
public UI\Executor::setInterval(int $microseconds): bool
public UI\Executor::setInterval(int $seconds, int $microseconds): bool
按文档的写法,明细示例里面做(1000000/60)除法的赋值明显是不对的,我尝试改为整型。虽然可以运行,但是实际运行效果和预计的有很大出入。
有兴趣的可以移步ui/classes/executor.c查看
<?php
use UI\Window;
use UI\Size;
use UI\Controls\Box;
use UI\Executor;
$win = new class("TEST", new Size(640, 480), false) extends Window {
public function addExecutor(Executor $executor) {
$this->executors[] = $executor;
}
protected function onClosing() {
foreach ($this->executors as $executor) {
$executor->kill();
}
$this->destroy();
UI\quit();
}
protected function __destruct()
{
foreach ($this->executors as $executor) {
$executor->kill();
}
$this->destroy();
UI\quit();
}
};
$animator = new class() extends Executor {
protected function onExecute() {
echo 'onExecute:'.microtime(1).PHP_EOL;
}
public function __construct() {
//间隔 1秒
parent::__construct(1000000);
}
};
$win->addExecutor($animator);
$win->show();
UI\run();
?>
以下3个写法是等价的,都是控制为1s
//间隔 1秒
parent::__construct(1000000);
//间隔 1秒
//parent::__construct(1,0);
//间隔 1秒
//parent::__construct(0,1000000);
onExecute:1636006041.6393 onExecute:1636006042.6396 onExecute:1636006043.6447 onExecute:1636006044.6519
符合预期
onExecute:1636006200.6372
onExecute:1636006202.6475
onExecute:1636006204.649
符合预期
3. 小于1s时,比如 500000 微妙即0.5秒
结果
onExecute:1636006383.6496
onExecute:1636006383.6509
onExecute:1636006384.6474
onExecute:1636006384.6485
onExecute:1636006385.6446
执行间隔远小于设定值,但却是可以做到1秒内2次。一脸懵逼~~
4. 其他情况
这里就简单描述下情况,比较混乱
小于0.4秒间隔,输出3行后卡住。
小于0.6秒间隔,输出的奇怪行数量
这毕竟是一个4年没有更新的项目了,没更新肯定有其原因,项目没有活力必然会有问题不能及时修复,现在就当了解和学习了。奇怪的执行间隔,确实是超出能力范围。我做了以下尝试:
Warning: PHP Startup: Unable to load dynamic library \'ui\'