/*
* three lib
*/
/* 字符串生成位图 */
http://www.libsdl.org/projects/SDL_ttf/release/
http://www.libsdl.org/download-1.2.php
https://freetype.org/
/* Freetype 2.13.0 */
./configure CC=aarch64-v01c01-linux-gnu-gcc --build=aarch64 --host=aarch64-v01c01-linux-gnu --enable-shared=no --enable-static=yes --with-zlib=no --prefix=/home/share/work/osd/three-lib/freetype/ --exec-prefix=/home/share/work/osd/three-lib/freetype
/* SDL 2.30.3 */
./configure CC=aarch64-v01c01-linux-gnu-gcc --build=aarch64 --host=aarch64-v01c01-linux-gnu --enable-shared=no --enable-static=yes --enable-video-wayland=no --enable-pulseaudio=no --prefix=/home/share/work/osd/three-lib/sdl/ --exec-prefix=/home/share/work/osd/three-lib/sdl
/* SDL_tff 2.22.0 */
./configure CC=aarch64-v01c01-linux-gnu-gcc --build=aarch64 --host=aarch64-v01c01-linux-gnu --enable-shared=no --enable-static=yes --prefix=/home/share/work/osd/three-lib/sdl_ttf/ --exec-prefix=/home/share/work/osd/three-lib/sdl_ttf --with-sdl-prefix=/home/share/work/osd/three-lib/sdl --with-sdl-exec-prefix=/home/share/work/osd/three-lib/sdl --with-ft-prefix=/home/share/work/osd/three-lib/freetype/ --with-ft-exec-prefix=/home/share/work/osd/three-lib/freetype
例子:
#include <stdio.h>
#include <time.h>
#include "SDL.h"
#include "SDL_ttf.h"
int main(int argc, const char *argv[])
{
char * pstr = "2024-06-04 18:00:29";
SDL_PixelFormat *fmt;
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("Roboto-Regular.ttf", 48);
if ( font == NULL )
{
fprintf(stderr, "Couldn't load %d pt font from %s: %s\n", 48, "ptsize", SDL_GetError());
}
SDL_Color forecol = { 0xff, 0xff, 0xff, 0xff };
text = TTF_RenderUTF8_Solid(font, pstr, forecol);
fmt = (SDL_PixelFormat*)malloc(sizeof(SDL_PixelFormat));
memset(fmt,0,sizeof(SDL_PixelFormat));
fmt->BitsPerPixel = 16;
fmt->BytesPerPixel = 2;
//fmt->colorkey = 0xffffffff;
//fmt->alpha = 0xff;
temp = SDL_ConvertSurface(text, fmt, 0);
SDL_SaveBMP(temp, "save.bmp");
SDL_FreeSurface(text);
SDL_FreeSurface(temp);
TTF_CloseFont(font);
TTF_Quit();
return 0;
}
例子编译:
/* test */
/* gcc 依赖库是从右往左 */
aarch64-v01c01-linux-gnu-gcc -o osd_test test.c -I/home/share/work/osd/three-lib/sdl/include/SDL2 -I/home/share/work/osd/three-lib/sdl_ttf/include/SDL2 -L/home/share/work/osd/three-lib/freetype/lib -L/home/share/work/osd/three-lib/sdl/lib -L/home/share/work/osd/three-lib/sdl_ttf/lib -lfreetype -lSDL2 -lSDL2_ttf -lpthread -lm
效果: