summaryrefslogtreecommitdiffstats
path: root/src/Gameboy/DrawContext.php
blob: 36a13f8e2b8d87cafacf52051e463016bb2efd74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
namespace GameBoy;

use Drawille\Canvas;

class DrawContext
{
    // 160 x 144

    protected $canvas;
    protected $currentSecond = 0;
    protected $framesInSecond = 0;
    protected $fps = 0;

    public function __construct()
    {
        $this->canvas = new Canvas();
    }

    /**
     * Put image on canvas
     * @param  Object $canvasBuffer $data = Each pixel = 4 items on array (RGBA)
     * @param  int $left
     * @param  int $top
     */
	public function putImageData($canvasBuffer, $left, $top)
    {
        $canvasBuffer = $canvasBuffer->data;

        for ($i = 0; $i < count($canvasBuffer); $i = $i + 4) {
            // IGNORE ALPHA
            $total = $canvasBuffer[$i] + $canvasBuffer[$i + 1] + $canvasBuffer[$i + 2];

            $x = ($i / 4) % 160;
            $y = ceil(($i / 4) / 160);

            if ($total > 350) {
                $this->canvas->set($x, $y);
            }
        }

        if ($this->currentSecond != time()) {
            $this->fps = $this->framesInSecond;
            $this->currentSecond = time();
            $this->framesInSecond = 1;
        } else {
            $this->framesInSecond++;
        }

        echo "\e[H\e[2J";
        echo 'FPS: ' . $this->fps . PHP_EOL;
        echo $this->canvas->frame();
        $this->canvas->clear();
    }

    public function fillRect($left, $top, $width, $height)
    {
        // echo 'Fill' . PHP_EOL;
    }
}