No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

83 líneas
1.9 KiB

  1. /*
  2. * makefont.c: create a font bitmap from a .ttf file.
  3. * $Id$
  4. *
  5. * Copyright: (c) 2004 Sam Hocevar <sam@zoy.org>
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License as published by Banlu Kemiyatorn. See
  9. * http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  10. *
  11. * Build example:
  12. * gcc -Wall makefont.c -o makefont `sdl-config --cflags --libs` -lSDL_ttf
  13. *
  14. * Usage:
  15. * makefont <font.ttf> <size> <text> <output.bmp>
  16. */
  17. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include "SDL.h"
  21. #include "SDL_ttf.h"
  22. int main(int argc, char *argv[])
  23. {
  24. unsigned char *text;
  25. SDL_Color bg = { 0xff, 0xff, 0xff, 0 };
  26. SDL_Color fg = { 0x00, 0x00, 0x00, 0 };
  27. SDL_Surface *surface;
  28. TTF_Font *font;
  29. int i;
  30. if(argc != 5)
  31. {
  32. fprintf(stderr, "usage: %s <font.ttf> <size> <text> <output.bmp>\n",
  33. argv[0]);
  34. return 1;
  35. }
  36. /* Load font */
  37. TTF_Init();
  38. font = TTF_OpenFont(argv[1], atoi(argv[2]));
  39. if(!font)
  40. {
  41. fprintf(stderr, "could not load font %s: %s\n",
  42. argv[1], SDL_GetError());
  43. TTF_Quit();
  44. return 1;
  45. }
  46. /* Add spaces to string */
  47. text = malloc(2 * strlen(argv[3]) * sizeof(char));
  48. for(i = 0; argv[3][i]; i++)
  49. {
  50. text[i * 2] = argv[3][i];
  51. text[i * 2 + 1] = ' ';
  52. }
  53. text[i * 2 - 1] = '\0';
  54. /* Render text to surface */
  55. TTF_SetFontStyle(font, TTF_STYLE_NORMAL);
  56. surface = TTF_RenderUTF8_Shaded(font, text, fg, bg);
  57. if(!surface)
  58. {
  59. fprintf(stderr, "surface rendering failed: %s\n", SDL_GetError());
  60. TTF_CloseFont(font);
  61. TTF_Quit();
  62. return 1;
  63. }
  64. /* Clean up surface */
  65. /* Save surface and free everything */
  66. SDL_SaveBMP(surface, argv[4]);
  67. SDL_FreeSurface(surface);
  68. TTF_CloseFont(font);
  69. TTF_Quit();
  70. return 0;
  71. }