• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            小明思考

            高性能服務(wù)器端計(jì)算
            posts - 70, comments - 428, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            用SDL寫游戲

            Posted on 2005-12-28 21:32 小明 閱讀(10106) 評論(10)  編輯 收藏 引用 所屬分類: Game Development
            SDL(Simple DirectMedia Layer)是一個跨平臺的multimedia library ,包含了對video,audio,keyboard,mouse的支持。
            它的接口比較簡潔,你甚至不需要知道Win32 API,就可以寫出游戲來。它封裝了跟
            平臺相關(guān)的部分。它使我想起了TC下面的graphics BGI

            官方地址:   http://www.libsdl.org/

            下面是我用SDL寫的一個小游戲
            run.jpg


            1.骨架程序

            #include "sdl.h"
            #pragma comment(lib,
            "sdl.lib")
            #pragma comment(lib,
            "sdlmain.lib")

            int main(int argc,char ** argv)
            {
                
            //Init SDL
                SDL_Init( SDL_INIT_VIDEO );
                atexit(SDL_Quit);
                screen 
            = SDL_SetVideoMode(400,480,0,SDL_SWSURFACE);
                SDL_WM_SetCaption(
            "RUN",NULL);

                
            //load resource
                
            //

                
            bool pause = false;
                
            bool gone = true;

                SDL_Event 
            event;
                
            while(gone)
                {
                    
            if(SDL_PollEvent(&event)>0)
                    {
                        
            if(event.type == SDL_QUIT)
                        {
                            
            break;
                        }
                        
            else if(event.type == SDL_KEYDOWN)
                        {
                    
            //handle event
                    
            //
                        }
                        
            else if(event.type == SDL_KEYUP)
                        {
                    
            //handle event
                    
            //
                        }
                    }
                    
            else if(!pause)
                    {
                        run(); 
                    }
                }

                
            //release resource
                
            //
                return 0;
            }


            SDL_Init : 初始化子系統(tǒng)
            SDL_SetVideoMode:設(shè)置分辨率
            SDL_WM_SetCaption: 設(shè)置窗口標(biāo)題
            SDL_PollEvent:查詢事件,用戶的輸入

            2.表面的概念

            表面(SDL_Surface)是SDL中一個重要的概念,精靈都是保存在表面中

            SDL_Surface * temp = SDL_CreateRGBSurface(SDL_SRCCOLORKEY, w, h, 32,
                            
            0000);
                        
            rc.x 
            = x * w ;
            rc.y 
            = y * h;
                        
            SDL_BlitSurface(surf,
            &rc,temp,0);
                       
            SDL_SetColorKey(temp,SDL_SRCCOLORKEY,transcolor);

            SDL_CreateRGBSurface 創(chuàng)建一個表面
            SDL_BlitSurface 表面之間的copy
            SDL_SetColorKey 設(shè)定表面的透明色

            ps:主表面是由SDL_SetVideoMode返回的,也可以通過SDL_GetVideoSurface查詢到

            3. 聲音的使用

            游戲離不開聲音,SDL也提供了支持

            void play_sound(const char *file)
            {
                
            int index;
                SDL_AudioSpec wave;
                Uint8 
            *data;
                Uint32 dlen;
                SDL_AudioCVT cvt;

                
            /* 尋找一個空的(或者完成了的)音頻口 */
                
            for ( index=0; index<NUM_SOUNDS; ++index ) {
                    
            if ( sounds[index].dpos == sounds[index].dlen ) {
                        
            break;
                    }
                }
                
            if ( index == NUM_SOUNDS )
                    
            return;

                
            /* 加載聲音文件,并轉(zhuǎn)換成16位、立體聲、22kHz格式 */
                
            if ( SDL_LoadWAV(file, &wave, &data, &dlen) == NULL ) {
                    fprintf(stderr, 
            "無法加載 %s: %s\n", file, SDL_GetError());
                    
            return;
                }
                SDL_BuildAudioCVT(
            &cvt, wave.format, wave.channels, wave.freq,
                                        AUDIO_S16,   
            2,             22050);
                cvt.buf 
            = (unsigned char *)malloc(dlen*cvt.len_mult);
                memcpy(cvt.buf, data, dlen);
                cvt.len 
            = dlen;
                SDL_ConvertAudio(
            &cvt);
                SDL_FreeWAV(data);

                
            /* 將音頻數(shù)據(jù)放入音頻口(立刻開始回放了) */
                
            if ( sounds[index].data ) {
                    free(sounds[index].data);
                }
                SDL_LockAudio();
                sounds[index].data 
            = cvt.buf;
                sounds[index].dlen 
            = cvt.len_cvt;
                sounds[index].dpos 
            = 0;
                SDL_UnlockAudio();
            }

            4.一些有用的模塊

            #include "sdl_image.h"

            /*
             * Return the pixel value at (x, y)
             * NOTE: The surface must be locked before calling this!
             
            */
            Uint32 getpixel(SDL_Surface 
            *surface, int x, int y)
            {
                
            int bpp = surface->format->BytesPerPixel;
                
            /* Here p is the address to the pixel we want to retrieve */
                Uint8 
            *= (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

                
            switch(bpp) {
                
            case 1:
                    
            return *p;

                
            case 2:
                    
            return *(Uint16 *)p;

                
            case 3:
                    
            if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
                        
            return p[0<< 16 | p[1<< 8 | p[2];
                    
            else
                        
            return p[0| p[1<< 8 | p[2<< 16;

                
            case 4:
                    
            return *(Uint32 *)p;

                
            default:
                    
            return 0;       /* shouldn't happen, but avoids warnings */
                }
            }


            /*
             * Set the pixel at (x, y) to the given value
             * NOTE: The surface must be locked before calling this!
             
            */
            void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
            {
                
            int bpp = surface->format->BytesPerPixel;
                
            /* Here p is the address to the pixel we want to set */
                Uint8 
            *= (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

                
            switch(bpp) {
                
            case 1:
                    
            *= pixel;
                    
            break;

                
            case 2:
                    
            *(Uint16 *)p = pixel;
                    
            break;

                
            case 3:
                    
            if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
                        p[
            0= (pixel >> 16& 0xff;
                        p[
            1= (pixel >> 8& 0xff;
                        p[
            2= pixel & 0xff;
                    } 
            else {
                        p[
            0= pixel & 0xff;
                        p[
            1= (pixel >> 8& 0xff;
                        p[
            2= (pixel >> 16& 0xff;
                    }
                    
            break;

                
            case 4:
                    
            *(Uint32 *)p = pixel;
                    
            break;
                }
            }

            bool showpic(SDL_Surface *surface,const char *filename)
            {
                SDL_Surface 
            *image;

                
            /* Load the BMP file into a surface */
                image 
            = IMG_Load(filename);
                
            if (image == NULL) {
                    fprintf(stderr, 
            "Couldn't load %s: %s\n",filename,SDL_GetError());
                    
            return false;
                }

                
            /* Blit onto the screen surface */
                
            if(SDL_BlitSurface(image, NULL, surface, NULL) < 0)
                    fprintf(stderr, 
            "BlitSurface error: %s\n", SDL_GetError());

                SDL_UpdateRect(surface, 
            00, image->w, image->h);

                
            /* Free the allocated BMP surface */
                SDL_FreeSurface(image);

                
            return true;
            }

            SDL_Surface
            * createsurf(const char * filename)
            {
                SDL_Surface 
            *image;

                
            /* Load the BMP file into a surface */
                image 
            = IMG_Load(filename);
                
            if (image == NULL) {
                    fprintf(stderr, 
            "Couldn't load %s: %s\n",filename,SDL_GetError());
                    
            return 0;
                }

                
            return image;
            }

            getpixel 取得像素的顏色
            putpixel 畫點(diǎn)
            showpic 加載一個圖片到某個表面
            createsurf 從文件中創(chuàng)造表面

            SDL有大量的示例和第三方庫可以使用,非常的不錯
            最后推薦一個SDL寫的超級瑪麗游戲
            http://smclone.sourceforge.net/



            Feedback

            # re: 用SDL寫游戲  回復(fù)  更多評論   

            2005-12-29 09:07 by 不及格的程序員 八神
            小明 愛好 好廣飯啊 呼呼

            # re: 用SDL寫游戲  回復(fù)  更多評論   

            2005-12-29 10:27 by ngaut
            小明兄可否提供該程序的代碼以及聲音,圖片等文件,我想學(xué)習(xí)下^_^
            ngaut#126.com

            # re: 用SDL寫游戲  回復(fù)  更多評論   

            2006-01-19 00:42 by hygol
            我也是做游戲開發(fā)的。我們現(xiàn)在項(xiàng)目中使用SDL庫播放音樂音效的時候遇到一個問題就是聲音的同時播放問題,能討論一下嗎

            # re: 用SDL寫游戲  回復(fù)  更多評論   

            2007-03-06 12:28 by rayfox
            SDL庫播放不是很好,推薦使用FMOD來播放.

            # re: 用SDL寫游戲[未登錄]  回復(fù)  更多評論   

            2008-01-25 22:18 by Henry
            SDL_mixer增強(qiáng)了對音頻 的支持.
            已經(jīng)可以支持OGG MOD MP3等格式

            # re: 用SDL寫游戲  回復(fù)  更多評論   

            2008-04-23 15:25 by daxia
            Can you give me the whole statics of how to use the SDL?
            If you want to,fortunately,than x very much,please send them
            to the email given above.

            # re: 用SDL寫游戲[未登錄]  回復(fù)  更多評論   

            2012-06-28 10:44 by liu
            可不可以把你的源代碼發(fā)給我一下,有急用,萬分感謝。。。。。我的qq郵箱為:354313920@qq.com

            # re: 用SDL寫游戲  回復(fù)  更多評論   

            2013-06-01 18:20 by 范文軒
            請學(xué)長給予幫助?。?!

            # re: 用SDL寫游戲  回復(fù)  更多評論   

            2013-06-01 18:23 by 范文軒
            十萬火急 請學(xué)長給予幫助!??! 需要源代碼 圖片 以及播放的聲音 郵箱453505600@qq.com

            # re: 用SDL寫游戲[未登錄]  回復(fù)  更多評論   

            2014-11-24 13:49 by lei
            可不可以把源代碼發(fā)給我,我在做final要用sdl2,但是學(xué)校根本沒學(xué),自己啃書啃了幾天也沒什么效果 郵箱oliverwan@live.com
            国产精品美女久久久免费| 久久精品视频一| 色播久久人人爽人人爽人人片aV| 人人狠狠综合久久亚洲| 亚洲午夜久久久久久久久久| 狠狠色丁香婷综合久久| 久久人人爽人人爽人人片AV东京热 | 久久九九亚洲精品| 狠狠色丁香婷婷久久综合五月| 中文字幕久久精品无码| 久久久WWW成人免费毛片| 久久国产亚洲高清观看| 伊人热热久久原色播放www| 91麻精品国产91久久久久| 伊人久久综合成人网| 蜜臀久久99精品久久久久久| 狠狠色丁香久久综合五月| 99精品久久久久久久婷婷| 久久久久女教师免费一区| 青青青国产精品国产精品久久久久| 性做久久久久久久久浪潮| 成人a毛片久久免费播放| 久久国产热精品波多野结衣AV| 久久久国产视频| 一本久久免费视频| 亚洲国产成人精品91久久久 | 无码人妻久久一区二区三区免费丨| 国产精品久久久久久久午夜片 | 欧美午夜A∨大片久久 | 亚洲国产精品成人久久蜜臀| 色偷偷888欧美精品久久久| 精品无码久久久久久尤物| 亚洲狠狠婷婷综合久久久久| 四虎久久影院| 亚洲人成电影网站久久| 亚洲午夜精品久久久久久浪潮| 久久久精品国产亚洲成人满18免费网站| 久久精品视频免费| 精品久久久久久99人妻| 久久99精品久久久久久水蜜桃| 91性高湖久久久久|