Skip to content

Instantly share code, notes, and snippets.

@bobmagicii
Last active May 17, 2024 05:30
Show Gist options
  • Save bobmagicii/1b5f5cdd0a45d2b3326f5e5226b7bf2a to your computer and use it in GitHub Desktop.
Save bobmagicii/1b5f5cdd0a45d2b3326f5e5226b7bf2a to your computer and use it in GitHub Desktop.
lulz with Aspectus\Terminal
{
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"preferred-install": {
"netherphp/*": "source",
"*": "dist"
},
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
"netherphp/composer-plugin": true
}
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
"netherphp/standards": "^0.2.9"
},
"require": {
"aspectus/terminal": "^0.1.4",
"ozdemirburak/iris": "^3.1"
}
}
<?php
require('vendor/autoload.php');
use Aspectus\Terminal\Xterm;
use Aspectus\Terminal\Xterm\Event\MouseInputEvent;
use OzdemirBurak\Iris\Color;
use function Amp\async;
use function Amp\delay;
class Roflwalker {
protected Xterm
$X;
protected int
$Width;
protected int
$Height;
protected int
$MX = -1;
protected int
$MY = -1;
protected Color\Hex
$Colour;
protected int
$Iter = 0;
protected float
$Time = 0.0;
protected int
$Framelimit = 60;
protected int
$Pace = 4;
public function
__Construct(Xterm $X, int $Width, int $Height) {
$this->X = $X;
$this->Width = $Width;
$this->Height = $Height;
$this->Colour = new Color\Hex('#FF0000');
$this->Time = microtime(TRUE);
return;
}
public function
__Destroy() {
//$this->Reset();
return;
}
public function
OnMouseInput(MouseInputEvent $Event):
void {
$this->MX = $Event->x;
$this->MY = $Event->y;
return;
}
public function
Next():
void {
$Time = microtime(TRUE);
$Frametime = ($Time - $this->Time);
$Framerate = 1 / $Frametime;
$Framemult = 1 + (1 - ($Framerate / 60));
if($Frametime < 1 / $this->Framelimit)
return;
$PosX = 0;
$PosY = 0;
$R = 0;
$G = 0;
$B = 0;
$Spun = 0;
$Spin = 0;
while($PosX < $this->Width) {
$PosY = 0;
$Spin = (int)(1 * $Framemult);
$this->Colour = $this->Colour->Spin($Spin);
$Spun += $Spin;
$R = hexdec($this->Colour->Red());
$G = hexdec($this->Colour->Green());
$B = hexdec($this->Colour->Blue());
while($PosY < $this->Height) {
($this->X)
->RGB($R, $G, $B)
->MoveCursorTo($PosY, $PosX)
->Write('#');
$PosY += 1;
}
$PosX += 1;
}
($this->X)
->MoveCursorTo(0, 0)
->Write(sprintf(
'Iteration: %d %.2f %.2f ',
$this->Iter,
$Framerate,
$Framemult
));
$this->Colour = $this->Colour->Spin(
(int)($Spun * 0.9)
* -1
);
$this->Iter += 1;
$this->Time = $Time;
$this->X->Flush();
return;
}
public function
Prepare():
static {
($this->X)
->EraseDisplay()
->BgDefault()
->Default()
->SetPrivateModeHideCursor()
->SetPrivateModeTrackMouseOnPressAndRelease()
->SetPrivateModeTrackMouseAll()
//->Subscribe(
// MouseInputEvent::class,
// $this->OnMouseInput(...)
//)
->Flush();
return $this;
}
};
class Terminlol {
protected Aspectus\Terminal\Xterm
$X;
protected int
$Width;
protected int
$Height;
public function
__Construct() {
$this->X = new Aspectus\Terminal\Xterm;
$this->Width = (int)`tput cols`;
$this->Height = (int)`tput lines`;
$this->X
->Write(sprintf('Ready: %d x %d', $this->Width, $this->Height))
->Flush();
return;
}
public function
Run():
void {
$Func = async(function () {
$Game = new Roflwalker($this->X, $this->Width, $this->Height);
$Game->Prepare();
while(TRUE) {
$Game->Next();
delay(0.0);
}
return;
});
$Func->Await();
return;
}
};
$Term = new Terminlol;
$Term->Run();
@bobmagicii
Copy link
Author

bobmagicii commented Jun 12, 2023

Is the call to delay needed?

probably not. that was originally how my framerate limiter was working just kept bumping it down till it was 0 lol. never touched amphp before XD on react i would have fired up a legit event loop, knowing the api in my head XD

@thgs
Copy link

thgs commented Jul 7, 2023

Yea been playing around with manual framerate limiter but ended up recreating what delay is. Can't avoid the drift when it happens anyway, except if you suspend every other line.

I will put a framebuffer implementation in Aspectus eventually (i had a different design and now im left with "migrating" the code to the new one). Said framebuffer you COULD use a tick method (repeat) that renders it, in the meantime, things write on it. But that does not save you from drift when the processing is taking longer than the interval.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment