LinuxÒÁµéÔ°ÂÛ̳'s Archiver

Edward_chen ·¢±íÓÚ 2004-9-6 16:38

ÓÐûÓÐÓÃsdl±à³ÌµÄ¸ßÊÖѽ£¿£¿

ÓÐûÓйýSDL±à³ÌµÄ¸ßÊÖѽ£¿£¿

lanche ·¢±íÓÚ 2004-9-7 14:28

¸ßÊÖ? ÎÒ»¹Ã»¼û¹ý¡£
.P R9v%i i L
KK ] m+P'l ÍøÉϺܶàSDLÓÎÏ·µÄ(´ó²¿·ÖÊÇÀÏÍâµÄ£¬ËùÒÔ±ðÀÏÈÏΪÖйúÈË´ÏÃ÷)£¬ÄÇЩԴÂëºÜÖµµÃѧϰ¡£

Edward_chen ·¢±íÓÚ 2004-9-8 10:37

ºÇºÇ£¬ÎÒÒ²ºÜÏëѧ£¬¿ÉϧÎÒÓ¢ÎÄÌ«²î£¬
'gFwf!ZJ :( +N Q*xB \A
¶øÇÒ¹ØÓÚSDLµÄÖÐÎÄÊé¼®£¨°üÀ¨·­Òë°æ£©¶¼ºÜÉÙ£¬¼¸ºõûÓУ¬
}(|it7b ÖÁÉÙµ½Ä¿Ç°ÎªÖ¹£¬ÎÒ»¹Ã»¼û¹ý¡£1swb1[*\ B
µ«ÔÚ¹úÍ⣬ÕâÀàÊéµ½ÊÇÓв»ÉÙ¡£fW6I1r MtV-fb4O,u s
:(

lanche ·¢±íÓÚ 2004-9-8 21:20

µ½SDLÖ÷Õ¾[url]www.libsdl.org[/url]È¥¿´¿´£¬ÓкöàdemoÔ´Â룬¿ÉÒÔѧµ½²»ÉÙ¶«Î÷¡£

Edward_chen ·¢±íÓÚ 2004-9-9 15:44

ºÃ,È¥¿´¿´

Edward_chen ·¢±íÓÚ 2004-9-13 12:12

¿´ÁË£¬³­ÁËÒ»¸öС³ÌÐòÊǹØÓÚSDL£¦OpenGLµÄ£¬ÎÒ¾õµÃºÜ²»´í

Example 2-8. SDL and OpenGL
p Fh6F(x Q
S`,H y M!v6d wA.y%K/|%~ /*d}Pw2s!j.uAE
* SDL OpenGL Tutorial.Em6JWS3y
* (c) Michael Vance, 2000?Z%|&g8G
* [email]briareos@lokigames.com[/email]
#| M:A*L Eo1cGli7m *
p0s/XW+W6BT * Distributed under terms of the LGPL.
T.g9zw"c L)U(K0m */#\%mSxut V4U~

pm1p?*rmOGL {p ~0P #include <SDL/SDL.h>&Y+z]}qwi)m*?-Do
#include <GL/gl.h>
@!}1k|SL/qQ X #include <GL/glu.h>
:[&zO3Q2H4Y /hw7qdC,L!_W#N
#include <stdio.h>
3sQ:|JeoV #include <stdlib.h>
C,B"vLPr
7a1K?%lv`4G static GLboolean should_rotate = GL_TRUE;d7kmN1Z;CtO"}B

{(Q4XZT^Tn)D static void quit_tutorial( int code )5~:^-d*`'q"{ NY
{
*}5R7_ vaEO     /*
V;I!?8v\&cJ1`&n      * Quit SDL so we can release the fullscreen3iV.A#]9_$f^
     * mode and restore the previous video settings,
/rU:j:a d M%JVF      * etc. v@*NUIV+Y9J
     */.t7\.y+|%vTO'Y
    SDL_Quit( );
v6\'j {P)B7c C6Q*B 9JK[|e8uZ2Z
    /* Exit program. */PUZJ)f-B{
    exit( code );
^,q]:cwY }
w)\1f PGUG
o'Nu.x"J5h static void handle_key_down( SDL_keysym* keysym )
_Y,|-n0e"\ {Q9Iihnt+c(Us

[3dr(o&M     /*
b]8D5i|2a5{%t~      * We're only interested if 'Esc' has
/\,s N] TE      * been presssed.
`n9S \ iX"\A`(a      *
,Pb w-Z:BgH'my      * EXERCISE:
O6K,^%mbT/\ tj      * Handle the arrow keys and have that change the
)J)w't/hA+a      * viewing position/angle.
h.FFTI!u(w0U%Ib0T      */$\` lt,Z^
    switch( keysym->sym ) {
Ab9z m-B-v!I(nx0@"F#C     case SDLK_ESCAPE:v+h:CF+j;k,k&Eu;p
        quit_tutorial( 0 );n,x z.x h`xc J
        break;I`df Gd,WgJ,_
    case SDLK_SPACE:*ak k8O/\%a c/h
        should_rotate = !should_rotate;(D7w+^"wsa
        break;+?j-cg8j1\
    default:I[`]I ^S"Y]`'}
        break;-Yr2V%\4lg3`
    }0V uUbS
9SX5uy0M3}
}] `O-@ c$b
XMt:nt,le
static void process_events( void )
Ul` D5aYQ*i)P5x5[1Z {
*R,xvrE$b     /* Our SDL event placeholder. */ O eFp[n8S(HZM
    SDL_Event event;
$_]s+R*v'j+T4Q"y l
O |M$G-B)|5pJ     /* Grab all the events off the queue. */;GGg H!o
    while( SDL_PollEvent( &event ) ) {
n |+} ij!mi+f!I
oz/Kn0h#FS         switch( event.type ) { ?G\%UKr q}-q9t9l uI
        case SDL_KEYDOWN:
0ACN]xgI"o             /* Handle key presses. */
vn8t)Z$C_Uu             handle_key_down( &event.key.keysym );4LC;avGJ
            break;
^k+Y2Af9v+Ix         case SDL_QUIT: Bo`!q%yA.j{ cT
            /* Handle quit requests (like Ctrl-c). */MLo9Y L'B3b!BD
            quit_tutorial( 0 );
*H"Jz#OLu]Q8?E"C5`w             break;6th0OY5b Hy)pfy-jF%G
        }
#T8N\ J/D!^'{W BCYV4kL
    }
)z+eQp*`5} i"@| )Xuo!{ C|
}
UF/ity1O8c %zP WQ,V
static void draw_screen( void )5i"Cr ]K{9P
{U$b NzZ_
    /* Our angle of rotation. */5?H h0kfd-D
    static float angle = 0.0f;3JxvB \ UhH'M
4YaNP"au
    /**R/WXXNa-J*@
     * EXERCISE:g:pcU8uD1l
     * Replace this awful mess with vertex'_/{u\$yh
     * arrays and a call to glDrawElements.1\*D#Ht2V.]2V
     *
s0Pi]B&Z?      * EXERCISE:
s N!mu-Z'A      * After completing the above, change
Z}"^+`"O+QM%m7Bj:yU      * it to use compiled vertex arrays.
1`9C.J+iq Z2P f      *
a$J@7`I0xf      * EXERCISE:,}$sIn [%Y0Q/C!V
     * Verify my windings are correct here ;).,Y:B6t:G/m"@}
     */+bO6l/r:J-ewd4J
    static GLfloat v0[] = { -1.0f, -1.0f,  1.0f };
9Ro4_ n(z2j     static GLfloat v1[] = {  1.0f, -1.0f,  1.0f };QBD J p;U*b
    static GLfloat v2[] = {  1.0f,  1.0f,  1.0f };
~7LW5{R;}K n     static GLfloat v3[] = { -1.0f,  1.0f,  1.0f };
m,Fb8G)PH5wx     static GLfloat v4[] = { -1.0f, -1.0f, -1.0f };j |zt R%] I{
    static GLfloat v5[] = {  1.0f, -1.0f, -1.0f };7~!|M'ar+\
    static GLfloat v6[] = {  1.0f,  1.0f, -1.0f };;D$iZ!f2KTgV
    static GLfloat v7[] = { -1.0f,  1.0f, -1.0f };
I?L ZS+NFe     static GLubyte red[]    = { 255,   0,   0, 255 };
qtG&Gv!sz a5O     static GLubyte green[]  = {   0, 255,   0, 255 };2]i~ Ll!G
    static GLubyte blue[]   = {   0,   0, 255, 255 };
^"sq-_0b,Qk'cy     static GLubyte white[]  = { 255, 255, 255, 255 };
rcZ @ ur*u I     static GLubyte yellow[] = {   0, 255, 255, 255 };
S ~bX&~     static GLubyte black[]  = {   0,   0,   0, 255 };*X V,J?x)D
    static GLubyte orange[] = { 255, 255,   0, 255 };'R3VSs mI)SV6er'C
    static GLubyte purple[] = { 255,   0, 255,   0 };
+d;Dg~PYY 'm$Jq;R6o%U
    /* Clear the color and depth buffers. */
Yz,c+fw     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
$Z;| pQP1i5h Q%d#zw}wq9G
    /* We don't want to modify the projection matrix. */b'Y9^b5U
    glMatrixMode( GL_MODELVIEW );
~V ym Q5S S%_     glLoadIdentity( );
Jjy]"I*{ cFE3}1D&s @%j\%Z
    /* Move down the z-axis. */(^s%@@m,Ed,X*y
    glTranslatef( 0.0, 0.0, -5.0 );
.c W?fT C8a7rK}ZIO
    /* Rotate. */
