Hello everybody.
While displaying a 800x600 image scaled to fit a 1024x768 screen, I noticed that the interpolation algorithm being used was nearest neighbour. This is the code where I construct the image:
Code:
@Override
public void init(GameContainer container) throws SlickException {
final InputStream in = getClass().getResourceAsStream("/slick/img/bg.png");
background = new Image(in, "bg", false);
}
public void render(GameContainer container, Graphics g) throws SlickException {
background.draw(0, 0, container.getWidth(), container.getHeight());
}
public static void main(String[] args) throws SlickException {
AppGameContainer app =
new AppGameContainer(new MyGame());
app.setDisplayMode(1024, 768, false);
app.start();
}
After that, I used the 4 arguments Image constructor:
Code:
@Override
public void init(GameContainer container) throws SlickException {
final InputStream in = getClass().getResourceAsStream("/slick/img/bg.png");
background = new Image(in, "bg", false, Image.FILTER_LINEAR);
}
But it had no effect.
Taking a look at the source code I found a possible bug in the method
load of Image class (see the comments on the code):
Code:
private void load(InputStream in, String ref, boolean flipped, int f, Color transparent) throws SlickException {
// this.filter receives SGL.GL_LINEAR value
this.filter = f == FILTER_LINEAR ? SGL.GL_LINEAR : SGL.GL_NEAREST;
try {
this.ref = ref;
int[] trans = null;
if (transparent != null) {
trans = new int[3];
trans[0] = (int) (transparent.r * 255);
trans[1] = (int) (transparent.g * 255);
trans[2] = (int) (transparent.b * 255);
}
// filter has SGL.GL_LINEAR value here (which is different from FILTER_LINEAR), so filter receives SGL.GL_NEAREST
texture = InternalTextureLoader.get().getTexture(in, ref, flipped, filter == FILTER_LINEAR ? SGL.GL_LINEAR : SGL.GL_NEAREST, trans);
} catch (IOException e) {
Log.error(e);
throw new SlickException("Failed to load image from: "+ref, e);
}
}
As a workaround, I am creating a Texture and constructing the image with it:
Code:
final InputStream in = getClass().getResourceAsStream("/slick/img/bg.png");
Texture texture = InternalTextureLoader.get().getTexture(in, "bg", false, GL11.GL_LINEAR);
background = new Image(texture);
The 0.3 tag on SVN seems to not have this issue, but the current build (b269) apparently is affected.