Hey everyone!
Today I decided to start a project and make 2D game. I keep hearing good things about LWJGL and OpenGl, so I was like why not!
So after following a few online tutorials and etc I learned how to use VBOs to render triangles to the game screen.
But since I want to stick to straight 2D I changed it to use Quads with a texture.
Now I am just wondering if this the proper way to do it.
//Temp Class for sprites
public class Sprite
{
public float x;
public float y;
public Texture texture;
}
//Function used to "run" the game
public void run()
{
//Create an array of sprites and init them
Sprite [] sprite = new Sprite[2];
sprite[0] = new Sprite();
sprite[0].x = 20;
sprite[0].y = 20;
sprite[0].texture = square; //previously loaded texture using the slick texture and texture loader
sprite[1] = new Sprite();
sprite[1].x = 125;
sprite[1].y = 125;
sprite[1].texture = smile; //previously loaded texture using the slick texture and texture loader
//Create the amount of vertexs and sie
final int amountOfVertex = 4;
final int vertexSize = 2; / 2 for x and y cord only
//Create the vertex buffer
FloatBuffer vertexData = BufferUtils.createFloatBuffer(amountOfVertex * vertexSize);
//create and init the texture coord buffer for mapping a texture to a quad
FloatBuffer texCoordData = BufferUtils.createFloatBuffer(amountOfVertex * vertexSize);
texCoordData.put(new float[]{
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f});
texCoordData.flip();
//Set up the vertex buffer
int vboVertexHandle = glGenBuffers();
glBindTexture(GL_TEXTURE_2D, square.getTextureID());
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//Set up the texture coord buffer
int texCoordHandle = glGenBuffers();
glBindTexture(GL_TEXTURE_2D, texCoordHandle);
glBindBuffer(GL_ARRAY_BUFFER, texCoordHandle);
glBufferData(GL_ARRAY_BUFFER, texCoordData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
//Init OpenGL
while(!Display.isCloseRequested())
{
//used for user input
gamepad.pollInput();
//Clear the buffer
glClear(GL_COLOR_BUFFER_BIT);
//Run though the sprite list and draw them to the screen
//Is this the part im doing right? Thanks :D
for(int i = 0; i < 2; i++)
{
//Create the verteex buffer data based on the sprite to draw
vertexData.put(new float[]{
sprite[i].x, sprite[i].y,
sprite[i].x + sprite[i].texture.getImageWidth(), sprite[i].y,
sprite[i].x + sprite[i].texture.getImageWidth(), sprite[i].y + sprite[i].texture.getImageHeight(),
sprite[i].x, sprite[i].y + sprite[i].texture.getImageHeight()});
vertexData.flip();
//bind the texture to use and handle the rest of the drawing
glBindTexture(GL_TEXTURE_2D, sprite[i].texture.getTextureID());
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, vboVertexHandle);
glVertexPointer(vertexSize, GL_FLOAT, 0, 0L);
glBindBuffer(GL_ARRAY_BUFFER, texCoordHandle);
glTexCoordPointer(vertexSize, GL_FLOAT, 0, 0L);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDrawArrays(GL_QUADS, 0, amountOfVertex);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
//Update and sync it to 60 FPS
Display.update();
Display.sync(60);
}