SDL2本身并没有对Window进行封装,这样的好处可以给使用者更大的灵活性,但是我们在写程序的时候,基本上都是面向对象的,所以做这种封装是很有必要的,一下是原作者对其进行的一个封装。
window07.h
/*
SDL Window的一个封装
*/
class Window07 {
public:
/*
初始化SDL,并且设置标题
*/
static void Init(std::string title = "Window");
///Quit SDL and destroy the window and renderer
static void Quit();
/**
绘制,可以进行角度控制。
*/
static void Draw(SDL_Texture *tex, SDL_Rect &dstRect, SDL_Rect *clip = NULL,
float angle = 0.0, int xPivot = 0, int yPivot = 0,
SDL_RendererFlip flip = SDL_FLIP_NONE);
/**
图片文件创建一个SDL_Texture
*/
static SDL_Texture* LoadImage(const std::string &file);
/**
TTF文件创建一个SDL_Texture
*/
static SDL_Texture* RenderText(const std::string &message, const std::string &fontFile, SDL_Color color, int fontSize);
///Clear the renderer
static void Clear();
///更新Render
static void Present();
///获取Render大小
static SDL_Rect Box();
private:
//unique_ptr是C++中智能指针,定义了一个window,一个SDL_Renderer
static std::unique_ptr<SDL_Window, void (*)(SDL_Window*)> mWindow;
static std::unique_ptr<SDL_Renderer, void (*)(SDL_Renderer*)> mRenderer;
static SDL_Rect mBox;
};
window07.cpp
#include <string>