s&n9Pw0`k     glRotatef( angle, 0.0, 1.0, 0.0 );|S+F~0M@1NalX
b5x0k ~.x B
    if( should_rotate ) {
8p6kq,mE!D4BS #m"S3a yxs+uHF
        if( ++angle > 360.0f ) {
8a"GG m0Y%_KOF)A^             angle = 0.0f;&}b$^:[`_N
        }|0Z u6\c:IJ
6xWhuW4X
    } t fA7|s3Q,_Xh5u
/Aq0F X'R
    /* Send our triangle data to the pipeline. */#b;}MWkJ`
    glBegin( GL_TRIANGLES );c_(h9tKg-k0f
{G0J(xp E+|
    glColor4ubv( red );
d ?r2L]     glVertex3fv( v0 );
m,ZY2q%q"}     glColor4ubv( green );}^M*l;F.TtB%k
    glVertex3fv( v1 );
-qt1akNlKQ     glColor4ubv( blue );
ME ] _1G kygW     glVertex3fv( v2 );
'M$i$N6CE X!o'G-z^4MT
    glColor4ubv( red );
*b V&m-lR ~}     glVertex3fv( v0 );
y{ OPvg3g     glColor4ubv( blue );ry+U.^q5apl
    glVertex3fv( v2 );+{,`7whot\
    glColor4ubv( white );
3t oy!S.{I3b     glVertex3fv( v3 );
\8lWtl3N
fv(X6B3Doy\@     glColor4ubv( green );
A&D3Z"rL#|4S     glVertex3fv( v1 );
[h,j;Nq#pH     glColor4ubv( black );
)VFP6evw-pSu     glVertex3fv( v5 );*pq h0^9S^&~
    glColor4ubv( orange );
(H+ps.j,z1x:Q uZ     glVertex3fv( v6 );
9~Q(O1tMb~"[
%f5i&K;S9un     glColor4ubv( green );
,@*dN,K2S}hf     glVertex3fv( v1 );
"yxy5x|*d2\     glColor4ubv( orange );
9gWVj cW@     glVertex3fv( v6 );
t)J Bx}Y/`n     glColor4ubv( blue );!p:S1c C"Yg/s
    glVertex3fv( v2 );
\-MCG^,mR|3W I ~})w QX
    glColor4ubv( black );V5Z!Fx q6pY
    glVertex3fv( v5 );*r8P7urcO#C
    glColor4ubv( yellow );E6~ ]s)R&}5R@Cz,F
    glVertex3fv( v4 );
