• <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年3月>
            2425262728291
            2345678
            9101112131415
            16171819202122
            23242526272829
            303112345

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

            常用鏈接

            留言簿(21)

            隨筆分類(lèi)

            隨筆檔案

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

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

            我的小游戲

            資源下載

            搜索

            •  

            積分與排名

            • 積分 - 492193
            • 排名 - 38

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

             

            //UVi Soft (2008)
            //Long Fei (lf426), E-mail: zbln426@163.com

            //FileName: SurfaceClass.h

            #ifndef SURFACE_CLASS_H
            #define SURFACE_CLASS_H

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

            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;
                
            void fillColor(Uint8 r = 0, Uint8 g = 0, Uint8 b = 0const;
            };

            class DisplaySurface
            {
            private:
                std::
            string fileName;
                SDL_Surface
            * pSurface;
                SDL_Surface
            * pScreen;
            public:
                DisplaySurface(
            const std::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 = 0int delta_y = 0const;
                
            void colorKey(Uint8 r = 0, Uint8 g = 0xFF, Uint8 b = 0xFF, Uint32 flag = SDL_SRCCOLORKEY);
            };

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

            #endif

            //UVi Soft (2008)
            //Long Fei (lf426), E-mail: zbln426@163.com

            #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());
            }


            void ScreenSurface::fillColor(Uint8 r, Uint8 g, Uint8 b) const
            {
                 
            if ( SDL_FillRect(pScreen, 0, SDL_MapRGB(pScreen->format, r, g, b)) < 0 )
                     
            throw ErrorInfo(SDL_GetError());
            }

            //AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

            //VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
            //class DisplaySurface

            DisplaySurface::DisplaySurface(
            const std::string& file_name, const ScreenSurface& screen):
            fileName(file_name)
            {
                SDL_Surface
            * pSurfaceTemp = IMG_Load(file_name.c_str());
                
            if ( pSurfaceTemp == 0 )
                    
            throw ErrorInfo(SDL_GetError());
                pSurface 
            = SDL_DisplayFormat(pSurfaceTemp);
                
            if ( pSurface == 0 )
                    
            throw ErrorInfo(SDL_GetError());
                SDL_FreeSurface(pSurfaceTemp);
                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::colorKey(Uint8 r, Uint8 g, Uint8 b, Uint32 flag)
            {
                Uint32 colorkey 
            = SDL_MapRGB(pSurface->format, r, g, b);
                
            if ( SDL_SetColorKey(pSurface, flag, colorkey ) < 0 )
                    
            throw ErrorInfo(SDL_GetError());
            }

            //AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

            //UVi Soft (2008)
            //Long Fei (lf426), E-mail: zbln426@163.com

            #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);
                
            //Fill background with white.(default is black)
                screen.fillColor(0xFF0xFF0xFF);

                
            //Load a sprite pic, and colorkey.(default color: R=0,G=0xFF,B=0xFF)
                DisplaySurface sprite("dots.png", screen);
                sprite.colorKey();

                
            //the 4 dots's coordinate.
                int atX[4= {05400540};
                
            int atY[4= {00380380};
                
            //the 4 dots's clip coordinate.
                int fromX[4= {01000 ,100};
                
            int fromY[4= {00100100};
                
            //dots's size.
                const int IMG_WIDTH = 100;
                
            const int IMG_HEIGHT = 100;
                
            //clip dots.
                for ( int i = 0; i < 4; i++ )
                    sprite.blit(atX[i], atY[i], fromX[i], fromY[i], IMG_WIDTH, IMG_HEIGHT);
                
            //Draw dots and screen.
                screen.flip();
                
                
            //press ESC or click X to quit.
                bool gameOver = false;
                SDL_Event gameEvent;
                
            while( gameOver == false ){
                    
            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;
                            }
                        }
                    }
                }

                
            return 0;
            }
            posted on 2008-03-20 12:46 lf426 閱讀(2964) 評(píng)論(5)  編輯 收藏 引用 所屬分類(lèi): SDL入門(mén)教程

            FeedBack:
            # re: SDL入門(mén)教程(八):2、裁剪精靈圖片的完整源代碼[未登錄](méi) 2008-09-24 14:51 伊凡
            做點(diǎn)小改動(dòng),結(jié)果小球運(yùn)動(dòng)的時(shí)候出現(xiàn)難看的“軌跡”
            不知道如何修改才能正常
            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);
            //Fill background with white.(default is black)
            screen.fillColor(0xFF, 0xFF, 0xFF);

            //Load a sprite pic, and colorkey.(default color: R=0,G=0xFF,B=0xFF)
            DisplaySurface sprite("dots.png", screen);
            sprite.colorKey();

            //the 4 dots's coordinate.
            int atX[4] = {0, 590, 0, 590};
            int atY[4] = {0, 0, 430, 430};
            //the 4 dots's clip coordinate.
            int fromX[4] = {0, 270, 0 ,270};
            int fromY[4] = {0, 0, 190, 190};
            //mouse's coordinate.
            int px = 0;
            int py = 0;
            //dots's size.
            const int IMG_WIDTH = 50;
            const int IMG_HEIGHT = 50;
            //clip dots.
            for ( int i = 0; i < 4; i++ )
            sprite.blit(atX[i], atY[i], fromX[i], fromY[i], IMG_WIDTH, IMG_HEIGHT);
            //Draw dots and screen.
            screen.flip();

            //press ESC or click X to quit.
            bool gameOver = false;
            SDL_Event gameEvent;
            while( gameOver == false ){
            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;
            }
            }
            //mouse event
            if ( gameEvent.type == SDL_MOUSEMOTION ) {
            px = gameEvent.motion.x;
            py = gameEvent.motion.y;
            }
            }
            for(int i = 0 ; i < 4 ;i++)
            {
            if ( atX[i] < px )
            atX[i]++;
            if ( atX[i] > px )
            atX[i]--;
            if ( atY[i] < py )
            atY[i]++;
            if ( atY[i] > py )
            atY[i]--;
            }
            //clip dots.
            for ( int i = 0; i < 4; i++ )
            sprite.blit(atX[i], atY[i], fromX[i], fromY[i], IMG_WIDTH, IMG_HEIGHT,2,2);
            //Draw dots and screen.
            screen.flip();
            }

            return 0;
            }  回復(fù)  更多評(píng)論
              
            # re: SDL入門(mén)教程(八):2、裁剪精靈圖片的完整源代碼[未登錄](méi) 2008-09-25 11:29 lf426
            每次畫(huà)圖前先清屏哈。最后一個(gè)循環(huán)改成:
            for ( int i = 0; i < 4; i++ ) {
            screen.fillColor(0xFF, 0xFF, 0xFF);
            sprite.blit(atX[i], atY[i], fromX[i], fromY[i], IMG_WIDTH, IMG_HEIGHT,2,2);
            }
            試試  回復(fù)  更多評(píng)論
              
            # re: SDL入門(mén)教程(八):2、裁剪精靈圖片的完整源代碼[未登錄](méi) 2008-09-27 09:55 伊凡
            非常感謝你的回答,我試了下,結(jié)果是這樣才正常
            //clip dots.
            for ( int i = 0; i < 4; i++ )
            sprite.blit(atX[i], atY[i], fromX[i], fromY[i], IMG_WIDTH, IMG_HEIGHT,2,2);

            //Draw dots and screen.
            screen.flip();
            screen.fillColor(0xFF, 0xFF, 0xFF);  回復(fù)  更多評(píng)論
              
            # re: SDL入門(mén)教程(八):2、裁剪精靈圖片的完整源代碼[未登錄](méi) 2008-09-27 09:56 伊凡
            如果這樣寫(xiě)
            for ( int i = 0; i < 4; i++ ) {
            screen.fillColor(0xFF, 0xFF, 0xFF);
            sprite.blit(atX[i], atY[i], fromX[i], fromY[i], IMG_WIDTH, IMG_HEIGHT,2,2);
            }

            就變成只有一個(gè)球了,也不太理解為什么會(huì)這樣。。。  回復(fù)  更多評(píng)論
              
            # re: SDL入門(mén)教程(八):2、裁剪精靈圖片的完整源代碼[未登錄](méi) 2008-09-27 11:17 lf426
            呵呵,我的失誤,忘記flip了。
            for ( int i = 0; i < 4; i++ ) {
            screen.fillColor(0xFF, 0xFF, 0xFF);
            sprite.blit(atX[i], atY[i], fromX[i], fromY[i], IMG_WIDTH, IMG_HEIGHT,2,2);
            screen.flip();
            }   回復(fù)  更多評(píng)論
              
            久久这里有精品视频| 69国产成人综合久久精品| 要久久爱在线免费观看| 97久久精品人妻人人搡人人玩| 88久久精品无码一区二区毛片 | 精品久久久久久亚洲| 亚洲精品无码久久久久sm| 欧美午夜A∨大片久久 | 久久99精品国产99久久6| 久久精品水蜜桃av综合天堂 | 久久精品嫩草影院| 婷婷久久综合| 99久久国产亚洲高清观看2024 | 无码乱码观看精品久久| 久久精品国产99国产精品澳门| 久久大香萑太香蕉av| 国内精品久久久久久麻豆| 蜜桃麻豆WWW久久囤产精品| 国产香蕉97碰碰久久人人| 国产成人无码久久久精品一| 成人国内精品久久久久一区| 99久久综合国产精品免费| 九九久久精品无码专区| 国产精品久久久久久福利漫画 | 18岁日韩内射颜射午夜久久成人| 亚洲精品美女久久777777| 中文字幕无码av激情不卡久久| 久久九九有精品国产23百花影院| 国产韩国精品一区二区三区久久| 精品综合久久久久久97| 国产精品久久久久9999高清| 欧洲人妻丰满av无码久久不卡| 99久久精品无码一区二区毛片| av国内精品久久久久影院| 色婷婷综合久久久久中文 | 亚洲天堂久久精品| 99久久免费国产精品热| 精品精品国产自在久久高清| 久久er99热精品一区二区| 久久久久成人精品无码中文字幕 | 久久精品国产黑森林|