SDL2.0_06_text
来源:互联网 发布:如何自学java 编辑:程序博客网 时间:2024/05/17 23:03
使用了SDL_ttf扩展库,字体文件*.ttf可以在Sourceforge上下载。
#include <iostream>#include <windows.h>#include <string>using namespace std;#include <SDL.h>#include <SDL_image.h>#include <SDL_ttf.h>const int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, SDL_Rect dst, SDL_Rect *clip = nullptr) {SDL_RenderCopy(ren, tex, clip, &dst);}void renderTexture(SDL_Texture *tex, SDL_Renderer *ren, int x, int y, SDL_Rect *clip = nullptr) {SDL_Rect dst;dst.x = x;dst.y = y;if (clip != nullptr) {dst.w = clip->w;dst.h = clip->h;}else SDL_QueryTexture(tex, NULL, NULL, &dst.w, &dst.h);renderTexture(tex, ren, dst, clip);}SDL_Texture *renderText(const string &message, const string &fontFile, SDL_Color color, int fontSize, SDL_Renderer *ren) {TTF_Font *font = TTF_OpenFont(fontFile.c_str(), fontSize);if (font == nullptr) {cout << "Failed to open font..." << endl;exit(0);}SDL_Surface *surf = TTF_RenderText_Blended(font, message.c_str(), color);SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, surf);SDL_FreeSurface(surf);TTF_CloseFont(font);return tex;}int main(int argc, char** argv){if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {cout << SDL_GetError() << endl;return 0;}if (TTF_Init() == -1) {cout << TTF_GetError() << endl;return 0;}SDL_Window *win = nullptr;win = SDL_CreateWindow("05:", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE);if (win == nullptr) {cout << SDL_GetError() << endl;return 0;}SDL_Renderer *ren = nullptr;ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);if (ren == nullptr) {cout << SDL_GetError() << endl;return 0;}SDL_Color color = { 128, 128, 128 };SDL_Texture *bg = renderText("Why I have to learn SDL?", "chivo-black-webfont.ttf", color, 64, ren);if (bg == nullptr) {cout << "Could not load font correctly!" << endl;return 0;}SDL_RenderClear(ren);int bw, bh;SDL_QueryTexture(bg, NULL, NULL, &bw, &bh);SDL_Event e;bool quit = false;while (!quit) {while (SDL_PollEvent(&e)){if (e.type == SDL_QUIT) {cout << "quit..." << endl;quit = true;}if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_ESCAPE) {quit = true;}}SDL_RenderClear(ren);renderTexture(bg, ren, SCREEN_WIDTH/2 - bw/2, SCREEN_HEIGHT/2 - bh/2);SDL_RenderPresent(ren);}SDL_DestroyTexture(bg);SDL_DestroyRenderer(ren);SDL_DestroyWindow(win);SDL_Quit();IMG_Quit();cout << "Success!" << endl;return 0;}
0 0
- SDL2.0_06_text
- SDL2.0
- SDL2.0 ubuntu complie
- SDL2.0上手试用
- ffmpeg and SDL2.0
- SDL2.0上手试用
- SDL2.0 播放YUV
- sdl2.0示例
- sdl2.0_opencv
- SDL2.0+ffmpeg2.3
- SDL2.0_01_hello world!
- SDL2.0_02_functions
- SDL2.0_03_SDL_image Library
- SDL2.0_04_event
- SDL2.0_05_sprite sheet
- SDL2.0_07_classes
- SDL2.0_08_timer
- SDL2.0多分屏显示
- python中的字符串处理函数
- 写出八位ASCII码
- 查看android程序的CPU和内存消耗情况
- PHP + mysql 遇到Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error
- 厦门巨龙笔试题目逻辑题
- SDL2.0_06_text
- 阿里巴巴公司DBA笔试题目
- 如何将 MFC ActiveX 控件标记为脚本和初始化安全
- 二叉查找树的实现(BST)(转载自http://blog.csdn.net/collonn/article/details/18732079)
- 【伊利丹】Hadoop-2.5.0-CDH5.2.0 RM HA实验记录
- win7(32/64)+apache2.4+php5.5+mysql5.6 环境搭建配置
- IOS 后台定位并向服务器发送当前位置
- ArcGIS API for Flex(六)加载自定义Web平铺图层
- 第一篇文章,献给自己最近在读的一首诗---《人的一生》