kE7mBa0D/B(a     glColor4ubv( purple );
ggo8V&vols1U     glVertex3fv( v7 );:L vg9O:_r%f v

dEL%q8]J/^     glColor4ubv( black );7T#r d:Xn P F8^
    glVertex3fv( v5 );
-U\.W#u"e     glColor4ubv( purple );
tF8k!u6zf"s {:o     glVertex3fv( v7 );
k&Vz n-l })hN     glColor4ubv( orange );gBL!N.@+?
    glVertex3fv( v6 );
j\6F&Lv@
i&z_K Ec:W     glColor4ubv( yellow );
KY^v*?S/ob'\     glVertex3fv( v4 );
\q-y_,oq     glColor4ubv( red );
%G4lm]G6}     glVertex3fv( v0 );F1L.V-tEPZ e
    glColor4ubv( white );
e_Djx!S9NrZ     glVertex3fv( v3 );T$k0dyrTf_m7s
k/OAIP
    glColor4ubv( yellow );jj H'kS kBS$Op~B
    glVertex3fv( v4 );$g/~:~8z7qq
    glColor4ubv( white );5u3WL d+?#CYA
    glVertex3fv( v3 );
i7y.B(X(N?+q y     glColor4ubv( purple );5scn,UC*q,CX@%W
    glVertex3fv( v7 );
k H*}/D#L [by
f)j0CM2o6X c'?c     glColor4ubv( white );
"}(gAM-tCh |C     glVertex3fv( v3 );+kH0J:PM4}4o3Zp
    glColor4ubv( blue );*e4i7uFw
    glVertex3fv( v2 ); he,b }pc
    glColor4ubv( orange );
