Simple DirecMedia Layer
Rmi Dewitte
Alexandru Poenaru
Department of Computer Science
Helsinki University of Technology
November 30th, 2006
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
1 / 22
Outline
Introducing SDL
SDL in Action
Developping with SDL
Conclusion
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
2 / 22
Introducing SDL
Outline
Introducing SDL
SDL in a Nutshell
Current Status
Community
SDL in Action
Developping with SDL
Conclusion
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
3 / 22
Introducing SDL
SDL in a Nutshell
SDL in a Nutshell
Overview
cross-platform multimedia library
developped by Loki Entertainment Software
low level access to many peripherals
a layer between an application and the underlying system
licensed under LGPL
started in 1998
Claimed objectives
save you development time
increase the reliability of your final product
ease portability and versatility
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
4 / 22
Introducing SDL
Current Status
Current Status
Version
SDL 1.2 stable version
SDL 1.3 dev version to support
SDL 2.0 on indefinite hold
Supported Languages
written in C, used with C++ natively
bindings for C#, Eiffel, Erlang, Haskell, Java, Lisp, Perl, PHP,
Python, Ruby, Smalltalk, etc
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
5 / 22
Introducing SDL
Community
Community
Take benefits from
the SDL mailing list
600 emails per month
highly responsive
archives are an enormous resource full of information
add-ons for SDL
steady flow of new projects and improvement of the code
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
6 / 22
SDL in Action
Outline
Introducing SDL
SDL in Action
SDL, a widespread library
Demo
Developping with SDL
Conclusion
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
7 / 22
SDL in Action
SDL, a widespread library
SDL, a widespread library
Application examples
games such as Unreal Tournament, Call to Power.
various multimedia and scientific applications
Statistics
more than 700 projects based on SDL on sourceforge.net
over 100 add-on libraries
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
8 / 22
SDL in Action
Demo
Demo
Lets play the famous TuxRacer.
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
9 / 22
Developping with SDL
Outline
1
Introducing SDL
SDL in Action
Developping with SDL
Graphics
Sound
Network
Event based input
Threads
Set up and shut down
Time and timers
Conclusion
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
10 / 22
Developping with SDL
Graphics
Graphics
Two choices
get at the raw pixels
use OpenGL to do hardware accelerated 2D and 3D graphics
For you SDL ...
takes care of setting the video modes
gets access to the frame buffer
initializes OpenGL
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
11 / 22
Developping with SDL
Graphics
Graphics
Code Sample
SDL_Surface screen = SDL_SetVideoMode ( 6 4 0 , 480 , 16 , SDL_SWSURFACE) ;
void ShowBMP( char f i l e , SDL_Surface screen , i n t x , i n t y )
{
SDL_Surface image ;
SDL_Rect d e s t ;
/ Load t h e BMP f i l e i n t o a s u r f a c e /
image = SDL_LoadBMP ( f i l e ) ;
/ B l i t onto t h e screen s u r f a c e .
The s u r f a c e s should n o t be l o c k e d a t t h i s p o i n t .
/
dest . x = x ; dest . y = y ;
d e s t . w = image>w; d e s t . h = image>h ;
S D L _ B l i t S u r f a c e ( image , NULL , screen , &d e s t ) ;
/ Update t h e changed p o r t i o n o f t h e screen /
SDL_UpdateRects ( screen , 1 , &d e s t ) ;
}
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
12 / 22
Developping with SDL
Sound
Sound
Sound support
query the capabilities of the sound card with a simple (minimal)
API
produce low latency sound on any device
high level sound libraries
add-ons provided for more powerful controls
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
13 / 22
Developping with SDL
Sound
Sound
Code Sample
# i n c l u d e "SDL.h"
# i n c l u d e "SDL_audio.h"
SDL_AudioSpec f m t ;
/ Set 16 b i t s t e r e o audio a t 22Khz /
f m t . f r e q = 22050 ; f m t . f o r m a t = AUDIO_S16 ; f m t . channels = 2 ;
f m t . samples = 512 ; f m t . c a l l b a c k = mixaudio ; f m t . u s e r d a t a = NULL ;
/ Open t h e audio d e v i c e and s t a r t p l a y i n g sound ! /
SDL_OpenAudio(& fmt , NULL ) ;
SDL_PauseAudio ( 0 ) ;
i n t i n d e x ; U i n t 8 data ; U i n t 3 2 d l e n ;
SDL_AudioCVT c v t ; SDL_AudioSpec wave ;
/ Load t h e sound f i l e and c o n v e r t i t t o 16 b i t s t e r e o a t 22kHz /
SDL_LoadWAV ( f i l e , &wave , &data , &d l e n ) ;
SDL_BuildAudioCVT (& c v t , wave . format , wave . channels , wave . f r e q , AUDIO_S16 , 2 , 22050) ;
c v t . b u f = m a l l o c ( d l e n c v t . l e n _ m u l t ) ; memcpy ( c v t . buf , data , d l e n ) ; c v t . l e n = d l e n ;
SDL_ConvertAudio (& c v t ) ;
SDL_FreeWAV ( data ) ;
{ . . . } / / Get a f r e e sound s l o t a t sounds [ i n d e x ]
/ Put t h e sound data i n t h e s l o t ( i t s t a r t s p l a y i n g i m m e d i a t e l y ) /
SDL_LockAudio ( ) ;
sounds [ i n d e x ] . data = c v t . b u f ; sounds [ i n d e x ] . d l e n = c v t . l e n _ c v t ; sounds [ i n d e x ] . dpos = 0 ;
SDL_UnlockAudio ( ) ;
SDL_CloseAudio ( ) ;
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
14 / 22
Developping with SDL
Network
Network
Charateristics
low level networking API
similar to the UNIX and Windows networking APIs
difference : use network features that are common across all the
supported operating systems.
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
15 / 22
Developping with SDL
Event based input
Event based input
Charateristics
inputs from the keyboard, mouse, and joystick
event based model
same events no matter which OS is used
Code Sample
while ( SDL_PollEvent (& event ) ) {
switch ( event . t y p e ) {
case SDL_MOUSEMOTION:
...
case SDL_MOUSEBUTTONDOWN:
...
case SDL_QUIT :
exit (0) ;
}}
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
16 / 22
Developping with SDL
Threads
Threads
Charateristics
simplified version of pthreads
mask the low level details
Code Sample
# i n c l u d e "SDL_thread.h"
i n t t h r e a d _ f u n c ( void unused ) {
SDL_mutexP ( l o c k ) ;
...
SDL_mutexV ( l o c k ) ;
...
SDL_Delay ( 1 0 0 ) ;
...
return ( 0 ) ;
}
SDL_mutex l o c k = SDL_CreateMutex ( ) ;
SDL_Thread t h r e a d = SDL_CreateThread ( t h r e a d _ f u n c , NULL ) ;
...
SDL_WaitThread ( thread , NULL ) ;
SDL_DestroyMutex ( l o c k ) ;
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
17 / 22
Developping with SDL
Set up and shut down
Set up and shut down
Charateristics
simple way to find out what the hardware
only a couple of lines of code you can set it all up.
Code Sample
# i n c l u d e < s t d l i b . h>
# i n c l u d e "SDL.h"
main ( i n t argc , char argv [ ] )
{
i f ( S D L _ I n i t ( SDL_INIT_AUDIO | SDL_INIT_VIDEO ) < 0 ) {
f p r i n t f ( s t d e r r , "Unable to init SDL: %s\n" , SDL_GetError ( ) ) ;
exit (1) ;
}
a t e x i t ( SDL_Quit ) ;
}
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
18 / 22
Developping with SDL
Time and timers
Time and timers
Charateristics
clean, simple, and reliable time and timer API
machine and OS independent.
no limitations in the number of timers
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
19 / 22
Conclusion
Outline
Introducing SDL
SDL in Action
Developping with SDL
Conclusion
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
20 / 22
Conclusion
Conclusion
What you should remember about SDL ?
very stable and scalable
cross platform and support in many languages
widely used and many user contributed libraries
permissive license
dynamic community
low level access
just a layer and the bindings could imply overhead
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
21 / 22
Conclusion
References
I
SDL Homepage, http ://www.libsdl.org
Bob Pendleton. Why SDL ?, August 2002,
http ://gameprogrammer.com/sdl.html
Bob Pendleton, Game programming with the SDL, June 2003,
Linux Journal 110, Specialized Systems Consultants
Ernest Pazera, Focus on Sdl, Oct 2002, Course Technology
Erik Yuzwa, Game Programming in C++ Start to Finish, January
11, 2006, Charles River Media
John Hall, A Crash Course in SDL, 2001, Linux Journal 81es,
Specialized Systems Consultants
Rmi Dewitte, Alexandru Poenaru (HUT)
Simple DirecMedia Layer
November 30th, 2006
22 / 22