Thread: SDL Performance problem

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    SDL Performance problem

    This runs fine in that the red dot is drawn at (10, 10) but it's slooooow. As it is loaded up you can see it appearing in the start bar really slowly and if I move the window it leaves copies all over the place.

    Code:
    #include <SDL/SDL.h>
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    
    bool SDLInitialise();
    void RenderFrame();
    void DrawPixel(int x, int y, unsigned int color);
    
    SDL_Surface *sdlsBackBuffer = NULL;
    unsigned int *BBWrite = NULL;
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        SDL_Event sdlEvent;
        bool bDone = false;
    
        SDLInitialise();
    
        while (! bDone)
        {
            RenderFrame();
    
            while (SDL_PollEvent(&sdlEvent))
            {
                switch (sdlEvent.type)
                {
                case SDL_KEYUP:
                    if (sdlEvent.key.keysym.sym == SDLK_ESCAPE)
                        bDone = true;
    
                case SDL_QUIT:
                    bDone = true;
                }
            }
        }
    
        return 0;
    }
    
    // SDLInitialise
    //
    bool SDLInitialise()
    {
        if (SDL_Init(SDL_INIT_VIDEO) != 0)
            return false;
    
        atexit(SDL_Quit);
    
        sdlsBackBuffer = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
        if (sdlsBackBuffer == NULL)
            return false;
    
        BBWrite = static_cast<unsigned int *> (sdlsBackBuffer->pixels);
        return true;
    }
    
    // RenderFrame
    //
    void RenderFrame()
    {
        if (SDL_MUSTLOCK(sdlsBackBuffer))
            if (SDL_LockSurface(sdlsBackBuffer) < 0)
                return;
    
        DrawPixel(10, 10, 0xff0000);
    
        if (SDL_MUSTLOCK(sdlsBackBuffer))
            SDL_UnlockSurface(sdlsBackBuffer);
        SDL_UpdateRect(sdlsBackBuffer, 0, 0, 800, 600);
    }
    
    // DrawPixel
    //
    void DrawPixel(int x, int y, unsigned int color)
    {
        unsigned ofs = y * (sdlsBackBuffer->pitch / 4);
        *(BBWrite + ofs + x) = color;
    }
    Is there anything drastically wrong here or does it always do this?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    How dose it work in full screen mode? I never tried it in windowed mode but it used to run fine for me in fullscreen mode.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    It seems to run better except it also seems to throw the y * screen->pitch + x thing out of whack; it draws off the top of the screen a bit.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Jesus, I just tried rand()(omly) plotting pixels and CPU usage peaks at 77%. Mad.

    EDIT: Actually maybe that's normal. But it's still quite sticky.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    You could just switch to directx and dump SDL thats what I did and I've never looked back.

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Yeah that's what I want to do too. Meh. Stuff SDL.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    BTW I just thought about it, aren't you suposed to always use main() with SDL? Or maybe it's SDL_Main or something? bah who cares?

  8. #8
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Isn't SDL Software-rendered? That's probably why if it is.
    To code is divine

  9. #9
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Quote Originally Posted by Quantum1024
    BTW I just thought about it, aren't you suposed to always use main() with SDL? Or maybe it's SDL_Main or something? bah who cares?
    Actually I just found the .lib "SDLmain.lib". How do I use this SDL main you speak of?

    EDIT: Nevermind. I've just linked it in and it runs nice now.
    Last edited by cboard_member; 04-09-2006 at 01:46 PM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems compiling this SDL app
    By Rider in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2007, 12:22 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. SDL problem
    By ElastoManiac in forum Game Programming
    Replies: 11
    Last Post: 01-11-2006, 02:45 AM
  4. A Little Performance Related SDL Question...
    By Comrade_Yeti in forum Game Programming
    Replies: 14
    Last Post: 12-09-2005, 07:46 PM
  5. beginner problem
    By The_Nymph in forum C Programming
    Replies: 4
    Last Post: 03-05-2002, 05:46 PM