青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆 - 96  文章 - 255  trackbacks - 0
<2008年3月>
2425262728291
2345678
9101112131415
16171819202122
23242526272829
303112345

E-mail:zbln426@163.com QQ:85132383 長期尋找對戰略游戲感興趣的合作伙伴。

常用鏈接

留言簿(21)

隨筆分類

隨筆檔案

SDL相關網站

我的個人網頁

我的小游戲

資源下載

搜索

  •  

積分與排名

  • 積分 - 495175
  • 排名 - 39

最新評論

閱讀排行榜

評論排行榜

注意:ttf字庫文件,可以在
C:\WINDOWS\Fonts
下尋找,比如例子中用到的times.ttf;
lazy.ttf請到Lazy Foo的相關教程True Type Fonts后面的示例中獲得。

//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"
#include 
"SDL/SDL_ttf.h"

class ScreenSurface
{
private:
    
static int screenNum;
    
int width;
    
int height;
    
int bpp;
    Uint32 flags;
    SDL_Surface
* pScreen;
    
char* windowName;
public:
    ScreenSurface();
    ScreenSurface(
int w, int h, char* window_name = 0int 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;
    
//for TextSurafce
    static int textNum;
    TTF_Font
* pFont;
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);
protected:
    
//for TextSurface
    DisplaySurface(const std::string& msg_name, const std::string& message, const ScreenSurface& screen,
                    Uint8 r, Uint8 g, Uint8 b, 
                    
const std::string& ttf_fileName, int ttf_size);
    
int tellTextNum() const;
    
void reduceTextNum();
    
void deleteFontPoint();
};

class TextSurface: public DisplaySurface
{
public:
    TextSurface(
const std::string& msg_name, const std::string& message, const ScreenSurface& screen,
                    Uint8 r 
= 0xFF, Uint8 g = 0xFF, Uint8 b = 0xFF
                    
const std::string& ttf_fileName = "lazy.ttf"int ttf_size = 28);
    
~TextSurface();
};

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), windowName(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, char* window_name, 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
++;
    
if ( window_name != 0 ) {
        windowName 
= window_name;
        SDL_WM_SetCaption(windowName, 
0);
    }
    
else
        windowName 
= 0;
}

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

int DisplaySurface::textNum = 0;

DisplaySurface::DisplaySurface(
const std::string& file_name, const ScreenSurface& screen):
fileName(file_name), pFont(
0)
{
    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());
}

//for TextSurface
DisplaySurface::DisplaySurface(const std::string& msg_name, const std::string& message, const ScreenSurface& screen,
                    Uint8 r, Uint8 g , Uint8 b, 
                    
const std::string& ttf_fileName, int ttf_size):
fileName(msg_name)
{
    
if ( textNum == 0 )
        
if ( TTF_Init() < 0 )
            
throw ErrorInfo("TTF_Init() failed!");
    
    SDL_Color textColor;
    textColor.r 
= r;
    textColor.g 
= g;
    textColor.b 
= b;

    pFont 
= TTF_OpenFont(ttf_fileName.c_str(), ttf_size);
    
if ( pFont == 0 )
        
throw ErrorInfo("TTF_OpenFont() failed!");

    pSurface 
= TTF_RenderText_Solid(pFont, message.c_str(), textColor);
    
if ( pSurface == 0 )
        
throw ErrorInfo("TTF_RenderText_solid() failed!");
    pScreen 
= screen.point();

    textNum
++;
}

int DisplaySurface::tellTextNum() const
{
    
return textNum;
}

void DisplaySurface::reduceTextNum()
{
    textNum
--;
}

void DisplaySurface::deleteFontPoint()
{
    TTF_CloseFont(pFont);
}

//AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

//VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
//class TextSurface

TextSurface::TextSurface(
const std::string& msg_name, const std::string& message, const ScreenSurface& screen,
                    Uint8 r, Uint8 g, Uint8 b, 
                    
const std::string& ttf_fileName, int ttf_size):
DisplaySurface(msg_name, message, screen, r, g, b, ttf_fileName, ttf_size)
{}