z;T"e#wb9k1_W)rU     glVertex3fv( v6 );
| tV7C| gM
y)i3~&K Q;|5])Ff     glColor4ubv( white );&k]0VA'b ^
    glVertex3fv( v3 );i Xn%u rq[7T
    glColor4ubv( orange );
"V-LkT_Fu\R!Q'Z     glVertex3fv( v6 );"Kr ClP'[
    glColor4ubv( purple );.o"EFs6w
    glVertex3fv( v7 );
L+mhy1G0^ e iJ
]:c G[W Q W5L{     glColor4ubv( green );
-D,G n2qC I3@G6\     glVertex3fv( v1 );
ba[i9FL3m,q     glColor4ubv( red );@0J8R0s'wU\Pdze
    glVertex3fv( v0 );{Fsu+F:?!f v3z
    glColor4ubv( yellow );M3L X*a ePFGE
    glVertex3fv( v4 );
%VWk0W ^}4M7g3E/S
8U3A ^(?G$g2BIz     glColor4ubv( green );
ug1c TP     glVertex3fv( v1 );
:z*s\6mp~T r     glColor4ubv( yellow );
)W'}ZC!q;a     glVertex3fv( v4 );
,B a'`D,|Y^#F!i5Jae2h     glColor4ubv( black );
7~C/P#hD     glVertex3fv( v5 );
.q;~sZx m/jML{
    glEnd( );
w#{6f HZd;oJ*O#Kak
&b kP'[@l     /*
'R)Ks0C!y}7_+eOT2z      * EXERCISE:
5_rxc"g9t(o(Qh*W      * Draw text telling the user that 'Spc'+J_#MIaw4@/Y
     * pauses the rotation and 'Esc' quits.
Xa+ZE8a4x7S"[      * Do it using vetors and textured quads.
JkMq8G L,R      */*FB"g9B5a7u;f

q1ds}KXB6jq     /*
Z tAg5P-nn0B      * Swap the buffers. This this tells the driver to
(W#_j1y7s-MY9^{ f|      * render the next frame from the contents of the
EF*uh"z0A(J QK      * back-buffer, and to set all rendering operationsaQUc#|+Vf,WN
     * to occur on what was the front-buffer.
.A$K7vM$p3h      *
%}A;r"JVFDH*C7D      * Double buffering prevents nasty visual tearing
K2P|Fq@      * from the application drawing on areas of the
PkUPYr"q6D      * screen that are being updated at the same time.GD7t0`"\4{ q1HC
     */Y IX/tq |b H
    SDL_GL_SwapBuffers( );
} l'jf*]a8O }-W W$e z(Tg?7N'{ i\w*e
*Of!F3U;U.N1n
static void setup_opengl( int width, int height )V}hj|d?+D'd-T[
{Xx!z2Um7C
    float ratio = (float) width / (float) height;:a)?#igr:C%a&X
-KR8X bQ F2v9R#\!NE:E
    /* Our shading model--Gouraud (smooth). */#^$U2C9m6BFm9qMfm
    glShadeModel( GL_SMOOTH );
.W;O&C"Wc i#L'S
vZ-l0]Q(f6a7i%a     /* Culling. */$Prn$L~y
    glCullFace( GL_BACK );/[(MY gl4}
    glFrontFace( GL_CCW );"Rb x+nD:IY
    glEnable( GL_CULL_FACE );
ZgmGkp5vY
UN*Jdk.xk0f2X     /* Set the clear color. */7Xr }.@$g y)j N
    glClearColor( 0, 0, 0, 0 );
kd+a g0jeiS}|HV
M+|6g~9ep.W%M1]     /* Setup our viewport. */0u+[Rg;U
    glViewport( 0, 0, width, height ); V;s6X[z/g/R,F(X

VBb0A HKI     /*
j5\J-d*O8\+|f,E      * Change to the projection matrix and setBkS@mF1`lF2Reu
     * our viewing volume.
%CUf9cuZ      */-D u%i}A)~aT
    glMatrixMode( GL_PROJECTION );
Y UWzZ/C     glLoadIdentity( );
;\4Z$E@5n/Oq     /*0Ute3Gp5hN{
     * EXERCISE:,OA,~8`wQ
     * Replace this with a call to glFrustum.xEd@j"G2~rND
     */
dxY"^:k{C.?0[h     gluPerspective( 60.0, ratio, 1.0, 1024.0 );
GV$D%E,Q[$U }4R Z&e H ~s!M8v|w
(oG]0{M8p8Nf
int main( int argc, char* argv[] )E?;Y/x!K"GX
{
f}p)i N#X     /* Information about the current video settings. */
6g(e,k({uhKKH     const SDL_VideoInfo* info = NULL;
p9M-k+kHr6q-c?     /* Dimensions of our window. */)U wq-y.y?^tCK U
    int width = 0;2\;~R3] s3wN-r.O
    int height = 0;
R1g0q7@vI _tx     /* Color depth in bits of our window. */:Q"iMP6dV p
    int bpp = 0;
:yqX0E(~ Bp"J     /* Flags we will pass into SDL_SetVideoMode. */
k7kX:HlfL     int flags = 0;
eksi q{l-eQ6} hi
.k%kbQh,aC3^     /* First, initialize SDL's video subsystem. */
E"B5N|9J4H E     if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
2F\$r+CY-Y6t         /* Failed, exit. */ p%qJcx}#X!A6SfB
        fprintf( stderr, "Video initialization failed: %s\n",
8~3rbS(mfs \              SDL_GetError( ) );(H0G.S8by4V
        quit_tutorial( 1 );6cO%b,@D[4LLL9W
    }PM`0~^%eF

(h7B#r/S-Z5G9Q     /* Let's get some video information. */
Gxh4h)prv     info = SDL_GetVideoInfo( );Y;B.B8Pf/d Q
9` B)?$Z.I _)iFv6B+H@
    if( !info ) {$U*]/|/MDdT!?y
        /* This should probably never happen. */X$F[.X:Z)z.Y7uX
        fprintf( stderr, "Video query failed: %s\n",
$F1_ ob$cAs3F              SDL_GetError( ) );
1M'``kZ         quit_tutorial( 1 );
|l2v1t Atp     }
(Q"j|/j5Z)b
hY[H9Y1r@     /*
2uC%fG`W-jq&^ u(A L      * Set our width/height to 640/480 (you would/} EAs(a_ c
     * of course let the user decide this in a normal
vpyqd(q+Ur      * app). We get the bpp we will request from+}$q@e0hy
     * the display. On X11, VidMode can't change v O ]Q tD\
     * resolution, so this is probably being overlyd/V4d!i!mu
     * safe. Under Win32, ChangeDisplaySettings
7VS-j+q9\-j#]+V'_'w8@3X,G      * can change the bpp.,U\D,By'~4I
     */
)| O,P7]^j     width = 640;m2GecQ)J9^;M
    height = 480;)}`5w E;Y m r_
    bpp = info->vfmt->BitsPerPixel;o`Rko k[ ?$Nzw

6^i'~({ oGN;S     /*/o;K9}0C/pT1f*S
     * Now, we want to setup our requested A:T.bu1o4J cY
     * window attributes for our OpenGL window.%E B+]1o r4VQRTT1\J
     * We want *at least* 5 bits of red, green)Vsf5R N
     * and blue. We also want at least a 16-bitZ\'jg y*} Dm:^
     * depth buffer.*R*Q3Z5U dqY
     *i Y7}q RcBf
     * The last thing we do is request a double
