2017-05-01 9 views
1

私はLWJGLとSlick Utilsを使ってゲームを作っています。私はアニメーションテクスチャを1つのPNG画像に含まれるフレームのセットとしてロードしようとしています。Slick-Utilsを使用してJavaのスプライトシートからテクスチャを読み込むにはどうすればよいですか?

私はSlickを使ってサブ画像を取得する方法を見つけようとしましたが、これまでのところ私がこのテーマで見つけたのは、Slickの外部でBufferedImagesを使って行う方法でした。 Slick Utilsライブラリを使用してこれを行う方法があるかどうかを知りたいのですが、これまでのプロジェクトで使用していた画像ロードコードのすべてがSlickを使用していたからです。

答えて

0

確かに、Slickはこれを行う手段を提供します。 org.newdawn.slick.SpriteSheet.initImpl()とそれ以降のorg.newdawn.slick.SpriteSheet.getSprite()を見ると、org.newdawn.slick.Image.getSubImage()を使用して既存のイメージの特定の部分をすばやく抽出する方法がわかります。

SpriteSheet.java

protected void initImpl() { 
    if (subImages != null) { 
     return; 
    } 

    int tilesAcross = ((getWidth()-(margin*2) - tw)/(tw + spacing)) + 1; 
    int tilesDown = ((getHeight()-(margin*2) - th)/(th + spacing)) + 1; 
    if ((getHeight() - th) % (th+spacing) != 0) { 
     tilesDown++; 
    } 

    subImages = new Image[tilesAcross][tilesDown]; 
    for (int x=0;x<tilesAcross;x++) { 
     for (int y=0;y<tilesDown;y++) { 
      //extract parts of the main image and store them in an array as sprites 
      subImages[x][y] = getSprite(x,y); 
     } 
    } 
} 

/** 
* Get a sprite at a particular cell on the sprite sheet 
* 
* @param x The x position of the cell on the sprite sheet 
* @param y The y position of the cell on the sprite sheet 
* @return The single image from the sprite sheet 
*/ 
public Image getSprite(int x, int y) { 
    target.init(); 
    initImpl(); 

    if ((x < 0) || (x >= subImages.length)) { 
     throw new RuntimeException("SubImage out of sheet bounds: "+x+","+y); 
    } 
    if ((y < 0) || (y >= subImages[0].length)) { 
     throw new RuntimeException("SubImage out of sheet bounds: "+x+","+y); 
    } 
    //Call Image.getSubImage() to get a portion of the image 
    return target.getSubImage(x*(tw+spacing) + margin, y*(th+spacing) + margin,tw,th); 
} 

あなたはそれを基準として使用することができるはずです。 SlickにバンドルされたTiledレンダラーをJava2Dに一度移植したことを覚えています。古い良質のPixelGrabberを使ってスプライトを抽出しています。

そして、あなたはSpriteSheetを使用することにした場合、あなたはorg.newdawn.slick.tiled.Layer.render()での使用の例を見つけることができます:

Layer.java

public void render(int x, int y, int sx, int sy, int width, int ty, 
     boolean lineByLine, int mapTileWidth, int mapTileHeight) { 
    for (int tileset = 0; tileset < map.getTileSetCount(); tileset++) { 
     TileSet set = null; 

     for (int tx = 0; tx < width; tx++) { 
      if ((sx + tx < 0) || (sy + ty < 0)) { 
       continue; 
      } 
      if ((sx + tx >= this.width) || (sy + ty >= this.height)) { 
       continue; 
      } 

      if (data[sx + tx][sy + ty][0] == tileset) { 
       if (set == null) { 
        set = map.getTileSet(tileset); 
        set.tiles.startUse(); 
       } 

       int sheetX = set.getTileX(data[sx + tx][sy + ty][1]); 
       int sheetY = set.getTileY(data[sx + tx][sy + ty][1]); 

       int tileOffsetY = set.tileHeight - mapTileHeight; 

       //Call SpriteSheet.renderInUse() to render the sprite cached at slot [sheetX, sheetY] 
       set.tiles.renderInUse(x + (tx * mapTileWidth), y 
         + (ty * mapTileHeight) - tileOffsetY, sheetX, 
         sheetY); 
      } 
     } 

     if (lineByLine) { 
      if (set != null) { 
       set.tiles.endUse(); 
       set = null; 
      } 
      map.renderedLine(ty, ty + sy, index); 
     } 

     if (set != null) { 
      set.tiles.endUse(); 
     } 
    } 
} 

SpriteSheet.java

public void renderInUse(int x,int y,int sx,int sy) { 
    //Draw the selected sprite at (x,y), using the width/height defined for this SpriteSheet 
    subImages[sx][sy].drawEmbedded(x, y, tw, th); 
} 

これがあなたを助けることを願ってください。

関連する問題