<commit_before>/*
 Copyright (C) 2014 by <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

/// <reference path="../../../lib/pow2.d.ts"/>
/// <reference path="../tileObject.ts" />
/// <reference path="../tileMapView.ts" />
/// <reference path="../tileMap.ts" />

module pow2 {
   export class TileMapRenderer extends pow2.SceneObjectRenderer {
      buffer:HTMLCanvasElement[][] = null; // A 2d grid of rendered canvas textures.
      bufferMapName:string = null; // The name of the rendered map.  If the map name changes, the buffer is re-rendered.
      bufferComplete:boolean = false; // True if the entire map was rendered with all textures loaded and ready.

      private _clipRect:Rect = new pow2.Rect();
      private _renderRect:Rect = new pow2.Rect();

      render(object:TileMap, view:TileMapView) {
         var squareUnits = 8;
         var squareSize = squareUnits * view.unitSize;
         if(!object.isLoaded()){
            return;
         }
         if(object.dirtyLayers){
            object.dirtyLayers = false;
            this.buffer = null;
            this.bufferComplete = false;
         }
         if(!this.bufferComplete || this.buffer === null || this.bufferMapName === null || this.bufferMapName !== object.mapName){
            var tileUnitSize = squareSize / view.unitSize;
            // Unit size is 16px, so rows/columns should be 16*16 for 256px each.
            var columns = Math.ceil(object.bounds.extent.x / squareUnits);
            var rows = Math.ceil(object.bounds.extent.y / squareUnits);
            this.buffer = new Array(columns);
            for(var col:number = 0; col < columns; col++){
               this.buffer[col] = new Array(rows);
            }
            this.bufferComplete = true;
            var layers:tiled.ITiledLayer[] = object.getLayers();
            for(var col:number = 0; col < columns; col++){
               for(var row:number = 0; row < rows; row++){
                  var xOffset = col * tileUnitSize;
                  var xEnd = xOffset + tileUnitSize;
                  var yOffset = row * tileUnitSize;
                  var yEnd = yOffset + tileUnitSize;
                  this.buffer[col][row] = view.renderToCanvas(squareSize,squareSize,(ctx) => {
                     for(var x = xOffset; x < xEnd; x++){
                        for(var y = yOffset; y < yEnd; y++){

                           // Each layer
                           _.each(layers,(l:tiled.ITiledLayer) => {
                              if(!l.visible){
                                 return;
                              }
                              var gid:number = object.getTileGid(l.name,x, y);
                              var meta:pow2.tiled.ITileInstanceMeta = object.getTileMeta(gid);
                              if (meta) {
                                 var image = meta.image;
                                 // Keep this inline to avoid more function calls.
                                 var dstH, dstW, dstX, dstY, srcH, srcW, srcX, srcY;
                                 if (!image || !image.complete) {
                                    this.bufferComplete = false;
                                    return;
                                 }
                                 srcX = meta.x;
                                 srcY = meta.y;
                                 srcW = meta.width;
                                 srcH = meta.height;
                                 dstX = (x - xOffset) * view.unitSize;
                                 dstY = (y - yOffset) * view.unitSize;
                                 dstW = dstH = view.unitSize;
                                 ctx.drawImage(image, srcX, srcY, srcW, srcH, dstX, dstY, dstW, dstH);
                              }
                           });
                        }
                     }
                     // Append chunks to body (DEBUG HACKS)

                     // var dataImage = new Image();
                     // dataImage.src = ctx.canvas.toDataURL();
                     // $('body').append(dataImage);
                  });
               }
            }
            this.bufferMapName = object.mapName;
         }<commit_msg>fix(TileMapRenderer): render tilemap with pow-core properly<commit_after>/*
 Copyright (C) 2014 by <NAME>

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

/// <reference path="../../../lib/pow2.d.ts"/>
/// <reference path="../tileObject.ts" />
/// <reference path="../tileMapView.ts" />
/// <reference path="../tileMap.ts" />

module pow2 {
   export class TileMapRenderer extends pow2.SceneObjectRenderer {
      buffer:HTMLCanvasElement[][] = null; // A 2d grid of rendered canvas textures.
      bufferMapName:string = null; // The name of the rendered map.  If the map name changes, the buffer is re-rendered.
      bufferComplete:boolean = false; // True if the entire map was rendered with all textures loaded and ready.

      private _clipRect:Rect = new pow2.Rect();
      private _renderRect:Rect = new pow2.Rect();

      render(object:TileMap, view:TileMapView) {
         var squareUnits = 8;
         var squareSize = squareUnits * view.unitSize;
         if(!object.isLoaded()){
            return;
         }
         if(object.dirtyLayers){
            object.dirtyLayers = false;
            this.buffer = null;
            this.bufferComplete = false;
         }
         if(!this.bufferComplete || this.buffer === null || this.bufferMapName === null || this.bufferMapName !== object.mapName){
            var tileUnitSize = squareSize / view.unitSize;
            // Unit size is 16px, so rows/columns should be 16*16 for 256px each.
            var columns = Math.ceil(object.bounds.extent.x / squareUnits);
            var rows = Math.ceil(object.bounds.extent.y / squareUnits);
            this.buffer = new Array(columns);
            for(var col:number = 0; col < columns; col++){
               this.buffer[col] = new Array(rows);
            }
            this.bufferComplete = true;
            var layers:tiled.ITiledLayer[] = object.getLayers();
            for(var col:number = 0; col < columns; col++){
               for(var row:number = 0; row < rows; row++){
                  var xOffset = col * tileUnitSize;
                  var xEnd = xOffset + tileUnitSize;
                  var yOffset = row * tileUnitSize;
                  var yEnd = yOffset + tileUnitSize;
                  this.buffer[col][row] = view.renderToCanvas(squareSize,squareSize,(ctx) => {
                     for(var x = xOffset; x < xEnd; x++){
                        for(var y = yOffset; y < yEnd; y++){

                           // Each layer
                           _.each(layers,(l:tiled.ITiledLayer) => {
                              if(!l.visible){
                                 return;
                              }
                              var gid:number = object.getTileGid(l.name,x, y);
                              var meta:pow2.tiled.ITileInstanceMeta = object.getTileMeta(gid);
                              if (meta) {
                                 var image:HTMLImageElement = (<any>meta.image).data;
                                 // Keep this inline to avoid more function calls.
                                 var dstH, dstW, dstX, dstY, srcH, srcW, srcX, srcY;
                                 if (!image || !image.complete) {
                                    this.bufferComplete = false;
                                    return;
                                 }
                                 srcX = meta.x;
                                 srcY = meta.y;
                                 srcW = meta.width;
                                 srcH = meta.height;
                                 dstX = (x - xOffset) * view.unitSize;
                                 dstY = (y - yOffset) * view.unitSize;
                                 dstW = dstH = view.unitSize;
                                 ctx.drawImage(image, srcX, srcY, srcW, srcH, dstX, dstY, dstW, dstH);
                              }
                           });
                        }
                     }
                     // Append chunks to body (DEBUG HACKS)

                     // var dataImage = new Image();
                     // dataImage.src = ctx.canvas.toDataURL();
                     // $('body').append(dataImage);
                  });
               }
            }
            this.bufferMapName = object.mapName;
         }
{'fraction_non_alphanumeric': 0.10150891632373114, 'fraction_numerical': 0.005909042946079983, 'mean_word_length': 1.3276031434184676, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 42, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3614105', 'n_tokens_mistral': 2408, 'n_tokens_neox': 2315, 'n_words': 683}