summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGabriel Rodrigues Couto <gabrielrcouto@gmail.com>2016-02-21 12:53:03 -0300
committerGabriel Rodrigues Couto <gabrielrcouto@gmail.com>2016-02-21 12:53:03 -0300
commitc1e3e036e2144bd57140643cdd8cf8efe63c8907 (patch)
treee831da99a94ac497cd57e40697a140dcbf460b2e
parente71aab0397520ee94a8b6194d2a76c5813f06e05 (diff)
downloadphp-terminal-gameboy-emulator-c1e3e036e2144bd57140643cdd8cf8efe63c8907.zip
php-terminal-gameboy-emulator-c1e3e036e2144bd57140643cdd8cf8efe63c8907.tar.gz
php-terminal-gameboy-emulator-c1e3e036e2144bd57140643cdd8cf8efe63c8907.tar.bz2
Tested on PHP 7 - ~14 FPS
Keyboard working :-) DR MARIO playable \o/
-rw-r--r--boot.php4
-rwxr-xr-xdocker-php-72
-rw-r--r--src/Gameboy/DrawContext.php3
-rw-r--r--src/Gameboy/Keyboard.php63
-rw-r--r--src/Gameboy/Settings.php3
5 files changed, 73 insertions, 2 deletions
diff --git a/boot.php b/boot.php
index ddc724a..ef2d8b6 100644
--- a/boot.php
+++ b/boot.php
@@ -3,11 +3,14 @@
require_once __DIR__.'/vendor/autoload.php';
use GameBoy\Core;
+use GameBoy\Keyboard;
use GameBoy\Settings;
$rom = base64_decode(file_get_contents('drmario.rom'));
$core = new Core($rom);
+$keyboard = new Keyboard($core);
+
$core->start();
if ($core->stopEmulator & 2 == 2) {
@@ -16,6 +19,7 @@ if ($core->stopEmulator & 2 == 2) {
while (true) {
$core->run();
+ $keyboard->check();
}
} else if (($core->stopEmulator & 2) == 0) {
echo "The GameBoy core is already running." . PHP_EOL;
diff --git a/docker-php-7 b/docker-php-7
new file mode 100755
index 0000000..11b7def
--- /dev/null
+++ b/docker-php-7
@@ -0,0 +1,2 @@
+#!/bin/bash
+docker run -it -v $PWD:/app -w /app php:cli /bin/bash
diff --git a/src/Gameboy/DrawContext.php b/src/Gameboy/DrawContext.php
index 7a4bbba..4081d69 100644
--- a/src/Gameboy/DrawContext.php
+++ b/src/Gameboy/DrawContext.php
@@ -26,12 +26,13 @@ class DrawContext
public function putImageData($canvasBuffer, $left, $top)
{
for ($i = 0; $i < count($canvasBuffer); $i = $i + 4) {
- // IGNORE ALPHA
+ // Sum of all colors, Ignore alpha
$total = $canvasBuffer[$i] + $canvasBuffer[$i + 1] + $canvasBuffer[$i + 2];
$x = ($i / 4) % 160;
$y = ceil(($i / 4) / 160);
+ // 350 is a good threshold for black and white
if ($total > 350) {
$this->canvas->set($x, $y);
}
diff --git a/src/Gameboy/Keyboard.php b/src/Gameboy/Keyboard.php
new file mode 100644
index 0000000..8ad007f
--- /dev/null
+++ b/src/Gameboy/Keyboard.php
@@ -0,0 +1,63 @@
+<?php
+namespace GameBoy;
+
+class Keyboard
+{
+ public $core;
+ public $file;
+ public $keyPressing = null;
+ public $started = false;
+
+ public function __construct(Core $core)
+ {
+ $this->core = $core;
+ exec('stty -icanon');
+ $this->file = fopen('php://stdin', 'r');
+ stream_set_blocking($this->file, false);
+ }
+
+ public function check()
+ {
+ $key = fread($this->file, 1);
+
+ if (! empty($key)) {
+ $this->keyDown($key);
+ } else if (! empty($this->keyPressing)) {
+ $this->keyUp($this->keyPressing);
+ }
+
+ $this->keyPressing = $key;
+ }
+
+ public function matchKey($key)
+ {
+ //Maps a keyboard key to a gameboy key.
+ //Order: Right, Left, Up, Down, A, B, Select, Start
+
+ $keyIndex = array_search($key, Settings::$settings[3]);
+
+ if ($keyIndex === false) {
+ return -1;
+ }
+
+ return $keyIndex;
+ }
+
+ public function keyDown($key)
+ {
+ $keyCode = $this->matchKey($key);
+
+ if ($keyCode > -1) {
+ $this->core->JoyPadEvent($keyCode, true);
+ }
+ }
+
+ public function keyUp($key)
+ {
+ $keyCode = $this->matchKey($key);
+
+ if ($keyCode > -1) {
+ $this->core->JoyPadEvent($keyCode, false);
+ }
+ }
+} \ No newline at end of file
diff --git a/src/Gameboy/Settings.php b/src/Gameboy/Settings.php
index d08b4db..ba9adab 100644
--- a/src/Gameboy/Settings.php
+++ b/src/Gameboy/Settings.php
@@ -15,7 +15,8 @@ class Settings
true,
//[3] - Keyboard button map.
- [39, 37, 38, 40, 88, 90, 16, 13],
+ //Order: Right, Left, Up, Down, A, B, Select, Start
+ ['d', 'a', 'w', 's', ',', '.', 'n', 'm'],
//[4] - Frameskip Amount (Auto frameskip setting allows the script to change this.)
0,