The best document in the world!

download The best document in the world!

of 61

Transcript of The best document in the world!

  • 8/21/2019 The best document in the world!

    1/160

    K a rtik T h a k o re

    With c o n trib u tio n s fro m

    https://github.com/PerlGameDev/SDL_Manual/contributors

    http://%20https//github.com/PerlGameDev/SDL_Manual/contributors

  • 8/21/2019 The best document in the world!

    2/160

    L a te x b a s e d o n th e P e r l6 b o o k : H T T P S://G itH ub . C o m/p e r l6 /bo o k

  • 8/21/2019 The best document in the world!

    3/160

    SDL SDLx   . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    SDLx::App   . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  • 8/21/2019 The best document in the world!

    4/160

    . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . .

  • 8/21/2019 The best document in the world!

    5/160

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  • 8/21/2019 The best document in the world!

    6/160

    M Qimplut MM usic   9.94a M d09292 M09296 !

  • 8/21/2019 The best document in the world!

    7/160

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

  • 8/21/2019 The best document in the world!

    8/160

  • 8/21/2019 The best document in the world!

    9/160

    Simple DirectMedia Layer   (or   libsdl) i s a c r os s- p la t fo rm C l i br a ry t h at p r ov id es a cc es s

    t o s ev er al i np ut a nd o ut pu t d ev ic es . I ts m os t p op ul ar u sa ge i s t o p ro vi de a cc es s t o t he

    v i de o f r am e bu f fe r a n d i n p u t d e vi c es f o r g a m e s . S D L a l s o h a s s e v e r al e x t en s io n l i b ra r i es t o

    p r o v id e f e a tu r e s s u c h a s te x t d is p la y , s o u n d m ix in g , im a g e h a n d lin g , a n d g r a p h ic s e f f e c ts .

    S DL P er l b i nd s s ev er a l o f t h es e l i br ar i es t og e th er i n t he   SDL::*   n a m e s pa c e . o r e o v er ,

    S D L P e r l p r o v i d e s s e v e r a l h i g h - l e v e l l i b r a r i e s i n t h e   SDLx::*   n a m e s pa c e th a t e n c a p su la te

    v a lu a b le g a m e - w r itin g a b s tr a c tio n s .

  • 8/21/2019 The best document in the world!

    10/160

    SDL SDLx

    T h e m a i n p u r p o s e o f t h e   SDLx::*   l a y e r i s t o s m o o t h o u t t h e d r u d g e r y o f u s i n g t h e   SDL::*

    la y e r d ir e c tly .

    D o n’ t w o rr y a b ou t u n de r s ta n di n g t h e d e ta i l s o f t h i s c o d e r i g ht n o w. C o mp a r e t h e

    c o m p l e x i t y a n d s i z e o f t h e c o d e l i s t i n g s .

    U s in g th e   SDL::*   l a y e r t o d r a w a b l u e r e c t a n g l e l o o k s s o m e t h i n g l i k e :

    1   u s e SD L

    2   u s e S D L : :V i de o

    3   use SDL::Surface

    4   u s e S D L : :R e ct

    5

    6   # t he s iz e o f t he w in do w b ox o r t he s cr ee n r es ol ut io n i f f ul ls cr ee n

    7   m y $ sc re en _w id th = 80 0

    8   m y $ sc re en _h ei gh t = 6 0 0

    9

    10   SDL::init(SDL_INIT_VIDEO)

    11

    12   # s et ti ng v id eo m od e

    13   my $screen_surface = SDL::Video::set_video_mode($screen_width,

    14   $screen_height,

    15   32,

    16   SDL_ANYFORMAT)

    17

    18   # d ra wi ng a r ec ta ng le w it h t he b lu e c ol or

    19   m y $ m a p p ed _ co l o r = S D L: : V id e o :: m ap _ R GB ( $ sc r ee n _ su r f ac e -> f o rm a t () ,

    20   0 , 0 , 2 55 )

    21   SDL::Video::fill_rect($screen_surface,

    22   S D L :: R ec t - >n e w ($ s cr e e n_ w i dt h / 4 , $ s c re e n _h e ig h t / 4 ,

    23   $ sc r ee n_ w id th / 2 , $ s cr ee n _h ei gh t / 2 ) ,

    24   $mapped_color)

  • 8/21/2019 The best document in the world!

    11/160

    25

    26   # u pd at e a n a re a o n t he s cr ee n s o i t' s v is ib le

    27   SDL::Video::update_rect($screen_surface, 0, 0,

    28   $screen_width, $screen_height)

    29

    30   # just t o h av e t ime to se e i t

    31   sleep(5)

    . . . w h il e d r a w i ng a b l ue r e c ta n g le i n t h e   SDLx::*   l a y e r i s a s s i m p l e a s :

    1   u s e s t ri c t

    2   u s e w a r ni n gs

    3

    4   u s e SD L

    5   u s e S D L x :: A pp

    6

    7   m y $ ap p = S DL x: :A pp -> ne w( w id th => 8 00 , h ei gh t = > 6 00 )

    8

    9   $ a pp -> dr a w_ re c t( [ $ a pp -> w id th / 4 , $ a pp -> h ei gh t / 4 ,

    10   $ ap p- >w id th / 2 , $ ap p- >h ei gh t / 2 , ] ,

    11   [ 0, 0 , 255, 255] )

    12

    13   $app->update()

    14

    15   sleep(5)

    The   SDLx::*   m od u l es a l s o p r o v i de a n d m a na g e h i g he r -l e v el c o nc e rn s f o r u s e rs , s u c h a s

    l a y e r s a n d g a m e l o o p s .

  • 8/21/2019 The best document in the world!

    12/160

    T h is b o o k h a s a t w o- f o ld p u rp o se : f i r st , t o i n t ro d uc e g a me d e v el o pm e nt t o P e rl p r og r a m-

    m e rs , a n d s e c o n d, t o i n t ro d uc e o d er n P e rl c o nc e p ts t h r ou g h g a m e d e ve l o pm e nt . W h i le

    t he e xa mp le s a s su me s om e e x pe ri e nc e w it h P er l , n o e xp er i en ce w it h S DL i n P er l o r a s

    libsdl   its e lf is n e c e s s a r y .

    T h e b o o k p r e s e n t s a p r o g r e s s i o n f r o m s i m p l e t o i n t e r m e d i a t e e x a m p l e s a n d p r o v i d e s s u g -

    g e st i o ns f o r m o re a d va n c ed e n de a vo r s . T h e c h a p t er s o f t h i s b o o k i n c re a s e p r o g r es s i ve l y

    i n c o m p l e x i t y , b u t e a c h c h a p t e r h a s a s i n g u l a r g o a l ( s u c h a s c h a p t e r f i v e ’ s  Making Pong)

    w h ic h s t a n d s a l o n e a s a n i n di v i du a l t u t o r ia l . S o ur c es a n d d a t a f i l e s a r e a l l a v a il a b le f r om

    http://sdl.perl.org/.

    W e a s s u m e t h e p r e s e n c e o f a r e c e n t v e r s i o n o f t h e P e r l l a n g u a g e ( a t l e a s t P e r l 5 . 1 0 ) a n d s u p -

    p o rt i n g p a c k ag e s . We a l s o a s s u m e t h a t y o u c a n i n s ta l l p a c ka g es f r o m t h e C PA N , i n c l u di n g

    S D L P e r l i t s e l f .

    Alien::SDL   w il l i ns ta ll b in ar ie s f or 3 2b it a nd 6 4b it s o t he re i s n o n ee d t o c om pi le a ny -

    thing.

    http://sdl.perl.org/

  • 8/21/2019 The best document in the world!

    13/160

    F i nk h a s p a c k a ge s f o r S D L P e rl a v ai l a bl e . H o we v er , t h e y d o n o t s u p p o rt P a n go , a l i b ra r y

    w h ic h p r o v id e s in te r n a liz a tio n s u p p o r t f o r te x t h a n d lin g .

    Installing Alien::SDL f r o m th e C P A N w ill c o m p ile S D L a n d its d e p e n d e n c ie s , p r o v id e d y o u

    h a v e in s ta lle d s e v e r a n n e c e s s a ry d e p e n d e n c ie s. We r e c o m m e n d th a t y o u in s ta ll libfreetype6,

    libX11,   libvorbis,   libogg,   libpng, a n d t h e i r h e a d e r s .

    o s t c u r r e n t G N U / L i n u x d i s t r i b u t i o n s i n c l u d e a l l t h e p a r t s n e e d e d f o r t h i s t u t o r i a l i n t h e

    d e fa u l t i n s t a ll a n d i n t h e ir p a ck a g e m a n a ge m en t s y st e m . I t i s a l s o a l w a ys p o ss i b le t o i n st a l l

    o n G NU /L in ux u si n g t he a va i la bl e o pe n s ou r ce c od e f ro m t he p r op er r ep o si t or i es . T he

    Alien::SDL   p e rl m o du l e a u to m at e s m u ch o f d o wn l o ad i ng , c o mp i l in g , a n d i n s ta l l in g t h e

    n e e d e d lib r a r ie s .

    Yo u c a n p r ob a bl y u s e y o ur d i s tr i b ut i o n’s p a ck a ge s . O n U b un t u a n d D e bi a n t r y :

    $ s u do a p t -g e t i n st a l l l i b sd l - ne t 1. 2 - de v l i b sd l - mi x er 1 . 2- d e v  \libsdl1.2-dev libsdl-image1.2-dev libsdl-ttf2.0-dev   \libsdl-gfx1.2-dev libsdl-pango-dev

    T o c o m p i l e f r o m s c r a t c h , y o u m u s t i n s t a l l a c o m p i l e r , s y s t e m h e a d e r p a c k a g e s , a n d s o m e

    lib r a r ie s a r e r e q u ir e d .

    $ s u do a p t -g e t i n st a l l b u i ld - e ss e nt i a l x o r g- d ev l i bx 1 1 -d e v l i bx v - de v   \libpango1.0-dev libfreetype6-dev libvorbis-dev libpng12-dev   \libogg-dev

  • 8/21/2019 The best document in the world!

    14/160

    B e f o r e i n s t a l l i n g S D L P e r l , e n s u r e t h a t y o u h a v e t h e m o s t r e c e n t v e r s i o n s o f t h e m o d u l e s

    n e c e s s a r y t o b u i l d S D L :

    $ s ud o c pa n C PA N

    $ s ud o c p an Y A ML M o du le :: B ui ld

    A f t e r t h e s e t w o s t e p s C P A N w i l l b e a b l e t o i n s t a l l S D L :

    $ s ud o c pa n S DL

    F o r m o s t p l a tf o r ms a C PA N i n s t a l l w i l l s u ff i c e. S u pp o r te d a n d t e s t e d p l a t f or m s a r e l i s te d

    at http://pass.cpantesters.org/distro/S/SDL.html.

    H o pe f u ll y t h i s b o o k a n sw e rs m o st o f y o ur q u es t i on s . F o r a d di t i on a l a s s is t a nc e , c o n ta c t t h e

    p r o je c t v ia :

    •   the web, b y v i s i t i n g t h e S D L P e r l h o m e p a g e a t   http://sdl.perl.org/.

    •   IRC , i n t h e   #sdl   c h a n n e l o n   irc.perl.org. T h is i s a v e ry a c ti v e a n d h e l p fu l r e s o u rc e .

    •   email, t h r o u g h t h e   [email protected]   m a ilin g lis t.

    http://sdl.perl.org/http://pass.cpantesters.org/distro/S/SDL.html

  • 8/21/2019 The best document in the world!

    15/160

    T h e c o d e e x a m p l e s i n t h i s b o o k a r e a v a i l a b l e f r o m https://github.com/PerlGameDev/SDL_

    Manual/tree/master/code_listings.

    T h a n k s t o c o n t r i b u t o r s a n d r e v i e w e r s f r o m t h e   #sdl   c h a n n e l, in c lu d in g :

    Alias

    bobross

    Blaizer

    cfedde

    chromatic

    FROGGS

    garu

       jamesw

    perlpilot

    PerlJam

    Pip

    waxhead

    https://github.com/PerlGameDev/SDL_Manual/tree/master/code_listingshttps://github.com/PerlGameDev/SDL_Manual/tree/master/code_listings

  • 8/21/2019 The best document in the world!

    16/160

    a n d m a n y m o r e

    ( Ap ol og ie s i f I h av e m is se d y ou l et m e k no w a nd I w il l a dd y ou .)

  • 8/21/2019 The best document in the world!

    17/160

    S D L’s p r im a ry p u r po s e i s t o d i s pl a y g r a p h ic s . I t d o es s o b y p r ov i d in g a n a b st r a ct i o n c a l l e d

    a  screen, w h i c h r e p r e s e n t s a  video device. T hi s v i de o d e v ic e i s a n i n t e rf ac e p ro v id ed b y

    y o u r o p e r a t i n g s y s t e m , s u c h a s X 1 1 o r D i r e c t X . B e f o r e y o u c a n d i s p l a y a n y t h i n g , y o u m u s t

    c r ea t e a s c r ee n . T h e   SDLx::App   c l a s s d o e s s o f o r y o u :

    1   u s e s t ri c t

    2   u s e w a r ni n gs

    3   u s e SD L

    4   u s e S D L x :: A pp

    5

    6   m y $ a pp = S D L x: : Ap p - >n e w( )

    7

    8   s le ep ( 2 )

    T h is e x am p le c a u se s a n e m pt y w i n d o w t o a p p ea r o n t h e d e s k to p . o st s y s te m s w i l l f i l l t h at

    w i nd o w w i t h t h e c o lo r b l a ck . O t he r s y s te m s m i g h t d i s pl a y a t r a ns p a re n t w i nd o w. S D L’s

  • 8/21/2019 The best document in the world!

    18/160

    d ef au l t b eh av i or i s t o f i ll t he s cr ee n w i th b l ac k. To e n fo rc e t h is b eh av io r o n a ll s ys t em s,

    y o u m u s t   update()   t h e a p p t o d r a w t o t h e w i n d o w :

    $app->update()

    SDLx::App

    SDLx::App   a l lo w s y o u t o s p ec i f y s e v e ra l o p t io n s f o r t h e s c r e e n a n d y o ur a p pl i c at i o n. F i r st

    a re t he p hy si ca l d im en si on s o f t he s cr ee n i ts el f. To m ak e t he s cr ee n o f t he   SDLx::App

    w i n d o w a 4 0 0 × 4 0 0 p i x e l s q u a r e , c h a n g e t h e i n i t i a l i z a t i o n l i n e t o :

    m y $ ap p = S DL x: :A pp -> ne w( w id th = > 4 00 , h ei gh t = > 4 00 )

    A n ot h e r i m p o rt a nt o p ti o n i s t h e w i n d o w’s t i t le . S o me s y st e ms d i sp l a y t h e p a t h t o t h e r u n -

    n i n g p r o g ra m . O t he r s l e a v e t h e t i t le b l a nk . Yo u c a n c h a ng e t h e d i sp l a ye d t i t le w i t h a n o t he r

    a r g u m e n t t o t h e   SDLx::App   constructor:

    m y $ ap p = S DL x: :A pp -> ne w( w id th = > 4 00 ,

    h ei g ht = > 4 00 ,

    tit le => 'Pong - A clone' )

    A t t h i s p o i n t y o u r s c r e e n w i l l b e :

    A b br e v ia t i on s f o r t h es e p a r am e te r s a r e a v ai l a bl e . I n st e a d o f   width,   height, a n d   title, y o u

    m a y u s e   w,   h, a n d   t   r e s p ec t i ve l y. T h e p r e vi o us e x am p le c o ul d a l s o b e w r i tt e n :

    m y $ ap p = S DL x: :A pp -> ne w( w = > 4 00 ,

    h => 400,

    t => 'Pon g - A clone ' )

  • 8/21/2019 The best document in the world!

    19/160

    F i g u r e 2 . 1 : Y o u r f i r s t S D L s c r e e n !

  • 8/21/2019 The best document in the world!

    20/160

  • 8/21/2019 The best document in the world!

    21/160

    S D L p r o v i d e s s e v e r a l w a y s t o d r a w g r a p h i c a l e l e m e n t s o n t h e s c r e e n i n t h r e e g e n e r a l c a t -

    e go ri e s: p r im it i ve s , i ma ge s, a nd t e xt . A ll d r aw in g o cc ur s o n a s u rf a ce , r ep r es en t ed b y

    the   SDLx::Surface   c l as s . E v en t h e   SDLx::App   i s a n   SDLx::Surface. T h ou g h t h i s m e a n s i t ’sp o ss i b le t o d r a w d i r e ct l y t o t h e a p p ’s s u r fa c e, t h er e a r e s e ve r a l a d v a nt a g es t o d r a wi n g o n

    m u ltip le s u r f a c e s .

    S D L’s s u r fa c e c o or d i na t e s y s t e m h a s i t s o r i gi n ( w he r e b o th t h e x a n d y c o o rd i n at e s h a v e

    t he v a lu e o f z e ro ) i n t h e u p p er l e ft c or n er. A s t he v al u e o f x i n cr ea s es , t he p o si t io n m ov e s

    t o t h e r i g ht o f t h e o r i gi n . A s t he v a lu e o f y i nc r ea se s , t h e p os it i on m ov es d ow nw ar d f r om

    t h e o r i g i n. T h e A P I a l wa y s l i s t s c o o r d in a te s i n x , y o r de r.

  • 8/21/2019 The best document in the world!

    22/160

    T h e S D L l i b r a r y d o c u m e n t a t i o n h a s a n e x t e n d e d d i s c u s s i o n o n c o o r d i n a t e s :   http:

    //sdltutorials.com/sdl-coordinates-and-blitting.

    Y o u c a n p r o d u c e o r i g i n a l p i c t u r e s k n o w i n g l i t t l e m o r e t h a n h o w t o d r a w t o a s u r f a c e w i t h

    SDL:

    F i g u r e 3 . 1 : A f i e l d o f f l o w e r s

    http://sdltutorials.com/sdl-coordinates-and-blittinghttp://sdltutorials.com/sdl-coordinates-and-blitting

  • 8/21/2019 The best document in the world!

    23/160

    A s m en ti o ne d e a rl i er, a l l d ra wi n g i n S DL r e qu ir e s a s u rf ac e . T he   SDLx::Surface   object

    p r o v i d e s a c c e s s t o m e t h o d s i n t h e f o r m o f :

    $surface->draw_{something}( .... )

    P a r a m e t e r s t o t h e s e m e t h o d s a r e g e n e r a l l y c o o r d i n a t e s a n d c o l o r s , p r o v i d e d a s a r r a y r e f e r -

    ences.

    S o m e p a r a m e ter s a r e s e ts o f c o o r d in a te p o s itio n s a n d d im e n s io ns . F o r e x a m p le , p a r a m e ter s

    t o d e s c r i b e a r e c t a n g l e o f   40x40   p i x e l s p l a c e d a t   (2 0, 2 0)   p i x e l u n i t s o n t h e s c r e e n m a k e a

    f o u r - e l e m e n t a r r a y r e f e r e n c e o f x , y , w i d t h , h e i g h t :

    m y $ re ct = [ 20 , 2 0, 4 0, 4 0]

    S D L c o lo r p a r a m e te r s r e q u ir e f o u r- e le m e nt a r r a y r e f e r e n c e s . T h e f ir s t th r e e n u m b e r s d e f in e

    t h e R e d , G r ee n , a n d B l ue i n t en s i ty o f t h e c o l o r. T h e f i n a l n u mb e r d e f i n es t h e t r a n s pa r e nc y

    o f t h e c o l o r .

    m y $ co lo r = [ 25 5, 2 55 , 2 55 , 2 55 ]

    T h e m a g n i t u d e o f e a c h c o l o r v a l u e d e t e r m i n e s h o w m u c h o f t h a t c o l o r c o m p o n e n t w i l l b e

    m i x ed i n t o t h e r e s u l t in g c o l o r. A 0 v a l ue s p ec i f ie s t h a t n o ne o f t h e c o l or c h an n el s h o ul d b e

    u s e d w h i l e 2 5 5 s p ec i f ie s a m a xi m um i n t en s i ty f o r a p a r ti c u la r c h an n el . T h e f i r s t v a l ue c o r-

    r e s p o n d s w i t h t h e R e d c h a n n e l , s o a h i g h e r n u m b e r t h e r e m e a n s m o r e r e d w i l l b e m i x e d i n t o

    t h e r e s u l ti n g c o l o r. I t i s a c o mm o n p r a c t ic e t o a c hi e v e a g r ay s ca l e o f v a r yi n g i n t e n si t y b y

  • 8/21/2019 The best document in the world!

    24/160

    s p ec i f yi n g t h e s a me v a lu e f o r e a ch o f t h e R e d, G r ee n , a n d B l ue c o l or c h an n el s . T h e f o u r th

    a n d f i n a l v a l u e d e s i g n a t e s t h e t r a n s p a r e n c y ( o r A l p h a c h a n n e l ) w h e r e a 0 v a l u e m a k e s t h e

    r e su l t in g c o lo r f u l ly t r a ns p a re n t a n d 2 5 5 m a ke s i t e n ti r e ly o p aq u e. A t r a ns p ar e n cy v a l ues o m e w h e r e i n b e t w e e n w i l l a l l o w u n d e r l y i n g ( p i x e l d a t a o f s u r f a c e s b e l o w t h e c u r r e n t o n e )

    c o l o r s t o b e b l e n d e d w i t h t h e s p e c i f i e d R G B v a l u e s i n t o t h e f i n a l c o l o r o u t p u t .

    Yo u m ay a l so r e pr es en t a c ol o r a s h ex ad ec i ma l v al u es , w he re t he v al u es o f t he n um be rs

    r a n g e f r o m 0 - 2 5 5 f o r 3 2 b i t d e p t h i n R G B A f o r m a t :

    m y $ co lo r = 0 x FF FF F FF F

    m y $ wh it e = 0 x FF FF F FF F

    m y $ bl ac k = 0 x 00 00 0 0F F

    m y $ re d = 0x FF 00 00 FF

    m y $ gr ee n = 0 x 00 FF 0 0F F

    m y $ b lu e = 0 x0 00 0F FF F

    . . . o r a s f o ur - by t e h e x a d ec i m al v a l ue s , w h e r e e a c h t w o- d i gi t b y te e n c od e s t h e s a me R G BA

    values:

    m y $ g ol d e nr o d = 0 x D AA 5 20 F F

    N O T E : D e p t h o f S u r f a c e

    T h e c o l o r d e p t h o f t h e s u r f a c e – h o w m a n y b i t s a r e a v a i l a b l e t o d e s c r i b e c o l o r s – i s a

    p r o p e r t y o f t h e r e l e v a n t   SDLx::Surface   or   SDLx::App. S et i t i n i t s c on st r uc t or :

    m y $ ap p = S DL x: :A pp -> ne w( d ep th = > 3 2 )

    T h e d e f a u l t b i t d e p t h i s 3 2 , s u c h t h a t e a c h c o l o r c o m p o n e n t h a s 2 5 6 p o s s i b l e v a l u e s .

    O t h e r o p t i o n s a r e 2 4 , 1 6 , a n d 8 .

  • 8/21/2019 The best document in the world!

    25/160

    All   SDLx::Surfaces a r e c o l le ct i on s o f p i xe ls . Yo u c an r ea d f r om a n d w r i te t o t h es e p i xe ls

    b y t r e a t i n g t h e s u r f a c e a s a n a r r a y r e f e r e n c e :

    $ a p p- > [$ x ] [$ y ] = $ c o lo r

    . .. w he r e   $color   i s a n u n s i g n e d i n t e g e r v a l u e u s i n g t h e h e x a d e c i m a l f o r m a t ( 0xRRGGBBAA)

    or   a n a n o n y m o u s a r r a y o f t h e f o r m   [ $ re d , $ g re e n, $ b lu e , $ a lp h a].

    D r a w i n g p r i m i t i v e s a r e s i m p l e s h a p e s t h a t S D L s u p p o r t s n a t i v e l y .

    F i g u r e 3 . 2 : D r a w i n g a l i n e

  • 8/21/2019 The best document in the world!

    26/160

  • 8/21/2019 The best document in the world!

    27/160

    A r e c ta n gl e i s a f o u r- s i de d , f i l l e d p o l y go n . R e ct a ng l e s a r e a c o mm o n b u i l di n g b l o c k f o r

    g am es . I n S DL , r ec ta ng le s a re t he m os t c os t e ff ec ti ve o f t he p ri mi ti ve s t o d ra w. T he

    d r a w r e c t   m e t h o d d r a w s a r e c t a n g l e o n a s u r f a c e :

    $ a pp -> dr a w_ re c t( [ 1 0, 2 0, 4 0, 4 0 ] , [ 25 5, 2 55 , 2 55 ,2 5 5] )

    T h i s d r a w s a w h i t e s q u a r e o f s i z e   40x40   o n t o t h e s c r e e n a t t h e p o s i t i o n   (10,20).

    F i g u r e 3 . 4 : D r a w i n g a C i r c l e

  • 8/21/2019 The best document in the world!

    28/160

    F i g u r e 3 . 5 : D r a w i n g a f i l l e d C i r c l e

    A c i r c l e i s a p r i mi t i ve a f i x ed r a d iu s a r o u n d a g i ve n p o i n t . C i r cl e s m a y b e f i l le d o r u n fi l l ed .The   d r a w c i r cl e   and   d r a w c i r c l e f i l l ed   m e t h o d s d r a w t h e s e t o a s u r f a c e :

    $app->draw_circle( [100, 100], 20, [255, 0, 0, 255] )

    $ ap p- >d ra w_ ci rc le _f il le d( [ 10 0, 1 00 ], 1 9, [ 0, 0 , 2 55 , 2 55 ] )

    T h e s e d r a w a n u n f i l l e d r e d c i r c l e a n d a f i l l e d b l u e c i r c l e .

    S D L p r o v i d e s m o r e c o m p l e x p r i m i t i v e s i n   SDL::GFX::Primitives.

  • 8/21/2019 The best document in the world!

    29/160

    I t ’ s e a s y t o c o m b i n e s e v e r a l p r i m i t i v e s t o d r a w a n i n t e r e s t i n g i m a g e s .

    1   u s e s t ri c t

    2   u s e w a r ni n gs

    3   u s e SD L

    4   u s e S D L x :: A pp

    5

    6   m y $ ap p = S DL x :: Ap p -> ne w(

    7   w => 500,

    8   h => 500,

    9   d => 32,

    10   t it l e = > ' Pr et t y F lo w er s'

    11   )

    12

    13   # A dd t he b lu e s ki es

    14   $a pp->dr aw_re ct( [ 0, 0 , 500 , 500 ], [ 20, 5 0, 1 70 , 255 ] )

    15

    16   # D ra w a g re en f ie ld

    17   $ ap p- >d ra w_ re ct ( [ 0 , 4 00 , 5 0 0, 1 00 ] , [ 5 0, 1 70 , 2 0, 1 00 ] )

    18

    19   # M ak e a s ur fa ce f or t he f lo we r

    20   m y $ fl ow er = S DL x: :S ur fa ce -> ne w( w id th = > 5 0, h ei gh t = > 1 00 ) 21

    22   # W it h a b la ck b ac kg ro un d

    23   $flower->draw_rect( [ 0, 0, 50, 100 ], [ 0, 0, 0, 0 ] )

    24

    25   # D ra w a p re tt y g r ee n s t em

    26   $ fl ow er -> dr aw _r ec t( [ 2 3, 3 0, 4 , 1 00 ] , [ 0 , 2 55 , 0 , 2 55 ] )

    27

    28   # A nd a s im pl e f l ow er b ud

    29   $ fl ow er -> dr aw _c ir cl e_ fi ll ed ( [ 2 5, 2 5 ] , 1 0, [ 1 50 , 0 , 0 , 2 55 ] )

    30   $flower->draw_circle( [ 25, 25 ], 10, [ 255, 0, 0, 255 ] )

    31

    32   # D ra w f l ow er o n $ a pp

    33   $ fl ow er -> bl it ( $ ap p, [ 0 , 0 , 5 0, 1 00 ] )

  • 8/21/2019 The best document in the world!

    30/160

    34

    35   $app->update()

    36

    37   sleep(1)

    F i g u r e 3 . 6 : L o o k s s o l o n e l y t h e r e a l l a l o n e

    T he e xa mp le s s o f ar h av e d r aw n o n o nl y a s in gl e s u rf ac e , t h e d i sp l ay. S DL m ak es i t p os -

    s ib l e t o w ri t e o n m ul t ip l e s ur f ac es . T he s e o th er s u rf ac e s e xi st o nl y i n m em or y u nt i l y ou

    d r a w t h e m t o t h e d i s p l a y .

  • 8/21/2019 The best document in the world!

    31/160

    T h e r e a r e s e v e r a l w a y s t o c r e a t e a n   SDLx::Surface   f o r u s e . T he m os t c om mo n i s t o c r e at e

    o n e m a n u a l l y w i t h a c o n s t r u c t o r c a l l :

    $ s ur fa ce = S D Lx :: Su r fa ce - >n ew ( w id th = > $ w id th , h ei gh t = > $ h ei gh t )

    SDL::Image   and   SDL::Video   c a n l o a d i m a g e s a s s u r f a c e s t o o .   SDL::Image   p r o v id e s s u p p o r t

    f o r a l l t y p e s o f i m a g e s , p r o v i d e d t h a t t h e u n d e r l y i n g   S D L i m a ge   l i b r a r y s u p p o r t s t h e i m a g e

    t yp e y ou w an t t o l o ad . F or e xa mp le ,   S D L i m a ge   m u s t s u p p o r t P N G i m a g e s t o u s e :

    $ s u rf a ce = S D L :: I ma g e :: l o ad ( ' p i ct u r e. p ng ' )

    I n t h e e v e n t t h a t t h e d e s i r e d   S D L i m a ge   l i b r a r y i s u n a v a i l a b l e , y o u c a n f a l l b a c k t o t h e b u i l t - i n

    s u p p o r t f o r t h e   .bmp   format.

    $ s u rf a ce = S D L :: V id e o :: l o ad _ BM P ( ' p i ct u re . b mp ' )

    The   SDLx::Sprite   m o d u l e p r o v i d e s a n o t h e r o p t i o n t o m a n i p u l a t e s u r f a c e s .

    T he f l ow er e x am pl e u s ed a m et ho d c al l ed   blit   to d ra w a s ur fa ce t o t he d is pl ay. T hi s

    m et h od c op ie s d at a f ro m o ne s ur f ac e t o a no th e r. I t ’s a f un da me nt a l o pe r at i on , b ut i t ’s a

    l o w l e v el o p er a t io n .   SDLx::Sprite   p r ov i d es h i gh e r l e ve l o p t io n s . B e si d es m a ki n g d r a w-

    in g s im p le r ,   SDLx::Sprite   a d ds s e ve r a l o t h e r f e at u r es u s ef u l f o r m o vi n g i m ag e s . H e r e’s a

    r e v is e d e x a m p le u s in g   SDLx::Sprite   f o r f lo w e r s :

    1   u s e s t ri c t

    2   u s e w a r ni n gs

    3   u s e SD L

    4   u s e S D L x :: A pp

  • 8/21/2019 The best document in the world!

    32/160

    5   use SDLx::Sprite

    6

    7   m y $ ap p = S DL x :: Ap p -> ne w(

    8   w => 500,

    9   h => 500,

    10   d => 32,

    11   t it l e = > ' Pr et t y F lo w er s'

    12   )

    13

    14   # A dd in g b lu e s ki es

    15   $a pp->dr aw_re ct( [ 0, 0 , 5 00 , 5 00 ], [ 20, 50 , 1 70, 255 ] )

    16

    17   # D ra w a g re en f ie ld

    18   $ ap p- >d ra w_ re ct ( [ 0 , 4 00 , 5 00 , 1 00 ] , [ 5 0, 1 70 , 2 0, 1 00 ] ) 19

    20   m y $ fl ow er = S DL x: :S pr it e- >n ew ( w id th = > 5 0, h ei gh t = > 1 00 )

    21

    22   # U se - > su rf a ce () t o a cc e ss a s pr it e 's S DL x :: Su r fa ce

    23

    24   # M ak e t h e b a ck gr ou n d b l a ck

    25   $flower->surface->draw_rect( [ 0, 0, 5 0, 1 00 ] , [ 0, 0, 0 , 0 ] )

    26

    27   # N ow f or a p re tt y g r ee n s t em

    28   $ fl ow er -> su rf ac e- >d ra w_ re ct ( [ 2 3, 3 0, 4 , 1 00 ] , [ 0 , 2 55 , 0 , 2 55 ] )

    29

    30   # A dd t he s im pl e f l ow er b ud

    31   $ fl ow er -> su rf ac e- >d ra w_ ci rc le _f il le d( [ 2 5, 2 5 ] , 1 0, [ 1 50 , 0 , 0 , 2 55 ] )

    32   $f lower- >surf ace->d raw_ci rcle( [ 25, 25 ], 10 , [ 255, 0, 0, 25 5 ] )

    33

    34   $ f lo we r- > dr aw _ xy ( $ a pp , 0 , 0 )

    35

    36   $app->update()

    37

    38   sleep(1)

    F l o we r s u s u a ll y d o n ’ t g r o w i n t h e s k y. F l ow e rs m a ke m o re s e ns e o n t h e g r o u nd . I t ’s e a s y

    t o i n s er t p l en t y o f i d e nt i c al f l o we r s f r o m a s i ng l e s p r i t e. R e pl a c e t h e l i n e:

  • 8/21/2019 The best document in the world!

    33/160

    $ f lo we r- > dr aw _ xy ( $ a pp , 0 , 0 )

    . . . w i t h:

    1   for (0 .. 500) {

    2   my $y = 425 - rand( 50)

    3   $ fl o we r- > dr aw _x y ( $a p p, r a nd (5 00 ) - 2 0 , $y )

    4   }

    . . . t o m ak e a n e nt i re f i el d o f f l ow er s.

  • 8/21/2019 The best document in the world!

    34/160

  • 8/21/2019 The best document in the world!

    35/160

    T h e c o r n er s t on e o f a n S D L a p pl i c at i o n i s e v en t h a n d l i ng . T h e u s e r p r e s s es a k e y o r m o ve s

    t h e m o u s e. T h e o p e r at i n g s y s t em s w i tc h es t h e f o c u s o f t h e a c t i v e w i n do w. T h e u s e r s e l e c ts

    t h e q u i t o p t io n f r om t he m en u o r t he o p er at i ng s y st em . T he s e a r e a ll e ve nt s . H ow d o y ou

    h a n d le th e m ?

    S DL p ro vi d es a n e ve n t q ue ue w hi ch h o ld s a l l e ve n ts t h at o c cu r u nt i l t h ey a re r em ov ed .

    E v er y t i m e a n e v en t o c c u r s, S D L p l a ce s i t i n t o t h e q u e u e. T h e   SDL::Event   o b je c t r e p r e s e n ts

    t h i s q u e u e i n P e r l , a l l o w i n g y o u t o a d d a n d r e m o v e e v e n t s c o n s t a n t l y :

    1   u s e s t ri c t

    2   u s e w a rn i n gs

    3   u se S DL

    4   use SDL::Event

    5   use SDL::Events

    6   u s e S D Lx : : Ap p

    7

  • 8/21/2019 The best document in the world!

    36/160

    8   my $app = SDL x::App ->new ( w => 200, h => 200 )

    9   m y $ e v en t = S D L :: E ve n t -> n e w( )

    10

    11   my $quit = 0

    12

    13   w hi l e ( !$ q ui t) {

    14   # U pd at es t he q ue ue t o r ec en t ev en ts

    15   SDL::Events::pump_events()

    16

    17   # p ro ce s s a l l a va i la bl e e ve nt s

    18   w h i le ( S D L: : E ve n t s: : po l l _e v e nt ( $e v e nt ) ) {

    19

    20   # c he ck b y E ve nt t yp e

    21   d o _k e y () i f $ e ve n t -> t y pe = = S D L_ K E YD O W N22   }

    23   }

    24

    25   sub do_ke y { $q uit = 1 }

    E v er y e v en t h a s a n a s so c i at e d t y p e w h ic h r e p r e s en t s t h e c a te g or y o f t h e e v e n t. T h e p r e v io u s

    e x a m p l e l o o k s f o r a k e y p r e s s e v e n t   1 . T h e S D L l i b r a ry d e f in e s s e v e ra l t y p e s o f e v en t s , a n d

    S D L p e rl m a ke s t h e m a v a il a b le a s c o n s t a nt s w i t h n a me s s u ch a s   S D L K E Y DO W N   and   S D L Q U IT.

    See   p e rl d o c S D L :: E v e nt s   f o r a l i s t o f a l l e v e n t t y p e s .

    C h ec k i ng f o r e v e r y p o s s i bl e e v e n t t y p e w i th i n t h a t e v en t l o o p c a n b e t e d io u s . T h e   SDLx::-Controller   a v a il a b le f r o m t h e   SDLx::App   o ff e rs t he u s e o f e ve nt c al l ba ck s w it h w hi ch t o

    h a nd le e v en ts . P r oc es si n g e ve nt s i s a m at t er o f s e tt i ng u p t he a pp r op ri a te c al l ba ck s a nd

    l e t t i n g S D L d o t h e h e a v y w o r k .

    SDL Events Types

    A d d i t i o n a l E v e n t t y p e s t h a t c a n b e c a p t u r e d b y S D L a r e :

    Keyboard

    1 S D L s e p a r a t e s t h e e v e n t o f p r e s s i n g a k e y f r o m t h e e v e n t o f r e l e a s i n g a k e y , w h i c h a l l o w s y o u t o i d e n t i f y

    c o m b i n a t i o n s o f k e y p r e s s e s , s u c h a s C t r l + P t o p r i n t .

  • 8/21/2019 The best document in the world!

    37/160

    S D L K E YD O WN S D L K E YU P   - K e y b o a r d b u t t o n p r e s s e d

    ouse

    S D L M O U S EM O T I ON   - o u s e m o t i o n o c c u r e d

    S D L M O U S EB U T T ON D O WN S D L M O U SE B U T TO N U P   - o u s e b u t t o n p r e s s e d

    Joystick

    SDL JOYAXISMOTION   - J o y s t i c k a x i s m o t i o n

    SDL JOYBALLMOTION   - J o y s t i c k t r a c k b a l l m o t i o n

    S D L J O Y H AT M O T IO N   - J o y s t i c k h a t p o s i t i o n c h a n g e

    S D L J O Y B UT T O N DO W N S D L J O Y B UT T O NU P   - J o y s t i c k b u t t o n p r e s s e d

    W i n d o w & S y s t e m

    S D L A C T I VE E V E NT   - A p p lic a tio n v is ib ility

    S D L V I D E OR E S I ZE   - W i n d o w r e s i z e d

    S D L V I D E OE X P O SE   - W in d o w e x p o s e d

    S D L Q U IT   - Q u i t r e q u e s t e d

    S D L U S E R EV E N T   - A u s e r - d e f i n e d e v e n t t y p e

    S D L S Y S W ME V E N T   - P la tf o r m - d e p e n d e n t w in d o w m a n a g e r e v e n t

    F o r m o r e i n f o r m a t i o n l o o k a t :

  • 8/21/2019 The best document in the world!

    38/160

    perldoc SDL::Event

    T he e xa mp l e a pp li c at i on s s o f a r h av e n ot e x it e d c le an l y. H an dl i ng q ui t e ve nt s i s m uc h

    better:

    1   u s e s t ri c t

    2   u s e w a r ni n gs

    3   u s e SD L

    4   u s e S D L : :E v en t

    5   u s e S D L x :: A pp

    6

    7   m y $ ap p = S DL x :: Ap p -> ne w(

    8   w => 200,

    9   h => 200,

    10   d => 32,

    11   t it l e = > " Qu it E ve nt s "

    12   )13

    14   $app->add_event_handler(   \& q ui t _ ev e n t ) 15   $app->run()

    16

    17   s u b q u i t _e v en t

    18   {

    19   # t h e c a ll ba ck r e c ei v es t h e a p pr o pr ia t e S DL : :E ve n t

    20   m y $ ev en t = s hi ft

    21

    22   # .. . a s w el l a s t he ca ll in g S DL x: :C on tr ol le r

    23   m y $ c on tr ol l er = s hi ft

    24

    25   # s t op pi n g th e c on tr o ll er w i ll e x it $ a pp -> r un () f o r u s

  • 8/21/2019 The best document in the world!

    39/160

    26   $ c on t r ol l er - > st o p i f $ e v en t - >t y pe = = S D L _Q U IT

    27   }

    SDLx::App   c al l s t h e e v e nt h a nd l e rs , f r om a n i n t er n al   SDLx::Controller. W he n t h is e ve nt

    h a n dl e r r e c e i ve s a q u it e v en t , i t c a l ls   SDLx::Controller::stop()   w h ic h c a u s e s   SDLx::App

    to e x it g r a c e f u lly .

    E x it i n g o n r e c ei v i ng t h e   S D L Q U I T   e v e n t i s s u c h a c o m m o n o p e r a t i o n t h a t   SDLx::App   pro-

    v i d e s i t a s a c o n s t r u c t o r o p t i o n :

    1   u s e s t ri c t

    2   u s e w a r ni n gs

    3   u s e SD L

    4   u s e S D L x :: A pp

    5

    6   m y $ ap p = S DL x :: Ap p -> ne w(

    7   w => 200,

    8   h => 200,

    9   d => 32,

    10   title => "Quit Events",

    11   e xi t _o n_ q ui t = > 1

    12   )

    13

    14   $app->run()

  • 8/21/2019 The best document in the world!

    40/160

    S DL e ve nt s a ls o a ll o w i n p ut h an dl i ng . C on si d er a s i mp l e p ai n t p r og ra m. I t w il l p ro vi d e

    a s ma l l b la ck w i nd ow. ov i ng t h e m o u se d ra ws o n t h is w in do w. P r es si n g a n um be r k e y

    c h oo s es a p a i nt c o l or. P r es s i ng   q   or   Q   e x i ts t h e p r o g r am . P r es s i ng   c   or   C   c l e a r s t h e s c r e e n .

    Pressing   Ctrl-S   s a v e s t h e i m a g e t o a f i l e n a m e d  painted.bmp.

    F i gu r e 4 . 1: S i mp l e P a in t : S m il e

    S t a r t b y d e f i n i n g t h e s a v i n g f u n c t i o n :

    1   s u b s a ve _ im ag e {

    2   i f ( S DL :: V id eo : :s av e_ B MP ( $ a pp , ' p ai nt e d. bm p ' ) = = 0

    3   & & - e ' p a in t ed . b mp ' )

  • 8/21/2019 The best document in the world!

    41/160

    4   {

    5   w ar n ' Sa ve d p ai nt ed .b mp t o ' . c wd ()

    6   }

    7   else

    8   {

    9   w ar n ' Co ul d n ot sa ve pa in te d .b mp : ' . S D L: :g e t_ er r or s( )

    10   }

    11   }

    K e y b o a r d h a n d l i n g r e q u i r e s s o m e c o l o r d a t a a s w e l l a s a k e y p r e s s c a l l b a c k :

    1   m y $ br us h_ co lo r = 0

    2

    3   sub keyboard_event

    4   {

    5   m y $ ev en t = s hi ft

    6

    7   i f ( $ ev e nt -> ty p e = = SD L_ K EY DO WN )

    8   {

    9   # c on v er t t h e k e y_ sy mb o l ( in t eg er ) t o a k ey na m e

    10   m y $ k e y_ n am e = S D L :: E v en t s: : g et _ k ey _ na m e ( $ e v en t -> k e y_ s y m ) 11

    12   # if $key_n ame is a di git, use it as a color

    13   $ br us h _c ol o r = $ k ey _n a me i f $ ke y_ n am e = ~ / ^\d$/14

    15   # g et th e k e yb oa r d m od i fi er (s ee pe rl do c S DL : :E ve n ts )

    16   my $mod_state = SDL::Events::get_mod_state()

    17

    18   # we ar e u si ng an y C TR L s o K MO D_ CT RL is fi ne

    19   s av e_ i ma ge ( ) i f $k ey _n a me =~ / ŝ $ / & & ( $ mo d_ s ta te & KM OD _ CT RL )

    20

    21   # c le ar t he s cr ee n

    22   $ ap p- >d ra w_ re ct ( [ 0, 0, $a pp -> w, $a pp -> h ] , 0 )

    23   i f $ ke y _n am e = ~ / ^ c$ /

  • 8/21/2019 The best document in the world!

    42/160

    24

    25   # e xi t

    26   $ ap p- > st op ( ) i f $ ke y_ n am e = ~ / ^q $ /

    27   }

    28

    29   $app->update()

    30   }

    31

    32   $app->add_event_handler(\&quit_event)33   $app->add_event_handler(\&keyboard_event)

    NOTE:   W h e n a d d i n g a c a l l b a c k t o   SDLx::App   w h i c h u s e s v a r i a b l e s d e c l a r e d o u t -

    s i d e o f t h e f u n c t i o n ( $ b r us h c o l or   and   @colors   i n t h i s c a s e ) , b e s u r e t o d e f i n e t h e mb e fo r e d e c l a r in g t h e s u br o ut i n e. N o rm a l P e r l s c op i n g a n d i n i ti a l iz a t io n r u l es a p -

    ply.

    H a nd l i ng m o us e e v en t s i s a l mo s t a s s t r ai g h tf o r wa r d a s k e yb o ar d e v en t s : = b eg i n p r og r a m-

    listing

    # t ra ck t he d ra wi ng s ta tu s

    m y $ dr aw in g = 0

    s u b m ou s e_ ev e nt {

    m y $ ev en t = s hi ft

    # d et ec t M ou se B ut to n e ve nt s a nd c he ck i f u se r i s c ur re nt ly d ra wi ng

    i f ( $ e ve n t -> t yp e = = S D L _M O U SE B UT T O ND O W N | | $ d ra w i ng )

    {

    # set drawi ng to 1

    $ dr aw in g = 1

    # get the X and Y valu es of the m ouse

  • 8/21/2019 The best document in the world!

    43/160

    m y $ x = $ ev e nt -> b ut to n _x

    m y $ y = $ ev e nt -> b ut to n _y

    # d ra w a r ec ta ng le a t t he s pe ci fi ed p os it io n

    $ ap p- > dr aw _ re ct ( [ $ x, $ y, 2 , 2 ] , $ co l or s[ $b r us h_ c ol or ] )

    $app->update()

    }

    # d i sa bl e d ra wi n g w he n u se r r el e as es m o us e b ut to n

    $ dr a wi ng = 0 i f ( $e ve n t- >t y pe = = S DL _M O US EB U TT ON U P )

    }

    $app->add_event_handler(   \& m ou s e _e v e nt )

    T h i s i s a l l o f t h e c o d e n e c e s s a r y t o m a k e a s i m p l e d r a w i n g a p p l i c a t i o n .

    Ta ke n ot e o f t wo t hi ng s. F ir st , S DL p er l i nv ok es t he e ve nt h an dl er s i n t he o rd er o f a t-

    t a c hm e nt . I f t h e u s e r p r e s s es   Q   a n d t h e n m o v e s t h e m o u s e , t h e a p p l i c a t i o n w i l l q u i t b e f o r e

    p r o c e s s in g th e m o u s e m o v e m e n t.

    S e c o n d , t h e a p p l i c a t i o n m a k e s n o d i s t i n c t i o n b e t w e e n r i g h t , m i d d l e , o r l e f t m o u s e c l i c k s .

    S D L p r o v i de s t h i s i n f o r ma t i on . S e e t h e   button button()   m e t h o d i n   SDL::Event.

    Hey!  The above document had some coding errors, which are explained below:

    A r o u n d l i n e 3 1 7 :

    = e n d p r o g r a m listin g w ith o u t m a tc h in g = b e g in . ( S ta c k : [ e m p ty] )

  • 8/21/2019 The best document in the world!

    44/160

  • 8/21/2019 The best document in the world!

    45/160

    J u s t a s a n i n t e r a c t i v e S D L a p p b u i l d s a r o u n d a n e v e n t l o o p , a g a m e b u i l d s a r o u n d a g a m e

    l o op . T h e s i m p le s t g a m e l o o p i s s o me t h in g l i k e:

    1   w h i le ( ! $ q u it )

    2   {

    3   get_events()

    4   calculate_next_positions()

    5   render()

    6   }

    T h e n a m e s o f t h e f u n c t i o n s c a l l e d i n t h i s l o o p h i n t a t t h e i r p u r p o s e s , b u t t h e s u b t l e t i e s o f

    e ve n t hi s s i mp l e c od e a re i mp or t an t .   g e t e v e n t s ()   o b vi o u sl y p r oc e s se s e v e nt s f r om t h e

    r e le v a nt i n pu t d e vi c e s ( k e y b oa r d , m o u s e , j o y s t ic k ) . P r oc e s si n g e v e nt s a t t h e s t a rt o f e v er y

    g a m e l o o p i t e r a t i o n h e l p s t o p r e v e n t l a g .

  • 8/21/2019 The best document in the world!

    46/160

    c a l cu l a t e n e x t p o s it i o n s   u pd at e s t h e g am e s t at e a cc or di n g t o u se r i np u t a s w el l a s a ny

    a c t i v e a n i m a t i o n s ( a p l a y e r w a l k i n g , a n e x p l o s i o n , a c u t s c e n e ) .   render()   f in a lly u p d a te s

    a n d d i s p l a y s t h e s c r e e n .

    C o n s i d e r a g a m e w i t h a m o v i n g l a s e r b o l t :

    1   u s e s t ri c t

    2   u s e w a r ni n gs

    3   u s e SD L

    4   u s e S D L : :E v en t

    5   use SDL::Events

    6   u s e S D L x :: A pp

    7

    8   m y $ ap p = S DL x :: Ap p -> ne w(

    9   widt h => 200,

    10   h ei g ht = > 2 00 ,

    11   t it le = > ' Pe w P ew '

    12   )

    13

    14   my $quit = 0 15

    16   # s ta rt l as er o n t h e l ef t

    17   my $lase r = 0

    18

    19   s u b g e t_ e ve nt s {

    20   m y $ e v en t = S D L :: E ve n t -> n e w( )

    21

    22   SDL::Events::pump_events

    23

    24   while( SDL::Events::poll_event($event) )

    25   {

    26   $ qu it = 1 i f $ ev en t- >t yp e = = S DL _Q UI T

    27   }

  • 8/21/2019 The best document in the world!

    47/160

    28   }

    29

    30   sub calculate_next_positions {

    31   # m ov e t he l as er

    32   $laser++

    33

    34   # if th e l as er go es of f t he sc re en , b ri ng it ba ck

    35   $ la se r = 0 i f $l as er > $ ap p- >w ()

    36   }

    37

    38   s ub r e nd er {

    39   # d r aw t h e b ac k gr ou nd f ir s t

    40   $ ap p- >d ra w_ re ct ( [ 0 , 0 , $ ap p- >w , $ ap p- >h ] , 0 )

    41

    42   # d ra w th e la se r ha lf wa y up t he s cr ee n

    43   $ ap p- >d ra w_ re ct ( [ $ la se r, $ ap p- >h / 2 , 1 0, 2 ] , [ 2 55 , 0 , 0 , 2 55 ] )

    44

    45   $app->update()

    46   }

    47

    48   w h i le ( ! $ q u it )

    49   {

    50   get_events()

    51   calculate_next_positions()

    52   render()

    53   }

    T h i s g a m e l o o p w o r k s v e r y w e l l f o r c o n s o l e s a n d o t h e r d e v i c e s w h e r e y o u k n o w e x a c t l y

    h ow m uc h C PU t i me t he g a me w il l g et f or e ve ry l o op i t er at i on . T ha t h ar d wa r e s t ab il i ty

    i s e as y t o p r ed ic t : e ac h a n im at i on a n d c a l cu l at i on w il l h ap p en a t t he s am e t i me f o r e ac h

    m a c h ine . U n f o r tun a te ly, th is is not   t r u e f o r m o d e r n o p e r a t i n g s y s t e m s a n d g e n e r a l p u r p o s e

    c o m p u tin g h a r d w a r e . C P U s p e e d s a n d w o r k lo a d s v a r y, s o f o r th is g a m e to p la y c o n s is te n tly

    a c r o s s m u l t i p l e m a c h i n e s a n d m y r i a d c o n f i g u r a t i o n s , t h e g a m e l o o p i t s e l f n e e d s t o r e g u l a t e

    its u p d a te s .

  • 8/21/2019 The best document in the world!

    48/160

    O ne w ay t o s o lv e t h is p ro bl e m i s t o r eg ul a te t h e n u m be r o f f r am es p er s ec on d t h e g a m e

    w il l p r o du ce . A frame   i s a c o m p l e t e r e d r a w o f t h e s c r e e n r e p r e s e n t i n g t h e u p d a t e d g a m e

    s t at e . I f e a c h i t e ra t i on o f t h e g a me l o o p d r a ws o n e f r a m e, t h e m o r e f r a m es p e r s e c o nd , t h e

    f as t er t h e g a m e i s r un n in g. I f t he g am e l oo p l i mi t s t h e n um be r o f f r am es p er s e co nd , t h e

    g a m e w i l l p e r f o r m c o n s i s t e n t l y o n a l l m a c h i n e s f a s t e n o u g h t o d r a w t h a t m a n y f r a m e s p e r

    second.

    Y o u c a n s e e t h i s w i t h t h e e x a m p l e p r o g r a m game fixed.pl. W h en r u n w i t h n o a r g u me n ts :

    $   perl game_fixed.pl

    . .. . t he F PS r at e w il l b e e rr at ic . T he l as er s ee ms t o c ha ng e i ts s pe ed r an do ml y. W he n

    r u n w i t h a s in g le a rg um en t , t he g am e s et s a n u pp er b ou nd o n t he n um be r o f f r am es p er

    second:

    $   perl game_fixed.pl 1

    T hi s w il l p re v en t t h e l a s er f r om g oi n g f a s te r t h an 6 0 f r am es p e r s e c on d. W h en r un w it h a

    s e c o n d a r g u m e n t , t h e g a m e w i l l s e t a l o w e r b o u n d o f f r a m e s p e r s e c o n d :

    $   perl game_fixed.pl 1 1

    A t t h i s p o i n t t h e F P S s h o u l d h o l d s t e a d y a t 6 0 f r a m e s p e r s e c o n d .

    1   u s e s t ri c t

    2   u s e w a rn i n gs

    3   u se S DL

    4   use SDL::Event

    5   use SDL::Events

    6   u s e S D Lx : : Ap p

    7

    8   m y $ a pp = S DL x: : Ap p- > ne w(

    9   widt h => 200,

  • 8/21/2019 The best document in the world!

    49/160

    10   h ei g ht = > 2 00 ,

    11   t it le = > ' Pe w P ew '

    12   )

    13

    14   m y ( $ st ar t, $ en d, $ de lt a_ ti me , $ FP S, $ fr am es ) = ( 0 , 0 , 0 , 0 , 0 )

    15

    16   # a im f or a rat e of 6 0 f ra mes p er s eco nd

    17   m y $ fi xe d_ ra te = 6 0

    18

    19   # c o mp en s at e f or t i me s s to r ed i n m ic ro s ec on d s

    20   m y $ fp s_ ch ec k = ( 10 00 / $ f ix ed _r at e )

    21

    22   my $ quit = 0

    23

    24   # st ar t l as er on th e l ef t

    25   my $ laser = 0

    26

    27   s ub g et _e v en ts {

    28   m y $ e ve n t = S D L: : E ve n t- > n ew ( )

    29

    30   SDL::Events::pump_events

    31

    32   w h i le ( S D L: : E ve n t s: : po l l _e v e nt ( $e v e nt ) ) {

    33   $ qu it = 1 i f $ ev en t- >t yp e = = S DL _Q UI T

    34   }

    35   }

    36

    37   sub calculate_next_positions {

    38   $laser++

    39

    40   $ la se r = 0 i f $ la se r > $ ap p- >w

    41   }

    42

    43   s ub r en de r {

    44   # d ra w t he b a ck gr o un d f i rs t

    45   $ ap p- >d ra w_ re ct ( [ 0 , 0 , $ ap p- >w , $ ap p- >h ] , 0 )

    46

    47   # d ra w t he l as er

  • 8/21/2019 The best document in the world!

    50/160

    48   $ ap p- >d ra w_ re ct ( [ $l as er , $ ap p- >h / 2, 10 , 2 ], [ 2 55 , 0 , 0 , 2 55 ] )

    49

    50   # draw the F PS

    51   $ ap p- >d ra w_ gf x_ te xt ( [ 10 , 1 0 ] , [ 25 5, 0, 25 5, 25 5 ] , " FP S: $F PS " )

    52

    53   $app->update()

    54   }

    55

    56   # C a ll ed a t t he e nd o f e ac h f ra me , w he th er w e d ra w o r n ot

    57   sub calculate_fps_at_frame_end

    58   {

    59   # T ic ks a re m ic ro s ec on ds s in c e l oa d t im e

    60   $ e n d = S D L: : g et _ ti c k s( )

    61

    62   # s m oo th t h e f ra me r a te b y a v er ag in g o ve r 1 0 f ra me s

    63   if ( $frames < 10 ) {

    64   $frames++

    65   $ de l ta _t im e + = $ e nd - $ st ar t

    66   }

    67 M M e M e M e M e M M 6M ( $ ] extes

    64   - ;

    65 $delt_tit $start

    66 }

    56 }

    59

    $end = SDL::get_ticks() h avermoot; ;

    3

  • 8/21/2019 The best document in the world!

    51/160

    86   {

    87   # i f d el ta t im e i s g oi ng t oo s lo w f or f ra me c he ck

    88   i f ( $ de lt a_ ti me > $ fp s_ ch ec k ) {

    89

    90   calculate_fps_at_frame_end()

    91

    92   # s ki p r en de r in g a n d c ol l is io n d et e ct io ns

    93   # ( he av y f un ct io ns i n t he g am e l oo p)

    94   next

    95   }

    96   }

    97

    98   calculate_next_positions()

    99   render()100

    101   # a n or m al f r am e w i th r e nd er i ng a ct u al ly p er fo r me d

    102   calculate_fps_at_frame_end()

    103

    104   # i f f ix in g t he u pp er b ou nd s o f t he f ra me r at e

    105   i f ( $A RGV[0 ] ) {

    106

    107   # i f d el ta t im e i s g oi ng t oo f as t c om pa re d t o t he f ra me c he ck

    108   i f ( $ de lt a_ ti me < $ fp s_ ch ec k ) {

    109

    110   # d el ay f or t he d if fe re nc e

    111   S D L :: d e la y ( $ f p s_ c h ec k - $ d el t a _t i m e )

    112   }

    113   }

    114   }

    T hi s m et ho d i s g e ne ra l ly s uf fi ci e nt f or m os t c om pu t er s . T he a ni ma t io n s w il l b e s mo ot h

    e n o u g h t o p r o v i d e t h e s a m e g a m e p l a y e v e n o n m a c h i n e s w i t h d i f f e r e n t h a r d w a r e .

    H ow ev er, t h is m et h od s t il l h a s s o m e s e r io us p r ob le ms . F ir s t, i f a c om pu te r i s t oo s l ow

    t o s u s t a i n a r a t e o f 6 0 F P S , t h e g a m e w i l l s k i p r e n d e r i n g s o m e f r a m e s , l e a d i n g t o s p a r s e

    a n d j i t t e r y a n i m a t i o n . i t w i l l s k i p a l o t o f r e n d e r i n g , a n d t h e a n i m a t i o n w i l l l o o k s p a r s e a n d

  • 8/21/2019 The best document in the world!

    52/160

       jittery. It might be better to set a lower bounds of 30 FPS, though it’s difficult to predict

    t h e b e s t f r a m e r a t e f o r a u s e r .

    T h e w o r st p r ob l e m i s t h at t h i s t e c hn i q ue s t i ll t i e s r e n d er i n g s p e ed t o t h e C P U s p e e d: a v e r y

    f a s t c o m p u t e r w i l l w a s t e C P U c y c l e s d e l a y i n g .

    T o f i x t h e p r o b l e m o f a c o m p u t e r b e i n g c o n s i s t e n t l y t o o f a s t o r t o o s l o w f o r t h e h a r d - c o d e d

    F PS r at e i s t o a dj us t t he F PS r at e a cc or di ng ly. A s lo w C PU m ay l im it i ts el f t o 3 0 F PS ,

    w h i l e a f a s t C P U m i g h t r u n a t 3 0 0 F P S . A l t h o u g h y o u m a y a c h i e v e a c o n s i s t e n t r a t e t h i sw a y ( c o n s i s t e n t f o r a n y o n e p a r t i c u l a r c o m p u t e r ) , t h i s t e c h n i q u e s t i l l p r e s e n t s t h e p r o b l e m

    o f d if f e r in g a n im a tio n s p e e d s b e tw e e n d if f e r e n t c o m p u te r s .

    B e tte r s o lu tio n s a r e a v a ila b le .

    T he p ro bl e m c au se d b y c o up li n g r en d er i ng t o t he C PU s pe ed h as a c on ve ni e nt s ol u ti o n.

    I n s t e a d o f u p d a t i n g o b j e c t p o s i t i o n s b a s e d o n h o w f a s t t h e c o m p u t e r c a n g e t t h r o u g h t h e

    g am e l oo p, d er i ve t he i r p os i ti on s f r om a p hy si c al m od el b as ed o n t h e p as s ag e o f t i me .

    O bj e ct s m ov in g a cc or d in g t o r e al w or ld t i me w il l h av e c on s is t en t b eh av i or a t a l l C PU

    s p e e d s a n d s m o o th in te r p o la tio n b e tw e e n f r a m e s .   SDLx::App p r o v id e s th is b e h a v io r th r o u g h

    m o v e m e n t a n d s h o w h a n d le r s .

    C o n s i d e r a s i m p l e p h y s i c s m o d e l f o r t h e l a s e r h a s a c o n s i s t e n t h o r i z o n t a l v e l o c i t y i n p i x e l s

    p e r t i m e s t e p a t t h e w i n d o w ’ s m i d - p o i n t :

    X = V el oc it y * t im e s te p,

    Y = 100

  • 8/21/2019 The best document in the world!

    53/160

    A s s u m i n g a v e l o c i t y o f 1 0 , t h e l a s e r w i l l p a s s t h r o u g h t h e c o o r d i n a t e s :

    0 , 1 00

    1 0, 1 00

    2 0, 1 00

    3 0, 1 00

    ...

    2 0 0, 1 00

    N ot e t ha t t h e s p e ed o f p ro ce s si n g t h e g am e l oo p n o l o ng er m at t er s . T he p os i ti on o f t h e

    l a s e r d e p e n d s i n s t e a d o n t h e p a s s a g e o f r e a l t i m e .

    T h e b i g g e s t p r o b l e m w i t h t h i s a p p r o a c h i s t h e r e q u i r e d b o o k k e e p i n g f o r t h e m a n y o b j e c t sa n d c a l l b ac k s. T h e i m p l e me n t at i o n o f s u ch c o mp l e x m o d e ls i s n o n- t r iv i a l; s e e t h e l e ng t h y

    d i s c u s s i o n i n t h e d o c u m e n t a t i o n o f t h e   SDLx::Controller   module.

    SDLx::App   u s in g t h e   SDLx::Controller   m o du l e p r o vi d e c a ll b a ck s t o h a n dl e b o t h a s p ec t s

    o f t hi s t yp e o f g am e l oo p . O ne i s t h e t h e m ov em en t h an dl e r, w hi c h i s a c a ll b ac k w he r e

    c a l cu l a ti o n s o f t h e n e x t s t e p f o r e a ch r e l ev a nt d a ta p o i nt i s c a l cu l at e d . I n t h e a b o v e e x a m pl e

    t h e m o v e m e n t h a n d l e r w o u l d c a l c u l a t e t h e   X   and   Y   v a l u e s , f o r e a c h t i m e s t e p b e t w e e n t h e

    f r a m e s o f a n im a tio n s .

    W he n w e a re r ea dy t o r en de r t he f ra me i t i s h an dl ed b y t he s ho w h an dl er. I n t he a bo ve

    e x a m p l e t h a t w o u l d m e a n t h e s h o w h a n d l e r w o u l d p r i n t o r r e n d e r t h e   X, Y   values.

    T h i s v e r s i o n o f t h e l a s e r e x a m p l e d e m o n s t r a t e s t h e u s e o f m o v e m e n t , s h o w h a n d l e r s , a n d a

    s i mp l e p h y s i cs m o de l . T h i s e x a m pl e a l s o s h o w s h o w   SDLx::App   c a n d o m o r e o f t h e w o r k ,

    e v e n p r o v i d i n g t h e e n t i r e g a m e l o o p :

    1   u s e s t ri c t

    2   u s e w a rn i n gs

    3   u se S DL

  • 8/21/2019 The best document in the world!

    54/160

    4   use SDL::Event

    5   u s e S D Lx : : Ap p

    6

    7   m y $ a pp = S DL x: : Ap p- > ne w(

    8   width => 2 00,

    9   h ei gh t = > 2 0 0,

    10   t it le = > ' Pe w P ew '

    11   )

    12

    13   my $laser = 0

    14   m y $ ve lo ci ty = 1 0

    15

    16   $app->add_event_handler(   \& q ui t _ ev e nt )

    17

    18   # t e ll a p p t o h an dl e t he a p pr op ri at e t im es t o

    19   # c a ll b o th r e nd er in g a nd p h ys ic s c al c ul at i on

    20

    21   $app->add_move_handler(   \&calculate_laser )22   $app->add_show_handler(   \& r e nd e r _l a se r ) 23

    24   $app->run()

    25

    26   s ub q ui t_ e ve nt {

    27   my $event = shift

    28   m y $ c on tr ol l er = s hi ft

    29

    30   $ c on t r ol l er - > st o p i f $ e v en t - >t y pe = = S D L _Q U IT

    31   }

    32

    33   s u b c a lc u l at e _l a s er {

    34

    35   # T he s te p is t he d if fe re nc e in T im e ca lc ul at ed f or t he n ex t ju mp

    36   my ( $step, $app , $t ) = @_

    37   $ la se r + = $ v el oc i ty * $ st ep

    38   $ la se r = 0 i f $ la se r > $ ap p- >w

    39   }

    40

    41   s ub r en de r _l as e r {

  • 8/21/2019 The best document in the world!

    55/160

    42   my ( $delta , $ap p ) = @_

    43

    44   # Th e d el ta ca n b e u se d t o r en de r b lu rr ed fr am es

    45

    46   # d ra w t he b ac kg r ou nd f ir st

    47   $ ap p- >d ra w_ re ct ( [ 0, 0, $a pp -> w, $a pp -> h ] , 0 )

    48

    49   # d ra w t he l as er

    50   $ ap p- >d ra w_ re ct ( [ $ la se r, $ ap p- >h / 2 , 1 0, 2 ] , [ 2 5 5, 0 , 0 , 25 5 ] )

    51   $app->update()

    52   }

    T o l e a r n m o r e a b o u t t h i s t o p i c p l e a s e , s e e a n e x c e l l e n t b l o g p o s t b y GafferOnGames.com:

    HTTP://GafferOnGames.Com/game-physics/fix-your-timestep.

    http://http//GafferOnGames.Com/game-physics/fix-your-timestep

  • 8/21/2019 The best document in the world!

    56/160

  • 8/21/2019 The best document in the world!

    57/160

    P o n g i s o n e o f t h e f i r st p o p ul a r v i d e o g a me s i n t h e w o r l d. A l la n A l c o r n c r e at e d i t f o r A t a r i,

    I n c. I t s r e l ea se i n 1 97 2 w as b ot h A ta ri ’s f i rs t g am e e ve r a n d t h e s p ar k w hi c h b e g an t h e

    v id e o g a m e in d u s tr y .

    P o n g s i m u l a t e s a t a b l e t e n n i s m a t c h (“p i n g p o n g”) . E ac h p l ay er c on tr o ls a p a dd le w hi c h

    m o v e s v e r t i c a l l y o n t h e s c r e e n t o h i t a b a l l b o u n c i n g b a c k a n d f o r t h b e t w e e n t h e p l a y e r s .

    Y o u e a r n a p o i n t i f y o u r o p p o n e n t i s u n a b l e t o r e t u r n t h e b a l l t o y o u r s i d e o f t h e s c r e e n .

    Y o u c a n r e c r e a t e P o n g y o u r s e l f w i t h P e r l a n d S D L .

  • 8/21/2019 The best document in the world!

    58/160

    S ta rt b y m ak i ng a s im pl e s cr e en f o r P o ng . O pe n a f i le i n y ou r f av ou r it e t e xt e di t or a nd

    type:

    1   #!/usr/bin/perl

    2   u s e s t ri c t

    3   u s e w a r ni n gs

    4

    5   u s e SD L

    6   u s e S D L x :: A pp

    7

    8   # c re at e t he m ai n s c re en

    9   m y $ ap p = S DL x :: Ap p -> ne w(

    10   width => 500,

    11   height => 500,

    12   title => 'My Pong Clone!',

    13   d t => 0.02,

    14   e xi t _o n_ q ui t = > 1 ,

    15   )

    16

    17   # l et 's r ol l!

    18   $app->run

    S a v e t h i s f i l e a s pong.pl   a n d r u n i t b y t y p i n g o n t h e c o m m a n d l i n e :

    perl pong.pl

    Y o u s h o u ld s e e a 5 0 0 x 5 0 0 b la c k w in d o w e n title d “ My Pong Clone!”. T h e o n ly n e w f e a t u re

    y ou m ig ht n ot h av e s ee n b ef or e i s t he   dt   p ar a me t er t o t h e   SDLx::App   co n s tr u c to r. T h is

    r e p r e s e n t s t h e l e n g t h , i n s e c o n d s , o f a m o v e m e n t s t e p a s m a n a g e d b y a n   SDLx::Controller

    o bj ec t . B ec au se t h e   SDLx::App   o bj ec t i s a ls o a n   SDLx::Controller   o bj e ct , i t c a n h an dl e

    S D L Q U IT   events.

  • 8/21/2019 The best document in the world!

    59/160

    Game Loop Granularity

    T h e g a m e l o o p r u n s u s i n g a p r o c e s s t h a t p e r f o r m s c a l c u l u s o n t h e t i m e p r o g r e s s i o n .

    T o d o t h i s i t u s e s t h e   dt   p a r a m e te r o f   SDLx::App.   dt   b y d e f a u l t i s s e t t o   0.01   granu-

    l a r it y. I n s i mp l e t e r ms   dt   d e t e r m i n e s h o w m a n y s m a l l s t e p s i n m o v e m e n t ( c a l l s t o

    t h e m o v e h a n d l e r ) s h o u l d b e m a d e p e r e a c h r e n d e r ( c a l l t o t h e s h o w h a n d l e r ) .

    W h a t i s i m p o r t a n t t o r e m e m b e r i s t h a t t h e   dt   g r a n u l a r i t y m u s t n e v e r b e s e t s o b i g

    t h at i t f a i ls t o c a pt u r e c o l l i si o n s a n d i n t er a c ti o n b e tw e en m o vi n g o b j e c ts . T h i s c a n

    e s p e c i a l l y h a p p e n w i t h f a s t m o v i n g o b j e c t s s u c h a s b u l l e t s , b u t t h e p r i n c i p l e a p p l i e s

    t o m an y s it u at i on . O n t h e o t h er h an d h av in g a t o o f i n e g r an ul a ri t y t a x es t he C PU

    resources.

    T h er e a r e t h r ee m a in g a me o b j ec t s i n P o ng : t w o p l a y er p a dd l e s a n d t h e b o u nc i ng b a l l. P a d-

    d l es a r e r e c ta n gl e s m o vi n g w h i ch m o ve v e rt i c al l y. T h ey ’ r e e a s y t o r e p re s e nt w i t h S D L x : :-

    R e ct o b j ec t s . F i r st , p u t   SDLx::Rect   in y o u r m o d u le ’ s d e c la r a tio n s :

    1   u s e SD L

    2   u s e S D L x :: A pp

    3   use SDLx::Rect;

    N e xt , a d d a h a sh r e fe r e nc e t o s t o re t h e f i r s t p l a y e r ’s p a dd l e . U s i ng a h a s h r e f e re n ce a l l ow s

    t h e p o s s i bi l i ty o f a d di n g m o r e i n fo r ma t i on l a t er. I n a m o re c o mp l ex g a me , c o ns i d er u s i ng

    a n a c t u a l o b j e c t w h i c h contains   an   SDLx::Rect. F o r n o w, t h i s w i l l s u f f ic e :

    1   my $player1 = {

    2   paddle => SDLx::Rect->new( 10, $app->h / 2, 10, 40 ),

    3   };

    4

  • 8/21/2019 The best document in the world!

    60/160

    5   # l et 's r ol l!

    6   $app->run

    T h i s c r e a t e s a 1 0 x 4 0 p a d d l e r e c t f o r t h e f i r s t p l a y e r o n t h e l e f t s i d e o f t h e s c r e e n ( x = 10)

    a n d s o m e w h a t i n t h e c e n t e r ( y = $app->h / 2) . T h e s e c o n d p l a y er ’ s p a d d le i s s i mi l a r:

    my $player2 = {

    paddle => SDLx::Rect->new( $app->w - 20, $app->h / 2, 10, 40),

    };

    # l et 's r ol l!

    $app->run

    T h e s e c o n d p a d d l e n e e d s t o a p p e a r o n t h e r i g h t s i d e o f t h e s c r e e n , s o i t s   x   p o si t i on i s t h e

    s cr ee n ’s w id th m in us 2 0. A s t h e p ad dl e h a s a w id t h o f 1 0 a n d t h e   x   p o s i t i o n r e f e r s t o t h e

    r e c t ’ s t o p - l e f t c o r n e r , t h e p a d d l e h a s a 1 0 p i x e l m a r g i n f r o m t h e r i g h t e d g e o f t h e s c r e e n .

    F i n a l l y , t h e b o u n c i n g b a l l i s a 1 0 x 1 0 r e c t i n t h e m i d d l e o f t h e s c r e e n :

    my $ball = {

    rect => SDLx::Rect->new( $app->w / 2, $app->h / 2, 10, 10 ),

    }; # let's roll! $app-run

    J u s t l i k e t h e o r i g i n a l P o n g , t h i s b a l l i s s q u a r e .

    W i t h t h e g a m e o b j e c t s c r e a t e d , a d d a s h o w h a n d l e r t o r e n d e r t h e m t o t h e s c r e e n :

    1   $app-add_show_handler(>

    2   sub {

    3   # first, clear the screen

    4   $app->draw_rect( [ 0, 0, $app->w, $app->h ], 0x000000FF );

    5

  • 8/21/2019 The best document in the world!

    61/160

    6   # then render the ball

    7   $app->draw_rect( $ball->{rect}, 0xFF0000FF );

    8

    9   # ... and each paddle

    10   $app->draw_rect( $player1->{paddle}, 0xFF0000FF );

    11   $app->draw_rect( $player2->{paddle}, 0xFF0000FF );

    12

    13   # finally, update the screen

    14   $app->update;

    15   }

    16   );

    17

    18   # l et 's r ol l!

    19   $app->run

    T hi s a p pr oa ch i s r at h er s i mp l e. T he c od e c l ea r s t h e s c re en b y p ai n ti n g a b la c k r ec t an gl e

    t he s iz e o f t h e s c r ee n, t he n p ai n ti n g o p a qu e r ed (0xFF0000FF) r e ct a n gl e s i n e a c h o b je c t ’s

    position.

    T h e r e s u l t c a n b e s e e n o n t h e s c r e e n s h o t :

    I t ’s t i m e t o l e t t h e p l a ye r m o ve t h e l e f t p a dd l e ! R e me m be r t h a t m o ti o n i s m e re l y c h a n gi n g

    a n o b j e c t’s p o s it i o n w i t h r e s p e ct t o t i me . I f t h i s m o t i on i s , i n t h e g a m e , a m a gi c a l t e l e po r -

    t at i on , y ou c an c ha ng e t he ( x , y ) c o or di na t es a nd b e d on e w it h i t . I f t h e m o t io n n ee ds t o

    r e p r e s e n t s o m e s o r t o f r e a l i s t i c p h y s i c s , t h e o b j e c t n e e d s t o m o v e a t a n u n d e r s t o o d s p e e d .

    P on g p ad dl e s h a v e a c o ns ta n t s p e ed , s o t he r e’s n o n ee d t o m od el a cc e le r at i on . A ls o, a s

    p ad dl es m ov e o nl y v er t ic a ll y, t he g am e o nl y n ee ds t o t ra c k v er t ic al v el oc i ty. A dd a   v y

    e l e m e n t t o e a c h p a d d l e s t r u c t u r e :

    m y $ pl ay er 1 = {

    p ad dl e = > S DL x: :R ec t- >n ew ( 1 0, $ ap p- >h / 2 , 1 0, 4 0 ) ,

  • 8/21/2019 The best document in the world!

    62/160

    F i g u r e 6 . 1 : F i r s t v i e w o f o u r P o n g c l o n e

    v_y => 0,

    }

    N ow w ha t? H ow d o es t h is n e w a t t ri bu t e h e l p m od if y t he p o si t io n o f a p a dd le ? Ve l oc it yr e p r e s en ts th e  displacement   h ow m uc h d is p la ce me nt h ap pe n s i n a u ni t o f t im e, a s i n 2 0

    k m/ h o r 4 m /s . I n t h i s P o n g c l on e, t h e u n it o f t i m e i s t he a p p’s   dt. T h e v e l o c i t y o f a p a d d l e

    is   v y   p i x e l s p e r   dt. H er e i s w he re t h e m o t io n h an dl e rs c om e i n h an dy :

    # handles the player's paddle movement

    $app->add_move_handler( sub {

    my ( $step, $app ) = @_;

    my $paddle = $player1->{paddle};

    my $v_y = $player1->{v_y};

    $paddle->y( $paddle->y ( $v_y * $step ) );

    });

  • 8/21/2019 The best document in the world!

    63/160

    I f y o u r e c a l l p r ev i o us c h ap t e rs , t h e c o de s h ou l d b e s t r ai g h tf o r wa r d. W h en   v y   i s 0 a t a n y

    g iv en r un c yc le , t h e p ad dl e w on ’t c ha ng e i t s   y   p os i ti o n. I f, h ow ev e r, t he r e i s a v e rt i ca l

    v e lo c i ty, t h e c o de u p da t e s t h e   y   p o si t io n b as ed o n h ow m uc h o f t he e xp ec t ed c yc l e t i me( th e a p p ’ s   dt) h a s p a s s e d . A v a l u e o f 1 i n   $step   i n d i c a t e s a f u l l c y c l e h a s o c c u r r e d , s o t h a t

    $v y * $step   i s t h e s a m e a s   $v y * 1, w h i c h s i m p l i f i e s t o   $v y   – t h e d e s i r e d s p e e d f o r o n e

    c y c le . I f t h e h a n d l er g e t s c a l l e d m o r e f r e q u en t l y, t h e p a d d l e w i l l m o ve a r e l at i v el y s h o rt e r

    amount.

    T h e s e c o n d p l a y e r ’ s p a d d l e w i l l u s e t h e s a m e m o t i o n m e c h a n i c s , s o i t w o n ’ t h u r t t o p r e p a r ef o r i t s m o t i o n :

    1   m y $ pl ay er 2 = {

    2   p ad dl e = > SD Lx :: Re ct -> ne w( $ a pp -> w - 2 0, $ a pp -> h / 2 , 1 0, 4 0 ),

    3   v_y => 0,

    4   }

    A n d a d d a n o t h e r m o t i o n h a n d l e r , j u s t l i k e o u r p l a y e r ’ s :

    1   # handles AI's paddle movement2   $app->add_move_handler( sub {

    3   my ( $step, $app ) = @_;

    4   my $paddle = $player2->{paddle};

    5   my $v_y = $player2->{v_y};

    6

    7   $paddle->y( $paddle->y ( $v_y * $step ) );

    8   });

    F or t h e s a ke o f s im pl i ci t y o f e xp l an at i on , t hi s c od e h as r ep et i ti on a r ea l p r og ra m

    w ou l d n o t w an t . T hi s r e pe ti t io n c ou l d g o a wa y i n s ev er a l w a y s. Yo u c ou l d u s e a na r ra y t o h o ld a l l m o v i ng e l e me n ts . Yo u c o u l d u s e a h e l pe r f u n c t io n t o c r ea t e a n e w

  • 8/21/2019 The best document in the world!

    64/160

    c l os u r e f o r e a ch p a dd l e . Yo u c o ul d t u r n t h e g a me o b je c t h a s h r e fe r e nc e s i n t o r e al

    o b j e c t s a n d a d d a   move()   or   update position()   method.

    P a d d le v e lo c ity   v y   h as a d e fa ul t v al ue o f z e ro , s o p ad dl e s b e g in b y n o t m ov i ng . T ha t ’s

    g o od , u n ti l t h e p l a y e r w a n t s t o m o ve t h e p a d d l e. To d i v in e t h e p l a y e r ’s i n t en t , t h e p r o gr a m

    m us t b in d t he u p a nd d ow n a rr ow k ey s o f t h e k e y bo ar d t o m an ip ul a te t h e p os i ti v e a nd

    n eg at i ve v el o ci t y o f t h e p ad dl e t h ro ug h a n e v en t h oo k . T hi s m ea n s l oa d in g t h e S DL :: -

    E v e n ts m o d u le :

    1   u s e S DL

    2   use SDL::Events;

    3   u s e S D L x: : Ap p

    4   u s e S D L x: : Re c t

    . . . a n d c r e a ti n g a n e v en t h o o k :

    1   # h an dl e s k e y bo ar d e ve nt s

    2   $app-add_event_handler(>3   sub {

    4   my ( $event, $app ) = @_;

    5

    6   # user pressing a key

    7   if ( $event->type == SDL_KEYDOWN ) {

    8

    9   # up arrow key means going up (negative velocity)

    10   if ( $event->key_sym == SDLK_UP ) {

    11   $player1->{v_y} = -2;

    12   }

    13

    14   # down arrow key means going down (positive velocity)

    15   elsif ( $event->key_sym == SDLK_DOWN ) {

  • 8/21/2019 The best document in the world!

    65/160

    16   $player1->{v_y} = 2;

    17   }

    18   }

    19   # user releasing a key

    20   elsif ( $event->type == SDL_KEYUP ) {

    21

    22   # up or down arrow keys released, stop the paddle

    23   if (

    24   $event->key_sym == SDLK_UP

    25   or $event->key_sym == SDLK_DOWN

    26   ) {

    27   $player1->{v_y} = 0;

    28   }

    29   }30   }

    31   )

    A g ai n , t h e re ’s n o t hi n g n e w. W h en e ve r t h e u s er p r e ss e s t h e u p a r ro w k e y, t h e p a d d l e s h o u l d

    m ov e u p . K ee p i n m in d t h at t he o ri gi n p oi nt o f 0 , 0 i n S DL i s t he t op -l ef t c or ne r, s o a

    negative   v y   w i l l d e cr e as e t h e p a dd l e ’s   y   a nd s en d i t up   t he s c re en . S i mi l ar l y, a dd in g a

    p o s i t i v e v a l u e t o   v y   w h e n e v e r t h e u s e r p r e s s e s t h e d o w n a r r o w k e y w i l l m o v e t h e p a d d l e

    d o wn . W h en t h e u s e r r e le a s es e i t he r a r r ow k e y, a s s ig n i ng z e ro t o   v y   s t o p s t h e m o t i o n .

    T h e b a l l ’ s m o v e m e n t i s s i m i l a r t o t h a t o f e i t h e r p a d d l e , e x c e p t t h a t i t a l s o h a s a h o r i z o n t a l

    v e lo c ity c o m p o n e n t o f   v x. A dd t h at t o t h e b al l s t ru ct u re :

    1   my $ball = {

    2   r ec t = > S DL x: :R ec t- >n ew ( $ ap p- >w / 2 , $ ap p- >h / 2 , 1 0, 1 0 ) ,

    3   v_x => -2.7,

    4   v_y => 1.8,

    5   }

  • 8/21/2019 The best document in the world!

    66/160

    T h e b a l l w i l l h a ve a n i n i ti a l v e l o c it y o f - 2 . 7 h o r iz o nt a l ly a n d 1 . 8 v e r ti c a ll y. J u s t a s a n e ga -

    t i v e v e r t i c a l v e l o c i t y m o v e s t h e o b j e c t u p , a n e g a t i v e h o r i z o n t a l v e l o c i t y m o v e s i t t o w a r d s

    t he l ef t s id e o f t he s cr ee n. T he b al l a ls o n ee ds a m ot io n h an dl er t o u pd at e i ts p os it io na c c o r d i n g t o i t s v e l o c i t y :

    1   h a nd le s t h e b a ll m o v em en t

    2   a p p -> a dd _ m ov e _ ha n dl e r ( s u b {

    3   my ( $step , $app ) = @_

    4   my $b all_r ect = $ball- >{rect }

    5

    6   $ b al l _ re c t -> x ( $ b al l _r e c t- > x + ( $ b al l -> { v _x } * $ s t e p) )

    7   $ b al l _ re c t -> y ( $ b al l _r e c t- > y + ( $ b al l -> { v _y } * $ s t e p) )

    8   )

    A l l o f t h e s e m o t i o n h a n d l e r s l o o k s i m i l a r s o f a r , b u t i f y o u ’ r e p a y i n g c l o s e a t t e n t i o n , y o u

    c a n p r o b ab l y s p o t a b u g c a u s ed b y m i s s i ng c o de . T ry r u nn i n g t h e g a m e . Yo u ’ ll s e e t h e b a ll

    g o i n g , g o i n g , a n d g o n e !

    T hi s h an dl e r n e e ds t o c o nf in e t he b al l t o t he s cr ee n . W he ne ve r t he b al l r ea ch es a t op o r

    b o tt o m e d g e o f t h e s c r e e n, i t n e ed s t o b o un c e. T h at ’s e a sy e n ou g h t o a d d:

    1   # h a n dl e s t he b a l l m ov em e nt

    2   $ a p p- > a dd _ mo v e _h a n dl e r( s u b {

    3   my ( $st ep, $a pp ) = @_4   my $ball _rect = $bal l->{re ct}

    5

    6   $ b a ll _ re c t -> x ( $ b al l _ re c t -> x + ( $ ba l l -> { v_ x } * $ st e p) )

    7   $ b a ll _ re c t -> y ( $ b al l _ re c t -> y + ( $ ba l l -> { v_ y } * $ st e p) )

    8

    9   # collision to the bottom of the screen

    10   if ( $ball_rect->bottom >= $app->h ) {

    11   $ball_rect->bottom( $app->h );

    12   $ball->{v_y} *= -1;

    13   }

    14

    15   # collision to the top of the screen

    16   elsif ( $ball_rect->top

  • 8/21/2019 The best document in the world!

    67/160

    17   $ball_rect->top( 0 );

    18   $ball->{v_y} *= -1;

    19   }

    20   })

    I f t h e n e w y ( "bottom"   or   "top") v a l u e w o u l d t a k e t h e b a l l o f f t h e s c r e e n i n p a r t o r i n w h o l e ,

    t h e h a n d l e r u p d a t e s t h e b a l l ’ s p o s i t i o n w i t h t h e f u r t h e s t p o s i t i o n p o s s i b l e w h i l e r e m a i n i n g

    o n t h e s c r e e n , s o t h a t t h e b a l l w i l l o n l y e v e r touch   t h a t e d g e . T h e h a n d l er a l s o r e v e rs e s   y y

    s o t h a t t h e b a l l w i l l b o u n c e b a c k o n t o t h e s c r e e n g o i n g t h e o p p o s i t e d i r e c t i o n a t t h e s a m e

    speed.

    T h a t f i x e s o n e b u g , b u t w h a t s h o u l d h a p p e n w h e n t h e b a l l h i t s t h e l e f t o r r i g h t e d g e s o f t h e

    s c re e n ? A c co r d in g t o t h e r u l e s o f P o ng , t h i s m e an s t h e p l a ye r o n t h e o p p o si t e s i d e s c or e d

    a p oi nt , a nd t he b al l s ho ul d g o b ac k t o t he c en te r o f t he s cr ee n. S ta rt b y a dd in g a   score

    a t t r i b u t e f o r e a c h p l a y e r :

    1   m y $ pl ay er 1 = {

    2   p ad dl e => S DL x: :R ec t- >n ew ( 10 , $a pp -> h / 2, 1 0, 4 0) ,

    3   v_y => 0,

    4   score => 0,

    5   }

    6

    7   m y $ pl ay er 2 = {

    8   p ad dl e = > SD Lx :: Re ct -> ne w( $ a pp -> w - 2 0, $ a pp -> h / 2 , 1 0, 4 0 ),

    9   v_y => 0,

    10   score => 0,

    11   }

    T he n u pd a te t he b a ll ’s m ot i on h an dl er t o h an dl e t h e o ut o f b ou nd s c on di t io n f o r t h e l e ft

    a n d r i g h t b o r d e r s :

    1   # h a n dl e s t he b a l l m ov em e nt

    2   $ a p p- > a dd _ mo v e _h a n dl e r( s u b {

  • 8/21/2019 The best document in the world!

    68/160

    3   my ( $st ep, $a pp ) = @_

    4   my $ball _rect = $bal l->{re ct}

    5

    6   $ b a ll _ re c t -> x ( $ b al l _ re c t -> x + ( $ ba l l -> { v_ x } * $ st e p) )

    7   $ b a ll _ re c t -> y ( $ b al l _ re c t -> y + ( $ ba l l -> { v_ y } * $ st e p) )

    8

    9   # c ol li si on t o th e bo tt om o f th e sc re en

    10   i f ( $b al l_ re ct -> bo tt om > = $a pp -> h ) {

    11   $ball_rect->bottom( $app->h )

    12   $ b al l - >{ v _ y} * = - 1

    13   }

    14

    15   # c ol li si on t o th e to p of t he s cr ee n

    16   e ls if ( $ ba ll _r ec t- >t op t op ( 0 )

    18   $ b al l - >{ v _ y} * = - 1

    19   }

    20

    21   # collision to the right: player 1 score!

    22   elsif ( $ball_rect->right >= $app->w ) {

    23   $player1->{score}++;

    24   reset_game();

    25   return;

    26   }

    27

    28   # collision to the left: player 2 score!

    29   elsif ( $ball_rect->left {score}++;

    31   reset_game();

    32   return;

    33   }

    34   })

    I f t h e b a l l h i t s t h e r i g h t e d g e o f t h e s c r e e n ( t h e a p p ’ s w i d t h ) , w e i n c r e a s e p l a y e r 1 ’ s s c o r e ,

    call   r e s e t g a m e( ), a nd r e tu r n w i t ho ut u pd at i ng t he b al l ’s p os i ti o n. I f t h e b a l l h it s t he l e ft

    e d g e o f t h e s c r e e n , d o t h e s a m e f o r p l a y e r 2 .

  • 8/21/2019 The best document in the world!

    69/160

    The   r e s e t g a m e( )   f u n c t i o n m u s t r e t u r n t h e b a l l t o t h e c e n t e r o f t h e s c r e e n :

    1   sub reset_game {

    2   $ball->{rect}->x( $app->w / 2 );

    3   $ball->{rect}->y( $app->h / 2 );

    4   }

    T he g a me ’s e xi s ti ng c ol l is i on d et e ct i on i s v er y s i mp le b ec a us e t h e p ad dl e s a nd b a ll c an

    o nl y c ol l id e w it h t he f i xe d e dg es o f t h e s c r ee n