#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;
}
SDL实现字符串转位图
最新推荐文章于 2024-07-25 22:24:14 发布