Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1427335
  • 博文数量: 430
  • 博客积分: 9995
  • 博客等级: 中将
  • 技术积分: 4388
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-24 18:04
文章存档

2013年(1)

2008年(2)

2007年(14)

2006年(413)

分类: LINUX

2006-05-26 11:46:53

Using SDL Threads

Last Updated 2/26/06
SDL has the ability to do cross platform multithreading. This tutorial will give you a simple demonstration on how to use SDL threads.
#include "SDL/SDL.h" #include "SDL/SDL_image.h" #include "SDL/SDL_thread.h" #include
First off we have to remember to include the header for SDL threads.
//The thread that will be used SDL_Thread *thread = NULL; //Quit flag bool quit = false;
Here are two important global variables.

First we have the SDL_Thread which is going to be the thread we are going to run. Then we have our "quit" variable, which we usually have as a local variable in main() but this time we need it to be global.
int my_thread( void *data ) { //While the program is not over while( quit == false ) { //Do the caption animation SDL_WM_SetCaption( "Thread is running", NULL ); SDL_Delay( 250 ); SDL_WM_SetCaption( "Thread is running.", NULL ); SDL_Delay( 250 ); SDL_WM_SetCaption( "Thread is running..", NULL ); SDL_Delay( 250 ); SDL_WM_SetCaption( "Thread is running...", NULL ); SDL_Delay( 250 ); } return 0; }
Here's the function that's going to be our thread. All it does is put a different caption every quarter of a second while the user has not quit. Now you see why the "quit" variable needed to be global.

In order for a function to be used as a thread in SDL it has to meet 2 requirements. First off it must return an int. Secondly it must have the argument that is a pointer to a data type of void. If the function does not meet both of these requirements it cannot be used as a thread.
//Create and run the thread thread = SDL_CreateThread( my_thread, NULL );
In the main function after everything is intialized and loaded, we call SDL_CreateThread().

SDL_CreateThread() takes in the function in the first argument, turns it into a thread and then runs the newly made thread. It returns a pointer to the thread so we can keep track of it.
//Apply the image to the screen apply_surface( 0, 0, image, screen ); //Update the screen if( SDL_Flip( screen ) == -1 ) { return 1; } //While the user hasn't quit while( quit == false ) { //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 = true; } } }
Then we show the image on the screen, then wait for the user to quit.

While this is going on, the caption is changing in our thread that is currently running parallel to what we're doing. Thanks to SDL's multithreading capabilities, you're able to do these two things simultaneously.
void clean_up() { //Stop the thread SDL_KillThread( thread ); //Free the surface SDL_FreeSurface( image ); //Quit SDL SDL_Quit(); }
Here's our clean up function.

First it we call SDL_KillThread() which instantly stops the thread. Typically you should wait for the thread to finish, but in this case it won't do any harm to just stop it. Then we free the surface, and quit SDL.

Multithreading can be very powerful, but also very hazardous. There's so much more you have to deal with than in single threaded programs. With the rise of multicore CPUs, it's tempting to jump right in.

If you're new to game programming, don't use it until you're more experienced. Multithreading can be more of a headache than it's worth. Only when you have a good handle on software architecture should you use this powerful tool.
Download the media and source code for this tutorial .
阅读(1217) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~