• <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>
            隨筆 - 96  文章 - 255  trackbacks - 0
            <2008年2月>
            272829303112
            3456789
            10111213141516
            17181920212223
            2425262728291
            2345678

            E-mail:zbln426@163.com QQ:85132383 長(zhǎng)期尋找對(duì)戰(zhàn)略游戲感興趣的合作伙伴。

            常用鏈接

            留言簿(21)

            隨筆分類(lèi)

            隨筆檔案

            SDL相關(guān)網(wǎng)站

            我的個(gè)人網(wǎng)頁(yè)

            我的小游戲

            資源下載

            搜索

            •  

            積分與排名

            • 積分 - 492144
            • 排名 - 38

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            作者:龍飛

            5.1:準(zhǔn)備工作。

            一張640*480大小的bmp文件作為背景,命名為:bg.bmp;
            一張128*128大小的bmp文件作為要在背景上移動(dòng)的圖片,命名為:image.bmp。

            5.2:頭文件SurfaceClass.h
            //FileName: SurfaceClass.h

            #ifndef SURFACE_CLASS_H
            #define SURFACE_CLASS_H

            #include 
            <iostream>
            #include 
            <string>
            #include 
            "SDL/SDL.h"

            using std::string;

            class ScreenSurface
            {
            private:
                
            static int screenNum;
                
            int width;
                
            int height;
                
            int bpp;
                Uint32 flags;
                SDL_Surface
            * pScreen;
            public:
                ScreenSurface();
                ScreenSurface(
            int w, int h, int b = 0, Uint32 f = 0);
                
            ~ScreenSurface();
                SDL_Surface
            * point() const;
                
            bool flip() const;
            };

            class DisplaySurface
            {
            private:
                
            string fileName;
                SDL_Surface
            * pSurface;
                SDL_Surface
            * pScreen;
            public:
                DisplaySurface(
            string file_name, const ScreenSurface& screen);
                
            ~DisplaySurface();
                SDL_Surface
            * point() const;
                
            bool blit() const;
                
            bool blit(int at_x, int at_y) const;
                
            bool blit(int at_x, int at_y,
                            
            int from_x, int from_y, int w, int h,
                            
            int delta_x = 2int delta_y = 2const;
                
            bool blitToSurface(const DisplaySurface& dst_surface,
                                    
            int at_x = 0int at_y = 0const;
                
            bool blitToSurface(const DisplaySurface& dst_surface,
                                    
            int at_x, int at_y,
                                    
            int from_x, int from_y, int w, int h,
                                    
            int delta_x = 2int delta_y = 2const;
            };

            #endif

            5.3:類(lèi)方法的實(shí)現(xiàn)文件:SurfaceClass.cpp
            #include "SurfaceClass.h"

            //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
            //class ScreenSurface

            int ScreenSurface::screenNum = 0;

            ScreenSurface::ScreenSurface():
            width(
            640), height(480), bpp(32), flags(0)
            {
                
            if ( screenNum > 0 )
                    
            throw "DONOT create more than ONE screen!";
                
            if ( SDL_Init(SDL_INIT_VIDEO ) < 0  )
                    
            throw SDL_GetError();
                pScreen 
            = SDL_SetVideoMode(width, height, bpp, flags);
                screenNum
            ++;
            }

            ScreenSurface::ScreenSurface(
            int w, int h, int b, Uint32 f):
            width(w), height(h), bpp(b), flags(f)
            {
                
            if ( screenNum > 0 )
                    
            throw "DONOT create more than ONE screen!";
                
            if ( SDL_Init(SDL_INIT_VIDEO < 0 ) )
                    
            throw SDL_GetError();
                pScreen 
            = SDL_SetVideoMode(width, height, bpp, flags);
                screenNum
            ++;
            }

            ScreenSurface::
            ~ScreenSurface()
            {
                SDL_Quit();
            }

            SDL_Surface
            * ScreenSurface::point() const
            {
                
            return pScreen;
            }

            bool ScreenSurface::flip() const
            {
                
            if ( SDL_Flip(pScreen) < 0 )
                    
            return false;
                
            else return true;
            }

            //AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

            //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
            //class DisplaySurface

            DisplaySurface::DisplaySurface(std::
            string file_name, const ScreenSurface& screen):
            fileName(file_name)
            {
                pSurface 
            = SDL_LoadBMP(file_name.c_str());
                
            if ( pSurface == 0 )
                    
            throw SDL_GetError();
                pScreen 
            = screen.point();
            }

            DisplaySurface::
            ~DisplaySurface()
            {
                SDL_FreeSurface(pSurface);
            }

            SDL_Surface
            * DisplaySurface::point() const
            {
                
            return pSurface;
            }

            bool DisplaySurface::blit() const
            {
                
            if ( SDL_BlitSurface(pSurface, 0, pScreen, 0< 0 )
                    
            return false;
                
            else return true;
            }


            bool DisplaySurface::blit(int at_x, int at_y) const
            {
                SDL_Rect offset;
                offset.x 
            = at_x;
                offset.y 
            = at_y;

                
            if ( SDL_BlitSurface(pSurface, 0, pScreen, &offset) < 0 )
                    
            return false;
                
            else return true;
            }

            bool DisplaySurface::blit(int at_x, int at_y,
                                      
            int from_x, int from_y, int w, int h,
                                      
            int delta_x, int delta_y) const
            {
                SDL_Rect offset;
                offset.x 
            = at_x - delta_x;
                offset.y 
            = at_y - delta_y;

                SDL_Rect dest;
                dest.x 
            = from_x - delta_x;
                dest.y 
            = from_y - delta_y;
                dest.w 
            = w + delta_x*2;
                dest.h 
            = h + delta_y*2;

                
            if ( SDL_BlitSurface(pSurface, &dest, pScreen, &offset) < 0 )
                    
            return false;
                
            else return true;
            }

            bool DisplaySurface::blitToSurface(const DisplaySurface& dst_surface, int at_x, int at_y) const
            {
                SDL_Rect offset;
                offset.x 
            = at_x;
                offset.y 
            = at_y;

                
            if ( &dst_surface == this )
                    
            throw "Cannot blit surface to itself!";

                
            if ( SDL_BlitSurface(pSurface, 0, dst_surface.point(), &offset) < 0 )
                    
            return false;
                
            else return true;
            }

            bool DisplaySurface::blitToSurface(const DisplaySurface& dst_surface,
                                                
            int at_x, int at_y,
                                                
            int from_x, int from_y, int w, int h,
                                                
            int delta_x, int delta_y) const
            {
                SDL_Rect offset;
                offset.x 
            = at_x - delta_x;
                offset.y 
            = at_y - delta_y;

                SDL_Rect dest;
                dest.x 
            = from_x - delta_x;
                dest.y 
            = from_y - delta_y;
                dest.w 
            = w + delta_x*2;
                dest.h 
            = h + delta_y*2;

                
            if ( &dst_surface == this )
                    
            throw "Cannot blit surface to itself!";

                
            if ( SDL_BlitSurface(pSurface, &dest, dst_surface.point(), &offset) < 0 )
                    
            return false;
                
            else return true;
            }

            //AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

            5.4:演示文件main.cpp
            #include "SurfaceClass.h"

            void game();
            int main(int argc ,char* argv[])
            {
                
            try {
                    game();
                }
                
            catch ( const char* s ) {
                    std::cerr 
            << s << std::endl;
                    
            return -1;
                }
                
                
            return 0;
            }

            void game()
            {
                
            //Create a SDL screen.
                const int SCREEN_WIDTH = 640;
                
            const int SCREEN_HEIGHT = 480;
                ScreenSurface screen(SCREEN_WIDTH, SCREEN_HEIGHT);

                
            //Create 2 SDL surface for the screen that just created.
                DisplaySurface backGround("bg.bmp", screen);
                DisplaySurface frontImage(
            "image.bmp", screen);
                
            //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
                
            //way2: If use blitToSurface, must get a copy of backGround.
            /*

                DisplaySurface backGroundCopy("bg.bmp", screen);
            */
                
            //AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

                
            //Blit backGround surface to screen and flip the screen.
                if ( backGround.blit() == false )
                    
            throw SDL_GetError();
                
            if ( screen.flip() == false )
                    
            throw SDL_GetError();

                
            //variable for main loop.
                
            //key event for up, down, left and right.
                Uint8* keys;
                
            //moving image's coordinate.
                int xpos = 0;
                
            int ypos = 0;
                
            //moving image's size.
                const int IMG_WIDTH = 128;
                
            const int IMG_HEIGHT = 128;
                
            //main loop.
                bool gameOver = false;
                
            while( gameOver == false ){
                    
            //press ESC or click X to quit.
                    SDL_Event gameEvent;
                    
            while ( SDL_PollEvent(&gameEvent) != 0 ){
                        
            if ( gameEvent.type == SDL_QUIT ){
                            gameOver 
            = true;
                        }
                        
            if ( gameEvent.type == SDL_KEYUP ){
                            
            if ( gameEvent.key.keysym.sym == SDLK_ESCAPE ){
                                gameOver 
            = true;
                            }
                        }
                    }
                    
            //key event to move image.
                    keys = SDL_GetKeyState(0);
                    
            if ( keys[SDLK_UP] || keys[SDLK_w] ){
                        ypos 
            -= 1;
                    }
                    
            if ( keys[SDLK_DOWN]|| keys[SDLK_s] ){
                        ypos 
            += 1;
                    }
                    
            if ( keys[SDLK_LEFT]|| keys[SDLK_a] ){
                        xpos 
            -= 1;
                    }
                    
            if ( keys[SDLK_RIGHT]|| keys[SDLK_d] ){
                        xpos 
            += 1;
                    }

                    
            //Hold moving image on the backGround area.
                    if ( xpos <= 0 )
                        xpos 
            = 0;
                    
            if ( xpos >= SCREEN_WIDTH - IMG_WIDTH )
                        xpos 
            = SCREEN_WIDTH - IMG_WIDTH;
                    
            if ( ypos <= 0 )
                        ypos 
            = 0;
                    
            if ( ypos >= SCREEN_HEIGHT - IMG_HEIGHT )
                        ypos 
            = SCREEN_HEIGHT - IMG_HEIGHT;

                    
            //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
                    
            //way1: blit surface to screen
                    
            //Blit a part of backGround ( a rectangular area ) to screen,
                    
            //then the original blitted backGround can be "cleaned".
                    if ( backGround.blit(xpos, ypos, xpos, ypos, IMG_WIDTH, IMG_HEIGHT) == false )
                        
            throw SDL_GetError();
                    
            //Blit the image to screen.
                    if ( frontImage.blit(xpos, ypos) == false )
                        
            throw SDL_GetError();
                    
            //Flip the screen
                    if ( screen.flip() == false )
                        
            throw SDL_GetError();
                    
            //AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

                    
            //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
                    
            //way2: blit surface to surface, then blit the last surface to screen
            /*

                    if ( backGroundCopy.blitToSurface(backGround, xpos, ypos, xpos, ypos, IMG_WIDTH, IMG_HEIGHT) == false )
                        throw SDL_GetError();
                    if ( frontImage.blitToSurface(backGround, xpos, ypos) == false )
                        throw SDL_GetError();
                    if ( backGround.blit() == false )
                        throw SDL_GetError();
                    if ( screen.flip() == false )
                        throw SDL_GetError();
            */
                    
            //AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
                }

                
            return;
            }
            posted on 2008-02-21 15:22 lf426 閱讀(3045) 評(píng)論(4)  編輯 收藏 引用 所屬分類(lèi): SDL入門(mén)教程

            FeedBack:
            # re: SDL入門(mén)教程(五):5、本章范例的完整源代碼[未登錄](méi) 2008-09-16 09:21 伊凡
            我把
            //key event to move image.
            keys = SDL_GetKeyState(0);
            if ( keys[SDLK_UP] || keys[SDLK_w] ){
            ypos -= 1;
            }
            if ( keys[SDLK_DOWN]|| keys[SDLK_s] ){
            ypos += 1;
            }
            if ( keys[SDLK_LEFT]|| keys[SDLK_a] ){
            xpos -= 1;
            }
            if ( keys[SDLK_RIGHT]|| keys[SDLK_d] ){
            xpos += 1;
            }

            改成
            //key event to move image.
            keys = SDL_GetKeyState(0);
            if ( keys[SDLK_UP] || keys[SDLK_w] ){
            ypos -= 3;
            }
            if ( keys[SDLK_DOWN]|| keys[SDLK_s] ){
            ypos += 3;
            }
            if ( keys[SDLK_LEFT]|| keys[SDLK_a] ){
            xpos -= 3;
            }
            if ( keys[SDLK_RIGHT]|| keys[SDLK_d] ){
            xpos += 3;
            }
            以后出現(xiàn)疊影。如果用變量來(lái)代替數(shù)字來(lái)實(shí)現(xiàn)變速疊影效果就更明顯。
            我想知道是為什么?顯卡刷新速度不夠快么?  回復(fù)  更多評(píng)論
              
            # re: SDL入門(mén)教程(五):5、本章范例的完整源代碼[未登錄](méi) 2008-09-16 11:17 lf426
            增加delta_x和delta_y的值。  回復(fù)  更多評(píng)論
              
            # re: SDL入門(mén)教程(五):5、本章范例的完整源代碼[未登錄](méi) 2008-09-16 14:39 伊凡
            謝謝你的回答
            還有這里是不是寫(xiě)多了?
            if ( SDL_Init(SDL_INIT_VIDEO < 0 ) )
            throw SDL_GetError();
            難道不是
            if ( SDL_Init(SDL_INIT_VIDEO )< 0 )
            throw SDL_GetError();

              回復(fù)  更多評(píng)論
              
            # re: SDL入門(mén)教程(五):5、本章范例的完整源代碼[未登錄](méi) 2008-09-16 22:37 lf426
            是我寫(xiě)錯(cuò)了,謝謝提醒。教程里面的源代碼還很不成熟,更進(jìn)一步的代碼請(qǐng)?jiān)凇癿ySDL_GameEngine”中察看。  回復(fù)  更多評(píng)論
              
            色综合久久88色综合天天| 久久久久久人妻无码| 亚洲精品tv久久久久久久久久| 四虎影视久久久免费| 无码人妻久久久一区二区三区| 精品综合久久久久久888蜜芽| 欧美久久精品一级c片片| 欧美粉嫩小泬久久久久久久| 精品久久久中文字幕人妻| 97热久久免费频精品99| 久久久受www免费人成| 久久精品国产亚洲av麻豆蜜芽| 韩国无遮挡三级久久| 亚洲国产日韩欧美久久| 久久精品国产亚洲AV无码偷窥 | 色综合久久88色综合天天 | 欧美综合天天夜夜久久| 国内精品伊人久久久影院| 久久99中文字幕久久| 噜噜噜色噜噜噜久久| www.久久热| 亚洲精品无码久久久久去q| 国产精品无码久久综合网| 狠狠综合久久综合88亚洲| 国产叼嘿久久精品久久| 久久久久亚洲av无码专区喷水 | 亚洲一区精品伊人久久伊人| 狠色狠色狠狠色综合久久 | 欧美午夜精品久久久久久浪潮| 99久久精品毛片免费播放| 久久99这里只有精品国产| 久久国产成人| 精品综合久久久久久97超人| 亚洲伊人久久综合中文成人网| 久久九九亚洲精品| 无码人妻少妇久久中文字幕蜜桃| 香蕉99久久国产综合精品宅男自| 免费国产99久久久香蕉| 久久精品aⅴ无码中文字字幕不卡| 超级碰碰碰碰97久久久久| 精品无码久久久久久久久久 |