Documente Academic
Documente Profesional
Documente Cultură
Rmi Dewitte
Alexandru Poenaru
1 / 22
Outline
Introducing SDL
SDL in Action
Conclusion
2 / 22
Introducing SDL
Outline
Introducing SDL
SDL in a Nutshell
Current Status
Community
SDL in Action
Conclusion
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)
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
5 / 22
Introducing SDL
Community
Community
6 / 22
SDL in Action
Outline
Introducing SDL
SDL in Action
SDL, a widespread library
Demo
Conclusion
7 / 22
SDL in Action
8 / 22
SDL in Action
Demo
Demo
9 / 22
Outline
1
Introducing SDL
SDL in Action
Conclusion
10 / 22
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
11 / 22
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 ) ;
}
12 / 22
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
13 / 22
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 ( ) ;
14 / 22
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.
15 / 22
16 / 22
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 ) ;
17 / 22
18 / 22
Charateristics
clean, simple, and reliable time and timer API
machine and OS independent.
no limitations in the number of timers
19 / 22
Conclusion
Outline
Introducing SDL
SDL in Action
Conclusion
20 / 22
Conclusion
Conclusion
21 / 22
Conclusion
References
I
22 / 22