LSDL-简易SDL封装之二
作者:Leven 日期:2008-04-17 19:54:21
昨天分析了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
- #ifndef _INCLUDE_ISURFACE_
- #define _INCLUDE_ISURFACE_
- #include "LsdlDefs.h"
- class LSDLAPI ISurface
- {
- protected:
- SDL_Surface *baseSurface;
- public:
- ISurface();
- ~ISurface();
- SDL_Surface *getSurface();
- virtual void FillRect(SDL_Rect *thisRect, Uint32 color);
- virtual void FillRect(SDL_Rect *thisRect, Uint8 r, Uint8 g, Uint8 b);
- virtual void FillRect(SDL_Rect *thisRect, Uint8 r, Uint8 g, Uint8 b, Uint8 a);
- };
- #endif
ISurface.cpp
- #include "ISurface.h"
- #include <iostream>
- ISurface::ISurface()
- {
- this->baseSurface=NULL;
- //std::locale::global(std::locale("Utf-8"));
- }
- ISurface::~ISurface()
- {
- if(this->baseSurface != NULL)
- SDL_FreeSurface(baseSurface);
- }
- SDL_Surface *ISurface::getSurface()
- {
- return this->baseSurface;
- }
- void ISurface::FillRect(SDL_Rect *thisRect, Uint32 color)
- {
- if(baseSurface == NULL)
- throw LException("You should init surface first.");
- SDL_FillRect(this->baseSurface,thisRect,color);
- }
- void ISurface::FillRect(SDL_Rect *thisRect, Uint8 r, Uint8 g, Uint8 b)
- {
- if(baseSurface == NULL)
- throw LException("You should init surface first.");
- SDL_FillRect(this->baseSurface,thisRect,SDL_MapRGB(this->baseSurface->format,r,g,b));
- }
- void ISurface::FillRect(SDL_Rect *thisRect, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
- {
- if(baseSurface == NULL)
- throw LException("You should init surface first.");
- SDL_FillRect(this->baseSurface,thisRect,SDL_MapRGBA(this->baseSurface->format,r,g,b,a));
- }
Screen.h
- #ifndef _INCLUDE_SCREEN_
- #define _INCLUDE_SCREEN_
- #include "LsdlDefs.h"
- #include "ISurface.h"
- class LSDLAPI Screen : public ISurface
- {
- public:
- int Width;
- int Heigth;
- int Bpp;
- Uint32 Flags;
- private:
- const char *caption;
- const char *icon;
- public:
- Screen();
- Screen(int width, int heigth, int bpp, Uint32 flags);
- ~Screen();
- void Init();
- void Init(int width, int heigth, int bpp, Uint32 flags);
- void Flip();
- void SetCaption(const char *title, const char *iconpath);
- };
- #endif
Screen.cpp
- #include "Screen.h"
- Screen::Screen()
- {
- }
- Screen::Screen(int width, int heigth, int bpp, Uint32 flags)
- {
- this->Width=width;
- this->Heigth=heigth;
- this->Bpp=bpp;
- this->Flags=flags;
- this->baseSurface=SDL_SetVideoMode(this->Width,this->Heigth,this->Bpp,this->Flags);
- if(this->baseSurface == 0)
- throw LException(SDL_GetError());
- }
- Screen::~Screen()
- {
- }
- void Screen::Init()
- {
- this->baseSurface=SDL_SetVideoMode(this->Width,this->Heigth,this->Bpp,this->Flags);
- if(this->baseSurface == 0)
- throw LException(SDL_GetError());
- }
- void Screen::Init(int width, int heigth, int bpp, Uint32 flags)
- {
- this->Width=width;
- this->Heigth=heigth;
- this->Bpp=bpp;
- this->Flags=flags;
- this->baseSurface=SDL_SetVideoMode(this->Width,this->Heigth,this->Bpp,this->Flags);
- if(this->baseSurface == 0)
- throw LException(SDL_GetError());
- }
- void Screen::Flip()
- {
- SDL_Flip(this->baseSurface);
- }
- void Screen::SetCaption(const char *title, const char *iconpath)
- {
- this->caption=title;
- this->icon=iconpath;
- SDL_WM_SetCaption(caption,icon);
- }
上一篇
下一篇
文章来自:
Tags: