PageRenderTime 349ms CodeModel.GetById 42ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/Play/play.js

http://github.com/mbebenita/Broadway
JavaScript | 63 lines | 53 code | 10 blank | 0 comment | 10 complexity | 9c601b35f88a30c8dd2105233cdadc10 MD5 | raw file
 1function getRGB(r, g, b) {
 2    return r << 24 | g << 16 | b;
 3}
 4
 5function drawLine(screen, x0, y0, x1, y1) {
 6    var dx = Math.abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
 7    var dy = Math.abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
 8    var err = (dx > dy ? dx : -dy) / 2;
 9
10    while (true) {
11        screen.setPixel(x0, y0, 200, 200, 200);
12        if (x0 === x1 && y0 === y1)
13            break;
14        var e2 = err;
15        if (e2 > -dx) {
16            err -= dy;
17            x0 += sx;
18        }
19        if (e2 < dy) {
20            err += dx;
21            y0 += sy;
22        }
23    }
24}
25
26var lastTime = new Date();
27
28var frame = 0;
29
30function paint(buffer, width, height, pitch, bpp) {
31    var uint32View = new Uint32Array(buffer);
32
33    var screen = {
34        setPixel : function(x, y, r, g, b) {
35            uint32View[(y * pitch / bpp) + x] = getRGB(r, g, b);
36        }
37    }
38
39    frame += 10;
40
41    for (y = 0; y < height; y++) {
42        start = y * pitch / bpp;
43        for (x = 0; x < width; x++) {
44            uint32View[start + x] = getRGB((x * x) / 256 + 3 * y + frame,
45                    (y * y) / 256 + x + frame, frame);
46        }
47    }
48
49    if (false) {
50        x0 = Math.round((Math.random() * width));
51        x1 = Math.round((Math.random() * width));
52        y0 = Math.round((Math.random() * height));
53        y1 = Math.round((Math.random() * height));
54        drawLine(screen, x0, x1, y0, y1);
55    }
56
57    newTime = new Date()
58    if (frame % 100 == 0) {
59        print("Frame Rate (fps): "
60                + (1000 / (newTime.getTime() - lastTime.getTime()) + "\n"))
61    }
62    lastTime = newTime;
63}