LSDL-简易SDL封装之一

SDL是一个十分方便应用的开源游戏库.可方便在各种平台进行游戏开发,最近我有幸接触了这个东西,虽然个人美工太差,但是发现这个库还是很有乐趣的.

如果您对SDL有兴趣,可以到SDL官方进行了解:http://www.libsdl.org/

由于SDL使用标准C开发,所以在使用上可能有些不大方便,因此我也有了想要对它进行个简单封装的想法,同时,看了"再别流年的技术实验室"的文章,感觉收获不小.

这也算是一个系列日记的第一篇了,这个系列主要是我在前几天对SDL做个一个十分十分简单的封装,我将之成为LSDL(您可以理解成Lite SDL或者Leven SDL呵呵,无聊的命名,不过为了懒得打字,以后就成为LSDL了),(,的确如此,个人水平有限),对整个封装设计过程的一个总结,感觉国内SDL的文章太少,也算是出点微薄之力吧.


说明:这次的LSDL本人只在win32+VC下进行了测试,不保证其他平台下的正常使用,同时也不保证在WIN32+vc下没有bug.

首先,为了更好的封装,LSDL将对几个功能库进行独立封装.目前已经完成的有Iconv和SDL_ttf库.

首先给出这两个封装类的结构:


这也是LSDL中的两个游离类,首先看Encoder,这个class的出现是为了解决SDL编码问题,编码问题往往是个非常麻烦的问题,各种平台的默认编码不同,编译器可能的处理也不同,为了统一处理,我们一般在程序中使用iconv来处理编码转换来统一字符编码问题,当然在LSDL中也是一样(SDL也封装了iconv,看个人喜好了),Encoder封装了iconv的编码转换,使得转换工作更加方便.
从图中可以看出,这是一个模板类,接受两个模板参数,T1是目标编码类型,T2是原始编码类型,同时,构造函数也接受两个参数,分别时原始编码和目标编码,比如我们要从char的GBK编码转换成wchar_t的UNICODE编码时,只需要定义
Encoder<wchar_t,char> encoder("Utf-16","GBK");
然后再使用Convert方法来进行编码转换,同时注意,Convert得到的字符串必须使用delete销毁,应用实例代码:

C++代码
  1. Encoder<Uint16,char> encoder("utf-16","GBK");   
  2. Uint16 *str=encoder.Convert("将该字符串由GBK转换成为Utf16.");   
  3. ....   
  4. delete str;  

第二个是TTFFont类,该类封装了SDL_ttf的主要功能,能方便在SDL的Surface中绘制文字.从图中可以看到TTFFont主要功能就是Render字符串,他的使用也是非常简单的,但是在初始化这个对象前必须调用静态方法TTFFont::Init()对SDL_ttf进行初始化,完成之后调用TTFFont::Quit()结束.下面给出一个结合上面的Encoder类和TTFFont的简单实例:

 

C++代码
  1. #include <iostream>   
  2. #include <string>   
  3.   
  4. #include "LSDL/LsdlDefs.h"   
  5. #include "LSDL/Encoder.h"   
  6. #include "LSDL/TTFFont.h"   
  7. #include "LSDL/Screen.h"   
  8.   
  9. #pragma comment(lib,"SDL.lib")   
  10. #pragma comment(lib,"SDLmain.lib")   
  11. #pragma comment(lib,"SDL_ttf.lib")   
  12. #pragma comment(lib,"iconv.lib")   
  13. #pragma comment(lib,"LSDL.Library.lib")   
  14.   
  15. using namespace std;   
  16.   
  17. int main(int argc, char *argv[])   
  18. {   
  19.     SDL_Init(SDL_INIT_VIDEO);   
  20.     TTFFont::Init();   
  21.     Screen screen; //目前只需要知道它是对Screen的封装,TTFFont将在它上面Render文字   
  22.     TTFFont font;   
  23.     Encoder<char,char> encoder("utf-8","GBK");   
  24.     char *msgStr;   
  25.     SDL_Color fontColor;   
  26.     fontColor.r=255;   
  27.     fontColor.g=255;   
  28.     fontColor.b=255;   
  29.     try{   
  30.         screen.Init(640,480,32,0); //初始化screen对象   
  31.         font.Load("E:libraryinMSYH.TTF",25); //载入字体文件,如果您要编译,请修改该路径   
  32.         msgStr=encoder.Convert("LevenBlog成功开发完成,闲来就看看SDL吧."); //字符串编码转换   
  33.     }catch(LException &e){   
  34.         cout<<"Error:"<<e.getError()<<endl;   
  35.         exit(-1);   
  36.     }   
  37.     font.RenderUtf8(screen,msgStr,fontColor,0,0); //将字符串Render到screen上   
  38.     screen.Flip();   
  39.     //消息循环   
  40.     bool gameOver=false;   
  41.     SDL_Event ev;   
  42.     while(!gameOver){   
  43.         while(!SDL_PollEvent(&ev)){   
  44.             if(ev.type == SDL_QUIT){   
  45.                 cout<<"Close Window."<<endl;   
  46.                 gameOver=true;   
  47.                 break;   
  48.             }else if(ev.type == SDL_KEYDOWN){   
  49.                 if(ev.key.keysym.sym == SDLK_ESCAPE){   
  50.                     cout<<"Press ESC to exit."<<endl;   
  51.                     gameOver=true;   
  52.                     break;   
  53.                 }   
  54.             }   
  55.         }   
  56.     }   
  57.     delete msgStr;   
  58.     TTFFont::Quit();   
  59.     SDL_Quit();   
  60.     return 0;   
  61. }   

 

