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

E-mail:zbln426@163.com QQ:85132383 长期寻找对战略游戏感兴趣的合作伙伴。

常用链接

留言簿(21)

随笔分类

随笔档案

SDL相关网站

我的个人网页

我的小游戏

资源下载

搜索

  •  

积分与排名

  • 积分 - 484865
  • 排名 - 37

最新评论

阅读排行榜

评论排行榜

注意: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 阅读(2246) 评论(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”,请不吝赐教,万分感谢。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理