Hello. I have recently been trying to create a simple program to display an animated image as practice with getting used to using textures in LWJGL (I only have experience using the animation functions in Allegro). I cannot successfully crop my image correctly. I understand that I load the texture once and crop from that, but my code does not crop the actual image. Instead, it copies and tiles the image. Also, I wish to crop from a specific location on the image, but I cannot figure out how to do that either, as my method does not seem to work.
Here is my code for drawing the sprite:
public void draw(){
texture.bind();
glBegin(GL_QUADS);
glTexCoord2f(frameX/imageWidth,frameY/imageHeight);
glVertex2i(x, y);
glTexCoord2f((float)(frameX + spriteWidth)/imageWidth,frameY/imageHeight);
glVertex2i(x + width, y);
glTexCoord2f((float)(frameX + spriteWidth)/imageWidth,(float)(frameY + spriteHeight)/imageHeight);
glVertex2i(x + width, y + height);
glTexCoord2f(frameX/imageWidth,(float)(frameY + spriteHeight)/imageHeight);
glVertex2i(x, y + height);
glEnd();
}
I have read that you can use pixel coordinates by dividing the location by the Image’s width or height where applicable. As I said, it can display the full image, but if I attempt to change the initial coordinate, it either squishes or stretches the image.
Here is where I create the image:
icon = new Sprite(25<<1, 25<<1, 40, 40, 40, 40, "res/smile2.png")
and the constructor:
public Sprite(int x, int y, int h, int w, int spriteX, int spriteY, String path) {
super(x, y, h, w);
this.texture = null;
sprites.add(this);
this.path = path;
this.frame = 0;
this.key = 0;
this.spriteWidth = spriteX;
this.spriteHeight = spriteY;
loadSprites();
}
finally, the animation call and the animate() method:
public void animate(int x_pos, int y_pos, int frame_start, int frame_end, int timeStep, boolean loop){
if(loop){
if(frame == frame_end)
frame = frame_start;
}
if(frame > frame_end){
frame = 0;
}
frameX = x_pos * frame;
frameY = y_pos;
if(key == timeStep){
frame++;
key = 0;
}
}
icon.animate(20, 20, 0, 2, 3, false);
There may be simpler way to do these things, but I really want to get the fundamentals of keying and animating things, specifically using LWJGL. Please help.