summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGabriel Rodrigues Couto <gabrielrcouto@gmail.com>2016-03-02 00:25:41 -0300
committerGabriel Rodrigues Couto <gabrielrcouto@gmail.com>2016-03-02 00:25:41 -0300
commit765a7b77dd7f0e188da328d274c37622aba4a169 (patch)
tree3e834195e147cbe8b73a7f6635dfdd231e2f1dd6 /src
parentcc403325cfd30fb2559dde2f474cbcb795e513f2 (diff)
downloadphp-terminal-gameboy-emulator-765a7b77dd7f0e188da328d274c37622aba4a169.zip
php-terminal-gameboy-emulator-765a7b77dd7f0e188da328d274c37622aba4a169.tar.gz
php-terminal-gameboy-emulator-765a7b77dd7f0e188da328d274c37622aba4a169.tar.bz2
FPS gain - Working around 20 FPS on PHP 7
CanvasBuffer redesigned for black&white DrawContext TerminalCanvas ignoring redraw of repeated frames
Diffstat (limited to 'src')
-rw-r--r--src/Canvas/DrawContextInterface.php2
-rw-r--r--src/Canvas/TerminalCanvas.php86
-rw-r--r--src/Core.php62
3 files changed, 98 insertions, 52 deletions
diff --git a/src/Canvas/DrawContextInterface.php b/src/Canvas/DrawContextInterface.php
index 8505ef5..5b79f8d 100644
--- a/src/Canvas/DrawContextInterface.php
+++ b/src/Canvas/DrawContextInterface.php
@@ -11,7 +11,7 @@ interface DrawContextInterface
/**
* Draw image on canvas.
*
- * @param array $canvasBuffer Each pixel => 4 items on array (RGBA)
+ * @param array $canvasBuffer If colored, each pixel => 4 items on array (RGBA)
* @param int $left
* @param int $top
*/
diff --git a/src/Canvas/TerminalCanvas.php b/src/Canvas/TerminalCanvas.php
index 05564ab..16f75e9 100644
--- a/src/Canvas/TerminalCanvas.php
+++ b/src/Canvas/TerminalCanvas.php
@@ -8,9 +8,16 @@ use GameBoy\Settings;
class TerminalCanvas implements DrawContextInterface
{
protected $canvas;
+ /**
+ * If is a color enabled canvas, set to true
+ * @var Boolean
+ */
+ public $colorEnabled = false;
protected $currentSecond = 0;
protected $framesInSecond = 0;
protected $fps = 0;
+ protected $lastFrame;
+ protected $lastFrameCanvasBuffer;
private $width = 0;
private $height = 0;
@@ -23,34 +30,13 @@ class TerminalCanvas implements DrawContextInterface
/**
* Draw image on canvas using braille font.
*
- * @param object $canvasBuffer $data = Each pixel = 4 items on array (RGBA)
+ * @param object $canvasBuffer $data = Each pixel (true/false)
* @param int $left
* @param int $top
*/
public function draw($canvasBuffer, $left, $top)
{
- //Corner pixel, to draw same size each time
- $this->canvas->set(0, 0);
- $this->canvas->set(159, 143);
-
- $y = 0;
-
- for ($i = 0; $i < count($canvasBuffer); $i = $i + 4) {
- // Sum of all colors, Ignore alpha
- $total = $canvasBuffer[$i] + $canvasBuffer[$i + 1] + $canvasBuffer[$i + 2];
-
- $x = ($i / 4) % 160;
-
- // 350 is a good threshold for black and white
- if ($total > 350) {
- $this->canvas->set($x, $y);
- }
-
- if ($x == 159) {
- ++$y;
- }
- }
-
+ //Calculate current FPS
if ($this->currentSecond != time()) {
$this->fps = $this->framesInSecond;
$this->currentSecond = time();
@@ -59,21 +45,53 @@ class TerminalCanvas implements DrawContextInterface
++$this->framesInSecond;
}
- $frame = $this->canvas->frame();
- $content = "\e[H\e[2J";
+ //If the last frame changed, we draw
+ // @TODO - The FPS will be wrong, need to find a way to update
+ // without redraw
+ if ($canvasBuffer != $this->lastFrameCanvasBuffer) {
+ //Clear the pixels from the canvas
+ $this->canvas->clear();
- if ($this->height > 0 && $this->width > 0) {
- $content = "\e[{$this->height}A\e[{$this->width}D";
- }
+ //Corner pixel, to draw same size each time
+ $this->canvas->set(0, 0);
+ $this->canvas->set(159, 143);
+
+ $y = 0;
+ $count = count($canvasBuffer);
+
+ for ($i = 0; $i < $count; $i++) {
+ $x = $i % 160;
+
+ if ($canvasBuffer[$i]) {
+ $this->canvas->set($x, $y);
+ }
+
+ if ($x == 159) {
+ ++$y;
+ }
+ }
- $content .= sprintf('FPS: %d - Frame Skip: %s'.PHP_EOL, $this->fps, Settings::$settings[4]);
- $content .= $frame;
+ $frame = $this->canvas->frame([
+ 'min_x' => 0,
+ 'max_x' => 79
+ ]);
- echo $content;
+ $this->lastFrame = $frame;
+ $this->lastFrameCanvasBuffer = $canvasBuffer;
- $this->canvas->clear();
+ $content = "\e[H\e[2J";
- $this->height = substr_count($frame, PHP_EOL) + 1;
- $this->width = strpos($frame, PHP_EOL);
+ if ($this->height > 0 && $this->width > 0) {
+ $content = "\e[{$this->height}A\e[{$this->width}D";
+ }
+
+ $content .= sprintf('FPS: %d - Frame Skip: %s'.PHP_EOL, $this->fps, Settings::$settings[4]);
+ $content .= $frame;
+
+ echo $content;
+
+ $this->height = substr_count($frame, PHP_EOL) + 1;
+ $this->width = strpos($frame, PHP_EOL);
+ }
}
}
diff --git a/src/Core.php b/src/Core.php
index b7256c4..4c9a9b0 100644
--- a/src/Core.php
+++ b/src/Core.php
@@ -1044,17 +1044,23 @@ class Core
//Get a CanvasPixelArray buffer:
//Create a white screen
- $this->canvasBuffer = array_fill(0, 4 * $this->width * $this->height, 255);
- $index = $this->pixelCount;
- $index2 = $this->rgbCount;
+ if ($this->drawContext->colorEnabled) {
+ $this->canvasBuffer = array_fill(0, 4 * $this->width * $this->height, 255);
+
+ $index = $this->pixelCount;
+ $index2 = $this->rgbCount;
- while ($index > 0) {
- $this->frameBuffer[--$index] = 0x00FFFFFF;
- $this->canvasBuffer[$index2 -= 4] = 0xFF;
- $this->canvasBuffer[$index2 + 1] = 0xFF;
- $this->canvasBuffer[$index2 + 2] = 0xFF;
- $this->canvasBuffer[$index2 + 3] = 0xFF;
+ while ($index > 0) {
+ $this->frameBuffer[--$index] = 0x00FFFFFF;
+ $this->canvasBuffer[$index2 -= 4] = 0xFF;
+ $this->canvasBuffer[$index2 + 1] = 0xFF;
+ $this->canvasBuffer[$index2 + 2] = 0xFF;
+ $this->canvasBuffer[$index2 + 3] = 0xFF;
+ }
+ } else {
+ $this->canvasBuffer = array_fill(0, 4 * $this->width * $this->height, false);
+ $this->frameBuffer = array_fill(0, $this->pixelCount, 0x00FFFFFF);
}
$this->drawContext->draw($this->canvasBuffer, 0, 0);
@@ -1270,7 +1276,12 @@ class Core
public function displayShowOff()
{
if ($this->drewBlank == 0) {
- $this->canvasBuffer = array_fill(0, 4 * $this->width * $this->height, 255);
+ if ($this->drawContext->colorEnabled) {
+ $this->canvasBuffer = array_fill(0, 4 * $this->width * $this->height, 255);
+ } else {
+ $this->canvasBuffer = array_fill(0, 4 * $this->width * $this->height, false);
+ }
+
$this->drawContext->draw($this->canvasBuffer, 0, 0);
$this->drewBlank = 2;
}
@@ -1383,13 +1394,30 @@ class Core
$bufferIndex = $this->pixelCount;
$canvasIndex = $this->rgbCount;
- while ($canvasIndex > 3) {
- //Red
- $this->canvasBuffer[$canvasIndex -= 4] = ($this->frameBuffer[--$bufferIndex] >> 16) & 0xFF;
- //Green
- $this->canvasBuffer[$canvasIndex + 1] = ($this->frameBuffer[$bufferIndex] >> 8) & 0xFF;
- //Blue
- $this->canvasBuffer[$canvasIndex + 2] = $this->frameBuffer[$bufferIndex] & 0xFF;
+ if ($this->drawContext->colorEnabled) {
+ // Generate colored CanvasBuffer Version
+ while ($canvasIndex > 3) {
+ //Red
+ $this->canvasBuffer[$canvasIndex -= 4] = ($this->frameBuffer[--$bufferIndex] >> 16) & 0xFF;
+ //Green
+ $this->canvasBuffer[$canvasIndex + 1] = ($this->frameBuffer[$bufferIndex] >> 8) & 0xFF;
+ //Blue
+ $this->canvasBuffer[$canvasIndex + 2] = $this->frameBuffer[$bufferIndex] & 0xFF;
+ }
+ } else {
+ // Generate black&white CanvasBuffer Version
+ while ($bufferIndex > 0) {
+ $r = ($this->frameBuffer[--$bufferIndex] >> 16) & 0xFF;
+ $g = ($this->frameBuffer[$bufferIndex] >> 8) & 0xFF;
+ $b = $this->frameBuffer[$bufferIndex] & 0xFF;
+
+ // 350 is a good threshold for black and white
+ if ($r + $g + $b > 350) {
+ $this->canvasBuffer[$bufferIndex] = true;
+ } else {
+ $this->canvasBuffer[$bufferIndex] = false;
+ }
+ }
}
//Draw out the CanvasPixelArray data: