GLFW窗口图标

时间:2017-02-19 00:47:04

标签: lwjgl glfw

我正在使用lwjgl在OpenGL上开展一个项目。我很难加载窗口的图标,因为它需要一个GLFWImage缓冲区。经过长时间的互联网搜索,这就是我所拥有的:

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中似乎不存在的方法。如果您对此有任何经验,那么指出我正确的方向甚至是有帮助的。

提前致谢。

1 个答案:

答案 0 :(得分:1)

这个解决方案很笨重;但是,它对我有用! :) 它基于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.*;

导入如下:

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

}

在另一个文件(名为IOUtil)中,我输入以下代码:

"src/hexsweeper/hex16.png"

window替换为您的文件,将set /p StartPosition=Start position (HH:MM:SS): set /p EndPosition=End position (HH:MM:SS): 替换为您的窗口,然后您应该设置。这对我有用,希望它适用于其他所有人!

注意:我没有编写大部分代码。它是由非常有帮助的lwjgl贡献者apostolos,Spasi和kappaOne制作的。