You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • Just to add, in Espruino, 'normal' arrays are quite slow to index, but Uint8Array/etc are pretty fast. It's not such an issue for small arrays though. Have you seen the performance page?

    One thing that can really help is to use forEach, so you can change:

          for(var i=0; i < pixels.length; i++) {
            // Draw pixel
            m.setPixel(pixels[i][0], pixels[i][1]);
          }
    

    to

          pixels.forEach(function(p) {m.setPixel(p[0], p[1]); }); // Draw pixels
    

    But as @allObjects suggests, I think drawing only the bits that have changed each frame would be a great help. You could actually have two instances of Graphics. One that contains the current set of squares, and another that is what you're drawing (containing the current squares and the shape that's dropping).

    So then instead of m.clear() you can do m.buffer.set(currentSquares.buffer) to copy the current squares into your 'display' buffer.

    Just an idea that might tidy up your code, but what about changing drawPixels to take the offset as an argument, so that instead of:

      switch(block.type) {
        case 'L':
          switch(block.rot) {
            case 1:
              this.drawPixels([[block.x, block.y],
                [block.x-1, block.y],
                [block.x-1, block.y+1],
                [block.x+1, block.y]]);
              break;
    ...
    

    You do:

      switch(block.type) {
        case 'L':
          switch(block.rot) {
            case 1:
              this.drawPixels(block.x, block.y, [[0,0],[-1, 0], [-1,1], [1,0]]);
              break;
    

    In fact you can tidy it up even more by putting all the blocks into a global array:

    var myBlocks = {
     "L": [0,
                 [[0,0],[-1, 0], [-1,1], [1,0]], // 1
                 [...], // 2
                 [...], // 3
                 [...]], // 4
       "J" : // ...
    // ...
    this.drawPixels(block.x, block.y,myBlocks[block.type][block.rot])­;
    

    That should be a load faster, and it'll use a lot less memory too.

About

Avatar for Gordon @Gordon started