Esempio n. 1
0
  /**
   * Create pixels for the tile
   *
   * @param attributes attributes of the tile variant
   * @param isSprite true if we want to retrieve sprite pixels, fals for normal tiles
   * @return rgb data
   */
  private int[] createPixels(final int attributes, final boolean isSprite) {
    // check whether we can use cached data
    final ColorPalette palette =
        this.video
            .getColorPalettes()[
            (attributes & 0x07)
                + (isSprite ? VideoChip.PALETTE_SPRITES : VideoChip.PALETTE_BACKGROUND)];
    int[] result = null;
    Integer hc = null;

    if (useCache) {
      hc = new Integer(palette.hashCode() ^ tileData.hashCode() ^ attributes);
      result = (int[]) tileCache.get(hc);
    }

    if (result == null) {
      // we cache some variables locally for better performance
      final int height_ = getHeight(), width_ = getWidth();
      final byte[][] colorIdxs_ = tileData.getColorIndexes();

      // create new empty RGB data space
      final int[] rgbData_ = new int[height_ * width_];

      // get the colors for all pixels
      final boolean isFlipHorizontally = (attributes & (1 << 5)) != 0;
      final boolean isFlipVertically = (attributes & (1 << 6)) != 0;
      final int cyAdd = (isFlipVertically ? -1 : 1), cxAdd = isFlipHorizontally ? -1 : 1;

      for (int y = 0, cy = isFlipVertically ? height_ - 1 : 0, yidx = 0;
          y < height_;
          ++y, cy += cyAdd, yidx += width_) {
        for (int x = 0, cx = isFlipHorizontally ? width_ - 1 : 0; x < width_; ++x, cx += cxAdd) {
          final int colIdx = colorIdxs_[cy][cx];

          if (isSprite && colIdx == 0) {
            rgbData_[yidx + x] = TRANSPARENT;
          } else {
            rgbData_[yidx + x] = palette.getColor(colIdx);
          }
        }
      }

      result = scale(rgbData_);

      // cache the result for later use
      if (useCache) {
        tileCache.put(hc, result);
      }
    }

    // at least the requested variant is valid afterwards
    this.areAllVariantsInvalid = false;

    return result;
  }
Esempio n. 2
0
 /** Clear the current contents of the tile cache */
 public static void resetCache() {
   if (null != tileCache) {
     tileCache.clear();
   }
 }