编译执行,结果如图所示:

本文最后将提供该Demo的完整工程下载(基于VS2008,请自行安装SDL等相关库)

呵呵,使用起来还是很简单吧.今天就到此为止了.最后给出使用TTFFont和Encoder的详细代码和Demo工程文件下载:

Encoder.h

C++代码
  1. #ifndef _INCLUDE_ENCODER_   
  2. #define _INCLUDE_ENCODER_   
  3.   
  4. #include <string.h>   
  5. #include <iconv.h>   
  6.   
  7. template <typename T1, typename T2>   
  8. class Encoder   
  9. {   
  10. private:   
  11.     iconv_t cd;   
  12. private:   
  13.     inline size_t getSize(const T2 *s);   
  14. public:   
  15.     Encoder(const char *toCode, const char *fromCode);   
  16.     ~Encoder();   
  17.     inline T1 *Convert(const T2 *inStr);   
  18. };   
  19.   
  20. template <typename T1, typename T2>   
  21. Encoder<T1,T2>::Encoder(const char *toCode, const char *fromCode)   
  22. {   
  23.     cd=iconv_open(toCode,fromCode);   
  24.     if(cd == 0)   
  25.         throw "libiconv Init Error.";   
  26. }   
  27.   
  28. template <typename T1, typename T2>   
  29. Encoder<T1,T2>::~Encoder()   
  30. {   
  31.     iconv_close(cd);   
  32. }   
  33.   
  34. template <typename T1, typename T2>   
  35. size_t Encoder<T1,T2>::getSize(const T2 *s)   
  36. {   
  37.     size_t result=0;   
  38.     const T2 *p;   
  39.     p=s;   
  40.     while(*p != 0)   
  41.     {   
  42.         p++;   
  43.         result++;   
  44.     }   
  45.     return result;   
  46. }   
  47.   
  48. template <typename T1, typename T2>   
  49. T1 *Encoder<T1,T2>::Convert(const T2 *inStr)   
  50. {   
  51.     T1 *buffer;   
  52.     T1 *p;   
  53.     size_t outSize=this->getSize(inStr) * sizeof(T2) * 2 + 2;   
  54.     size_t inSize=this->getSize(inStr);   
  55.     buffer=new T1[outSize + 1];   
  56.     memset(buffer,0,outSize + 1);   
  57.     p=buffer;   
  58.     if(iconv(cd,(const char **)&inStr,&inSize,(char **)&p,&outSize) == -1)   
  59.     {   
  60.         throw "Convert Error.";   
  61.     }   
  62.     return buffer;   
  63. }   
  64.   
  65.   
  66. #endif   
  67.   

 

TTFFont.h

