嵌入式linux:3520a SDL_tff库做bmp 也就是osd

转自:http://blog.csdn.net/xyyangkun/article/details/8528941

之前用点陈字库做过3515,3520a的字库,现在为新需求,要用矢量字库做osd。决定用SDL_tff库做。

配置freetype:

xy@xy-pc:~/aaa/freetype-2.4.10$ CC=arm-hisiv200-linux-gcc ./configure --prefix=/home/xy/aaa/bin   --host=arm-linux
--prefix 指定编译后,相关库文件存放的路径;

编译安装:make ,make install。

配置SDL:

xy@xy-pc:~/aaa/SDL-1.2.15$ CC=arm-hisiv200-linux-gcc CXX=arm-hisiv200-linux-cpp ./configure --prefix=/home/xy/aaa/bin   --host=arm-linux --disable-alsa  --disable-pulseaudio

编译安装。


配置SDL_tff:

xy@xy-pc:~/aaa/SDL_ttf-2.0.11$CC=arm-hisiv200-linux-gcc ./configure --with-freetype-prefix=/home/xy/aaa/bin --host=arm-linux

编译安装.


  1. /* 
  2.  * main.c 
  3.  * 
  4.  *  Created on: 2013-1-22 
  5.  *      Author: xy 
  6.  */  
  7. #include <stdlib.h>  
  8. #include <string.h>  
  9. #include "SDL/SDL.h"  
  10. #include "SDL/SDL_ttf.h"  
  11.   
  12.   
  13. int main(int argc,char **argv)  
  14. {  
  15.     TTF_Font *font;  
  16.     SDL_Surface *text, *temp;  
  17.   
  18.      /* Initialize the TTF library */  
  19.      if ( TTF_Init() < 0 ) {  
  20.          fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError());  
  21.          SDL_Quit();  
  22.          return(2);  
  23.      }  
  24.   
  25.   
  26.     font = TTF_OpenFont("cu.ttf", 48);  
  27.     if ( font == NULL ) {  
  28.         fprintf(stderr, "Couldn't load %d pt font from %s: %s\n",  
  29.                     "ptsize", 18, SDL_GetError());  
  30.     }  
  31.   
  32. //  TTF_SetFontStyle(font, 0);  
  33. //  TTF_SetFontOutline(font, 0);  
  34. //  TTF_SetFontKerning(font, 1);  
  35. //  TTF_SetFontHinting(font, 0);  
  36.   
  37.     //SDL_Color forecol=     { 0xFF, 0xFF, 0xFF, 0 };  
  38.     SDL_Color forecol=   { 0x00, 0x00, 0x00, 0 };  
  39.     char *string="你好啊";  
  40.     text = TTF_RenderUTF8_Solid(font, string, forecol);  
  41.   
  42.     //SDL_LoadBMP  
  43.   
  44.     SDL_SaveBMP(text, "1.bmp");  
  45.   
  46.   
  47.   
  48.   
  49.     SDL_FreeSurface(text);  
  50.     TTF_CloseFont(font);  
  51.     TTF_Quit();  
  52.   
  53. }  

就可以保存成1.bmp了。输入了汉字。代码保存的文本格式应该utf-8.



对于海思3520a用的是rgb1555格式,所以应该转成那种格式:

  1. /* Convert to 16 bits per pixel */  
  2. SDL_Surface *temp = SDL_CreateRGBSurface(SDL_SWSURFACE,  
  3.         text->w, text->h, 16,\  
  4.                     0x00FF0000, 0x0000FF00, 0x000000FF,/*0x00FF0000, 0x0000FF00, 0x000000FF*/  
  5.                     0);  
  6. SDL_Rect bounds;  
  7. if (temp != NULL)  
  8. {  
  9.     bounds.x = 0;  
  10.     bounds.y = 0;  
  11.     bounds.w = text->w;  
  12.     bounds.h = text->h;  
  13.     if (SDL_LowerBlit(text, &bounds, temp, &bounds) < 0) {  
  14.         SDL_FreeSurface(text);  
  15.         SDL_SetError("Couldn't convert image to 16 bpp");  
  16.         text = NULL;  
  17.     }  
  18. }  
  19. stBitmap.u32Width = temp->w;  
  20. stBitmap.u32Height = temp->h;  
  21. stBitmap.pData= temp->pixels;  
  22. stBitmap.enPixelFormat= PIXEL_FORMAT_RGB_1555 ;  
  23. SDL_FreeSurface(text);  
  24. SDL_FreeSurface(temp);  

添加以上代码,基本就可以达到了。

因为自己的不细心,调试了大半天。真是不应该啊。其实早知道这样做,但是思维上总是背道而驰。

以后编程应该更细心,至少节省时间啊。。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SDL2 中的 `SDL_ConvertSurface` 函数可以转换表面的像素格式,但是不是所有的像素格式都可以转换成 ARGB1555 格式。如果你尝试将一个 BMP 图像转换成 ARGB1555 格式,可能会失败。 原因是 BMP 图像格式不支持 ARGB1555 格式。BMP 图像格式只支持 BGR24、BGR555、BGR565、BGR888 和 BGRA8888 等像素格式。因此,如果你将 BMP 图像加载到 SDL2 中,它的像素格式可能是其中的一种。 如果你需要将一个表面转换成 ARGB1555 格式,可以使用 `SDL_CreateRGBSurface` 函数手动创建一个 ARGB1555 格式的表面。以下是一个创建 ARGB1555 表面的示例: ```c int width = 640, height = 480; Uint32 rmask, gmask, bmask, amask; #if SDL_BYTEORDER == SDL_BIG_ENDIAN rmask = 0x7C00; gmask = 0x03E0; bmask = 0x001F; amask = 0x8000; #else rmask = 0x001F; gmask = 0x03E0; bmask = 0x7C00; amask = 0x8000; #endif SDL_Surface* surface = SDL_CreateRGBSurface(0, width, height, 16, rmask, gmask, bmask, amask); ``` 在此示例中,我们手动指定了每个像素通道所占用的位数,并使用掩码来指定每个通道的位移和位数。如果你需要设置不同的位数或不同的掩码,请根据需要进行更改。 创建表面后,你可以使用 `SDL_BlitSurface` 函数将其他表面的像素复制到 ARGB1555 格式的表面上。在复制像素时,SDL2 会自动将像素格式转换为 ARGB1555 格式。 ```c SDL_Surface* src_surface = ...; // 其他表面 SDL_Rect src_rect = {0, 0, width, height}; SDL_Rect dst_rect = {0, 0, width, height}; SDL_BlitSurface(src_surface, &src_rect, surface, &dst_rect); ``` 这样,你就可以将其他表面中的像素复制到 ARGB1555 格式的表面上了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值