What is the best approach for creating a movable 2D camera and applying it to my game in openGL?
I have seen people use
glTranslatef(-focusCam.x + (SCREENX / 2), -focusCam.y + (SCREENY / 2));
before sending there data off to the graphics card for processing.
But is that way to do it for best performance?
Assuming my game loop is like so:
while(runGame)
{
//Add onto frame count and get the current time
frames++;
gameClockNow = timeGetTime();
//The game update loop
//Reset the loop count and execute the loop
loops = 0;
while( (timeGetTime() >= gameClock) && (loops < MAXUPDATES) )
{
//Poll for event inputs
pollForInput();
//Update game objects
//updateGame();
//Update the game clock and loop count
gameClock += updateTime;
loops++;
}
//Calculate the interpolation
alpha = ((timeGetTime() - gameClock) + updateTime) / updateTime;
//Calculate the FPS
calculateFPS(gameClockNow, startTime);
//Calculate interpolation for the debug player sprite
interpolateAllObjects(alpha);
//Draw the game
drawGame();
And my drawGame Function looks like this and uses vertex arrays:
void drawGame()
{
batcher.begin();
//Draw the sprites based on COUNTSPRITE using the atlas draw method
//Where it takes in the x and y position of the sprite, the sprites width and height, its x and y origin on the atals in pixels, and the width and height of the atlas (128.0 x 128.0 in or case)
for(int i = 0; i < COUNTSPRITE; i++)
batcher.drawAtlasSprite((float)spriteList[i].x, (float)spriteList[i].y, 64, 64, spriteList[i].OriginX, spriteList[i].OriginY, 128.0f, 128.0f);
batcher.end();
batcher.swapBuffer();
}
Also Im not sure if you need this but here is my init funciton for openGL :
void initOpenGl()
{
//Clear openGl items
glDisable(GL_DEPTH_TEST);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//Set the view port
glViewport(0, 0, SCREENX, SCREENY);
//Set / load the projection matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//Set the ortho
glOrtho(0.0f, SCREENX, SCREENY, 0.0f, -1.0f, 1.0f);
//Set / load the model view
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Enable use of textures
glEnable(GL_TEXTURE_2D);
//Enable blending of alpha layer
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}