2

On Mac OS X El Capitan, OpenGL version 4.1, my LWJGL 3.0 app hangs when calling the Slick2D function TextureLoader.getTexture()

Here's the code I'm attempting to use to load the texture. It is run on the same thread as the main loop, and is called after the window is set up.

FileInputStream file = new FileInputStream("src/AppIcon.png");
texture = TextureLoader.getTexture("PNG", file);

The file does exist, and the code works fine when I comment out the code for texturing, which is this method

public int loadTexture(String filename){
    Texture texture = null;
    try{
        FileInputStream file = new FileInputStream(filename + ".png");
        //The app freezes here
        texture = TextureLoader.getTexture("png", file);
        //"LOADED" is never printed to the console.
        System.out.println("LOADED");
    }
    catch(FileNotFoundException e){
        e.printStackTrace();
    }
    catch(IOException e){
        e.printStackTrace();
    }
     return texture.getTextureID();
}

The texture I'm attempting to use is a 1024 x 1024 PNG image,

enter image description here

I've also tried using a much smaller 16 x 16 pixel image,

16x16

but I get the same result.

Both images are physically okay, no errors are logged, and the last thing that is printed in console is from Slick2D, stating

INFO:Use Java PNG Loader = true

Is this an OS specific bug, or am I doing something wrong?

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
  • 1
    Can you run `pngcheck` on your images and check if they are physically okay? They probably are - chances are pretty low *two* images are bad (unless you created them both with the same (bad) software), but still worth checking. Can you add one of these images to your question? – Jongware Nov 22 '15 at 03:27
  • 1
    @Jongware I've added both of the images to the question. They are both physically okay, and I created them using Photoshop – Jojodmo Nov 22 '15 at 03:32

2 Answers2

3

As it turns out, Slick2D is not compatible with GLFW on OS X. Because of this, I had to use the stb binding, which is LWJGL 3.0's STBImage, org.lwjgl.stb.STBImage.

Here's the code that I use

public int loadTexture(String filename){
    ByteBuffer imageBuffer;
    try{
        imageBuffer = readFile(filename);
    }
    catch (IOException e) {
        throw new RuntimeException(e);
    }

    IntBuffer w = BufferUtils.createIntBuffer(1);
    IntBuffer h = BufferUtils.createIntBuffer(1);
    IntBuffer comp = BufferUtils.createIntBuffer(1);

    ByteBuffer image = STBImage.stbi_load_from_memory(imageBuffer, w, h, comp, 0);
    if(image == null){
        throw new RuntimeException("Failed to load image: " + STBImage.stbi_failure_reason());
    }

    this.width = w.get(0);
    this.height = h.get(0);
    this.comp = comp.get(0);

    if(this.comp == 3){
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, this.width, this.height, 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, image);
    }
    else{
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, this.width, this.height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, image);

        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
    }

    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
    GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);

    GL11.glEnable(GL11.GL_TEXTURE_2D);

    return GL11.glGenTextures();
}

private ByteBuffer readFile(String resource) throws IOException{
    File file = new File(resource);

    FileInputStream fis = new FileInputStream(file);
    FileChannel fc = fis.getChannel();

    ByteBuffer buffer = BufferUtils.createByteBuffer((int) fc.size() + 1);

    while(fc.read(buffer) != -1);

    fis.close();
    fc.close();
    buffer.flip();

    return buffer;
}

And it works as expected

enter image description here

Jojodmo
  • 23,357
  • 13
  • 65
  • 107
0

I think that the code just has a path to the image, but despite the filetype you put after it (.png) it doesn't know the filetype, try:

FileInputStream file = new FileInputStream(filename + ".png", PNG);

if that doesn't work, I could have messed up the "", so if you get an error again, try this instead (but it likely won't work)

FileInputStream file = new FileInputStream(filename + ".png", "PNG");
Bryan
  • 13
  • 4
  • 1
    Thanks for the response, but this doesn't work. `FileInputStream` only takes one argument. – Jojodmo Nov 22 '15 at 17:58
  • Maybe try ".PNG" instead of ".png"? EDIT: oh, I see that it is OS specific... Weird, because I can pass two arguments.... Maybe it's my LWJGL or Slick-Util version :/ – Bryan Nov 23 '15 at 07:26