2
\$\begingroup\$

I loaded some textures just one time before I go to my mainloop(glutMainLoop). I am using SDL for the texture loading. However, when I compile, my game is running at 6-10 fps. When I don't load the textures(so the game is running without the textures), I have 30-50 fps. Please help me, I don't know what can cause this low fps.

Texture loading using SDL and OpenGL:

#include "LoadTextures.h"
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
LoadTextures::LoadTextures(){}
GLuint LoadTextures::texture[10];

void LoadTextures::loadStuff(const char * imgPath, int amount){
        SDL_Surface * surface = IMG_Load(imgPath);

        glGenTextures(1, &texture[amount]);
        glBindTexture(GL_TEXTURE_2D, texture[amount]);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, surface->format->BytesPerPixel, surface->w,surface->h, 0, GL_RGBA,GL_UNSIGNED_BYTE,surface->pixels);

        SDL_FreeSurface(surface);
}

It would be a mess to post my whole code, but this is where all my textures are loaded, in the beginning of my code(so just one time, no more).

Sorry for bad English, please help...

EDIT:

This is where I render the sky texture(that causes the lag). Information: LT.texture is an array where all the (just one time) loaded textures are stored in so I can use them. I use double buffering.

void Drawing::drawSky(){
    ///THE SUN
    glPushMatrix();
    glBindTexture(GL_TEXTURE_2D, LT.texture[6]);
    glEnable(GL_TEXTURE_2D);

    glColor4f(1.0f, 1.0f, 1.0f,1.0f);

    glBegin(GL_QUADS);
        glTexCoord2f(0, 0); glVertex2f(-10.0f,-5.0f);
        glTexCoord2f(0, 1); glVertex2f(-10.0f,4.0f);
        glTexCoord2f(1, 1); glVertex2f(100.0f,4.0f);
        glTexCoord2f(1, 0); glVertex2f(100.0f,-5.0f);
    glEnd();

    glDisable(GL_TEXTURE_2D);
}

EDIT: This is where I swapped buffers:

void Main::display(){
    ///BACKGROUND COLOR(AKA THE SKY)
    glClearColor(0.0f,0.0f,1.0f,1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    ///LOAD THE IDENTITY MATRIX TO RESET THE DRAWING LOCATIONS(AKA CLEARING THE SCREEN)
    glLoadIdentity();

    ///SO EVERYTHING HAS THE CORRECT SIZE
    glTranslatef(0.0f, 0.0f, -5.0f);

    //draw.drawSky();
    draw.drawSun();

    ///CAMERA MOVEMENT
    glTranslatef(cameraX, 0.0f, 0.0f);

    ///DRAWING AND RENDERING TEXTURES AND ANIMATIONS
    draw.drawTerrain();
    draw.drawGrass();
    draw.drawClouds();
    draw.drawPlayer();
    draw.walkAnimation();

    ///SO THE PLAYER CAN JUMP
    if(inputClass.flying){
        inputClass.returnFly(0.007f);
    }

    ///FPS COUNTING AND CALCULATING THE DELTA TIME
    float recentTime = (float)clock();
    deltaTime = recentTime - startTime;
    startTime =  recentTime;

    ///FPS SHOWING TO CONSOLE WINDOW
    //std::cout << "Delta: " << deltaTime << std::endl;
    std::cout <<  "FPS: " << (1 / deltaTime) * 1000 << std::endl;

    ///MOVEMENT OF THE CAMERA
    cameraX += cameraSpeed * deltaTime;

    ///SWAP THE BUFFERS FOR SMOOTH RENDERING
    glutSwapBuffers();
  }
\$\endgroup\$
9
  • \$\begingroup\$ Have you tried looking at your Task Manager? From there you can see if your game is using too much memory, CPU power, etc. The information would be really helpful to anyone trying to help you. \$\endgroup\$ – Sturlen Nov 6 '15 at 12:04
  • \$\begingroup\$ How big is that texture? Put a print in the load function to be 100% sure it is called only once. And are you sure this behaviour is wrong? Do other OpenGL programs run faster? Can you show us the function actually doing the rendering? \$\endgroup\$ – Jonas Beck Nov 6 '15 at 12:10
  • \$\begingroup\$ When I run the game at full screen with the sky texture(it is a quite big texture, I am using it as background), it has 5fps and takes 25% of my CPU I have dual core 2.5ghz 4gb RAM. I found out that my sky texture causes the lag, if I unload the sky texture, I have around 17 fps, but still not much. I hope this was enough information for you. Other openGL programs(I created) run way faster, probably because they don't use textures. OpenGL games like Minecraft just run fine. I will update my question so you can see the function for the rendering. \$\endgroup\$ – OpenGLmaster1992 Nov 6 '15 at 12:16
  • \$\begingroup\$ Try moving the glEnable and glDisable calls out of the game loop. Enable once at initialization. Disable at cleanup. Tell me if that helps. \$\endgroup\$ – The Light Spark Nov 6 '15 at 12:41
  • \$\begingroup\$ It causes even a bit more lag.. \$\endgroup\$ – OpenGLmaster1992 Nov 6 '15 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.