![cover](https://img-blog.csdnimg.cn/c63dc7cd182b4b32b0a3f9d805dbdf6f.jpg)
C语言SDL2示例-绘制HelloWorld
SDL(Simple DirectMedia Layer)是一套开源的跨平台多媒体开发库,C 语言编写,多用于嵌入式、游戏、模拟器、媒体播放器等多媒体应用领域。它提供了绘制图像、播放声音、获取键盘输入等相关的 API,大大降低多媒体应用开发难度的同时,也让开发者只要用相同或是相似的代码就可以开发出跨多个平台(Linux、Windows、Mac OS X等)的应用软件。本项目是使用手机自带的原生字体
·
简介:
SDL(Simple DirectMedia Layer)是一套开源的跨平台多媒体开发库,C 语言编写,多用于嵌入式、游戏、模拟器、媒体播放器等多媒体应用领域。 它提供了绘制图像、播放声音、获取键盘输入等相关的 API,大大降低多媒体应用开发难度的同时,也让开发者只要用相同或是相似的代码就可以开发出跨多个平台(Linux、Windows、Mac OS X等)的应用软件。
本项目是使用手机自带的原生字体在屏幕上绘制HelloWorld字样
运行实例
#include #include #include // Function for loading font and drawing text into SDL_Texture static SDL_Texture *loadText(SDL_Renderer *renderer, const char *text) { if (TTF_Init() == -1) { fprintf(stderr, "TTF_Init Error: %s\n", TTF_GetError()); return NULL; } TTF_Font *font = TTF_OpenFont("/system/fonts/Roboto-Regular.ttf", 256); if (font == NULL) { fprintf(stderr, "TTF_OpenFont Error: %s\n", TTF_GetError()); return NULL; } SDL_Color color = {255, 255, 255}; SDL_Surface *surface = TTF_RenderText_Solid(font, text, color); SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface); SDL_FreeSurface(surface); if (texture == NULL) { fprintf(stderr, "SDL_CreateTextureFromSurface Error: %s\n", SDL_GetError()); return NULL; } return texture; } int main(int argc, char *argv[]) { if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { fprintf(stderr, "SDL_Init Error: %s\n", SDL_GetError()); return 1; } SDL_Window *window = SDL_CreateWindow("Hello SDL2-ttf", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, 0); if (window == NULL) { fprintf(stderr, "SDL_CreateWindow Error: %s\n", SDL_GetError()); return 1; } SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0); if (renderer == NULL) { fprintf(stderr, "SDL_CreateRenderer Error: %s\n", SDL_GetError()); return 1; } // Better scaling quality SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "2"); SDL_Texture *textTexture = loadText(renderer, "Hello, world!"); if (textTexture == NULL) { fprintf(stderr, "Couldn't load text texture\n"); return 1; } int tw, th; SDL_QueryTexture(textTexture, NULL, NULL, &tw, &th); int w, h; SDL_GetRendererOutputSize(renderer, &w, &h); SDL_Rect dest; // Our text always has width much bigger than height, use this dest.x = 0; dest.w = w; dest.h = th * w / tw; dest.y = (h - dest.h) / 2; SDL_RenderClear(renderer); SDL_RenderCopy(renderer, textTexture, NULL, &dest); SDL_RenderPresent(renderer); SDL_Delay(5000); SDL_DestroyWindow(window); SDL_Quit(); return }
![Logo](https://devpress.csdnimg.cn/79de2bf0b7994defa4242ef90d5513fa.jpg)
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)