SDL实现字符串转位图

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "SDL.h"
#include "SDL_ttf.h"
#include "time.h"

 
#define FONT_PATH       "./font/hisi_osd.ttf"
int string_to_bmp(char *pu8Str);
int cnt = 0;
int main()
{
    
    time_t now;
    struct tm *ptm;
    char timestr[100] = {0};
    while(1)
    {
        usleep(1000000);
        time(&now);
        ptm = localtime(&now);
        snprintf(timestr,100,"时间%d-%02d-%02d %02d:%02d:%02d",ptm->tm_year+1900,ptm->tm_mon+1,ptm->tm_mday,ptm->tm_hour,ptm->tm_min,ptm->tm_sec);
        string_to_bmp(timestr);
        memset(timestr,0,100);
       if(cnt++ > 5)
        {
             break;
        }
    }
    return 0;
}
 
int string_to_bmp(char *pu8Str)
{
    TTF_Font *font;  
    SDL_Surface *text, *temp;  

 
    if (TTF_Init() < 0 ) 
    {  
        fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError());  
        SDL_Quit();
    }  
 
    font = TTF_OpenFont(FONT_PATH, 40); 
    if ( font == NULL ) 
    {  
        fprintf(stderr, "Couldn't load %d pt font from %s: %s\n",18,"ptsize", SDL_GetError());  
    }  

    SDL_Color forecol = { 0xff, 0xff, 0xff, 0 };  
    text = TTF_RenderUTF8_Solid(font, pu8Str, forecol);
    temp = SDL_CreateRGBSurface(SDL_SWSURFACE,text->w,text->h,24,0x00000000,0x00000000,0x00000000,0xff000000);
    SDL_Rect bounds;
    if ( temp != NULL )
    {
        bounds.x = 0;
        bounds.y = 0;
        bounds.w = text->w;
        bounds.h = text->h;
        SDL_LowerBlit(text,&bounds,temp,&bounds);
        //{
        //    SDL_FreeSurface(temp);
        //   SDL_SetError("couldnt convert image to 24 bpp\n");
        //    text =NULL;
        //   return -1;
        //}
    }//else{
        //printf("sdl creat RGB surface failed\n");
        //return -1;

    //}
    char savename[10] = {0};
    snprintf(savename,10,"%d.bmp",cnt);
    printf("savename = %s\n",savename);
    SDL_SaveBMP(temp, savename); 
        if(temp ==NULL)
        {
            printf("%s:[%d] the temp is NULL !  \n",__FUNCTION__,__LINE__);
        }
    SDL_FreeSurface(text);  
    TTF_CloseFont(font);  
    TTF_Quit();  
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值