SDL and thread support
Posted: Thu Nov 29, 2007 9:29 am
Hi,
I've been trying the SDL and thread support but it seems there's something I'm missing ...
A simple example : creating a thread that prints the sequence 1-2-3-4 with a 1 second delay while the main program prints '*' every 5 seconds.
The thread seems to be created right and starts running, but as soon the main program SDL_Delay(5000) ends, the thread stops. Try changing the delay to something bigger and the same happens.
(BTW : For quick compiling, I uploaded it along with the makefile to http://www.esnips.com/web/ps2-homebrew/. Look for SDL thread.zip and unzip to your sdl\test\ folder)
I've been trying the SDL and thread support but it seems there's something I'm missing ...
A simple example : creating a thread that prints the sequence 1-2-3-4 with a 1 second delay while the main program prints '*' every 5 seconds.
Code: Select all
// adapted from : http://lazyfoo.net/SDL_tutorials/lesson33/index.php
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include <SDL_thread.h>
//The event structure
SDL_Event event;
//The thread that will be used
SDL_Thread *thread = NULL;
//Quit flag
int quit = 0;
int init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVENTTHREAD | SDL_INIT_TIMER ) == -1 )
{
return 0;
}
return 1;
}
/*
void clean_up()
{
//Stop the thread
SDL_KillThread( thread );
//Quit SDL
SDL_Quit();
}
*/
int my_thread( void *data )
{
//While the program is not over
while( quit == 0 )
{
scr_printf("1");
SDL_Delay( 1000 );
scr_printf("2");
SDL_Delay( 1000 );
scr_printf("3");
SDL_Delay( 1000 );
scr_printf("4");
SDL_Delay( 1000 );
}
scr_printf("over");
return 0;
}
int main(int argc, char *argv[])
{
//Initialize
SifInitRpc(0);
init_scr();
if( init() == 0 )
{
return 1;
}
//Create and run the thread
thread = SDL_CreateThread( my_thread, NULL );
//Forever
while(1)
{
/*
//While there's events to handle
while( SDL_PollEvent( &event ) )
{
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = 1;
}
}
*/
SDL_Delay(5000);
scr_printf("*");
}
// Never got here ...
/*
//Clean up
clean_up();
*/
return 0;
}
(BTW : For quick compiling, I uploaded it along with the makefile to http://www.esnips.com/web/ps2-homebrew/. Look for SDL thread.zip and unzip to your sdl\test\ folder)