A problem for Java NIO FileChannel.map(....) don't work..why?

it’s my code :


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL14;

public class GameMain2 {
	private ByteBuffer byteBuffer;
	/**
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		new GameMain2().launch();
	}

	public void launch() {
		try {
			Display.setDisplayMode(new DisplayMode(800, 600));
			Display.setTitle("YourGame");
			Display.create();
			this.initGL();
		}
		catch (LWJGLException e) {
			e.getStackTrace();
		}

		while (!Display.isCloseRequested()) {
			this.render();
			Display.update();
			Display.sync(60);
		}
		Display.destroy();
	}

	private void initGL() {
		GL11.glClearColor(0, 0, 0, 0);
		
		GL11.glOrtho(0, 800, 0, 600, -1, 1); 

		//GL11.glEnable(GL11.GL_TEXTURE_2D);
		
		try {
			File f = new File("src/img/logo.png");
			FileInputStream fileIn = new FileInputStream(f);
			FileChannel fc = fileIn.getChannel();
			this.byteBuffer = fc.map(MapMode.READ_ONLY, 0, fc.size());
			//fileIn.close();
			System.out.println(this.byteBuffer.position());
		}
		catch (IOException e) {
			e.getStackTrace();
		}
	}
	
	private void render() {
		GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
		//GL11.glColor3f(0, 1, 1);
		GL14.glWindowPos2d(20, 20);
		//GL11.glBitmap(142, 142, 0, 0, 0, 0, this.byteBuffer);
		GL11.glDrawPixels(142, 142, GL11.GL_RGB, GL11.GL_BYTE, this.byteBuffer);
	}
}

Q1:the System.out.println(this.byteBuffer.position()); is still return 0,which means that no byte map into byteBuffer…
Q2:when I use FileChannel.read() to read byte to byteBuffer it’s ok,but when I render use glDrawPixels it always return :
Number of remaining buffer elements is 4332, must be at least 60492. Because at most 60492 elements can be returned, a buffer with at least 60492 elements is required, regardless of actual returned element count
I don’t know what It means…

the .position() of the buffer is no indication of whether any data was loaded. When reading bytes from a mapped bytebuffer, the data is automatically loaded from disk - .position() is there for whatever you want to use it for. The data is there.

You are trying to feed PNG encoded data into a method that expects pixels.

On a sidenote… how long are you going to try to make glDrawPixels(…) work? It’s the absolute slowest way to render an image. Try to read up on textures instead.

[quote]the .position() of the buffer is no indication of whether any data was loaded. When reading bytes from a mapped bytebuffer, the data is automatically loaded from disk - .position() is there for whatever you want to use it for. The data is there.
[/quote]
sorry I had make a big mistake :stuck_out_tongue:

[quote]You are trying to feed PNG encoded data into a method that expects pixels.
[/quote]
so I can’t direct use the png file because it had not decode right?