Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
}
unset($autoload);

use GameBoy\Canvas\TerminalCanvas;
use GameBoy\Canvas\SdlCanvas;
use GameBoy\Core;
use GameBoy\Keyboard;

Expand Down Expand Up @@ -44,7 +44,7 @@

$rom = file_get_contents($filename);

$canvas = new TerminalCanvas();
$canvas = new SdlCanvas();
$core = new Core($rom, $canvas);
$keyboard = new Keyboard($core);

Expand Down
46 changes: 46 additions & 0 deletions src/Canvas/SdlCanvas.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace GameBoy\Canvas;

use GameBoy\Settings;

class SdlCanvas implements DrawContextInterface
{
private $serial = 0;
public $colorEnabled = true;
public $sdl;
public $renderer;
public $pixels;
public $greenoffset = 0;

public function __construct() {

$this->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);
}
}
}
3 changes: 2 additions & 1 deletion src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -1072,6 +1072,7 @@ public function executeIteration()
// no break
case 2:
$this->untilEnable--;
usleep(1000);
// no break
}
//Execute Interrupt:
Expand Down
37 changes: 34 additions & 3 deletions src/Keyboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)
Expand Down