用SDL显示不同的风格文字

来源:互联网 发布:广州恒大淘宝足球俱乐部票务商城 编辑:程序博客网 时间:2024/05/20 07:35
  v1.0.1
  更新:把文字加载成surface时,忘了释放surface了。
  备注:以下代码是在c4droid运行。
  注意:这里的字体路径是安卓默认的字体路径,如果闪退,则需要改成自己的字体路径!
 
  效果:

 

#include <SDL2/SDL.h>

#include <SDL2/SDL_ttf.h>

SDL_Window*window=NULL;

SDL_Renderer*renderer=NULL;

//窗口类

class Window

{

    public:

    //记录是否加载成功

    bool load;

    //构造函数

    Window(char title[20], Uint32 w=720, Uint32 h=1280, int flag=0)

    {

        load=false;

        if (!SDL_Init(SDL_INIT_VIDEO))

        if (window=SDL_CreateWindow(title, 0, 0, w, h, flag))

        if (renderer=SDL_CreateRenderer(window, -1, 0))

        {

            //加载窗口成功

            load=true;

        }

    }

    //析构函数

    ~Window()

    {

        SDL_DestroyRenderer(renderer);

        SDL_DestroyWindow(window);

        SDL_Quit();

    }

    //填充窗口:白色

    void fullWindow()

    {

        SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);

        SDL_RenderFillRect(renderer, NULL);

    }

    //刷新窗口

    void reflush()

    {

        SDL_RenderPresent(renderer);

        SDL_RenderClear(renderer);

    }

};

//文字类

class Font

{

    public:

    TTF_Font*font;

    //构造函数

    Font(int size=50)

    {

        TTF_Init();

        font=TTF_OpenFont("/system/fonts/DroidSansFallback.ttf", size);

    }

    //析构函数

    ~Font()

    {

        TTF_CloseFont(font);

        font=0;

        TTF_Quit();

    }

    //把文字加载成texture

    SDL_Texture*loadTex(char*str, int type=4)

    {

        switch (type)
        {
            case 0:TTF_SetFontStyle(font,TTF_STYLE_BOLD);break; //粗体
            case 1:TTF_SetFontStyle(font,TTF_STYLE_ITALIC);break; //斜体
            case 2:TTF_SetFontStyle(font,TTF_STYLE_UNDERLINE);break; //下划线
            case 3:TTF_SetFontStyle(font,TTF_STYLE_STRIKETHROUGH);break; //删除线
            case 4:TTF_SetFontStyle(font,TTF_STYLE_NORMAL);break; //正常
        }

        //文字颜色:黑色

        SDL_Color color={0, 0, 0, 255};

        SDL_Surface*surface=TTF_RenderUTF8_Blended(font, str, color);

        SDL_Texture*texture=SDL_CreateTextureFromSurface(renderer, surface);
       
        SDL_FreeSurface(surface);

        return texture;

    }

};

//纹理类

class Tex

{

    public:

    SDL_Texture*tex[5];

    Font font;

    //构造函数

    Tex()

    {

        char*str[5]={"粗体", "斜体", "下划线", "删除线", "正常"};

        for (int i=0; i<5; i++)

        {

            tex[i]=font.loadTex(str[i], i);

        }

    }

    //析构函数

    ~Tex()

    {

        for (int i=0; i<5; i++)

        {

            if (tex[i]!=NULL)

            SDL_DestroyTexture(tex[i]);

        }

    }

    //显示tex,参数x,y是tex的显示坐标

    void showTex(int n, int x, int y)

    {

        int w,h;
        //获取纹理大小
        SDL_QueryTexture(tex[n], NULL, NULL, &w, &h);

        SDL_Rect box={x, y, w, h};

        SDL_RenderCopy(renderer, tex[n], NULL, &box);

    }

};

//主函数

int main(int argc, char**argv)

{

    Window win("显示不同风格的文字");

    Tex tex;

    while (win.load)

    {

        for (int i=0, y=200; i<5; i++, y+=100)

        {

            tex.showTex(i, 200, y);

        }

        win.reflush();

        win.fullWindow();

    }
    }