0

今天我终于弄清楚我需要做些什么来为我的 GIF 制作动画,但现在我有了它,我的程序上的 FPS 大大降低了。我将我的 GIF 放在一个单独的函数中,因此每当我尝试更新 GIF 的帧时,它不必重新加载每个纹理。如果有办法解决我的 FPS 丢失问题,我还计划加载更多 GIF,所以我需要一种方法来减少所有这些 GIF 的延迟。有什么简单的方法可以解决这个问题吗?

我的代码:

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();
}

主要.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 );

}
4

0 回答 0