[SDL] SDL Threads: graphics and events in separate contexts?

Brian brian.ripoff at gmail.com
Sun Jan 6 13:02:28 PST 2008


Well on Windows it appears to work:

#include "SDL.h"
#include "SDL_thread.h"

int inputThread( void *param )
{
    volatile bool *running = static_cast<volatile bool*>(param);
    SDL_Event event;
    while(SDL_WaitEvent(&event))
    {
        if( event.type == SDL_QUIT)
        {
            *running = false;
            return 0;
        }
    }
    return 1;
}

int main(int, char**)
{
    SDL_Init(SDL_INIT_VIDEO);

    SDL_Surface *screen = SDL_SetVideoMode(800,600,32,SDL_OPENGL);

    if( !screen )
    {
        std::cerr << SDL_GetError() << std::endl;
        return 1;
    }

    volatile bool running = true;
    SDL_Thread *thread = SDL_CreateThread(&inputThread,(void*)(&running));
    while(running)
    {
        glClearColor(rand() / static_cast<float>(RAND_MAX),0,0,1);
        glClear(GL_COLOR_BUFFER_BIT);
        glBegin(GL_LINES);


            glVertex3f(6.0, 4.0, 2.0);
            glVertex3f(2.0, -4.0, 3.3);

            glVertex3f(5.0, 8.0, 8.0);
            glVertex3f(-4.7, 5.0, -3.0);

            glVertex3f(0.0, 0.0, 0.0);
            glVertex3f(6.0, -1.0, -7.0);

        glEnd();
        SDL_GL_SwapBuffers();

        SDL_PumpEvents();
    }

    SDL_WaitThread(thread,0);
    return 0;
}

For thread safety you might need to use something like fast events
(http://gameprogrammer.com/fastevents/fastevents1.html). Though from
the docs I got the impression that the SDL event queue is supposed to
be thread safe.

On Jan 6, 2008 7:42 PM, Vladimir Pouzanov <farcaller at gmail.com> wrote:
> On Sunday 06 January 2008 21:38:13 Brian wrote:
> > Graphics, in general, must be done from the thread main() is in. So
> > simply swap the tasks around: opengl in main() and event processing in
> > spawned one.
>
> I've already tried that with same result...
>
>
> --
> Sincerely,
> Vladimir "Farcaller" Pouzanov                     Hack&Dev Team
> PGP/GPG: 0x3A40FF29                       <http://hackndev.com>
> Fingerprint: FA36 877A 2DC3 B56F CAB5  7DB3 4C97 A596 3A40 FF29
>
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
>


More information about the SDL mailing list