2015-01-31 2 views
0

처음에는 libgdx가 주로 게임용으로 사용됩니다. 나는 방금 그 사실을 알았고 단순한 사진 프레임과 같은 다른 가능한 (가능한) 목적을 부여하려고했습니다. 아래의 코드는 개념 증명의 한 부분이며 더 큰 앱으로 발전해야하므로 실행됩니다.픽스맵의 Libgdx 이미지는 단색 검정색으로 그려져 있습니다.

아래 나는 지금까지 내가했던 것에 매우 간단한 수업을 게시했습니다. 그것이하는 일은 매초마다 다른 스레드에서 디스크에서 이미지를로드하고 픽스맵에 놓은 다음 GL 스레드에 텍스처를 만듭니다 (모든 것을 올바르게 이해하는 경우).

많은 시행 착오 끝에이 코드를 보았습니다. OpenGL 스레드에서 텍스처를 만들어야한다는 것을 알게 된 것은 1 시간이 걸렸습니다. 텍스처가 스레드 외부에서 생성 될 때 이미지는로드 된 텍스처가없는 큰 검은 색 상자입니다.

음,이 클래스 버전을 스레드에서 만든 텍스처로 실행했을 때 마침내 이미지가 표시되고 모든 [num] 초가 멋지게 변색됩니다.

하지만 15 회 실행 한 후에 텍스처가 GL 스레드 외부에서 생성 된 것처럼 이미지가 다시 검은 색 상자로 나타나기 시작합니다. 콘솔에 아무런 예외도 인쇄되지 않습니다.

응용 프로그램은 메모리 분할 128/128의 Raspberry Pi에서 실행됩니다. 이미지는 1920 * 1080 (프로그레시브가 아닌)의 JPEG 이미지입니다.
VIRT : 249m
RES : 37m
SHR : 10m 명령 줄은 다음과 같습니다 자바 -Xmx128m -DPI = 사실 -DLWJGJ_BACKEND = GLES -Djava.library.path = libs가 : 상단에 따라 다음과 같이 메모리 사용은/opt/vc/lib :. -classpath * :. + : org.pidome.raspberry.mirrorclient.BootStrapper

나는 새 이미지가로드 될 때 상승 RES를 볼 수 있지만로드 한 후이를 다시 37

에에서 System.out.println ("스왑 카운터"입니다 swapCounter); 스레드가 실행될 때 출력을 계속 제공합니다.

15 명이 반복 된 후에 텍스처가 더 이상 표시되지 않고 이미지가 단색 검정이라는 문제를 해결할 수있는 올바른 방향을 제시 할 수 있습니까?

public class PhotosActor { 

    List<Image> images = new ArrayList<>(); 

    private String imgDir = "appimages/photos/"; 
    List<String> fileSet = new ArrayList<>(); 

    private final ScheduledExecutorService changeExecutor = Executors.newSingleThreadScheduledExecutor(); 

    Stage stage; 

    int swapCounter = 0; 

    public PhotosActor(Stage stage) { 
     this.stage = stage; 
    } 

    public final void preload(){ 
     loadFileSet(); 
     changeExecutor.scheduleAtFixedRate(switchimg(), 10, 10, TimeUnit.SECONDS); 
    } 

