diff --git a/boot.php b/boot.php index d2e7cfe..a218f31 100644 --- a/boot.php +++ b/boot.php @@ -10,7 +10,7 @@ } unset($autoload); -use GameBoy\Canvas\TerminalCanvas; +use GameBoy\Canvas\SdlCanvas; use GameBoy\Core; use GameBoy\Keyboard; @@ -44,7 +44,7 @@ $rom = file_get_contents($filename); -$canvas = new TerminalCanvas(); +$canvas = new SdlCanvas(); $core = new Core($rom, $canvas); $keyboard = new Keyboard($core); diff --git a/src/Canvas/SdlCanvas.php b/src/Canvas/SdlCanvas.php new file mode 100644 index 0000000..4bf0519 --- /dev/null +++ b/src/Canvas/SdlCanvas.php @@ -0,0 +1,46 @@ +sdl = SDL_CreateWindow('PHP TerminalGameboy', SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 576, SDL_WINDOW_SHOWN); + $this->renderer = SDL_CreateRenderer($this->sdl, 0, SDL_RENDERER_SOFTWARE); + SDL_SetRenderDrawColor($this->renderer, 0, 0, 0, 255); + SDL_RenderClear($this->renderer); + + } + public function draw($canvasBuffer) + { + //print_r($canvasBuffer); die(); + //echo count($canvasBuffer); + if(count($canvasBuffer) > 0) { + for ($y = 0; $y < 144; $y++) { + for ($x = 0; $x < 160; $x++) { + $index = ($x + ($y * 160))*4; + if($canvasBuffer[$index] != "") { + $fill = [$canvasBuffer[$index], $canvasBuffer[$index+1], $canvasBuffer[$index+2]]; + if(!isset($this->pixels[$x][$y])) { + $this->pixels[$x][$y] = new \SDL_Rect($x*4, $y*4, 4, 4); + } + SDL_SetRenderDrawColor($this->renderer, $fill[0], $fill[1], $fill[2], 255); + SDL_RenderFillRect($this->renderer, $this->pixels[$x][$y]); + } + + } + } + SDL_RenderPresent($this->renderer); + } + } +} diff --git a/src/Core.php b/src/Core.php index 3423d2f..4d87e3a 100644 --- a/src/Core.php +++ b/src/Core.php @@ -289,7 +289,7 @@ class Core public $bgEnabled = true; - public $spritePriorityEnabled = true; + public $spritePriorityEnabled = 0x80; // true if there are any images to be invalidated public $tileReadState = []; @@ -1072,6 +1072,7 @@ public function executeIteration() // no break case 2: $this->untilEnable--; + usleep(1000); // no break } //Execute Interrupt: diff --git a/src/Keyboard.php b/src/Keyboard.php index 85001ae..c0b0ef9 100644 --- a/src/Keyboard.php +++ b/src/Keyboard.php @@ -5,20 +5,29 @@ class Keyboard { public $core; + public $event; public $file; + public $keymap; public $keyPressing = null; public $started = false; + public $close = 0; public function __construct(Core $core) { $this->core = $core; - exec('stty -icanon -echo'); - $this->file = fopen('php://stdin', 'r'); - stream_set_blocking($this->file, false); + $this->event = new \SDL_Event; + $this->keymap = + [ + "119"=>'w',"97"=>'a',"115"=>'s',"100"=>'d',"110"=>'n',"109"=>'m',"44"=>',',"46"=>'.' + ]; + //exec('stty -icanon -echo'); + //$this->file = fopen('php://stdin', 'r'); + //stream_set_blocking($this->file, false); } public function check() { + /* $key = fread($this->file, 1); if (!empty($key)) { @@ -28,6 +37,28 @@ public function check() } $this->keyPressing = $key; + */ + while( SDL_PollEvent( $this->event ) ){ + /* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */ + if(isset($this->event->key->keysym)) { + if(isset($this->keymap[$this->event->key->keysym->sym])) { + $key = $this->keymap[$this->event->key->keysym->sym]; + switch($this->event->type){ + case SDL_KEYDOWN: + $this->keyDown($key); + echo "SDL_KEYDOWN ".$this->event->key->keysym->sym."\n"; + break; + case SDL_KEYUP: + $this->keyUp($key); + echo "SDL_KEYUP ".$this->event->key->keysym->sym."\n"; + break; + default: + break; + } + $this->keyPressing = $key; + } } + break; + } } public function matchKey($key)