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.
 
 
 
 
 
 

264 líneas
5.7 KiB

  1. /*
  2. * libcaca ASCII-Art library
  3. * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "config.h"
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "caca.h"
  28. #include "caca_internals.h"
  29. struct caca_frame
  30. {
  31. int w, h;
  32. int dx, dy;
  33. char *chars;
  34. int *color;
  35. };
  36. struct caca_sprite
  37. {
  38. int nf;
  39. struct caca_frame *frames;
  40. };
  41. struct caca_sprite *caca_load_sprite(const char *file)
  42. {
  43. char buf[BUFSIZ];
  44. struct caca_sprite *sprite;
  45. FILE *fd;
  46. fd = fopen(file, "r");
  47. if(fd == NULL)
  48. return NULL;
  49. sprite = malloc(sizeof(struct caca_sprite));
  50. if(sprite == NULL)
  51. goto sprite_alloc_failed;
  52. sprite->nf = 0;
  53. sprite->frames = NULL;
  54. while(!feof(fd))
  55. {
  56. int x, y;
  57. int w = 0, h = 0, dx = 0, dy = 0;
  58. struct caca_frame *frame;
  59. /* Get width and height */
  60. if(!fgets(buf, BUFSIZ, fd))
  61. break;
  62. sscanf(buf, "%i %i %i %i", &w, &h, &dx, &dy);
  63. if(w <= 0 || h <= 0 || w > BUFSIZ / 2)
  64. break;
  65. if(sprite->nf)
  66. {
  67. void *tmp = realloc(sprite->frames,
  68. (sprite->nf + 1) * sizeof(struct caca_frame));
  69. if(tmp == NULL)
  70. goto frame_failed;
  71. sprite->frames = tmp;
  72. sprite->nf++;
  73. }
  74. else
  75. {
  76. sprite->frames = malloc((sprite->nf + 1) * sizeof(struct caca_frame));
  77. if(sprite->frames == NULL)
  78. goto sprite_failed;
  79. sprite->nf++;
  80. }
  81. frame = &sprite->frames[sprite->nf - 1];
  82. frame->w = w;
  83. frame->h = h;
  84. frame->dx = dx;
  85. frame->dy = dy;
  86. frame->chars = malloc(w * h * sizeof(char));
  87. if(frame->chars == NULL)
  88. {
  89. sprite->nf--;
  90. goto frame_failed;
  91. }
  92. frame->color = malloc(w * h * sizeof(int));
  93. if(frame->color == NULL)
  94. {
  95. free(frame->chars);
  96. sprite->nf--;
  97. goto frame_failed;
  98. }
  99. for(y = 0; y < h; y++)
  100. {
  101. if(!fgets(buf, BUFSIZ, fd))
  102. goto frame_failed;
  103. for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
  104. frame->chars[w * y + x] = buf[x];
  105. for(; x < w; x++)
  106. frame->chars[w * y + x] = ' ';
  107. }
  108. for(y = 0; y < h; y++)
  109. {
  110. if(!fgets(buf, BUFSIZ, fd))
  111. goto frame_failed;
  112. for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
  113. frame->color[w * y + x] = buf[x] - 'a';
  114. for(; x < w; x++)
  115. frame->color[w * y + x] = ' ' - 'a';
  116. }
  117. continue;
  118. }
  119. if(sprite->nf == 0)
  120. goto sprite_failed;
  121. fclose(fd);
  122. return sprite;
  123. frame_failed:
  124. while(sprite->nf)
  125. {
  126. free(sprite->frames[sprite->nf - 1].color);
  127. free(sprite->frames[sprite->nf - 1].chars);
  128. sprite->nf--;
  129. }
  130. sprite_failed:
  131. free(sprite);
  132. sprite_alloc_failed:
  133. fclose(fd);
  134. return NULL;
  135. }
  136. int caca_get_sprite_frames(struct caca_sprite *sprite)
  137. {
  138. if(sprite == NULL)
  139. return 0;
  140. return sprite->nf;
  141. }
  142. int caca_get_sprite_width(struct caca_sprite *sprite, int f)
  143. {
  144. if(sprite == NULL)
  145. return 0;
  146. if(f < 0 || f >= sprite->nf)
  147. return 0;
  148. return sprite->frames[f].w;
  149. }
  150. int caca_get_sprite_height(struct caca_sprite *sprite, int f)
  151. {
  152. if(sprite == NULL)
  153. return 0;
  154. if(f < 0 || f >= sprite->nf)
  155. return 0;
  156. return sprite->frames[f].h;
  157. }
  158. int caca_get_sprite_dx(struct caca_sprite *sprite, int f)
  159. {
  160. if(sprite == NULL)
  161. return 0;
  162. if(f < 0 || f >= sprite->nf)
  163. return 0;
  164. return sprite->frames[f].dx;
  165. }
  166. int caca_get_sprite_dy(struct caca_sprite *sprite, int f)
  167. {
  168. if(sprite == NULL)
  169. return 0;
  170. if(f < 0 || f >= sprite->nf)
  171. return 0;
  172. return sprite->frames[f].dy;
  173. }
  174. void caca_draw_sprite(int x, int y, struct caca_sprite *sprite, int f)
  175. {
  176. int i, j, oldcol;
  177. struct caca_frame *frame;
  178. if(sprite == NULL)
  179. return;
  180. if(f < 0 || f >= sprite->nf)
  181. return;
  182. frame = &sprite->frames[f];
  183. oldcol = caca_get_color();
  184. for(j = 0; j < frame->h; j++)
  185. {
  186. for(i = 0; i < frame->w; i++)
  187. {
  188. int col = frame->color[frame->w * j + i];
  189. if(col >= 0)
  190. {
  191. caca_set_color(col);
  192. caca_putchar(x + i - frame->dx, y + j - frame->dy,
  193. frame->chars[frame->w * j + i]);
  194. }
  195. }
  196. }
  197. caca_set_color(oldcol);
  198. }
  199. void caca_free_sprite(struct caca_sprite *sprite)
  200. {
  201. int i;
  202. if(sprite == NULL)
  203. return;
  204. for(i = sprite->nf; i--;)
  205. {
  206. struct caca_frame *frame = &sprite->frames[i];
  207. free(frame->chars);
  208. free(frame->color);
  209. }
  210. free(sprite->frames);
  211. free(sprite);
  212. }