You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

sprite.c 7.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * libcaca ASCII-Art library
  3. * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  19. * 02111-1307 USA
  20. */
  21. /** \file sprite.c
  22. * \version \$Id$
  23. * \author Sam Hocevar <sam@zoy.org>
  24. * \brief Sprite loading and blitting
  25. *
  26. * This file contains a small framework for sprite loading and blitting.
  27. */
  28. #include "config.h"
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include "caca.h"
  33. #include "caca_internals.h"
  34. #if !defined(_DOXYGEN_SKIP_ME)
  35. struct caca_frame
  36. {
  37. int w, h;
  38. int dx, dy;
  39. char *chars;
  40. int *color;
  41. };
  42. struct caca_sprite
  43. {
  44. int nf;
  45. struct caca_frame *frames;
  46. };
  47. #endif
  48. /**
  49. * \brief Allocate a sprite loaded from a file.
  50. *
  51. * \param file The filename.
  52. * \return The sprite, or NULL if an error occured.
  53. */
  54. struct caca_sprite *caca_load_sprite(char const *file)
  55. {
  56. char buf[BUFSIZ];
  57. struct caca_sprite *sprite;
  58. FILE *fd;
  59. fd = fopen(file, "r");
  60. if(fd == NULL)
  61. return NULL;
  62. sprite = malloc(sizeof(struct caca_sprite));
  63. if(sprite == NULL)
  64. goto sprite_alloc_failed;
  65. sprite->nf = 0;
  66. sprite->frames = NULL;
  67. while(!feof(fd))
  68. {
  69. int x, y;
  70. int w = 0, h = 0, dx = 0, dy = 0;
  71. struct caca_frame *frame;
  72. /* Get width and height */
  73. if(!fgets(buf, BUFSIZ, fd))
  74. break;
  75. sscanf(buf, "%i %i %i %i", &w, &h, &dx, &dy);
  76. if(w <= 0 || h <= 0 || w > BUFSIZ / 2)
  77. break;
  78. if(sprite->nf)
  79. {
  80. void *tmp = realloc(sprite->frames,
  81. (sprite->nf + 1) * sizeof(struct caca_frame));
  82. if(tmp == NULL)
  83. goto frame_failed;
  84. sprite->frames = tmp;
  85. sprite->nf++;
  86. }
  87. else
  88. {
  89. sprite->frames = malloc((sprite->nf + 1) * sizeof(struct caca_frame));
  90. if(sprite->frames == NULL)
  91. goto sprite_failed;
  92. sprite->nf++;
  93. }
  94. frame = &sprite->frames[sprite->nf - 1];
  95. frame->w = w;
  96. frame->h = h;
  97. frame->dx = dx;
  98. frame->dy = dy;
  99. frame->chars = malloc(w * h * sizeof(char));
  100. if(frame->chars == NULL)
  101. {
  102. sprite->nf--;
  103. goto frame_failed;
  104. }
  105. frame->color = malloc(w * h * sizeof(int));
  106. if(frame->color == NULL)
  107. {
  108. free(frame->chars);
  109. sprite->nf--;
  110. goto frame_failed;
  111. }
  112. for(y = 0; y < h; y++)
  113. {
  114. if(!fgets(buf, BUFSIZ, fd))
  115. goto frame_failed;
  116. for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
  117. frame->chars[w * y + x] = buf[x];
  118. for(; x < w; x++)
  119. frame->chars[w * y + x] = ' ';
  120. }
  121. for(y = 0; y < h; y++)
  122. {
  123. if(!fgets(buf, BUFSIZ, fd))
  124. goto frame_failed;
  125. for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
  126. frame->color[w * y + x] = buf[x] - 'a';
  127. for(; x < w; x++)
  128. frame->color[w * y + x] = ' ' - 'a';
  129. }
  130. continue;
  131. }
  132. if(sprite->nf == 0)
  133. goto sprite_failed;
  134. fclose(fd);
  135. return sprite;
  136. frame_failed:
  137. while(sprite->nf)
  138. {
  139. free(sprite->frames[sprite->nf - 1].color);
  140. free(sprite->frames[sprite->nf - 1].chars);
  141. sprite->nf--;
  142. }
  143. sprite_failed:
  144. free(sprite);
  145. sprite_alloc_failed:
  146. fclose(fd);
  147. return NULL;
  148. }
  149. /**
  150. * \brief Return the number of frames in a sprite.
  151. *
  152. * \param sprite The sprite.
  153. * \return The number of frames.
  154. */
  155. int caca_get_sprite_frames(struct caca_sprite const *sprite)
  156. {
  157. if(sprite == NULL)
  158. return 0;
  159. return sprite->nf;
  160. }
  161. /**
  162. * \brief Return the width of a sprite.
  163. *
  164. * \param sprite The sprite.
  165. * \param f The frame index.
  166. * \return The width of the given frame of the sprite.
  167. */
  168. int caca_get_sprite_width(struct caca_sprite const *sprite, int f)
  169. {
  170. if(sprite == NULL)
  171. return 0;
  172. if(f < 0 || f >= sprite->nf)
  173. return 0;
  174. return sprite->frames[f].w;
  175. }
  176. /**
  177. * \brief Return the height of a sprite.
  178. *
  179. * \param sprite The sprite.
  180. * \param f The frame index.
  181. * \return The height of the given frame of the sprite.
  182. */
  183. int caca_get_sprite_height(struct caca_sprite const *sprite, int f)
  184. {
  185. if(sprite == NULL)
  186. return 0;
  187. if(f < 0 || f >= sprite->nf)
  188. return 0;
  189. return sprite->frames[f].h;
  190. }
  191. /**
  192. * \brief Return the X coordinate of a sprite's handle.
  193. *
  194. * \param sprite The sprite.
  195. * \param f The frame index.
  196. * \return The X coordinate of the given frame's handle.
  197. */
  198. int caca_get_sprite_dx(struct caca_sprite const *sprite, int f)
  199. {
  200. if(sprite == NULL)
  201. return 0;
  202. if(f < 0 || f >= sprite->nf)
  203. return 0;
  204. return sprite->frames[f].dx;
  205. }
  206. /**
  207. * \brief Return the Y coordinate of a sprite's handle.
  208. *
  209. * \param sprite The sprite.
  210. * \param f The frame index.
  211. * \return The Y coordinate of the given frame's handle.
  212. */
  213. int caca_get_sprite_dy(struct caca_sprite const *sprite, int f)
  214. {
  215. if(sprite == NULL)
  216. return 0;
  217. if(f < 0 || f >= sprite->nf)
  218. return 0;
  219. return sprite->frames[f].dy;
  220. }
  221. /**
  222. * \brief Draw a sprite's specific frame at the given coordinates. If the
  223. * frame does not exist, nothing is displayed.
  224. *
  225. * \param x The X coordinate.
  226. * \param y The Y coordinate.
  227. * \param sprite The sprite.
  228. * \param f The frame index.
  229. * \return void
  230. */
  231. void caca_draw_sprite(int x, int y, struct caca_sprite const *sprite, int f)
  232. {
  233. int i, j;
  234. enum caca_color oldfg, oldbg;
  235. struct caca_frame *frame;
  236. if(sprite == NULL)
  237. return;
  238. if(f < 0 || f >= sprite->nf)
  239. return;
  240. frame = &sprite->frames[f];
  241. oldfg = caca_get_fg_color();
  242. oldbg = caca_get_bg_color();
  243. for(j = 0; j < frame->h; j++)
  244. {
  245. for(i = 0; i < frame->w; i++)
  246. {
  247. int col = frame->color[frame->w * j + i];
  248. if(col >= 0)
  249. {
  250. caca_set_color(col, CACA_COLOR_BLACK);
  251. caca_putchar(x + i - frame->dx, y + j - frame->dy,
  252. frame->chars[frame->w * j + i]);
  253. }
  254. }
  255. }
  256. caca_set_color(oldfg, oldbg);
  257. }
  258. /**
  259. * \brief Free the memory associated with a sprite.
  260. *
  261. * \param sprite The sprite to be freed.
  262. * \return void
  263. */
  264. void caca_free_sprite(struct caca_sprite *sprite)
  265. {
  266. int i;
  267. if(sprite == NULL)
  268. return;
  269. for(i = sprite->nf; i--;)
  270. {
  271. struct caca_frame *frame = &sprite->frames[i];
  272. free(frame->chars);
  273. free(frame->color);
  274. }
  275. free(sprite->frames);
  276. free(sprite);
  277. }