    private Runnable switchimg(){ 
     Runnable run =() -> { 
      try { 
       swapCounter++; 
       FileInputStream input = new FileInputStream(fileSet.get(new Random().nextInt(fileSet.size()))); 
       Gdx2DPixmap gpm = new Gdx2DPixmap(input, Gdx2DPixmap.GDX2D_FORMAT_RGB888); 
       input.close(); 
       Pixmap map = new Pixmap(gpm); 
       Gdx.app.postRunnable(() -> { 
        System.out.println("Swap counter: " +swapCounter); 
        Texture tex = new Texture(map); 
        map.dispose(); 
        Image newImg = new Image(tex); 
        newImg.addAction(Actions.sequence(Actions.alpha(0),Actions.fadeIn(1f),Actions.delay(5),Actions.run(() -> { 
         if(images.size()>1){ 
          Image oldImg = images.remove(1); 
          oldImg.getActions().clear(); 
          oldImg.remove(); 
         } 
        }))); 
        images.add(0,newImg); 
        stage.addActor(newImg); 
        newImg.toBack(); 
        if(images.size()>1){ images.get(1).toBack(); } 
       }); 
      } catch (Exception ex) { 
       Logger.getLogger(PhotosActor.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     }; 
     return run; 
    } 

    private void loadFileSet(){ 
     File[] files = new File(imgDir).listFiles(); 
     for (File file : files) { 
      if (file.isFile()) { 
       System.out.println("Loading: " + imgDir + file.getName()); 
       fileSet.add(imgDir + file.getName()); 
      } 
     } 
    } 


} 

미리 감사와 환호, 존 :

여기에 내 현재 코드 (이름 PhotosActor에 잘못된 내용이 먼저 배우로 그것을 시도의 결과)입니다.

답변

0

나는이 문제를 직접 해결할 수있었습니다. 몇 분 전에 나는 그 질감을 처리해야한다는 것을 깨달았습니다. 나는 이미지를 제거하면 텍스처가 제거되었다고 생각했다. 어떤 명확히하지 않았다 (또는 나는 최신 버전으로 갱신해야한다).

public class PhotoImage extends Image { 

    Texture tex; 

    public PhotoImage(Texture tex){ 
     super(tex); 
     this.tex = tex; 
    } 

    public void dispose(){ 
     try { 
      this.tex.dispose(); 
     } catch(Exception ex){ 
      System.out.println(ex.getMessage()); 
     } 
    } 
} 

모든 위치에 내가이 광 화상 클래스로 변경 이미지 클래스에 다스 려했다 :

그래서 내가 한 일은 이미지 클래스를 확장하는 새로운 클래스를 만들 수 있었다.제거 기능에서

public class PhotosActor { 

    List<PhotoImage> images = new ArrayList<>(); 

    private String imgDir = "appimages/photos/"; 
    List<String> fileSet = new ArrayList<>(); 

    private final ScheduledExecutorService changeExecutor = Executors.newSingleThreadScheduledExecutor(); 

    Stage stage; 

    int swapCounter = 0; 

    public PhotosActor(Stage stage) { 
     this.stage = stage; 
    } 

    public final void preload(){ 
     loadFileSet(); 
     changeExecutor.scheduleAtFixedRate(switchimg(), 10, 10, TimeUnit.SECONDS); 
    } 

    private Runnable switchimg(){ 
     Runnable run =() -> { 
      try { 
       swapCounter++; 
       byte[] byteResult = readLocalRandomFile(); 
       Pixmap map = new Pixmap(byteResult, 0, byteResult.length); 
       Gdx.app.postRunnable(() -> { 
        System.out.println("Swap counter: " +swapCounter); 
        Texture tex = new Texture(map); 
        map.dispose(); 
        PhotoImage newImg = new PhotoImage(tex); 
        images.add(0,newImg); 
        stage.addActor(newImg); 
        addTransform(newImg); 
       }); 
      } catch (Exception ex) { 
       Logger.getLogger(PhotosActor.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     }; 
     return run; 
    } 

    public void addTransform(Image img){ 
     switch(new Random().nextInt(3)){ 
      case 0: 
       img.toBack(); 
       if(images.size()>1){ images.get(1).toBack(); } 
       img.addAction(Actions.sequence(Actions.alpha(0),Actions.fadeIn(1f),Actions.delay(5),Actions.run(() -> { 
        removeOldImg(); 
       }))); 
      break; 
      case 1: 
       img.toBack(); 
       if(images.size()>1){ images.get(1).toBack(); } 
       img.setPosition(1920f, 1080f); 
       img.addAction(Actions.sequence(Actions.moveTo(0f, 0f, 5f),Actions.run(() -> { 
        removeOldImg(); 
       }))); 
      break; 
      case 2: 
       img.toBack(); 
       if(images.size()>1){ images.get(1).toBack(); } 
       img.setScale(0f, 0f); 
       img.setPosition(960f, 540f); 
       img.addAction(Actions.sequence(Actions.parallel(Actions.scaleTo(1f, 1f, 5f), Actions.moveTo(0f, 0f, 5f)),Actions.run(() -> { 
        removeOldImg(); 
       }))); 
      break; 
     } 
    } 

    private void removeOldImg(){ 
     if(images.size()>1){ 
      PhotoImage oldImg = images.remove(1); 
      oldImg.remove(); 
      oldImg.getActions().clear(); 
      oldImg.dispose(); 
     } 
     System.out.println("Amount of images: " + images.size()); 
    } 

    private byte[] readLocalRandomFile() throws Exception{ 
     FileInputStream input = null; 
     try { 
      input = new FileInputStream(fileSet.get(new Random().nextInt(fileSet.size()))); 
      ByteArrayOutputStream out; 
      try (InputStream in = new BufferedInputStream(input)) { 
       out = new ByteArrayOutputStream(); 
       byte[] buf = new byte[1024]; 
       int n = 0; 
       while (-1 != (n = in.read(buf))) { 
        out.write(buf, 0, n); 
       } 
       out.close(); 
       return out.toByteArray(); 
      } catch (IOException ex) { 
       Logger.getLogger(PhotosActor.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } catch (FileNotFoundException ex) { 
      Logger.getLogger(PhotosActor.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     throw new Exception("No data"); 
    } 

    private void loadFileSet(){ 
     File[] files = new File(imgDir).listFiles(); 
     for (File file : files) { 
      if (file.isFile()) { 
       System.out.println("Loading: " + imgDir + file.getName()); 
       fileSet.add(imgDir + file.getName()); 
      } 
     } 
    } 


} 

내가 이제 텍스처를 제거하는

oldImg.dispose(); 

를 추가했습니다 : 수정 클래스는 몇 가지 지금처럼 보인다. 이미지 전환은 이제 Raspberry Pi에서 50+ fps로 실행되며 이미지 회전 카운터가 켜짐 : 88입니다. 사람들이 당신의 시간에 대해 감사하는 곳이 있다면!

관련 문제