glfont.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. glfont: An example of using the SDL_ttf library with OpenGL.
  3. Copyright (C) 2001-2018 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /* A simple program to test the text rendering feature of the TTF library */
  19. /* quiet windows compiler warnings */
  20. #define _CRT_SECURE_NO_WARNINGS
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "SDL.h"
  25. #include "SDL_ttf.h"
  26. #ifdef HAVE_OPENGL
  27. #include "SDL_opengl.h"
  28. #define DEFAULT_PTSIZE 18
  29. #define DEFAULT_TEXT "The quick brown fox jumped over the lazy dog"
  30. #define WIDTH 640
  31. #define HEIGHT 480
  32. static char *Usage =
  33. "Usage: %s [-utf8|-unicode] [-b] [-i] [-u] [-fgcol r,g,b] [-bgcol r,g,b] \
  34. <font>.ttf [ptsize] [text]\n";
  35. void SDL_GL_Enter2DMode(int width, int height)
  36. {
  37. /* Note, there may be other things you need to change,
  38. depending on how you have your OpenGL state set up.
  39. */
  40. glPushAttrib(GL_ENABLE_BIT);
  41. glDisable(GL_DEPTH_TEST);
  42. glDisable(GL_CULL_FACE);
  43. glEnable(GL_TEXTURE_2D);
  44. /* This allows alpha blending of 2D textures with the scene */
  45. glEnable(GL_BLEND);
  46. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  47. glViewport(0, 0, width, height);
  48. glMatrixMode(GL_PROJECTION);
  49. glPushMatrix();
  50. glLoadIdentity();
  51. glOrtho(0.0, (GLdouble)width, (GLdouble)height, 0.0, 0.0, 1.0);
  52. glMatrixMode(GL_MODELVIEW);
  53. glPushMatrix();
  54. glLoadIdentity();
  55. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  56. }
  57. void SDL_GL_Leave2DMode()
  58. {
  59. glMatrixMode(GL_MODELVIEW);
  60. glPopMatrix();
  61. glMatrixMode(GL_PROJECTION);
  62. glPopMatrix();
  63. glPopAttrib();
  64. }
  65. /* Quick utility function for texture creation */
  66. static int power_of_two(int input)
  67. {
  68. int value = 1;
  69. while (value < input) {
  70. value <<= 1;
  71. }
  72. return value;
  73. }
  74. GLuint SDL_GL_LoadTexture(SDL_Surface *surface, GLfloat *texcoord)
  75. {
  76. GLuint texture;
  77. int w, h;
  78. SDL_Surface *image;
  79. SDL_Rect area;
  80. Uint8 saved_alpha;
  81. SDL_BlendMode saved_mode;
  82. /* Use the surface width and height expanded to powers of 2 */
  83. w = power_of_two(surface->w);
  84. h = power_of_two(surface->h);
  85. texcoord[0] = 0.0f; /* Min X */
  86. texcoord[1] = 0.0f; /* Min Y */
  87. texcoord[2] = (GLfloat)surface->w / w; /* Max X */
  88. texcoord[3] = (GLfloat)surface->h / h; /* Max Y */
  89. image = SDL_CreateRGBSurface(
  90. SDL_SWSURFACE,
  91. w, h,
  92. 32,
  93. #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
  94. 0x000000FF,
  95. 0x0000FF00,
  96. 0x00FF0000,
  97. 0xFF000000
  98. #else
  99. 0xFF000000,
  100. 0x00FF0000,
  101. 0x0000FF00,
  102. 0x000000FF
  103. #endif
  104. );
  105. if (image == NULL) {
  106. return 0;
  107. }
  108. /* Save the alpha blending attributes */
  109. SDL_GetSurfaceAlphaMod(surface, &saved_alpha);
  110. SDL_SetSurfaceAlphaMod(surface, 0xFF);
  111. SDL_GetSurfaceBlendMode(surface, &saved_mode);
  112. SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE);
  113. /* Copy the surface into the GL texture image */
  114. area.x = 0;
  115. area.y = 0;
  116. area.w = surface->w;
  117. area.h = surface->h;
  118. SDL_BlitSurface(surface, &area, image, &area);
  119. /* Restore the alpha blending attributes */
  120. SDL_SetSurfaceAlphaMod(surface, saved_alpha);
  121. SDL_SetSurfaceBlendMode(surface, saved_mode);
  122. /* Create an OpenGL texture for the image */
  123. glGenTextures(1, &texture);
  124. glBindTexture(GL_TEXTURE_2D, texture);
  125. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  126. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  127. glTexImage2D(GL_TEXTURE_2D,
  128. 0,
  129. GL_RGBA,
  130. w, h,
  131. 0,
  132. GL_RGBA,
  133. GL_UNSIGNED_BYTE,
  134. image->pixels);
  135. SDL_FreeSurface(image); /* No longer needed */
  136. return texture;
  137. }
  138. static void cleanup(int exitcode)
  139. {
  140. TTF_Quit();
  141. SDL_Quit();
  142. exit(exitcode);
  143. }
  144. int main(int argc, char *argv[])
  145. {
  146. char *argv0 = argv[0];
  147. SDL_Window *window;
  148. SDL_GLContext context;
  149. TTF_Font *font;
  150. SDL_Surface *text;
  151. int ptsize;
  152. int i, done;
  153. SDL_Color white = { 0xFF, 0xFF, 0xFF, 0 };
  154. SDL_Color black = { 0x00, 0x00, 0x00, 0 };
  155. SDL_Color *forecol;
  156. SDL_Color *backcol;
  157. GLenum gl_error;
  158. GLuint texture;
  159. int x, y, w, h;
  160. GLfloat texcoord[4];
  161. GLfloat texMinX, texMinY;
  162. GLfloat texMaxX, texMaxY;
  163. float color[8][3]= {{ 1.0, 1.0, 0.0},
  164. { 1.0, 0.0, 0.0},
  165. { 0.0, 0.0, 0.0},
  166. { 0.0, 1.0, 0.0},
  167. { 0.0, 1.0, 1.0},
  168. { 1.0, 1.0, 1.0},
  169. { 1.0, 0.0, 1.0},
  170. { 0.0, 0.0, 1.0}};
  171. float cube[8][3]= {{ 0.5, 0.5, -0.5},
  172. { 0.5, -0.5, -0.5},
  173. {-0.5, -0.5, -0.5},
  174. {-0.5, 0.5, -0.5},
  175. {-0.5, 0.5, 0.5},
  176. { 0.5, 0.5, 0.5},
  177. { 0.5, -0.5, 0.5},
  178. {-0.5, -0.5, 0.5}};
  179. SDL_Event event;
  180. int renderstyle;
  181. int dump;
  182. enum {
  183. RENDER_LATIN1,
  184. RENDER_UTF8,
  185. RENDER_UNICODE
  186. } rendertype;
  187. char *message;
  188. /* Look for special execution mode */
  189. dump = 0;
  190. /* Look for special rendering types */
  191. renderstyle = TTF_STYLE_NORMAL;
  192. rendertype = RENDER_LATIN1;
  193. /* Default is black and white */
  194. forecol = &black;
  195. backcol = &white;
  196. for (i=1; argv[i] && argv[i][0] == '-'; ++i) {
  197. if (strcmp(argv[i], "-utf8") == 0) {
  198. rendertype = RENDER_UTF8;
  199. } else
  200. if (strcmp(argv[i], "-unicode") == 0) {
  201. rendertype = RENDER_UNICODE;
  202. } else
  203. if (strcmp(argv[i], "-b") == 0) {
  204. renderstyle |= TTF_STYLE_BOLD;
  205. } else
  206. if (strcmp(argv[i], "-i") == 0) {
  207. renderstyle |= TTF_STYLE_ITALIC;
  208. } else
  209. if (strcmp(argv[i], "-u") == 0) {
  210. renderstyle |= TTF_STYLE_UNDERLINE;
  211. } else
  212. if (strcmp(argv[i], "-dump") == 0) {
  213. dump = 1;
  214. } else
  215. if (strcmp(argv[i], "-fgcol") == 0) {
  216. int r, g, b;
  217. if (sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3) {
  218. fprintf(stderr, Usage, argv0);
  219. return(1);
  220. }
  221. forecol->r = (Uint8)r;
  222. forecol->g = (Uint8)g;
  223. forecol->b = (Uint8)b;
  224. } else
  225. if (strcmp(argv[i], "-bgcol") == 0) {
  226. int r, g, b;
  227. if (sscanf (argv[++i], "%d,%d,%d", &r, &g, &b) != 3) {
  228. fprintf(stderr, Usage, argv0);
  229. return(1);
  230. }
  231. backcol->r = (Uint8)r;
  232. backcol->g = (Uint8)g;
  233. backcol->b = (Uint8)b;
  234. } else {
  235. fprintf(stderr, Usage, argv0);
  236. return(1);
  237. }
  238. }
  239. argv += i;
  240. argc -= i;
  241. /* Check usage */
  242. if (!argv[0]) {
  243. fprintf(stderr, Usage, argv0);
  244. return(1);
  245. }
  246. /* Initialize the TTF library */
  247. if (TTF_Init() < 0) {
  248. fprintf(stderr, "Couldn't initialize TTF: %s\n",SDL_GetError());
  249. SDL_Quit();
  250. return(2);
  251. }
  252. /* Open the font file with the requested point size */
  253. ptsize = 0;
  254. if (argc > 1) {
  255. ptsize = atoi(argv[1]);
  256. }
  257. if (ptsize == 0) {
  258. i = 2;
  259. ptsize = DEFAULT_PTSIZE;
  260. } else {
  261. i = 3;
  262. }
  263. font = TTF_OpenFont(argv[0], ptsize);
  264. if (font == NULL) {
  265. fprintf(stderr, "Couldn't load %d pt font from %s: %s\n",
  266. ptsize, argv[0], SDL_GetError());
  267. cleanup(2);
  268. }
  269. TTF_SetFontStyle(font, renderstyle);
  270. if(dump) {
  271. for(i = 48; i < 123; i++) {
  272. SDL_Surface* glyph = NULL;
  273. glyph = TTF_RenderGlyph_Shaded(font, i, *forecol, *backcol);
  274. if(glyph) {
  275. char outname[64];
  276. sprintf(outname, "glyph-%d.bmp", i);
  277. SDL_SaveBMP(glyph, outname);
  278. }
  279. }
  280. cleanup(0);
  281. }
  282. /* Set a 640x480 video mode */
  283. window = SDL_CreateWindow("glfont",
  284. SDL_WINDOWPOS_UNDEFINED,
  285. SDL_WINDOWPOS_UNDEFINED,
  286. WIDTH, HEIGHT, SDL_WINDOW_OPENGL);
  287. if (window == NULL) {
  288. fprintf(stderr, "Couldn't create window: %s\n", SDL_GetError());
  289. cleanup(2);
  290. }
  291. context = SDL_GL_CreateContext(window);
  292. if (context == NULL) {
  293. fprintf(stderr, "Couldn't create OpenGL context: %s\n", SDL_GetError());
  294. cleanup(2);
  295. }
  296. /* Render and center the message */
  297. if (argc > 2) {
  298. message = argv[2];
  299. } else {
  300. message = DEFAULT_TEXT;
  301. }
  302. switch (rendertype) {
  303. case RENDER_LATIN1:
  304. text = TTF_RenderText_Blended(font, message, *forecol);
  305. break;
  306. case RENDER_UTF8:
  307. text = TTF_RenderUTF8_Blended(font, message, *forecol);
  308. break;
  309. case RENDER_UNICODE:
  310. {
  311. /* This doesn't actually work because you can't pass
  312. UNICODE text in via command line, AFAIK, but...
  313. */
  314. Uint16 unicode_text[BUFSIZ];
  315. int index;
  316. for (index = 0; (message[0] || message[1]); ++index) {
  317. unicode_text[index] = ((Uint8 *)message)[0];
  318. unicode_text[index] <<= 8;
  319. unicode_text[index] |= ((Uint8 *)message)[1];
  320. message += 2;
  321. }
  322. text = TTF_RenderUNICODE_Blended(font,
  323. unicode_text, *forecol);
  324. }
  325. break;
  326. default:
  327. text = NULL; /* This shouldn't happen */
  328. break;
  329. }
  330. if (text == NULL) {
  331. fprintf(stderr, "Couldn't render text: %s\n", SDL_GetError());
  332. TTF_CloseFont(font);
  333. cleanup(2);
  334. }
  335. x = (WIDTH - text->w)/2;
  336. y = (HEIGHT - text->h)/2;
  337. w = text->w;
  338. h = text->h;
  339. printf("Font is generally %d big, and string is %d big\n",
  340. TTF_FontHeight(font), text->h);
  341. /* Convert the text into an OpenGL texture */
  342. glGetError();
  343. texture = SDL_GL_LoadTexture(text, texcoord);
  344. if ((gl_error = glGetError()) != GL_NO_ERROR) {
  345. /* If this failed, the text may exceed texture size limits */
  346. printf("Warning: Couldn't create texture: 0x%x\n", gl_error);
  347. }
  348. /* Make texture coordinates easy to understand */
  349. texMinX = texcoord[0];
  350. texMinY = texcoord[1];
  351. texMaxX = texcoord[2];
  352. texMaxY = texcoord[3];
  353. /* We don't need the original text surface anymore */
  354. SDL_FreeSurface(text);
  355. /* Initialize the GL state */
  356. glViewport(0, 0, WIDTH, HEIGHT);
  357. glMatrixMode(GL_PROJECTION);
  358. glLoadIdentity();
  359. glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
  360. glMatrixMode(GL_MODELVIEW);
  361. glLoadIdentity();
  362. glEnable(GL_DEPTH_TEST);
  363. glDepthFunc(GL_LESS);
  364. glShadeModel(GL_SMOOTH);
  365. /* Wait for a keystroke, and blit text on mouse press */
  366. done = 0;
  367. while (!done) {
  368. while (SDL_PollEvent(&event)) {
  369. switch (event.type) {
  370. case SDL_MOUSEMOTION:
  371. x = event.motion.x - w/2;
  372. y = event.motion.y - h/2;
  373. break;
  374. case SDL_KEYDOWN:
  375. case SDL_QUIT:
  376. done = 1;
  377. break;
  378. default:
  379. break;
  380. }
  381. }
  382. /* Clear the screen */
  383. glClearColor(1.0, 1.0, 1.0, 1.0);
  384. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  385. /* Draw the spinning cube */
  386. glBegin(GL_QUADS);
  387. glColor3fv(color[0]);
  388. glVertex3fv(cube[0]);
  389. glColor3fv(color[1]);
  390. glVertex3fv(cube[1]);
  391. glColor3fv(color[2]);
  392. glVertex3fv(cube[2]);
  393. glColor3fv(color[3]);
  394. glVertex3fv(cube[3]);
  395. glColor3fv(color[3]);
  396. glVertex3fv(cube[3]);
  397. glColor3fv(color[4]);
  398. glVertex3fv(cube[4]);
  399. glColor3fv(color[7]);
  400. glVertex3fv(cube[7]);
  401. glColor3fv(color[2]);
  402. glVertex3fv(cube[2]);
  403. glColor3fv(color[0]);
  404. glVertex3fv(cube[0]);
  405. glColor3fv(color[5]);
  406. glVertex3fv(cube[5]);
  407. glColor3fv(color[6]);
  408. glVertex3fv(cube[6]);
  409. glColor3fv(color[1]);
  410. glVertex3fv(cube[1]);
  411. glColor3fv(color[5]);
  412. glVertex3fv(cube[5]);
  413. glColor3fv(color[4]);
  414. glVertex3fv(cube[4]);
  415. glColor3fv(color[7]);
  416. glVertex3fv(cube[7]);
  417. glColor3fv(color[6]);
  418. glVertex3fv(cube[6]);
  419. glColor3fv(color[5]);
  420. glVertex3fv(cube[5]);
  421. glColor3fv(color[0]);
  422. glVertex3fv(cube[0]);
  423. glColor3fv(color[3]);
  424. glVertex3fv(cube[3]);
  425. glColor3fv(color[4]);
  426. glVertex3fv(cube[4]);
  427. glColor3fv(color[6]);
  428. glVertex3fv(cube[6]);
  429. glColor3fv(color[1]);
  430. glVertex3fv(cube[1]);
  431. glColor3fv(color[2]);
  432. glVertex3fv(cube[2]);
  433. glColor3fv(color[7]);
  434. glVertex3fv(cube[7]);
  435. glEnd();
  436. /* Rotate the cube */
  437. glMatrixMode(GL_MODELVIEW);
  438. glRotatef(5.0, 1.0, 1.0, 1.0);
  439. /* Show the text on the screen */
  440. SDL_GL_Enter2DMode(WIDTH, HEIGHT);
  441. glBindTexture(GL_TEXTURE_2D, texture);
  442. glBegin(GL_TRIANGLE_STRIP);
  443. glTexCoord2f(texMinX, texMinY); glVertex2i(x, y);
  444. glTexCoord2f(texMaxX, texMinY); glVertex2i(x+w, y);
  445. glTexCoord2f(texMinX, texMaxY); glVertex2i(x, y+h);
  446. glTexCoord2f(texMaxX, texMaxY); glVertex2i(x+w, y+h);
  447. glEnd();
  448. SDL_GL_Leave2DMode();
  449. /* Swap the buffers so everything is visible */
  450. SDL_GL_SwapWindow(window);
  451. }
  452. SDL_GL_DeleteContext(context);
  453. TTF_CloseFont(font);
  454. cleanup(0);
  455. /* Not reached, but fixes compiler warnings */
  456. return 0;
  457. }
  458. #else /* HAVE_OPENGL */
  459. int main(int argc, char *argv[])
  460. {
  461. printf("No OpenGL support on this system\n");
  462. return 1;
  463. }
  464. #endif /* HAVE_OPENGL */
  465. /* vi: set ts=4 sw=4 expandtab: */