Ejemplo n.º 1
0
 public void setField2DBackground(Field2D field, HashMap pathMap, String fileName) {
   setField2D(field);
   LImage background = null;
   if (fileName != null) {
     setTileBackground(fileName);
     background = getBackground();
   } else {
     background = LImage.createImage(getWidth(), getHeight(), false);
   }
   LGraphics g = background.getLGraphics();
   for (int i = 0; i < field.getWidth(); i++) {
     for (int j = 0; j < field.getHeight(); j++) {
       int index = field.getType(j, i);
       Object o = pathMap.get(new Integer(index));
       if (o != null) {
         if (o instanceof LImage) {
           g.drawImage(
               ((LImage) o).getBufferedImage(),
               field.tilesToWidthPixels(i),
               field.tilesToHeightPixels(j),
               null);
         } else if (o instanceof Actor) {
           addObject(((Actor) o), field.tilesToWidthPixels(i), field.tilesToHeightPixels(j));
         }
       }
     }
   }
   g.dispose();
   setBackground(background);
 }
Ejemplo n.º 2
0
 /**
  * 复制指定的LImage图像区域
  *
  * @param image
  * @param g
  * @param x
  * @param y
  * @param width
  * @param height
  * @param dx
  * @param dy
  */
 public static void copyArea(
     LImage image, LGraphics g, int x, int y, int width, int height, int dx, int dy) {
   LImage tmp = image.getSubImage(x, y, width, height);
   g.drawImage(tmp, x + dx, y + dy);
   tmp.dispose();
   tmp = null;
 }
Ejemplo n.º 3
0
 /**
  * 返回指定资源文件中的指定资源为LImage
  *
  * @param fileName
  * @param resName
  * @return
  */
 public static LImage openLImage(String fileName, String resName) {
   byte[] buffer = null;
   try {
     buffer = LPKResource.openResource(fileName, resName);
     return LImage.createImage(buffer);
   } catch (Exception e) {
     throw new RuntimeException("File not found. ( " + resName + " )");
   }
 }
Ejemplo n.º 4
0
 /**
  * 创建默认的选择器背景图片
  *
  * @param w
  * @param h
  * @return
  */
 private LImage createDefaultDialog(int w, int h) {
   if (lazyDialog == null) {
     lazyDialog = new HashMap<String, LImage>(10);
   }
   int hash = 1;
   hash = LSystem.unite(hash, w);
   hash = LSystem.unite(hash, h);
   String key = String.valueOf(hash);
   LImage o = (LImage) lazyDialog.get(key);
   if (o == null) {
     o = LImage.createImage(w, h, true);
     LGraphics g = o.getLGraphics();
     LGradation.getInstance(Color.white, Color.black, w, h).drawHeight(g, 0, 0);
     g.setColor(Color.black);
     g.drawRect(0, 0, w - 1, h - 1);
     g.dispose();
     lazyDialog.put(key, o);
   }
   return o;
 }
Ejemplo n.º 5
0
  public void setTileBackground(LImage image) {
    if (image == null) {
      return;
    }

    int layerWidth = getWidth();
    int layerHeight = getHeight();
    int tileWidth = image.getWidth();
    int tileHeight = image.getHeight();

    LImage background = LImage.createImage(layerWidth, layerHeight, false);
    LGraphics g = background.getLGraphics();
    for (int x = 0; x < layerWidth; x += tileWidth) {
      for (int y = 0; y < layerHeight; y += tileHeight) {
        g.drawImage(image, x, y);
      }
    }
    g.dispose();

    setBackground(background);
  }
Ejemplo n.º 6
0
 public static LTextureData getTextureData(BufferedImage img) {
   if (img == null) {
     throw new RuntimeException("Source image is null !");
   }
   try {
     return new GLLoader(img);
   } catch (Exception e) {
     LTextures.destroyAll();
     LImage.disposeAll();
     LSystem.gc();
   }
   return new GLLoader(img);
 }
Ejemplo n.º 7
0
 public static LTextureData getTextureData(String fileName) {
   if (fileName == null) {
     throw new RuntimeException("Path is null !");
   }
   String key = fileName.trim().toLowerCase();
   LTextureData data = lazyLoader.get(key);
   if (data == null || data.source == null) {
     try {
       lazyLoader.put(key, data = new GLLoader(fileName));
     } catch (Exception e) {
       LTextures.destroyAll();
       LImage.disposeAll();
       LSystem.gc();
       lazyLoader.put(key, data = new GLLoader(fileName));
     }
   }
   return data;
 }
Ejemplo n.º 8
0
 protected void setPixels() {
   int[] dest = image.getPixels();
   if (lastDispose > 0) {
     if (lastDispose == 3) {
       int n = frameCount - 2;
       if (n > 0) {
         lastImage = getFrame(n - 1);
       } else {
         lastImage = null;
       }
     }
     if (lastImage != null) {
       int[] prev = lastImage.getPixels();
       image.setPixels(prev, width, height);
       if (lastDispose == 2) {
         LGraphics g = image.getLGraphics();
         Color c = null;
         if (transparency) {
           c = new Color(0, 0, 0, 0);
         } else {
           c = new Color(lastBgColor);
         }
         g.setColor(c);
         g.fill(lastRect);
         g.dispose();
       }
     }
   }
   int pass = 1;
   int inc = 8;
   int iline = 0;
   for (int i = 0; i < ih; i++) {
     int line = i;
     if (interlace) {
       if (iline >= ih) {
         pass++;
         switch (pass) {
           case 2:
             iline = 4;
             break;
           case 3:
             iline = 2;
             inc = 4;
             break;
           case 4:
             iline = 1;
             inc = 2;
         }
       }
       line = iline;
       iline += inc;
     }
     line += iy;
     if (line < height) {
       int k = line * width;
       int dx = k + ix;
       int dlim = dx + iw;
       if ((k + width) < dlim) {
         dlim = k + width;
       }
       int sx = i * iw;
       while (dx < dlim) {
         int index = ((int) pixels[sx++]) & 0xff;
         int c = act[index];
         if (c != 0) {
           dest[dx] = c;
         }
         dx++;
       }
     }
   }
   image.setPixels(dest, width, height);
 }
