2017-05-01 14 views
1

저는 LWJGL과 Slick Utils를 사용하여 게임을 만들고 있습니다. 단일 PNG 이미지에 포함 된 프레임 세트로 애니메이션 텍스처를로드하려고합니다.Slick-Utils를 사용하여 자바에서 스프라이트 시트의 텍스처를로드하려면 어떻게해야합니까?

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); 
} 

희망이 당신을 도와줍니다.