2017-02-19 6 views
1

私はOpenGLでlwjglでプロジェクトを進めています。 GLFWImageバッファが必要なので、ウィンドウのアイコンを読み込むのが苦労していました。インターネットを精練の長い時間の後に、これは私が持っているものです。GLFWウィンドウアイコン

try { 
      BufferedImage originalImage = 
        ImageIO.read(new File("favicon.png")); 
      ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      ImageIO.write(originalImage, "png", baos); 
      baos.flush(); 
      byte[] imageInByte = baos.toByteArray(); 
      ByteBuffer buF = ByteBuffer.wrap(imageInByte); 
      GLFWImage.Buffer b = new GLFWImage.Buffer(buF); 
      glfwSetWindowIcon(window, b); 
     } catch (IOException io){ 
      System.out.println("Could not load window icon!"); 
      System.out.println(io.toString()); 
     } 

このような出力を備えたJavaランタイムのクラッシュ:

# A fatal error has been detected by the Java Runtime Environment: 
# The crash happened outside the Java Virtual Machine in native code. 
# See problematic frame for where to report the bug. 

私はに方法を見つけることができませんでしたこのようなエラーは発生しません。公式のglfwのドキュメントでは、LWJGLには存在しないような方法を使用するように言われています。あなたがこれについての経験があれば、正しい方向に向けるだけでも役立つでしょう。

ありがとうございます。

+0

私はこの同じ問題を抱えています。しかし、私は興味深いものを見つけました。つまり、b = new Buffer行の直前にbuF.flip()を追加すると、クラッシュを防ぐことができますが、アイコンを変更できません。また、私はGLFWImage.BufferではなくBufferでしか動作しませんが、おそらくIDEに依存しています。 –

答えて

0

このソリューションは扱いにくいです。しかし、それは私のために働く! :) これはlwjglのコードに基づいています。eventsデモですが、私はデモIOUtilを実装しなければならないことを使用しています。

import java.nio.ByteBuffer; 
import java.nio.IntBuffer; 

import org.lwjgl.glfw.GLFWImage; 

import static org.lwjgl.stb.STBImage.*; 
import static org.lwjgl.system.MemoryUtil.*; 

そして、私は次のコードを入れて(IOUtilという名前の)別のファイルに:

import org.lwjgl.BufferUtils; 

import java.io.IOException; 
import java.io.InputStream; 
import java.nio.ByteBuffer; 
import java.nio.channels.Channels; 
import java.nio.channels.ReadableByteChannel; 
import java.nio.channels.SeekableByteChannel; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 

import static org.lwjgl.BufferUtils.*; 

public final class IOUtil { 

private IOUtil() { 
} 

private static ByteBuffer resizeBuffer(ByteBuffer buffer, int newCapacity) { 
    ByteBuffer newBuffer = BufferUtils.createByteBuffer(newCapacity); 
    buffer.flip(); 
    newBuffer.put(buffer); 
    return newBuffer; 
} 

/** 
* Reads the specified resource and returns the raw data as a ByteBuffer. 
* 
* @param resource the resource to read 
* @param bufferSize the initial buffer size 
* 
* @return the resource data 
* 
* @throws IOException if an IO error occurs 
*/ 
public static ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException { 
    ByteBuffer buffer; 

    Path path = Paths.get(resource); 
    if (Files.isReadable(path)) { 
     try (SeekableByteChannel fc = Files.newByteChannel(path)) { 
      buffer = BufferUtils.createByteBuffer((int)fc.size() + 1); 
      while (fc.read(buffer) != -1) ; 
     } 
    } else { 
     try (
      InputStream source = IOUtil.class.getClassLoader().getResourceAsStream(resource); 
      ReadableByteChannel rbc = Channels.newChannel(source) 
     ) { 
      buffer = createByteBuffer(bufferSize); 

      while (true) { 
       int bytes = rbc.read(buffer); 
       if (bytes == -1) 
        break; 
       if (buffer.remaining() == 0) 
        buffer = resizeBuffer(buffer, buffer.capacity() * 2); 
      } 
     } 
    } 

    buffer.flip(); 
    return buffer; 
} 

} 

を次のように輸入されている

ByteBuffer icon16; 
ByteBuffer icon32; 
try { 
    icon16 = IOUtil.ioResourceToByteBuffer("src/hexsweeper/hex16.png", 2048); 
    icon32 = IOUtil.ioResourceToByteBuffer("src/hexsweeper/hex32.png", 4096); 
} catch (Exception e) { 
    throw new RuntimeException(e); 
} 


IntBuffer w = memAllocInt(1); 
IntBuffer h = memAllocInt(1); 
IntBuffer comp = memAllocInt(1); 

try (GLFWImage.Buffer icons = GLFWImage.malloc(2)) { 
    ByteBuffer pixels16 = stbi_load_from_memory(icon16, w, h, comp, 4); 
    icons 
     .position(0) 
     .width(w.get(0)) 
     .height(h.get(0)) 
     .pixels(pixels16); 

    ByteBuffer pixels32 = stbi_load_from_memory(icon32, w, h, comp, 4); 
    icons 
     .position(1) 
     .width(w.get(0)) 
     .height(h.get(0)) 
     .pixels(pixels32); 

    icons.position(0); 
    glfwSetWindowIcon(window, icons); 

    stbi_image_free(pixels32); 
    stbi_image_free(pixels16); 
} 

:アイコンを設定するためのコードはこれです"src/hexsweeper/hex16.png"をあなたのファイルに置き換えますが、windowはあなたのウィンドウに置き換えてください。設定する必要があります。これは私のために働いた、それは他の誰のために働くことを願って!

注:このコードの大部分は書きませんでした。それは、すばらしく役立つlwjglの貢献者、apostolos、Spasi、およびkappaOneによって作成されました。

関連する問題