• <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è)

            我的小游戲

            資源下載

            搜索

            •  

            積分與排名

            • 積分 - 492164
            • 排名 - 38

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            作者:龍飛

            6.1:用bool作為命令是不是畫(huà)蛇添足了?

                    不知道為什么,我總覺(jué)得總是用if結(jié)構(gòu)來(lái)調(diào)用命令讓人讀起程序來(lái)很不連貫。所以,我決定重新修改下,并且異常拋出改為使用類(lèi)對(duì)象,這樣是不是更C++一點(diǎn)呢?:)

            6.2:修改后的代碼。
            //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;
                
            void 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;
                
            void blit() const;
                
            void blit(int at_x, int at_y) const;
                
            void blit(int at_x, int at_y,
                            
            int from_x, int from_y, int w, int h,
                            
            int delta_x = 2int delta_y = 2const;
                
            void blitToSurface(const DisplaySurface& dst_surface,
                                    
            int at_x = 0int at_y = 0const;
                
            void 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;
            };

            class ErrorInfo
            {
            private:
                
            string info;
            public:
                ErrorInfo():info(
            "Unknown ERROR!")
                {}
                ErrorInfo(
            const char* c_str)
                {
                    info 
            = string(c_str);
                }
                ErrorInfo(
            const string& str):info(str)
                {}
                
            void show() const
                {
                    std::cerr 
            << info << std::endl;
                }
            };

            #endif

            #include "SurfaceClass.h"

            //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
            //class ScreenSurface

            int ScreenSurface::screenNum = 0;

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

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

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

            void ScreenSurface::flip() const
            {
                
            if ( SDL_Flip(pScreen) < 0 )
                    
            throw ErrorInfo(SDL_GetError());
            }

            //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 ErrorInfo(SDL_GetError());
                pScreen 
            = screen.point();
            }

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

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

            void DisplaySurface::blit() const
            {
                
            if ( SDL_BlitSurface(pSurface, 0, pScreen, 0< 0 )
                    
            throw ErrorInfo(SDL_GetError());
            }


            void 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 )
                    
            throw ErrorInfo(SDL_GetError());
            }

            void 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 )
                    
            throw ErrorInfo(SDL_GetError());
            }

            void 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 ErrorInfo("Cannot blit surface to itself!");

                
            if ( SDL_BlitSurface(pSurface, 0, dst_surface.point(), &offset) < 0 )
                    
            throw ErrorInfo(SDL_GetError());
            }

            void 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 ErrorInfo("Cannot blit surface to itself!");

                
            if ( SDL_BlitSurface(pSurface, &dest, dst_surface.point(), &offset) < 0 )
                    
            throw ErrorInfo(SDL_GetError());
            }

            //AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA


             

            #include "SurfaceClass.h"

            int game(int argc, char* argv[]);
            int main(int argc ,char* argv[])
            {
                
            int mainRtn = 0;
                
            try {
                    mainRtn 
            = game(argc, argv);
                }
                
            catch ( const ErrorInfo& info ) {
                    info.show();
                    
            return -1;
                }
                
                
            return mainRtn;
            }

            int game(int argc ,char* argv[])
            {
                
            //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.
                backGround.blit();
                screen.flip();

                
            //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".
                    backGround.blit(xpos, ypos, xpos, ypos, IMG_WIDTH, IMG_HEIGHT);
                    
            //Blit the image to screen.
                    frontImage.blit(xpos, ypos);
                    
            //Flip the screen
                    screen.flip();
                    
            //AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

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

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

                
            return 0;
            }
            posted on 2008-02-22 18:15 lf426 閱讀(2266) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): SDL入門(mén)教程
            999久久久免费国产精品播放| 久久久久综合国产欧美一区二区| MM131亚洲国产美女久久| 1000部精品久久久久久久久| 国产精品免费久久| 午夜人妻久久久久久久久| 国产精品成人无码久久久久久 | 97r久久精品国产99国产精| 91精品国产91久久久久久| 久久精品国产亚洲av麻豆图片 | 亚洲国产精品热久久| 久久婷婷国产剧情内射白浆| 日韩精品国产自在久久现线拍| 国产精品一区二区久久精品涩爱| 91亚洲国产成人久久精品网址| 亚洲国产精品无码久久一线| 欧美日韩中文字幕久久久不卡 | 久久综合久久综合九色| 77777亚洲午夜久久多喷| 欧美麻豆久久久久久中文| 一级做a爱片久久毛片| 国内精品久久久久伊人av| 中文字幕日本人妻久久久免费 | 久久香综合精品久久伊人| 九九精品久久久久久噜噜| 婷婷久久综合九色综合九七| 88久久精品无码一区二区毛片 | 污污内射久久一区二区欧美日韩 | 曰曰摸天天摸人人看久久久| 97久久天天综合色天天综合色hd | 91久久福利国产成人精品| 69久久精品无码一区二区| 欧美亚洲色综久久精品国产| 亚洲中文久久精品无码| 国产成人精品久久| 性欧美丰满熟妇XXXX性久久久 | 91精品日韩人妻无码久久不卡| 久久99精品综合国产首页| 久久久久综合网久久| 91久久福利国产成人精品| 国内精品久久久久久久影视麻豆|