Hi!
I've found and fixed two bugs.
The first one was in TileManager class. I was trying to used it as a background (non fixed), but when the camera was moved on x < 0 || y < 0, the tilemanager made a rapid move. The reason was the position calculation. It was just rounding the offset to cell size multiply.
original code in tilemanager render method:
int startx = Math.max(0, (int) StaticRef.getCamera().x / tileWidth);
int starty = Math.max(0, (int) StaticRef.getCamera().y / tileHeight);
float coordstartx = Math.max(-StaticRef.getCamera().x % tileWidth, x
- StaticRef.getCamera().x);
float coordstarty = Math.max(-StaticRef.getCamera().y % tileHeight, y
- StaticRef.getCamera().y);
fixed code
int startx = (int) Math.max(0, (int) (-x + StaticRef.getCamera().x) / tileWidth);
int starty = Math.max(0, (int) (-y + StaticRef.getCamera().y) / tileHeight);
float coordstartx = Math.max((x-StaticRef.getCamera().x) % tileWidth, x
- StaticRef.getCamera().x);
float coordstarty = Math.max((y-StaticRef.getCamera().y) % tileHeight, y
- StaticRef.getCamera().y);
The second bug was in sprite render method when using an alpha mask. Everything was ok when the sprite texture was black. When the texture was colorful every pixel with color other than black wasn't masked, just kind a semi transparent. When using my fix the mask texture has to have an alpha channel which is applied to the sprite texture.
original code in sprite render method:
if (alphaMask != null && colorAlpha == 1) {
GL11.glColor4f(1, 1, 1, 1);
GL11.glBlendFunc(GL11.GL_DST_COLOR, GL11.GL_ZERO);
alphaMask.bind();
drawPlain(translatex - cx, translatey - cy, alphaMask);
GL11.glColor4f(colorRed, colorGreen, colorBlue, colorAlpha);
GL11.glBlendFunc(GL11.GL_ONE, GL11.GL_ONE);
image.bind();
drawPlain(translatex - cx, translatey - cy, image);
}
fixed code
if (alphaMask != null && colorAlpha == 1) {
GL11.glColor4f(1, 1, 1, 1);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_ZERO, GL11.GL_ONE_MINUS_SRC_ALPHA); // aplying alpha from mask
alphaMask.bind();
drawPlain(translatex - cx, translatey - cy, alphaMask);
GL11.glColor4f(colorRed, colorGreen, colorBlue, colorAlpha);
GL11.glBlendFunc(GL11.GL_ONE_MINUS_DST_ALPHA, GL11.GL_DST_ALPHA); // aplying colors from texture
image.bind();
drawPlain(translatex - cx, translatey - cy, image);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); // restoring default function
}
Edit: The Sprite's render fix wasn't a good idea. It fixes one, breaks other. This is an improved fix using a texture combiner. GL11 and GL13 are imported static.
Modified part of the render method:if (alphaMask != null && colorAlpha == 1) {
// alpha texture
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glColor4f(1, 1, 1, 1f);
glBindTexture(GL_TEXTURE_2D, alphaMask.getID());
// sprite texture
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glColor4f(colorRed, colorGreen, colorBlue, colorAlpha);
glBindTexture(GL_TEXTURE_2D, image.getID());
// textures combining
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB,
GL_MODULATE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA, GL_TEXTURE0);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_TEXTURE1);
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA,
GL_SRC_ALPHA);
// drawing
drawPlain(translatex - cx, translatey - cy, image, true);
glDisable(GL_TEXTURE_2D);
glActiveTexture(GL_TEXTURE0);
glDisable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_2D);
}
Modified part of the drawPlain methodprotected void drawPlain(float x, float y, ITexture image, boolean multiT) {
ITexture mask;
if (multiT && (mask = image.getAlphaMask()) != null) {
glBegin(GL_QUADS);
glMultiTexCoord2f(GL_TEXTURE0, mask.getXStart(), mask.getYStart());
glMultiTexCoord2f(GL_TEXTURE1, image.getXStart(), image.getYStart());
glVertex2f(x, y);
glMultiTexCoord2f(GL_TEXTURE0, mask.getXEnd(), mask.getYStart());
glMultiTexCoord2f(GL_TEXTURE1, image.getXEnd(), image.getYStart());
glVertex2f(x + width, y);
glMultiTexCoord2f(GL_TEXTURE0, mask.getXEnd(), mask.getYEnd());
glMultiTexCoord2f(GL_TEXTURE1, image.getXEnd(), image.getYEnd());
glVertex2f(x + width, y + height);
glMultiTexCoord2f(GL_TEXTURE0, mask.getXStart(), mask.getYEnd());
glMultiTexCoord2f(GL_TEXTURE1, image.getXStart(), image.getYEnd());
glVertex2f(x, y + height);
glEnd();
} else (...)