LSDL-简易SDL封装之二

昨天分析了LSDL中的两个游离类,从今天开始,我们要分析LSDL中的主要类了,这回我们看一看LSDL目前所有的类图:




从图中可以看出,所有的LSDL关键类都直接或者间接继承自ISurface类,该类对所有Surface对象进行了抽象,该类封装了SDL_Surface对象,同时能对自己进行FillRect,虽然定义上这不是一个抽象类,但是该类仍然是抽象的,实例化它没有什么实际意义.

LSDL的第一个关键类是Screen,该类代表了Screen对象,也就是整个程序的"底板"部分,它拥有公共属性:窗口高度,宽度,显示bpp以及初始化参数,该参数的意义和SDL_Init中的Flags完全一致,该类拥有公开成员函数Flip,该函数负责将screen更新.而SetCaption顾名思义,就是设置窗口标题的.

对LSDL的设计中,我们要注意如何更好的封装和抽象,我们可以把一个SDL程序的显示部分看做是一层层的Surface构建的,就像绘画一样,首先是底板,然后在上面覆盖一层背景,然后再背景上覆盖一层其他图形,然后可以在背景的其他地方再次覆盖一层其他的Surface,然后在这个Surface上又可以再覆盖一层Surface...这样下去,我们就将整个画面拆分成多个ISurface对象,这就是全局抽象,把所有的Surface的公共部分提取出来,对其隐藏封装,比如,所有的层都是一个拥有一个SDL_Surface,因此,ISurface自然要包含一个SDL_Surface,然后,每个层都要能渲染自身,因此,ISurface提供了FillRect方法.同时,为了方便别人的渲染,而对于Screen来说,它不仅包含了ISurface的特性,还有很多其他的固有特性,比如,screen有高度宽度,有显示参数,这些都是screen固有的,自然要包含在Screen中,同时Screen能刷新自己,而其他对象显然无法做到,因此,Screen有与众不同的Flip方法.同样,Screen能拥有标题,所有SetCaption方法也是它独有的.

使用这样的思维方式,我们可以继续对非Screen的Surface对象进行思考,那么非Surface对象能有哪些相同和不同的地方了?今天打字蛮多,太累,下次继续分析吧.

最后给出ISurface和Screen的源代码:

ISurface.h

 

C++代码
  1. #ifndef _INCLUDE_ISURFACE_   
  2. #define _INCLUDE_ISURFACE_   
  3.   
  4. #include "LsdlDefs.h"   
  5.   
  6. class LSDLAPI ISurface   
  7. {   
  8. protected:   
  9.     SDL_Surface *baseSurface;   
  10. public:   
  11.     ISurface();   
  12.     ~ISurface();   
  13.     SDL_Surface *getSurface();   
  14.     virtual void FillRect(SDL_Rect *thisRect, Uint32 color);   
  15.     virtual void FillRect(SDL_Rect *thisRect, Uint8 r, Uint8 g, Uint8 b);   
  16.     virtual void FillRect(SDL_Rect *thisRect, Uint8 r, Uint8 g, Uint8 b, Uint8 a);   
  17. };   
  18.   
  19. #endif   
  20.   


ISurface.cpp

 

C++代码
  1. #include "ISurface.h"   
  2. #include <iostream>   
  3.   
  4. ISurface::ISurface()   
  5. {   
  6.     this->baseSurface=NULL;   
  7.     //std::locale::global(std::locale("Utf-8"));   
  8. }   
  9.   
  10. ISurface::~ISurface()   
  11. {   
  12.     if(this->baseSurface != NULL)   
  13.         SDL_FreeSurface(baseSurface);   
  14. }   
  15.   
  16. SDL_Surface *ISurface::getSurface()   
  17. {   
  18.     return this->baseSurface;   
  19. }   
  20.   
  21. void ISurface::FillRect(SDL_Rect *thisRect, Uint32 color)   
  22. {   
  23.     if(baseSurface == NULL)   
  24.         throw LException("You should init surface first.");   
  25.     SDL_FillRect(this->baseSurface,thisRect,color);   
  26. }   
  27.   
  28. void ISurface::FillRect(SDL_Rect *thisRect, Uint8 r, Uint8 g, Uint8 b)   
  29. {   
  30.     if(baseSurface == NULL)   
  31.         throw LException("You should init surface first.");   
  32.     SDL_FillRect(this->baseSurface,thisRect,SDL_MapRGB(this->baseSurface->format,r,g,b));   
  33. }   
  34.   
  35. void ISurface::FillRect(SDL_Rect *thisRect, Uint8 r, Uint8 g, Uint8 b, Uint8 a)   
  36. {   
  37.     if(baseSurface == NULL)   
  38.         throw LException("You should init surface first.");   
  39.     SDL_FillRect(this->baseSurface,thisRect,SDL_MapRGBA(this->baseSurface->format,r,g,b,a));   
  40. }   
  41.   

 

