LSDL-简易SDL封装之一
作者:Leven 日期:2008-04-16 23:21:49
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销毁,应用实例代码:
- Encoder<Uint16,char> encoder("utf-16","GBK");
- Uint16 *str=encoder.Convert("将该字符串由GBK转换成为Utf16.");
- ....
- delete str;
第二个是TTFFont类,该类封装了SDL_ttf的主要功能,能方便在SDL的Surface中绘制文字.从图中可以看到TTFFont主要功能就是Render字符串,他的使用也是非常简单的,但是在初始化这个对象前必须调用静态方法TTFFont::Init()对SDL_ttf进行初始化,完成之后调用TTFFont::Quit()结束.下面给出一个结合上面的Encoder类和TTFFont的简单实例:
- #include <iostream>
- #include <string>
- #include "LSDL/LsdlDefs.h"
- #include "LSDL/Encoder.h"
- #include "LSDL/TTFFont.h"
- #include "LSDL/Screen.h"
- #pragma comment(lib,"SDL.lib")
- #pragma comment(lib,"SDLmain.lib")
- #pragma comment(lib,"SDL_ttf.lib")
- #pragma comment(lib,"iconv.lib")
- #pragma comment(lib,"LSDL.Library.lib")
- using namespace std;
- int main(int argc, char *argv[])
- {
- SDL_Init(SDL_INIT_VIDEO);
- TTFFont::Init();
- Screen screen; //目前只需要知道它是对Screen的封装,TTFFont将在它上面Render文字
- TTFFont font;
- Encoder<char,char> encoder("utf-8","GBK");
- char *msgStr;
- SDL_Color fontColor;
- fontColor.r=255;
- fontColor.g=255;
- fontColor.b=255;
- try{
- screen.Init(640,480,32,0); //初始化screen对象
- font.Load("E:libraryinMSYH.TTF",25); //载入字体文件,如果您要编译,请修改该路径
- msgStr=encoder.Convert("LevenBlog成功开发完成,闲来就看看SDL吧."); //字符串编码转换
- }catch(LException &e){
- cout<<"Error:"<<e.getError()<<endl;
- exit(-1);
- }
- font.RenderUtf8(screen,msgStr,fontColor,0,0); //将字符串Render到screen上
- screen.Flip();
- //消息循环
- bool gameOver=false;
- SDL_Event ev;
- while(!gameOver){
- while(!SDL_PollEvent(&ev)){
- if(ev.type == SDL_QUIT){
- cout<<"Close Window."<<endl;
- gameOver=true;
- break;
- }else if(ev.type == SDL_KEYDOWN){
- if(ev.key.keysym.sym == SDLK_ESCAPE){
- cout<<"Press ESC to exit."<<endl;
- gameOver=true;
- break;
- }
- }
- }
- }
- delete msgStr;
- TTFFont::Quit();
- SDL_Quit();
- return 0;
- }
编译执行,结果如图所示:
本文最后将提供该Demo的完整工程下载(基于VS2008,请自行安装SDL等相关库)
呵呵,使用起来还是很简单吧.今天就到此为止了.最后给出使用TTFFont和Encoder的详细代码和Demo工程文件下载:
Encoder.h
- #ifndef _INCLUDE_ENCODER_
- #define _INCLUDE_ENCODER_
- #include <string.h>
- #include <iconv.h>
- template <typename T1, typename T2>
- class Encoder
- {
- private:
- iconv_t cd;
- private:
- inline size_t getSize(const T2 *s);
- public:
- Encoder(const char *toCode, const char *fromCode);
- ~Encoder();
- inline T1 *Convert(const T2 *inStr);
- };
- template <typename T1, typename T2>
- Encoder<T1,T2>::Encoder(const char *toCode, const char *fromCode)
- {
- cd=iconv_open(toCode,fromCode);
- if(cd == 0)
- throw "libiconv Init Error.";
- }
- template <typename T1, typename T2>
- Encoder<T1,T2>::~Encoder()
- {
- iconv_close(cd);
- }
- template <typename T1, typename T2>
- size_t Encoder<T1,T2>::getSize(const T2 *s)
- {
- size_t result=0;
- const T2 *p;
- p=s;
- while(*p != 0)
- {
- p++;
- result++;
- }
- return result;
- }
- template <typename T1, typename T2>
- T1 *Encoder<T1,T2>::Convert(const T2 *inStr)
- {
- T1 *buffer;
- T1 *p;
- size_t outSize=this->getSize(inStr) * sizeof(T2) * 2 + 2;
- size_t inSize=this->getSize(inStr);
- buffer=new T1[outSize + 1];
- memset(buffer,0,outSize + 1);
- p=buffer;
- if(iconv(cd,(const char **)&inStr,&inSize,(char **)&p,&outSize) == -1)
- {
- throw "Convert Error.";
- }
- return buffer;
- }
- #endif
TTFFont.h
- #ifndef _INCLUDE_TTFFONT_
- #define _INCLUDE_TTFFONT_
- #include "LsdlDefs.h"
- #include "ISurface.h"
- #include <SDL/SDL_ttf.h>
- class LSDLAPI TTFFont
- {
- private:
- char fontPath[PATH_MAX];
- int size;
- TTF_Font *font;
- public:
- static int Init();
- static void Quit();
- TTFFont();
- TTFFont(const char *path, int fontSize);
- ~TTFFont();
- void Load(const char *path, int fontSize);
- void Render(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect);
- void Render(ISurface &surface, const char *str, SDL_Color color, int x, int y);
- void Render(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, SDL_Rect rect);
- void Render(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, int x, int y);
- void RenderSolid(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect);
- void RenderSolid(ISurface &surface, const char *str, SDL_Color color, int x, int y);
- void RenderUtf8(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect);
- void RenderUtf8(ISurface &surface, const char *str, SDL_Color color, int x, int y);
- void RenderUtf8(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, SDL_Rect rect);
- void RenderUtf8(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, int x ,int y);
- void RenderUtf8Solid(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect);
- void RenderUtf8Solid(ISurface &surface, const char *str, SDL_Color color, int x, int y);
- void RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Rect rect);
- void RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, int x, int y);
- void RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Color bc, SDL_Rect rect);
- void RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Color bc, int x, int y);
- void RenderUnicodeSolid(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Rect rect);
- void RenderUnicodeSolid(ISurface &surface, const Uint16 *str, SDL_Color color, int x, int y);
- };
- #endif
TTFFont.cpp
- #include "TTFFont.h"
- int TTFFont::Init()
- {
- return TTF_Init();
- }
- void TTFFont::Quit()
- {
- TTF_Quit();
- }
- TTFFont::TTFFont()
- {
- font=NULL;
- }
- TTFFont::TTFFont(const char *path, int fontSize)
- {
- strcpy(fontPath, path);
- size=fontSize;
- font=TTF_OpenFont(fontPath,size);
- if(font == 0)
- throw LException("Open font file Error.");
- }
- TTFFont::~TTFFont()
- {
- if(font != NULL)
- {
- TTF_CloseFont(font);
- }
- }
- void TTFFont::Load(const char *path, int fontSize)
- {
- strcpy(fontPath, path);
- size=fontSize;
- font=TTF_OpenFont(fontPath,size);
- if(font == 0)
- throw LException("Open font file Error.");
- }
- void TTFFont::Render(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect)
- {
- SDL_Surface *text;
- text=TTF_RenderText_Blended(font,(const char *)str,color);
- SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);
- SDL_FreeSurface(text);
- }
- void TTFFont::Render(ISurface &surface, const char *str, SDL_Color color, int x, int y)
- {
- SDL_Rect rect;
- rect.x=x;
- rect.y=y;
- this->Render(surface,str,color,rect);
- }
- void TTFFont::Render(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, SDL_Rect rect)
- {
- SDL_Surface *text;
- text=TTF_RenderText_Shaded(font,str,color,bc);
- SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);
- SDL_FreeSurface(text);
- }
- void TTFFont::Render(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, int x, int y)
- {
- SDL_Rect rect;
- rect.x=x;
- rect.y=y;
- Render(surface,str,color,bc,rect);
- }
- void TTFFont::RenderSolid(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect)
- {
- SDL_Surface *text;
- text=TTF_RenderText_Solid(font,(const char *)str,color);
- SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);
- SDL_FreeSurface(text);
- }
- void TTFFont::RenderSolid(ISurface &surface, const char *str, SDL_Color color, int x, int y)
- {
- SDL_Rect rect;
- rect.x=x;
- rect.y=y;
- RenderSolid(surface,str,color,rect);
- }
- void TTFFont::RenderUtf8(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect)
- {
- SDL_Surface *text;
- text=TTF_RenderUTF8_Blended(font,(const char *)str,color);
- SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);
- SDL_FreeSurface(text);
- }
- void TTFFont::RenderUtf8(ISurface &surface, const char *str, SDL_Color color, int x, int y)
- {
- SDL_Rect rect;
- rect.x=x;
- rect.y=y;
- this->RenderUtf8(surface,str,color,rect);
- }
- void TTFFont::RenderUtf8(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, SDL_Rect rect)
- {
- SDL_Surface *text;
- text=TTF_RenderUTF8_Shaded(font,(const char *)str,color,bc);
- SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);
- SDL_FreeSurface(text);
- }
- void TTFFont::RenderUtf8(ISurface &surface, const char *str, SDL_Color color, SDL_Color bc, int x ,int y)
- {
- SDL_Rect rect;
- rect.x=x;
- rect.y=y;
- this->RenderUtf8(surface,str,color,bc,rect);
- }
- void TTFFont::RenderUtf8Solid(ISurface &surface, const char *str, SDL_Color color, SDL_Rect rect)
- {
- SDL_Surface *text;
- text=TTF_RenderUTF8_Solid(font,(const char *)str,color);
- SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);
- SDL_FreeSurface(text);
- }
- void TTFFont::RenderUtf8Solid(ISurface &surface, const char *str, SDL_Color color, int x, int y)
- {
- SDL_Rect rect;
- rect.x=x;
- rect.y=y;
- this->RenderUtf8(surface,str,color,rect);
- }
- void TTFFont::RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Rect rect)
- {
- SDL_Surface *text;
- text=TTF_RenderUNICODE_Blended(font,(const Uint16 *)str,color);
- SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);
- SDL_FreeSurface(text);
- }
- void TTFFont::RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, int x, int y)
- {
- SDL_Rect rect;
- rect.x=x;
- rect.y=y;
- this->RenderUnicode(surface,str,color,rect);
- }
- void TTFFont::RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Color bc, SDL_Rect rect)
- {
- SDL_Surface *text;
- text=TTF_RenderUNICODE_Shaded(font,(const Uint16 *)str,color,bc);
- SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);
- SDL_FreeSurface(text);
- }
- void TTFFont::RenderUnicode(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Color bc, int x, int y)
- {
- SDL_Rect rect;
- rect.x=x;
- rect.y=y;
- this->RenderUnicode(surface,str,color,bc,rect);
- }
- void TTFFont::RenderUnicodeSolid(ISurface &surface, const Uint16 *str, SDL_Color color, SDL_Rect rect)
- {
- SDL_Surface *text;
- text=TTF_RenderUNICODE_Solid(font,(const Uint16 *)str,color);
- SDL_BlitSurface(text,NULL,surface.getSurface(),&rect);
- SDL_FreeSurface(text);
- }
- void TTFFont::RenderUnicodeSolid(ISurface &surface, const Uint16 *str, SDL_Color color, int x, int y)
- {
- SDL_Rect rect;
- rect.x=x;
- rect.y=y;
- this->RenderUnicode(surface,str,color,rect);
- }
文中Demo下载(VS2008工程):
点击下载此文件
Leven 于 2008.04.16
不过还有一点没贴出来,等下一起给你发过去吧.
上一篇
下一篇
文章来自:
Tags:
gatt@21cn.com