I've tried to make a simple class for loading spritesheets and drawing 2D textures from it. However, when I try to render one of the sprites, it doesn't do that properly. I either get nothing or just some pink dots (https://i.stack.imgur.com/9T8vO.png). Can someone please help me find out what is wrong?
public enum Art
{
GREEN("misc_1", 7, 5, 27, 10),
BLUE("misc_1", 6, 37, 28, 5),
MAGENTA("misc_1", 19, 68, 28, 6);
private String spritesheet;
private int coordX;
private int coordY;
private int width;
private int height;
Art(String s, int x, int y, int w, int h)
{
this.spritesheet = s;
this.coordX = x;
this.coordY = y;
this.width = w;
this.height = h;
}
public String getSpritesheet()
{
return this.spritesheet;
}
public void render(int x, int y, int w, int h)
{
if (spritesheets.containsKey(this.getSpritesheet()))
{
Texture tex = spritesheets.get(this.getSpritesheet());
if (glGetInteger(GL_TEXTURE_BINDING_2D) != tex.getTextureID())
{
tex.bind();
}
float i = coordX / tex.getWidth();
float j = coordY / tex.getHeight();
float k = (coordX + width) / tex.getWidth();
float l = (coordY + height) / tex.getHeight();
int xx = x + w;
int yy = y + h;
glBegin(GL_QUADS);
glTexCoord2f(i, j);
glVertex2i(x, y);
glTexCoord2f(k, j);
glVertex2i(xx, y);
glTexCoord2f(k, l);
glVertex2i(xx, yy);
glTexCoord2f(i, l);
glVertex2i(x, yy);
glEnd();
}
}
private static boolean isLoaded = false;
public static HashMap<String, Texture> spritesheets = new HashMap<String, Texture>();
public static void loadTextures()
{
if (!isLoaded)
{
try
{
Texture misc_1 = TextureLoader.getTexture("PNG", new FileInputStream("res/misc_1.png"));
spritesheets.put("misc_1", misc_1);
} catch (IOException e)
{
e.printStackTrace();
}
isLoaded = true;
}
}
}
If it helps, this is what my initGL method looks like:
private void initGL()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1280, 720, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
EDIT: I just had to change getWidth() and getHeight() to getImageWidth() and getImageHeight(), that resolved the issue.