2a ^7yY2Za      * buffered window. '1' turns on double
ohn ayM}      * buffering, '0' turns it off.o oU7c5Ki/V&O8Y
     *F*}3ib2C&`
     * Note that we do not use SDL_DOUBLEBUF in
2CtpOMh$AK      * the flags to SDL_SetVideoMode. That does
-Q2q)h*vn      * not affect the GL attribute state, only#{/R tTg
     * the standard 2D blitting setup.%icAHi
     */
Q| xx R6i*{z9a5I d     SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );fa|{;A G
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );~(`'C'xe!Z^
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );{3v ofY{M
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );V2YfXIEv3r o5G
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
;C:ok+[0sp0O,l I
3Qa cX*h{"mmS&`     /*
9? k0M4@5q(h'sfp      * We want to request that SDL provide us _KZz\Q hcL
     * with an OpenGL window, in a fullscreen
3ma2LJ,\#J)q      * video mode. ^0dij8_9j
     *
,A q._^U      * EXERCISE:
$X!~r&al'Ed      * Make starting windowed an option, and8kTSINyN!n
     * handle the resize events properly withlY3{aX w
     * glViewport.
At7l4mX&|3cE      */&?1b `(BmHE&@
    flags = SDL_OPENGL | SDL_FULLSCREEN; e8H(Y0_KH-aOv]3T

1nE~3x+U%W     /*Ya![2P{2`e
     * Set the video mode
h0K)k8W'yt      */
#] Q%E gJcB E3w6a     if( SDL_SetVideoMode( width, height, bpp, flags ) == 0 ) {M:BVw;T e
        /* !^/I(hRD8`!KGm
         * This could happen for a variety of reasons,
"[~4Z:]Mb!\#F7Xq          * including DISPLAY not being set, the specified
{ f/H'Vf@*u7Q.AW          * resolution not being available, etc.~@dFr*b&I/J }
         */D(c*[Ydry!r
        fprintf( stderr, "Video mode set failed: %s\n",
4u7f%Z7N4d              SDL_GetError( ) );0rp7m;v/O7A h
        quit_tutorial( 1 );t4R|(G/w {
    }:zO1`Z(Si"^zQa e'x
6?8{ m8M3C0kp:k
    /*
0m eQJN a8[      * At this point, we should have a properly setup q9mjZs
     * double-buffered window for use with OpenGL.
%Tg&h},d-F/|%?6fO      */\8@S:{ xx'Gr[I
    setup_opengl( width, height );
t3mLb!qg$d7n#A o\)yQk3lT
    /*v*rp~[g
     * Now we want to begin our normal app process--
-~e I9d?5_(C$A+y9[      * an event loop with a lot of redrawing.
#`K0J4RLc9v      */
\ |],d f5sl     while( 1 ) {2a Pp |%txdk
        /* Process incoming events. */qdta:V2[Y Xd
        process_events( );
t-M5d$w&Dh yP9v         /* Draw the screen. */-]a&O P3u\6^+G/U
        draw_screen( );
#|1nm C RW     }
1E5cm\"Y| eMjdR,D$j.B_'F Moh
    /*,Xl'{ ?@Se6rb
     * EXERCISE:
o CjLtb p      * Record timings using SDL_GetTicks() and @,P:f%T U?U
     * and print out frames per second at program
:m`3am(A1vO      * end.
uDu*_ l T      */
7m({[,~ZZ w B+A h%nm6_%D!h
    /* Never reached. */
Py5K)R!k%YU.AV;z bd     return 0;
#jA:r'o,n"Fs }
4ng7??r:{ ]l&F{'osn|0u
#gcc program.c -L/usr/X11R6/lib -lSDL -lGL -lGLU -o program

akinggw ·¢±íÓÚ 2006-7-19 09:25

ÕâÀïÓÐÎÒ·­ÒëºÍ×Ô¼ºÐ´µÄһЩSDL½Ì³Ì£¬»¹ÔÚı»®Ò»¸ö2DµÄÍøÂçÓÎÏ·£¬Ï£Íû´ó¼Ò¶à¶àÖ§³Ö£¬ÎÒµÄQQÊÇ395314452
I1Q1j"x%M #gq` ?`%D
[url="http://popul.jqcq.com/computer/gdev/"]http://popul.jqcq.com/computer/gdev/[/url]

analysesnow ·¢±íÓÚ 2006-8-3 00:48

żÏÂÔØÁË60±¾LINUX Ó¢ÎİæµÄÊ飬¾ÍÊÇûÓÐÒ»±¾¹ØÓÚSDL±à³ÌµÄ£¬ÓôÃÆ~~~O,VP#c-je(U4US
±ðµÄʲô¶¼ÓУ¬º¹ING

linuxpgy ·¢±íÓÚ 2006-9-6 14:08

SDL±à³Ì£¿£¿Èç¹ûÄã×°SDL-develÕâ¸ö°üµÄ»°£¬Ó¦¸ÃÔÚ/usr/share/doc/SDL-XXXÓÐËüµÄÏêϸAPI½éÉܵÄ~~

chenrenjin ·¢±íÓÚ 2006-12-20 19:00

ÎÒ¸Õ½Ó´¥linuxϱà³Ì£¬ËùÒÔ²»¶®£¬Âé·³¸÷λ°ïÎÒ¿´¿´ÊÇʲôÎÊÌâ V iyC%nO#sl
³ÌÐò´úÂë
mry3I~
`T3r#@IPg qE;?D #include <stdio.h>
y!c-pxTiI"t R #include <stdlib.h>;Je9Hb*S:i xaT6VQ.^5s7V
#include <SDL.h>)@A1rl&[$OfVA
#include <SDL_image.h>
y.vF&EHa\8eT.P)R #define DIR_UP 0\\$t }1R*`X
#define DIR_LEFT 1
9UA8u7a_Q?q0` #define DIR_DOWN 2
3O wee Xr6\1h^\$m.Z #define DIR_RIGHT 3
O)GY Kl3~3} SDL_Surface *screen;6M.C9g|*J~+q F/@
typedef struct Sprite_t {
vJBl'E u$@ int width;.IlNI l?
int height;5Y a9w#NWyo
int x;
5@ R`(VLg:z V int y;
BS+x^1b0^ int ox;
0o+j7BZ'xz Hp'RT int oy;F m9i0E+bh
int dir;^0\ v{2\r zr&t/U
int subframe;XM,F9S#^&fW%f s
int speed;
Z|t/y2L9fG-VHK:~ int timer;+^{B.{'nw
int moving; //use to calculate subframe
5Aa O2c[8I SDL_Surface *image;8Av'nk`9@ ~*{
}Sprite_t;
m|-X"iQ1vc$kb typedef enum GameState {
jQe'i |B!|8N GAME_RUNNING,
(bHc C Nil"q&W GAME_OVER,'kCyND[)y0\ _
}GameState;$~L3[~|xA.q&zl
void Init_SDL()
8D S!B)p y6v[4T F H {Z2r3P'M(H;I
if ( SDL_Init( SDL_INIT_VIDEO) < 0 ) { j0k1in[
fprintf(stderr,"Can't init SDL: %s\n",SDL_GetError());
q,|`A9z1X`#Nv2^ exit(1);8OV#|g!nP
}
FGQ*icRY'wL screen = SDL_SetVideoMode(640, 480, 16, SDL_SWSURFACE);
k;r7P:q}r)F7L if ( screen == NULL ) {
4K^3| j[T]6|;f fprintf(stderr, "Error: %s\n",SDL_GetError());
Cn)kl]&h)|n f(?K W exit(1);
A!v[(t `8HN }W;\4D{nC+a ]L
atexit(SDL_Quit); l6T2q,~+u9W#U F
}"@H)z1oCF9b
void DrawSprite(SDL_Surface *surface,Sprite_t *sprite) Pm Ri-w }1[
{$z9k6Klx9Ps9Eo
SDL_Rect src_rect,des_rect;h8O"O!D.Sd3A
sprite->subframe=sprite->timer/4;
y0T&?P/B src_rect.w=des_rect.w=sprite->width;
-cJ4~ }ShO src_rect.h=des_rect.h=sprite->height;0A4w(I'Tj!B,gp
src_rect.x=sprite->subframe*sprite->width;?%sK,j$V
src_rect.y=sprite->dir*sprite->height;q-f \-jW [Z
des_rect.x=sprite->x;
QiQD/j(M&m des_rect.y=sprite->y;-QS)PCTZMZX
//Draw sprite to new position
_!K4?/ZKJ*` SDL_BlitSurface(sprite->image,&src_rect,surface,&des_rect);5j0W4@/o1K
}*RjLB4u9s
void ClearSprite(SDL_Surface *surface,Sprite_t *sprite)
5o7Qvnr E`t { SL-R@IHc+r
SDL_Rect mask;
0^8F:QVNP#] mask.w=sprite->width;k |^X^Bt j
mask.h=sprite->height;
y3Io y.sds@ mask.x=sprite->ox;3d#Y UZ:j#rv
mask.y=sprite->oy;:E`p&ut:H1k
//Clear sprite from the old position GdlOEq6c{o
SDL_FillRect(surface,&mask,SDL_MapRGB(surface->format,0,0,0));
$b{HL$F#~ } z"je!G9f `
Uint32 TimeLeft() #W4\oulc.K}d
{
\@ Q2it N| static Uint32 next_time=0;
"@ j2r4o OY_ _ Uint32 now; [-yu7{+bt%a5f
now=SDL_GetTicks();
e CWfeb^Gf if(next_time<=now){
b]|E}%u,n$F PO next_time=now+30;
C6e:h wR.Kt}2|1C return(0);
J*NjH\z)OaBJ }:E,ycg.]G}
return(next_time-now);
$f(P9O WlL }
i0hr f[$e7U XNJw4Km

T%X3zbn/c,r int main()/ExCZY9ZU/EOi s
{ e9rZHg/K)O~9e:h
GameState game_state;
#H8S G bd#e k%S Sprite_t tank;
ab#N"?+_ SDL_Event event; kP {6a#vF#z4|.?)Q
Init_SDL();2v(P+}7G5e5~:]
tank.image=IMG_Load("tank.png");
'G!s2};Z`p5X g6f `c SDL_SetColorKey(tank.image,SDL_SRCCOLORKEY,SDL_MapRGB(tank.image->format,255,0,255));
s*EG,wl tank.image=SDL_DisplayFormat(tank.image);gE L-N4k&z
game_state=GAME_RUNNING;
A[ Lrx0G7` r tank.dir=DIR_RIGHT;
'tQ:Ek K tank.width=32;
P6_pMu0w-ZEiz^ tank.height=32;
SHVW u| tank.x=300;
~9h_w7bq{%M tank.y=200;%f"X9eO4drP
tank.ox=tank.x;U/A{:_+Jk!sK
tank.oy=tank.y;
;LnW2r#D%h0x tank.moving=0;*?x t}JD:u,QSs:T
tank.speed=3;Pk.ulM+[W AC
tank.timer=0;x+G U;L}"[
while(game_state==GAME_RUNNING){
4_)j8xu.s ClearSprite(screen,&tank); K5Ow%g:i[;V~0A P
//Get Input
$m hgc%_&@`+f*Ve\ SDL_PollEvent(&event);^8~(};Z*D3TiZ
switch(event.type){C9y/M Vo+FM;he
case SDL_KEYDOWN: 'M2@IWCGQ
switch(event.key.keysym.sym){Z;M1m Z(S
case SDLK_ESCAPE:L;LTY,xH]3?
game_state=GAME_OVER;
t{rtqbj[h break;7L%q-p2t*c]zL
case SDLK_UP:.]J7MV6H
tank.dir=DIR_UP;e%iw R;E4xt{p#W
tank.y-=tank.speed;
8rD pO#?$M break;Qs/L4K#p1`+@7JN\2_W
case SDLK_DOWN:
2LQ6u XM;` tank.dir=DIR_DOWN;
c Y B~.Ay tank.y+=tank.speed;
lCc{D_&l;W break;
7ZS&RprO-c case SDLK_LEFT:9u)Gg;e$@xw_:y:k'^
tank.dir=DIR_LEFT;r@[F:YVdZ)e6_
tank.x-=tank.speed;
P,KE-NgY/l break;
'P$y'h,~N case SDLK_RIGHT:*\c g,qSS^9M
tank.dir=DIR_RIGHT;6q5BA-Z%C+?us |
tank.x+=tank.speed;S VT^Ks$N6N[H_
break;
%a+u4_H;k'A default:
5z;}_ mb7bU break;
l!VL2nZm }W|[4|.p5L/Z0e
case SDL_KEYUP: xQR$Dabk1k
break;
P9r1o6T#fTY%q$x default:(E/t&zz$wk6h
break;6Bg3h%_&]
}
9_}h} ^4pG8E
E-[!^fue#rp DrawSprite(screen,&tank);
W:`]!g#bb^MMj6l tank.ox=tank.x;R4d4J"@ xd m8i.Lc
tank.oy=tank.y;
A!z1B(^0DF` SDL_UpdateRect(screen,0,0,0,0);
5j4Y%H)I`7yO SDL_Delay(TimeLeft()); 8Br8S#u0}
tank.timer++;Kv(F/?aZ p1t
tank.timer%=8;.Xa.Y*j?qS
}}eZ&HUs
SDL_FreeSurface(tank.image); t*^A X+LJ'Q
}3F n#i`,Y{f'H!Io2p _R }2[

'ggB}nN b+lulz ±àÒëµÄʱºòÌáʾ
1Ma/P$F1i7o h{9I /tmp/ccCxlEey.o(.text+0xlf8):in function 'main';
"Hqyz;^E:F2|O~ test.c:undefined reference to 'IMG_Load'ku]-c+D&d
collect2:ld ·µ»Ø 1!zF `JUG^O+}
ÊDz»ÊÇtank.image=IMG_Load("tank.png");Õâ¾äÓÐÎÊÌ⣿
,b0x;~7k4M Vl Çë¸÷λָµãÒÔÏÂÎÒ¸ÃÔõô¸Ä£¬Ð»Ð»

wannachan ·¢±íÓÚ 2007-1-16 20:43

Äã±àÒëʱµÄ²ÎÊýÊÇʲô? ÄãÈ·¶¨ÄãµÄincludeºÍlibÎļþ¼ÐÏÂÃæÓÐÁËÕýÈ·µÄ.hÎļþºÍÁ´½Ó¿âûÓÐ?L2L+I _!q ? b?+Q
  
%@Dc#m})f0n   »¹ÓÐ, ±àÒëʱ²ÎÊýÒª¼ÓÉÏ-lSDL

Ò³: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.