Screen.h

C++代码
  1. #ifndef _INCLUDE_SCREEN_   
  2. #define _INCLUDE_SCREEN_   
  3.   
  4. #include "LsdlDefs.h"   
  5. #include "ISurface.h"   
  6.   
  7. class LSDLAPI Screen : public ISurface   
  8. {   
  9. public:   
  10.     int Width;   
  11.     int Heigth;   
  12.     int Bpp;   
  13.     Uint32 Flags;   
  14. private:   
  15.     const char *caption;   
  16.     const char *icon;   
  17. public:   
  18.     Screen();   
  19.     Screen(int width, int heigth, int bpp, Uint32 flags);   
  20.     ~Screen();   
  21.     void Init();   
  22.     void Init(int width, int heigth, int bpp, Uint32 flags);   
  23.     void Flip();   
  24.     void SetCaption(const char *title, const char *iconpath);   
  25. };   
  26.   
  27. #endif   
  28.   

 

Screen.cpp

 

C++代码
  1. #include "Screen.h"   
  2.   
  3. Screen::Screen()   
  4. {   
  5.        
  6. }   
  7.   
  8. Screen::Screen(int width, int heigth, int bpp, Uint32 flags)   
  9. {   
  10.     this->Width=width;   
  11.     this->Heigth=heigth;   
  12.     this->Bpp=bpp;   
  13.     this->Flags=flags;   
  14.     this->baseSurface=SDL_SetVideoMode(this->Width,this->Heigth,this->Bpp,this->Flags);   
  15.     if(this->baseSurface == 0)   
  16.         throw LException(SDL_GetError());   
  17. }   
  18.   
  19. Screen::~Screen()   
  20. {   
  21. }   
  22.   
  23. void Screen::Init()   
  24. {   
  25.     this->baseSurface=SDL_SetVideoMode(this->Width,this->Heigth,this->Bpp,this->Flags);   
  26.     if(this->baseSurface == 0)   
  27.         throw LException(SDL_GetError());   
  28. }   
  29.   
  30. void Screen::Init(int width, int heigth, int bpp, Uint32 flags)   
  31. {   
  32.     this->Width=width;   
  33.     this->Heigth=heigth;   
  34.     this->Bpp=bpp;   
  35.     this->Flags=flags;   
  36.     this->baseSurface=SDL_SetVideoMode(this->Width,this->Heigth,this->Bpp,this->Flags);   
  37.     if(this->baseSurface == 0)   
  38.         throw LException(SDL_GetError());   
  39. }   
  40.   
  41. void Screen::Flip()   
  42. {   
  43.     SDL_Flip(this->baseSurface);   
  44. }   
  45.   
  46. void Screen::SetCaption(const char *title, const char *iconpath)   
  47. {   
  48.     this->caption=title;   
  49.     this->icon=iconpath;   
  50.     SDL_WM_SetCaption(caption,icon);   
  51. }   
  52.   
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: C  C++  SDL  LSDL 
评论: 0 | 引用: 0 | 查看次数: 602
发表评论
用户名:
密 码: 游客发言不需要密码.
验证码: 验证码
内 容:
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.
字数限制 500 字 | HTML代码允许 关闭 | 评论可修改 关闭