C++代码
  1. #ifndef _INCLUDE_TTFFONT_   
  2. #define _INCLUDE_TTFFONT_   
  3.   
  4. #include "LsdlDefs.h"   
  5. #include "ISurface.h"   
  6.   
  7. #include <SDL/SDL_ttf.h>   
  8.   
  9. class LSDLAPI TTFFont   
  10. {   
  11. private:   
  12.     char fontPath[PATH_MAX];   
  13.     int size;   
  14.     TTF_Font *font;   
  15. public:   
  16.     static int Init();   
  17.     static void Quit();   
  18.     TTFFont();   
  19.     TTFFont(const char *path, int fontSize);   
  20.     ~TTFFont();   
  21.     void Load(const char *path, int fontSize);   
  22.     void Render(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect);   
  23.     void Render(ISurface &surface, const char *str, SDL_Color color, int x, int y);   
  24.     void Render(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, SDL_Rect rect);   
  25.     void Render(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, int x, int y);   
  26.     void RenderSolid(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect);   
  27.     void RenderSolid(ISurface &surface, const char *str, SDL_Color color, int x, int y);   
  28.     void RenderUtf8(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect);   
  29.     void RenderUtf8(ISurface &surface, const char *str, SDL_Color color, int x, int y);   
  30.     void RenderUtf8(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, SDL_Rect rect);   
  31.     void RenderUtf8(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, int x ,int y);   
  32.     void RenderUtf8Solid(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect);   
  33.     void RenderUtf8Solid(ISurface &surface, const char *str, SDL_Color color, int x, int y);   
  34.     void RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Rect rect);   
  35.     void RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, int x, int y);   
  36.     void RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Color bc, SDL_Rect rect);   
  37.     void RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Color bc, int x, int y);   
  38.     void RenderUnicodeSolid(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Rect rect);   
  39.     void RenderUnicodeSolid(ISurface &surface, const Uint16 *str, SDL_Color color, int x, int y);   
  40. };   
  41.   
  42.   
  43. #endif  

 

TTFFont.cpp

