PDA

View Full Version : GIF dramatically decreasing FPS



PocketTNT
05-25-2016, 10:46 PM
Today I finally figured out what I needed to do to animate my GIF but now that I have it animated the FPS on my program lowered dramatically. I have my GIF in a serperate function so it doesn't have to reload every texture whenever I try to update the frames of the GIF. I also plan on loading more GIFs if there is a way to fix my FPS drop issue so I need a way to reduce lag on all of them. Is there any easy way to fix this?

My code:

LUtil.cpp:


#include "LUtil.h"
#include <IL/il.h>
#include <IL/ilu.h>
#include "LSpriteSheet.h"

int add = 0;
int zoom = 0;
double leftzoom = -SCREEN_WIDTH;
double topzoom = -SCREEN_HEIGHT;
double rightzoom = SCREEN_WIDTH;
double bottomzoom = SCREEN_HEIGHT;
float sy = 0.f;
int scroll = 0;
GLfloat gCameraX = 0.f, gCameraY = 0.f, gCameraZ = 0.f;
//Sprite texture

LSpriteSheet gArrowSprites;
LSpriteSheet gSamusSprites;
LSpriteSheet jSamusSprites;

bool initGL()
{
//Initialize GLEW
GLenum glewError = glewInit();
if( glewError != GLEW_OK )
{
printf( "Error initializing GLEW! %s\n", glewGetErrorString( glewError ) );
return false;
}

//Make sure OpenGL 2.1 is supported
if( !GLEW_VERSION_2_1 )
{
printf( "OpenGL 2.1 not supported!\n" );
return false;
}

//Set the viewport
glViewport( 0.f, -0.f, SCREEN_WIDTH, SCREEN_HEIGHT );

//Initialize Projection Matrix
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( leftzoom, rightzoom, bottomzoom, topzoom, 1.0, -1.0 );

//Initialize Modelview Matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();

glPushMatrix();

//Initialize clear color
glClearColor( 0.f, 0.f, 0.f, 1.f );

//Enable texturing
glEnable( GL_TEXTURE_2D );

glEnable( GL_BLEND );
glDisable( GL_DEPTH_TEST );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

//Check for error
GLenum error = glGetError();
if( error != GL_NO_ERROR )
{
printf( "Error initializing OpenGL! %s\n", gluErrorString(error));
return false;
}

//Initialize DevIL
ilInit();
ilClearColour( 255, 255, 255, 000 );

//Check for error
ILenum ilError = ilGetError();
if( ilError != IL_NO_ERROR )
{
printf( "Error initializing DevIL! %s\n", iluErrorString(ilError) );
return false;
}

return true;
}

LFRect clip;

bool loadMedia()
{
//Load texture
if( !gArrowSprites.loadTextureFromFile( "Textures/SamusM.png" ) )
{
printf( "Unable to load texture!\n" );
return false;
}
clip = { 0.f, 0.f, 330.f, 355.f };

//Top left
clip.x = 0.f;
clip.y = 0.f;
gArrowSprites.addClipSprite( clip );

//Top right
clip = {0.f, 0.f, 310.f, 480.f};

clip.x = 331.f;
clip.y = 0.f;
gArrowSprites.addClipSprite( clip );

clip = {0.f, 0.f, 330.f, 125.f};
//Bottom left
clip.x = 0.f;
clip.y = 355.f;

gArrowSprites.addClipSprite( clip );

clip = { 0.f, 0.f, 330.f, 355.f };

//Top left
clip.x = 0.f;
clip.y = 480.f;
gArrowSprites.addClipSprite( clip );

//Top right
clip = {0.f, 0.f, 310.f, 480.f};

clip.x = 331.f;
clip.y = 480.f;
gArrowSprites.addClipSprite( clip );

clip = {0.f, 0.f, 330.f, 125.f};
//Bottom left
clip.x = 0.f;
clip.y = 835.f;

gArrowSprites.addClipSprite( clip );

//Generate VBO
if( !gArrowSprites.generateDataBuffer() )
{
printf( "Unable to clip sprite sheet!\n" );
return false;
}

if( !jSamusSprites.loadTextureFromFile( "Textures/SamusAran.png" ) )
{
printf( "Unable to load texture!\n" );
return false;
}
clip = { 0.f, 0.f, 375.f, 562.f };

//Top left
clip.x = 0.f;
clip.y = 0.f;
jSamusSprites.addClipSprite( clip );

if( !jSamusSprites.generateDataBuffer() )
{
printf( "Unable to clip sprite sheet!\n" );
return false;
}
return true;
}

bool textureLoaded = false;
bool loadGif(){
if(textureLoaded == false){
if( !gSamusSprites.loadTextureFromFile( "Textures/SamusG.png" ) ){
printf( "Unable to load texture!\n" );
return false;
}
textureLoaded = true;
}
sy += 214.f;

if(sy == 8988.f){
sy = 0.f;
}

clip = {0.f, 0.f, 213.f, 214.f};

clip.x = 0.f;
clip.y = sy;

gSamusSprites.addClipSprite( clip );


if( !gSamusSprites.generateDataBuffer() )
{
printf( "Unable to clip sprite sheet!\n" );
return false;
}
return true;
}

