Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

makefont.c 2.0 KiB

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