Ejemplo n.º 9
0
  /**
   * 将LImage转化为LTextureData
   *
   * @param image
   * @return
   */
  private void create(LImage image) {
    if (image == null) {
      return;
    }
    int srcWidth = image.getWidth();
    int srcHeight = image.getHeight();

    this.hasAlpha = image.hasAlpha();

    if (GLEx.isPowerOfTwo(srcWidth) && GLEx.isPowerOfTwo(srcHeight)) {
      this.width = srcWidth;
      this.height = srcHeight;
      this.texHeight = srcHeight;
      this.texWidth = srcWidth;
      this.source = image.getByteBuffer();
      this.pixels = image.getPixels();

      if (image.isAutoDispose()) {
        image.dispose();
        image = null;
      }
      return;
    }

    int texWidth = GLEx.toPowerOfTwo(srcWidth);
    int texHeight = GLEx.toPowerOfTwo(srcHeight);

    this.width = srcWidth;
    this.height = srcHeight;
    this.texHeight = texHeight;
    this.texWidth = texWidth;

    LImage texImage = new LImage(texWidth, texHeight, hasAlpha);

    LGraphics g = texImage.getLGraphics();

    g.drawImage(image, 0, 0);

    if (this.height < texHeight - 1) {
      copyArea(texImage, g, 0, 0, width, 1, 0, texHeight - 1);
      copyArea(texImage, g, 0, height - 1, width, 1, 0, 1);
    }
    if (this.width < texWidth - 1) {
      copyArea(texImage, g, 0, 0, 1, height, texWidth - 1, 0);
      copyArea(texImage, g, width - 1, 0, 1, height, 1, 0);
    }

    this.source = texImage.getByteBuffer();
    this.pixels = texImage.getPixels();

    if (image.isAutoDispose()) {
      image.dispose();
      image = null;
    }
  }
Ejemplo n.º 10
0
 public void setTileBackground(String fileName) {
   setTileBackground(LImage.createImage(fileName));
 }
Ejemplo n.º 11
0
 public void paintObjects(LGraphics g, int minX, int minY, int maxX, int maxY) {
   if (objects == null) {
     return;
   }
   synchronized (objects) {
     int paintSeq = 0;
     boolean isListener = false;
     Iterator iter = objects.iterator();
     Actor thing = null;
     while (iter.hasNext()) {
       thing = (Actor) iter.next();
       if (!thing.isVisible()) {
         continue;
       }
       isListener = (thing.actorListener != null);
       thing.update(elapsedTime);
       if (isListener) {
         thing.actorListener.update(elapsedTime);
       }
       RectBox rect = thing.getRectBox();
       int actorX = minX + thing.getX();
       int actorY = minY + thing.getY();
       int actorWidth = rect.getWidth();
       int actorHeight = rect.getHeight();
       if (actorX + actorWidth < minX
           || actorX > maxX
           || actorY + actorHeight < minY
           || actorY > maxY) {
         continue;
       }
       LImage actorImage = thing.getImage();
       if (actorImage != null) {
         thing.setLastPaintSeqNum(paintSeq++);
         boolean isBitmapFilter = ImageFilterType.NoneFilter != thing.filterType;
         Image bitmap = actorImage.getBufferedImage();
         if (isBitmapFilter) {
           bitmap = factory.doFilter(bitmap, thing.filterType);
         }
         int rotation = thing.getRotation();
         if (thing.alpha < 1.0) {
           g.setAlpha(thing.alpha);
         }
         if (rotation != 0) {
           double halfWidth = actorImage.getWidth() / 2;
           double halfHeight = actorImage.getHeight() / 2;
           double newWidth = actorX + halfWidth;
           double newHeight = actorY + halfHeight;
           atform.setToIdentity();
           atform.scale(thing.scaleX, thing.scaleY);
           atform.translate(newWidth, newHeight);
           atform.rotate(Math.toRadians(rotation));
           atform.translate(-newWidth, -newHeight);
           atform.translate(actorX, actorY);
           g.drawImage(bitmap, atform);
         } else {
           int width = (int) (actorImage.getWidth() * thing.scaleX);
           int height = (int) (actorImage.getHeight() * thing.scaleY);
           g.drawImage(bitmap, actorX, actorY, width, height);
         }
         if (isBitmapFilter) {
           bitmap.flush();
           bitmap = null;
         }
         if (thing.alpha < 1.0) {
           g.setAlpha(1.0F);
         }
       }
       if (actorX == 0 && actorY == 0) {
         thing.draw(g);
         if (isListener) {
           thing.actorListener.draw(g);
         }
       } else {
         g.translate(actorX, actorY);
         thing.draw(g);
         if (isListener) {
           thing.actorListener.draw(g);
         }
         g.translate(-actorX, -actorY);
       }
     }
   }
 }