diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Gameboy/DrawContext.php | 3 | ||||
-rw-r--r-- | src/Gameboy/Keyboard.php | 63 | ||||
-rw-r--r-- | src/Gameboy/Settings.php | 3 |
3 files changed, 67 insertions, 2 deletions
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, |