The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
high CPU usage
Gekoncze


Joined: 06 Aug 2011
Posts: 6
Location: Czech republic
Hi,
I have a problem with high CPU usage of my application using SDL+OpenGL. When I run it on Linux mint 9 ( gnome 2 ) it uses only a little CPU, but when I run it on fedora 16 ( gnome 3 ), it uses 100% CPU. I found out that when my app is runing in gnome 3, then gnome-shell uses about 90% of CPU. I tried creating a blank project and there (on fedora) gnome-shell uses about 33% CPU. I don't have ATI drivers installed on fedora, because they are not yet fully compatible with gnome 3, so I am using mesa3d (I don't think it may affect it when drawing blank scene).
So my question is what can cause this?
I am not sure if I draw it right way so I will include my code:
Code:

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_opengl.h>
#include <GL/gl.h>

void ggDraw( void )
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 640, 480, 0, -1, 1);

    glFlush();
    SDL_GL_SwapBuffers();
}

int main( int argc, char** argv )
{
    if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER ) < 0 )
    {
        printf("could not init SDL\n");
        return -1;
    }
    atexit(SDL_Quit);

    SDL_Surface *screen = NULL;
    screen = SDL_SetVideoMode( 640, 480, 32, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL );
    if( screen == NULL )
    {
        printf("could not set video mode\n");
        SDL_Quit();
    }

    Uint8 *keystate = NULL;
    keystate = SDL_GetKeyState( NULL );
    if( keystate == NULL )
    {
        printf("could not get keystate\n");
        SDL_Quit();
    }

   glCullFace( GL_BACK );
   glEnable( GL_CULL_FACE );
   glEnable( GL_TEXTURE_2D );
   glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
   glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
   glShadeModel( GL_SMOOTH );
   glViewport( 0, 0, 640, 480 );

    unsigned char alive = 1;
    long int nowt = 0;
    long int oldt = 0;
    long int time = 0;

    const int FPS = 60;

    while( alive )
    {
        nowt = SDL_GetTicks();

        time = nowt - oldt;
        if( time >= 1000/FPS )
        {
            oldt = nowt;
            SDL_PumpEvents();
            if( keystate[SDLK_ESCAPE] ) alive = 0;
            ggDraw();
        }
        else SDL_Delay(1);
    }

    printf("\nGame ended peacefully.\n");
    SDL_Quit();
    return 0;
}

__________
SDL version: 1.2.14
Re: high CPU usage
MBrening


Joined: 09 Nov 2009
Posts: 79
Location: Shawnee, Kansas
Gekoncze wrote:
Hi,
I have a problem with high CPU usage of my application using SDL+OpenGL. When I run it on Linux mint 9 ( gnome 2 ) it uses only a little CPU, but when I run it on fedora 16 ( gnome 3 ), it uses 100% CPU. I found out that when my app is runing in gnome 3, then gnome-shell uses about 90% of CPU. I tried creating a blank project and there (on fedora) gnome-shell uses about 33% CPU. I don't have ATI drivers installed on fedora, because they are not yet fully compatible with gnome 3, so I am using mesa3d (I don't think it may affect it when drawing blank scene).
So my question is what can cause this?


I think the drivers are going to be your issue. Mesa3d is the software rendering version of OpenGL. So when you are using Mesa, the processor has to do all of the graphic processing. When you can use true opengl, then it gets moved to the graphics card. It still seems high to me, but that would be my first guess.
Re: high CPU usage
Gekoncze


Joined: 06 Aug 2011
Posts: 6
Location: Czech republic
MBrening wrote:
Gekoncze wrote:
Hi,
I have a problem with high CPU usage of my application using SDL+OpenGL. When I run it on Linux mint 9 ( gnome 2 ) it uses only a little CPU, but when I run it on fedora 16 ( gnome 3 ), it uses 100% CPU. I found out that when my app is runing in gnome 3, then gnome-shell uses about 90% of CPU. I tried creating a blank project and there (on fedora) gnome-shell uses about 33% CPU. I don't have ATI drivers installed on fedora, because they are not yet fully compatible with gnome 3, so I am using mesa3d (I don't think it may affect it when drawing blank scene).
So my question is what can cause this?


I think the drivers are going to be your issue. Mesa3d is the software rendering version of OpenGL. So when you are using Mesa, the processor has to do all of the graphic processing. When you can use true opengl, then it gets moved to the graphics card. It still seems high to me, but that would be my first guess.

yes, but when I am drawing blank scene, it uses 33% of CPU... why?
Gekoncze


Joined: 06 Aug 2011
Posts: 6
Location: Czech republic
I also found out that when I comment out SDL_GL_SwapBuffers(); then the cpu usage of gnome-shell is about 4%. (but of course nothing is then displayed)
do what you need to when you need to
necron


Joined: 10 Dec 2011
Posts: 33
Here is a look at what i do to ensure that things work at a given rate and that I know how many frames per second I'm getting, cycles per second, and when to update what game data or animation.:
//time tracking td1=time|cycletime|ftime=time since last frame/snapshot
td1=SDL_GetTicks(); timeNow=td1;
cycletime=((float)(td1-td2));
td2=td1;
ftime=ftime+cycletime;

//stime=how long between snapshots
sum_frames++;
//find out if it's snapshot time
if(ftime>snaptime) {take_snapshot = 1; ftime = 1.666;}

//find out the frames per second
if(td1-td3>1000)
{
td3 = td1; display_fps = sum_frames; sum_frames = 0;
}
Easy fix
takide


Joined: 07 Feb 2012
Posts: 27
Give your system some CPU! put a small delay in there to regulate framerate