Exemple #1
0
  @Override
  protected void draw(final GLState pGLState, final Camera pCamera) {
    final int tileColumns = this.mTileColumns;
    final int tileRows = this.mTileRows;
    final int tileWidth = this.mTMXTiledMap.getTileWidth();
    final int tileHeight = this.mTMXTiledMap.getTileHeight();

    final float scaledTileWidth = tileWidth * this.mScaleX;
    final float scaledTileHeight = tileHeight * this.mScaleY;

    final float[] cullingVertices = this.mCullingVertices;
    RectangularShapeCollisionChecker.fillVertices(
        0, 0, this.mWidth, this.mHeight, this.getLocalToSceneTransformation(), cullingVertices);

    final float layerMinX = cullingVertices[SpriteBatch.VERTEX_INDEX_X];
    final float layerMinY = cullingVertices[SpriteBatch.VERTEX_INDEX_Y];

    final float cameraMinX = pCamera.getXMin();
    final float cameraMinY = pCamera.getYMin();
    final float cameraWidth = pCamera.getWidth();
    final float cameraHeight = pCamera.getHeight();

    /* Determine the area that is visible in the camera. */
    final float firstColumnRaw = (cameraMinX - layerMinX) / scaledTileWidth;
    final int firstColumn =
        MathUtils.bringToBounds(0, tileColumns - 1, (int) Math.floor(firstColumnRaw));
    final int lastColumn =
        MathUtils.bringToBounds(
            0, tileColumns - 1, (int) Math.ceil(firstColumnRaw + cameraWidth / scaledTileWidth));

    final float firstRowRaw = (cameraMinY - layerMinY) / scaledTileHeight;
    final int firstRow = MathUtils.bringToBounds(0, tileRows - 1, (int) Math.floor(firstRowRaw));
    final int lastRow =
        MathUtils.bringToBounds(
            0, tileRows - 1, (int) Math.floor(firstRowRaw + cameraHeight / scaledTileHeight));

    for (int row = firstRow; row <= lastRow; row++) {
      for (int column = firstColumn; column <= lastColumn; column++) {
        this.mSpriteBatchVertexBufferObject.draw(
            GLES20.GL_TRIANGLE_STRIP,
            this.getSpriteBatchIndex(column, row) * SpriteBatch.VERTICES_PER_SPRITE,
            SpriteBatch.VERTICES_PER_SPRITE);
      }
    }
  }