Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * libee ASCII-Art library
  3. * Copyright (c) 2002, 2003 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program 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
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include "config.h"
  23. #ifdef USE_SLANG
  24. # include <slang.h>
  25. #elif USE_NCURSES
  26. # include <curses.h>
  27. #endif
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "ee.h"
  32. struct ee_frame
  33. {
  34. int w, h;
  35. int dx, dy;
  36. char *chars;
  37. int *color;
  38. };
  39. struct ee_sprite
  40. {
  41. int nf;
  42. struct ee_frame *frames;
  43. };
  44. struct ee_sprite *ee_load_sprite(const char *file)
  45. {
  46. char buf[BUFSIZ];
  47. struct ee_sprite *sprite;
  48. FILE *fd;
  49. fd = fopen(file, "r");
  50. if(fd == NULL)
  51. return NULL;
  52. sprite = malloc(sizeof(struct ee_sprite));
  53. sprite->nf = 0;
  54. sprite->frames = NULL;
  55. while(!feof(fd))
  56. {
  57. int x, y;
  58. int w = 0, h = 0, dx = 0, dy = 0;
  59. struct ee_frame *frame;
  60. /* Get width and height */
  61. if(!fgets(buf, BUFSIZ, fd))
  62. break;
  63. sscanf(buf, "%i %i %i %i", &w, &h, &dx, &dy);
  64. if(w <= 0 || h <= 0 || w > BUFSIZ / 2)
  65. break;
  66. if(sprite->nf++)
  67. sprite->frames = realloc(sprite->frames,
  68. sprite->nf * sizeof(struct ee_frame));
  69. else
  70. sprite->frames = malloc(sprite->nf * sizeof(struct ee_frame));
  71. frame = &sprite->frames[sprite->nf - 1];
  72. frame->w = w;
  73. frame->h = h;
  74. frame->dx = dx;
  75. frame->dy = dy;
  76. frame->chars = malloc(w * h * sizeof(char));
  77. frame->color = malloc(w * h * sizeof(int));
  78. for(y = 0; y < h; y++)
  79. {
  80. if(!fgets(buf, BUFSIZ, fd))
  81. goto failed;
  82. for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
  83. frame->chars[w * y + x] = buf[x];
  84. for(; x < w; x++)
  85. frame->chars[w * y + x] = ' ';
  86. }
  87. for(y = 0; y < h; y++)
  88. {
  89. if(!fgets(buf, BUFSIZ, fd))
  90. goto failed;
  91. for(x = 0; x < w && buf[x] && buf[x] != '\r' && buf[x] != '\n'; x++)
  92. frame->color[w * y + x] = buf[x] - 'a';
  93. for(; x < w; x++)
  94. frame->color[w * y + x] = ' ' - 'a';
  95. }
  96. continue;
  97. failed:
  98. free(sprite->frames[sprite->nf - 1].chars);
  99. free(sprite->frames[sprite->nf - 1].color);
  100. sprite->nf--;
  101. break;
  102. }
  103. fclose(fd);
  104. if(sprite->nf == 0)
  105. {
  106. free(sprite);
  107. return NULL;
  108. }
  109. return sprite;
  110. }
  111. int ee_get_sprite_frames(struct ee_sprite *sprite)
  112. {
  113. if(sprite == NULL)
  114. return 0;
  115. return sprite->nf;
  116. }
  117. int ee_get_sprite_width(struct ee_sprite *sprite, int f)
  118. {
  119. if(sprite == NULL)
  120. return 0;
  121. if(f < 0 || f >= sprite->nf)
  122. return 0;
  123. return sprite->frames[f].w;
  124. }
  125. int ee_get_sprite_height(struct ee_sprite *sprite, int f)
  126. {
  127. if(sprite == NULL)
  128. return 0;
  129. if(f < 0 || f >= sprite->nf)
  130. return 0;
  131. return sprite->frames[f].h;
  132. }
  133. int ee_get_sprite_dx(struct ee_sprite *sprite, int f)
  134. {
  135. if(sprite == NULL)
  136. return 0;
  137. if(f < 0 || f >= sprite->nf)
  138. return 0;
  139. return sprite->frames[f].dx;
  140. }
  141. int ee_get_sprite_dy(struct ee_sprite *sprite, int f)
  142. {
  143. if(sprite == NULL)
  144. return 0;
  145. if(f < 0 || f >= sprite->nf)
  146. return 0;
  147. return sprite->frames[f].dy;
  148. }
  149. void ee_draw_sprite(int x, int y, struct ee_sprite *sprite, int f)
  150. {
  151. int i, j, oldcol;
  152. struct ee_frame *frame;
  153. if(sprite == NULL)
  154. return;
  155. if(f < 0 || f >= sprite->nf)
  156. return;
  157. frame = &sprite->frames[f];
  158. oldcol = ee_get_color();
  159. for(j = 0; j < frame->h; j++)
  160. {
  161. for(i = 0; i < frame->w; i++)
  162. {
  163. int col = frame->color[frame->w * j + i];
  164. if(col >= 0)
  165. {
  166. ee_set_color(col);
  167. ee_putchar(x + i - frame->dx, y + j - frame->dy,
  168. frame->chars[frame->w * j + i]);
  169. }
  170. }
  171. }
  172. ee_set_color(oldcol);
  173. }
  174. void ee_free_sprite(struct ee_sprite *sprite)
  175. {
  176. int i;
  177. if(sprite == NULL)
  178. return;
  179. for(i = sprite->nf; i--;)
  180. {
  181. struct ee_frame *frame = &sprite->frames[i];
  182. free(frame->chars);
  183. free(frame->color);
  184. }
  185. free(sprite->frames);
  186. free(sprite);
  187. }