C++代码
  1. #include "TTFFont.h"   
  2.   
  3. int TTFFont::Init()   
  4. {   
  5.     return TTF_Init();   
  6. }   
  7.   
  8. void TTFFont::Quit()   
  9. {   
  10.     TTF_Quit();   
  11. }   
  12.   
  13. TTFFont::TTFFont()   
  14. {   
  15.     font=NULL;   
  16. }   
  17.   
  18. TTFFont::TTFFont(const char *path, int fontSize)   
  19. {   
  20.     strcpy(fontPath, path);   
  21.     size=fontSize;   
  22.     font=TTF_OpenFont(fontPath,size);   
  23.     if(font == 0)   
  24.         throw LException("Open font file Error.");   
  25. }   
  26.   
  27. TTFFont::~TTFFont()   
  28. {   
  29.     if(font != NULL)   
  30.     {   
  31.         TTF_CloseFont(font);   
  32.     }   
  33. }   
  34.   
  35. void TTFFont::Load(const char *path, int fontSize)   
  36. {   
  37.     strcpy(fontPath, path);   
  38.     size=fontSize;   
  39.     font=TTF_OpenFont(fontPath,size);   
  40.     if(font == 0)   
  41.         throw LException("Open font file Error.");   
  42. }   
  43.   
  44. void TTFFont::Render(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect)   
  45. {   
  46.     SDL_Surface *text;   
  47.     text=TTF_RenderText_Blended(font,(const char *)str,color);   
  48.     SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);   
  49.     SDL_FreeSurface(text);   
  50. }   
  51.   
  52. void TTFFont::Render(ISurface &surface, const char *str, SDL_Color color, int x, int y)   
  53. {   
  54.     SDL_Rect rect;   
  55.     rect.x=x;   
  56.     rect.y=y;   
  57.     this->Render(surface,str,color,rect);   
  58. }   
  59.   
  60. void TTFFont::Render(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, SDL_Rect rect)   
  61. {   
  62.     SDL_Surface *text;   
  63.     text=TTF_RenderText_Shaded(font,str,color,bc);   
  64.     SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);   
  65.     SDL_FreeSurface(text);   
  66. }   
  67.   
  68. void TTFFont::Render(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, int x, int y)   
  69. {   
  70.     SDL_Rect rect;   
  71.     rect.x=x;   
  72.     rect.y=y;   
  73.     Render(surface,str,color,bc,rect);   
  74. }   
  75.   
  76. void TTFFont::RenderSolid(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect)   
  77. {   
  78.     SDL_Surface *text;   
  79.     text=TTF_RenderText_Solid(font,(const char *)str,color);   
  80.     SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);   
  81.     SDL_FreeSurface(text);   
  82. }   
  83.   
  84. void TTFFont::RenderSolid(ISurface &surface, const char *str, SDL_Color color, int x, int y)   
  85. {   
  86.     SDL_Rect rect;   
  87.     rect.x=x;   
  88.     rect.y=y;   
  89.     RenderSolid(surface,str,color,rect);   
  90. }   
  91.   
  92. void TTFFont::RenderUtf8(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect)   
  93. {   
  94.     SDL_Surface *text;   
  95.     text=TTF_RenderUTF8_Blended(font,(const char *)str,color);   
  96.     SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);   
  97.     SDL_FreeSurface(text);   
  98. }   
  99.   
  100. void TTFFont::RenderUtf8(ISurface &surface, const char *str, SDL_Color color, int x, int y)   
  101. {   
  102.     SDL_Rect rect;   
  103.     rect.x=x;   
  104.     rect.y=y;   
  105.     this->RenderUtf8(surface,str,color,rect);   
  106. }   
  107.   
  108. void TTFFont::RenderUtf8(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, SDL_Rect rect)   
  109. {   
  110.     SDL_Surface *text;   
  111.     text=TTF_RenderUTF8_Shaded(font,(const char *)str,color,bc);   
  112.     SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);   
  113.     SDL_FreeSurface(text);   
  114. }   
  115.   
  116. void TTFFont::RenderUtf8(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, int x ,int y)   
  117. {   
  118.     SDL_Rect rect;   
  119.     rect.x=x;   
  120.     rect.y=y;   
  121.     this->RenderUtf8(surface,str,color,bc,rect);   
  122. }   
  123.   
  124. void TTFFont::RenderUtf8Solid(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect)   
  125. {   
  126.     SDL_Surface *text;   
  127.     text=TTF_RenderUTF8_Solid(font,(const char *)str,color);   
  128.     SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);   
  129.     SDL_FreeSurface(text);   
  130. }   
  131.   
  132. void TTFFont::RenderUtf8Solid(ISurface &surface, const char *str, SDL_Color color, int x, int y)   
  133. {   
  134.     SDL_Rect rect;   
  135.     rect.x=x;   
  136.     rect.y=y;   
  137.     this->RenderUtf8(surface,str,color,rect);   
  138. }   
  139.   
  140. void TTFFont::RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Rect rect)   
  141. {   
  142.     SDL_Surface *text;   
  143.     text=TTF_RenderUNICODE_Blended(font,(const Uint16 *)str,color);   
  144.     SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);   
  145.     SDL_FreeSurface(text);   
  146. }   
  147.   
  148. void TTFFont::RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, int x, int y)   
  149. {   
  150.     SDL_Rect rect;   
  151.     rect.x=x;   
  152.     rect.y=y;   
  153.     this->RenderUnicode(surface,str,color,rect);   
  154. }   
  155.   
  156. void TTFFont::RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Color bc, SDL_Rect rect)   
  157. {   
  158.     SDL_Surface *text;   
  159.     text=TTF_RenderUNICODE_Shaded(font,(const Uint16 *)str,color,bc);   
  160.     SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);   
  161.     SDL_FreeSurface(text);   
  162. }   
  163.   
  164. void TTFFont::RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Color bc, int x, int y)   
  165. {   
  166.     SDL_Rect rect;   
  167.     rect.x=x;   
  168.     rect.y=y;   
  169.     this->RenderUnicode(surface,str,color,bc,rect);   
  170. }   
  171.   
  172. void TTFFont::RenderUnicodeSolid(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Rect rect)   
  173. {   
  174.     SDL_Surface *text;   
  175.     text=TTF_RenderUNICODE_Solid(font,(const Uint16 *)str,color);   
  176.     SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);   
  177.     SDL_FreeSurface(text);   
  178. }   
  179.   
  180. void TTFFont::RenderUnicodeSolid(ISurface &surface, const Uint16 *str, SDL_Color color, int x, int y)   
  181. {   
  182.     SDL_Rect rect;   
  183.     rect.x=x;   
  184.     rect.y=y;   
  185.     this->RenderUnicode(surface,str,color,rect);   
  186. }   

 文中Demo下载(VS2008工程):

下载文件点击下载此文件

Leven 于 2008.04.16

文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: SDL  C  C++  LSDL 
评论: 2 | 引用: 0 | 查看次数: 669
2 1/1 页
gatt [2008-05-20 13:14:38 ]
可不可以提供源码?
gatt@21cn.com
leven [2008-05-20 15:24:49 ]
我这边贴出的就是完整源码了啊。
不过还有一点没贴出来,等下一起给你发过去吧.
2 1/1 页
发表评论
用户名:
密 码: 游客发言不需要密码.
验证码: 验证码
内 容:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 500 字 | HTML代码允许 关闭 | 评论可修改 关闭