TextSurface::
~TextSurface()
{
    deleteFontPoint();
    reduceTextNum();
    
if ( tellTextNum() == 0 )
        TTF_Quit();
}

//AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

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

    
//Load a textSurface
    TextSurface myText("logo""UVi Soft", screen, 000xFF"times.ttf"80);
    TextSurface lazy(
"lazy""by lf426 (zbln426@163.com)", screen, 0xff00);
    
//Display text
    myText.blit(170180);
    lazy.blit(
150,400);
    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-24 20:31 lf426 閱讀(2360) 評論(2)  編輯 收藏 引用 所屬分類: SDL入門教程

FeedBack:
# re: SDL入門教程(九):2、顯示文本的完整代碼 2008-07-20 14:58 zhou
class TextSurface: public DisplaySurface
{
};

DisplaySurface作為父類需要提供虛析構函數,否則:

DisplaySurface*psurface = new TextSurface;
delete psurface;

這樣調用時,TextSurface的析構函數不會被調用。
  回復  更多評論
  
# re: SDL入門教程(九):2、顯示文本的完整代碼[未登錄] 2008-07-21 13:38 lf426
謝謝。你說得很對,當初在開始寫本節教程的時候還沒有把SDL幾種surface的關系整理清晰。在后面的SurfaceClass中,我設置了一個BaseSurface作為Picture和Text兩種面的父類,這個面就使用了虛析構函數了。更新的代碼在“mySDL_GameEngine”,請不吝賜教,萬分感謝。  回復  更多評論
  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              欧美精品一区二区三区高清aⅴ| 国产日韩欧美一区| 亚洲激情啪啪| 久久精品中文字幕一区二区三区 | 欧美午夜一区二区| 国产精品久久久久天堂| 国产精品国产成人国产三级| 国产欧美日韩亚洲| 狠狠爱综合网| 亚洲伦理在线观看| 一区二区激情小说| 亚洲女人天堂av| 久久一区二区三区四区| 久久99伊人| 欧美国产日韩二区| 免费影视亚洲| 日韩一本二本av| 欧美一区二区啪啪| 欧美va亚洲va日韩∨a综合色| 欧美激情偷拍| 国产精品视频精品视频| 亚洲国产一区二区三区高清| 亚洲视频导航| 蜜桃久久精品乱码一区二区| 99精品欧美一区二区三区综合在线| 亚洲欧美精品伊人久久| 欧美激情亚洲| 一区在线视频| 欧美亚洲日本网站| 亚洲全黄一级网站| 欧美呦呦网站| 国产精品v欧美精品∨日韩| 在线看片成人| 久久精品亚洲精品| 亚洲人被黑人高潮完整版| 欧美在线观看一区二区三区| 欧美三级午夜理伦三级中文幕| 好吊色欧美一区二区三区四区| 一区二区不卡在线视频 午夜欧美不卡在 | 国产精品自在欧美一区| 亚洲国产精品尤物yw在线观看| 午夜在线不卡| 久久亚洲一区二区三区四区| 西瓜成人精品人成网站| 亚洲国产天堂久久国产91| 午夜精品偷拍| 国产欧美日韩综合一区在线观看| 99国产精品久久久久久久久久| 浪潮色综合久久天堂| 亚洲欧美在线观看| 国产精品系列在线播放| 亚洲综合另类| 亚洲天堂成人在线视频| 欧美揉bbbbb揉bbbbb| 宅男精品视频| 日韩视频三区| 国产精品久久久对白| 亚洲综合精品一区二区| 亚洲私人影院在线观看| 欧美午夜免费| 午夜精品在线观看| 亚洲欧美中文另类| 国产一区二区黄色| 久久久久国产精品一区| 欧美一区2区三区4区公司二百| 欧美一级在线视频| 亚洲天堂网在线观看| 欧美日韩在线一区| 亚洲欧美另类久久久精品2019| 一本色道久久综合亚洲精品婷婷 | 欧美日韩亚洲一区二区三区在线观看 | 国产综合久久| 狼狼综合久久久久综合网 | 亚洲电影av在线| 欧美激情 亚洲a∨综合| 夜色激情一区二区| 亚洲午夜av在线| 欧美一区二区三区日韩视频| 欧美三级在线视频| 国产农村妇女精品一区二区| 一区视频在线看| 欧美国产日韩一区二区在线观看| 久久久午夜电影| 日韩视频永久免费观看| 一区二区三区国产| 国产美女精品免费电影| 蜜臀av一级做a爰片久久| 欧美激情久久久久| 午夜精品国产更新| 久久久久久久久久久成人| 亚洲人成艺术| 亚洲综合成人在线| 亚洲黄色大片| 亚洲男人的天堂在线aⅴ视频| 精品51国产黑色丝袜高跟鞋| 韩国视频理论视频久久| 欧美黑人国产人伦爽爽爽| 亚洲国产精品专区久久| 午夜激情一区| 久久精品色图| 一本一本a久久| 欧美影院一区| 国产精品99久久久久久宅男 | 免费观看一级特黄欧美大片| 欧美v日韩v国产v| 欧美亚洲日本网站| 欧美精品1区2区3区| 久久岛国电影| 亚洲综合精品一区二区| 亚洲精品少妇| 亚洲一区在线免费观看| 好吊一区二区三区| 在线观看日韩av| 在线亚洲欧美视频| 影音先锋亚洲精品| 亚洲制服少妇| 亚洲视频在线二区| 久久久亚洲影院你懂的| 久久超碰97人人做人人爱| 欧美日韩在线精品| 亚洲人成网站在线播| 一区二区三区在线观看国产| 亚洲欧美日本另类| 香蕉久久精品日日躁夜夜躁| 欧美日韩一区二区免费视频| 亚洲国产cao| 亚洲高清视频一区二区| 久久国产福利国产秒拍| 久久国产精品久久w女人spa| 国产精品久久久久一区二区三区共 | 亚洲视频国产视频| 欧美激情第4页| 亚洲电影观看| 亚洲黄色av| 欧美成人一区在线| 欧美福利小视频| 亚洲国产美女| 欧美韩日一区| 99精品国产高清一区二区| 在线亚洲欧美专区二区| 欧美a级大片| 亚洲精品一区二区网址| 在线视频精品一区| 国产精品xnxxcom| 亚洲亚洲精品在线观看 | 一区二区三区产品免费精品久久75| 美女黄色成人网| 最新精品在线| 亚洲中午字幕| 欧美日韩另类字幕中文| 亚洲精品国产精品国自产观看| 一级成人国产| 国产精自产拍久久久久久蜜| 欧美一区二区视频网站| 欧美不卡视频一区发布| 日韩五码在线| 国产精自产拍久久久久久蜜| 久久精品一区二区| 亚洲欧洲综合| 欧美中文字幕在线| 亚洲国产欧美久久| 国产精品xnxxcom| 久久久久久夜| 99成人在线| 午夜精品偷拍| 一区二区视频在线观看| 欧美激情综合| 欧美影片第一页| 亚洲高清自拍| 亚洲视频电影图片偷拍一区| 亚洲美女中文字幕| 国产精品日韩高清| 免费观看欧美在线视频的网站| 亚洲人成在线观看一区二区| 欧美亚洲网站| 亚洲精品乱码久久久久久| 欧美一级大片在线免费观看| 亚洲电影免费观看高清完整版在线| 在线一区日本视频| 国内揄拍国内精品久久| 欧美日本在线视频| 久久国产福利国产秒拍| av成人免费| 麻豆freexxxx性91精品| 亚洲一区二区三区久久| 在线看日韩av| 国产美女精品免费电影| 欧美日本免费| 久久亚洲国产精品一区二区| 日韩一区二区精品葵司在线| 久久综合网络一区二区| 亚洲一区二区在线看| 亚洲精品国产欧美| 黄色成人在线观看| 国产欧美一区二区精品秋霞影院| 欧美国产在线观看| 免费人成网站在线观看欧美高清| 西瓜成人精品人成网站| 夜夜嗨av一区二区三区| 亚洲国产日本|