void update()
{

}

void render()
{
//Clear color buffer

glClear( GL_COLOR_BUFFER_BIT );

glMatrixMode(GL_MODELVIEW);
glPopMatrix();

glPushMatrix();

if(add == 0){
if(zoom < 100){
double const f_zoom = 1.0 - 0.01 * zoom;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(leftzoom * f_zoom, rightzoom * f_zoom, bottomzoom * f_zoom, topzoom * f_zoom, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
if(zoom < -102){
double const f_zoom = 1.0 + 0.01 * zoom;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(leftzoom * f_zoom, rightzoom * f_zoom, bottomzoom * f_zoom, topzoom * f_zoom, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

glTranslatef( -1080.f, -782.f, 0.f );
gArrowSprites.renderSprite( 0 );

//Render top right arrow
glTranslatef( +1080.f, +782.f, 0.f );
glTranslatef( 1085.f, -120.f, 0.f );
gArrowSprites.renderSprite( 1 );

//Render bottom left arrow
glTranslatef( -1085.f, +120.f, 0.f );
glTranslatef( 0.f, 885.f ,0.f );
gArrowSprites.renderSprite( 2 );

glTranslatef(0.f, -885.f, 0.f);
jSamusSprites.renderSprite( 0 );

glTranslatef( -620.f, -855.f, 0.f );
glTranslatef( 620.f, 0.f , 0.f );
gSamusSprites.renderSprite( 0 );

}
if(add == 1){
if(zoom < 100){
double const f_zoom = 1.0 - 0.01 * zoom;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(leftzoom * f_zoom, rightzoom * f_zoom, bottomzoom * f_zoom, topzoom * f_zoom, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}
if(zoom < -102){
double const f_zoom = 1.0 + 0.01 * zoom;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(leftzoom * f_zoom, rightzoom * f_zoom, bottomzoom * f_zoom, topzoom * f_zoom, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}

glTranslatef( -1080.f, -782.f, 0.f );
gArrowSprites.renderSprite( 3 );

glTranslatef( +1080.f, +782.f, 0.f );
glTranslatef( 1085.f, -120.f, 0.f );
gArrowSprites.renderSprite( 4 );

glTranslatef( -1085.f, +120.f, 0.f );
glTranslatef( 0.f, 885.f ,0.f );
gArrowSprites.renderSprite( 5 );

glTranslatef(0.f, -885.f, 0.f);
jSamusSprites.renderSprite( 0 );

glTranslatef( -620.f, -855.f, 0.f );
glTranslatef( 620.f, 0.f , 0.f );
gSamusSprites.renderSprite( 0 );
}

glLoadIdentity();


glutSwapBuffers();

}
void handleKeys( unsigned char key, int x, int y )
{
//If the user presses q
if( key == 'q' && add == 0 )
{
add++;
}
else if( key == 'q' && add == 1)
{
add--;
}
//Update the sprite rectangles so the texture change takes effect
if( key == 27 ) {
exit(0);
}
if(key == 'a') {
gCameraX += 8.f;
}
else if (key == 'd') {
gCameraX -= 8.f;
}
else if (key == 'w') {
gCameraY += 8.f;
}
else if (key == 's') {
gCameraY -= 8.f;
}
else if (key == '+' && zoom != 99) {
zoom += 1;
}
else if (key == '-' && zoom != -101) {
zoom -= 1;
}
//Take saved matrix off the stack and reset it
glMatrixMode( GL_MODELVIEW );
glPopMatrix();
glLoadIdentity();

//Move camera to position
glTranslatef( gCameraX, gCameraY, gCameraZ );

//Save default matrix again with camera translation
glPushMatrix();
}

Main.cpp:


#include "LUtil.h"

void runMainLoop( int val );

void runGifLoop( int val2 );

int main( int argc, char* args[] )
{

glutInit( &argc, args );

glutInitContextVersion( 2, 1 );

glutInitDisplayMode( GLUT_DOUBLE );
glutInitWindowSize( SCREEN_WIDTH, SCREEN_HEIGHT );
glutCreateWindow( "Samus" );

if( !initGL() )
{
printf( "Unable to initialize graphics library!\n" );
return 1;
}

if( !loadMedia() )
{
printf( "Unable to load media!\n" );
return 2;
}

glutKeyboardFunc( handleKeys );
glutDisplayFunc( render );

glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, 0 );
glutTimerFunc( 1000 / SCREEN_FPS, runGifLoop, 0 );
glutMainLoop();

return 0;
}

void runMainLoop( int val )
{
update();
render();

glutTimerFunc( 1000 / SCREEN_FPS, runMainLoop, val );
}

void runGifLoop( int val2 )
{

loadGif();

glutTimerFunc( 1000 / SCREEN_FPS, runGifLoop, val2 );

}

PocketTNT
05-26-2016, 07:03 AM
The texture is no longer being loaded every frame, but I'm still getting lag from the GIF

Dark Photon
05-26-2016, 09:16 PM
Start with a draw loop that runs fast. Incrementally add until your performance is reduced.

Alternatively, selectively disable things until your performance is good again.

Or if you think your bottleneck may be on the CPU side, just run this through a decent profiling tool.